Commit f7014e7
authored
[ENH] dataclass refactor of openmlparameter and openmlsetup classes (#1582)
#### Metadata
* Reference Issue: fixes #1541
* New Tests Added: No
* Documentation Updated: No
#### Details
Edited the OpenMLParameter in `openml/setups/setup.py` to use `@dataclass` decorator. This significantly reduces the boilerplate code in the following places:
- OpenMLSetup
**Before:**
```python
class OpenMLSetup:
"""Setup object (a.k.a. Configuration)...."""
def __init__(self, setup_id: int, flow_id: int, parameters: dict[int, Any] | None):
if not isinstance(setup_id, int):
raise ValueError("setup id should be int")
if not isinstance(flow_id, int):
raise ValueError("flow id should be int")
if parameters is not None and not isinstance(parameters, dict):
raise ValueError("parameters should be dict")
self.setup_id = setup_id
self.flow_id = flow_id
self.parameters = parameters
```
**After:**
```python
@DataClass
class OpenMLSetup:
"""Setup object (a.k.a. Configuration)...."""
setup_id: int
flow_id: int
parameters: dict[int, Any] | None
def __post_init__(self) -> None:
if not isinstance(self.setup_id, int):
raise ValueError("setup id should be int")
if not isinstance(self.flow_id, int):
raise ValueError("flow id should be int")
if self.parameters is not None and not isinstance(self.parameters, dict):
raise ValueError("parameters should be dict")
```
- OpenMLParameter
**Before:**
```python
class OpenMLParameter:
"""Parameter object (used in setup)...."""
def __init__( # noqa: PLR0913
self,
input_id: int,
flow_id: int,
flow_name: str,
full_name: str,
parameter_name: str,
data_type: str,
default_value: str,
value: str,
):
self.id = input_id
self.flow_id = flow_id
self.flow_name = flow_name
self.full_name = full_name
self.parameter_name = parameter_name
self.data_type = data_type
self.default_value = default_value
self.value = value
```
**After:**
```python
@DataClass
class OpenMLParameter:
"""Parameter object (used in setup)...."""
input_id: int
flow_id: int
flow_name: str
full_name: str
parameter_name: str
data_type: str
default_value: str
value: str
def __post_init__(self) -> None:
# Map input_id to id for backward compatibility
self.id = self.input_id
```
## Tests
For tests, I have used `xfail` temporarily to bypass the preexisting test failures in `tests\test_setups\test_setup_functions.py`.1 parent aba2586 commit f7014e7
1 file changed
Lines changed: 27 additions & 37 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| 11 | + | |
10 | 12 | | |
11 | 13 | | |
12 | 14 | | |
| |||
20 | 22 | | |
21 | 23 | | |
22 | 24 | | |
23 | | - | |
24 | | - | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
25 | 31 | | |
26 | 32 | | |
27 | | - | |
| 33 | + | |
28 | 34 | | |
29 | 35 | | |
30 | | - | |
| 36 | + | |
31 | 37 | | |
32 | 38 | | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | 39 | | |
38 | 40 | | |
39 | 41 | | |
| |||
66 | 68 | | |
67 | 69 | | |
68 | 70 | | |
| 71 | + | |
69 | 72 | | |
70 | 73 | | |
71 | 74 | | |
| |||
91 | 94 | | |
92 | 95 | | |
93 | 96 | | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
113 | 109 | | |
114 | 110 | | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
125 | 115 | | |
126 | 116 | | |
127 | 117 | | |
| |||
0 commit comments