Skip to content

Commit f7014e7

Browse files
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

File tree

openml/setups/setup.py

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# License: BSD 3-Clause
22
from __future__ import annotations
33

4+
from dataclasses import asdict, dataclass
45
from typing import Any
56

67
import openml.config
78
import openml.flows
89

910

11+
@dataclass
1012
class OpenMLSetup:
1113
"""Setup object (a.k.a. Configuration).
1214
@@ -20,20 +22,20 @@ class OpenMLSetup:
2022
The setting of the parameters
2123
"""
2224

23-
def __init__(self, setup_id: int, flow_id: int, parameters: dict[int, Any] | None):
24-
if not isinstance(setup_id, int):
25+
setup_id: int
26+
flow_id: int
27+
parameters: dict[int, Any] | None
28+
29+
def __post_init__(self) -> None:
30+
if not isinstance(self.setup_id, int):
2531
raise ValueError("setup id should be int")
2632

27-
if not isinstance(flow_id, int):
33+
if not isinstance(self.flow_id, int):
2834
raise ValueError("flow id should be int")
2935

30-
if parameters is not None and not isinstance(parameters, dict):
36+
if self.parameters is not None and not isinstance(self.parameters, dict):
3137
raise ValueError("parameters should be dict")
3238

33-
self.setup_id = setup_id
34-
self.flow_id = flow_id
35-
self.parameters = parameters
36-
3739
def _to_dict(self) -> dict[str, Any]:
3840
return {
3941
"setup_id": self.setup_id,
@@ -66,6 +68,7 @@ def __repr__(self) -> str:
6668
return header + body
6769

6870

71+
@dataclass
6972
class OpenMLParameter:
7073
"""Parameter object (used in setup).
7174
@@ -91,37 +94,24 @@ class OpenMLParameter:
9194
If the parameter was set, the value that it was set to.
9295
"""
9396

94-
def __init__( # noqa: PLR0913
95-
self,
96-
input_id: int,
97-
flow_id: int,
98-
flow_name: str,
99-
full_name: str,
100-
parameter_name: str,
101-
data_type: str,
102-
default_value: str,
103-
value: str,
104-
):
105-
self.id = input_id
106-
self.flow_id = flow_id
107-
self.flow_name = flow_name
108-
self.full_name = full_name
109-
self.parameter_name = parameter_name
110-
self.data_type = data_type
111-
self.default_value = default_value
112-
self.value = value
97+
input_id: int
98+
flow_id: int
99+
flow_name: str
100+
full_name: str
101+
parameter_name: str
102+
data_type: str
103+
default_value: str
104+
value: str
105+
106+
def __post_init__(self) -> None:
107+
# Map input_id to id for backward compatibility
108+
self.id = self.input_id
113109

114110
def _to_dict(self) -> dict[str, Any]:
115-
return {
116-
"id": self.id,
117-
"flow_id": self.flow_id,
118-
"flow_name": self.flow_name,
119-
"full_name": self.full_name,
120-
"parameter_name": self.parameter_name,
121-
"data_type": self.data_type,
122-
"default_value": self.default_value,
123-
"value": self.value,
124-
}
111+
result = asdict(self)
112+
# Replaces input_id with id for backward compatibility
113+
result["id"] = result.pop("input_id")
114+
return result
125115

126116
def __repr__(self) -> str:
127117
header = "OpenML Parameter"

0 commit comments

Comments
 (0)