Skip to content

Commit 697391c

Browse files
committed
fix formatting
1 parent d598714 commit 697391c

8 files changed

Lines changed: 31 additions & 22 deletions

File tree

src/hyperactive/_registry/_lookup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def all_objects(
158158
ROOT = str(Path(__file__).parent.parent) # package root directory
159159

160160
def _coerce_to_str(obj):
161-
if isinstance(obj, (list, tuple)):
161+
if isinstance(obj, list | tuple):
162162
return [_coerce_to_str(o) for o in obj]
163163
if isclass(obj):
164164
obj = obj.get_tag("object_type")

src/hyperactive/integrations/sklearn/opt_cv.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""opt_cv module for Hyperactive optimization."""
22

33
from collections.abc import Callable
4-
from typing import Union
54

65
from sklearn.base import BaseEstimator, clone
76

@@ -107,7 +106,7 @@ def __init__(
107106
estimator,
108107
optimizer,
109108
*,
110-
scoring: Union[Callable, str, None] = None,
109+
scoring: Callable | str | None = None,
111110
refit: bool = True,
112111
cv=None,
113112
):

src/hyperactive/opt/_adapters/_search_space_adapter.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ def validate(self):
8686
"""
8787
if not isinstance(self._original_space, dict):
8888
raise TypeError(
89-
f"Search space must be a dict, got {type(self._original_space).__name__}"
89+
"Search space must be a dict, "
90+
f"got {type(self._original_space).__name__}"
9091
)
9192

9293
if not self._original_space:
@@ -99,13 +100,14 @@ def _validate_dimension(self, name: str, values):
99100
"""Validate a single dimension."""
100101
if isinstance(values, tuple):
101102
self._validate_continuous(name, values)
102-
elif isinstance(values, (list, np.ndarray)):
103+
elif isinstance(values, list | np.ndarray):
103104
self._validate_discrete(name, values)
104105
else:
105106
raise TypeError(
106107
f"Parameter '{name}': expected list (discrete) or tuple (continuous), "
107108
f"got {type(values).__name__}. "
108-
f"Use [a, b, c] for discrete values or (low, high) for continuous ranges."
109+
"Use [a, b, c] for discrete values "
110+
"or (low, high) for continuous ranges."
109111
)
110112

111113
def _validate_continuous(self, name: str, values: tuple):
@@ -130,7 +132,7 @@ def _validate_continuous(self, name: str, values: tuple):
130132
low, high = values[0], values[1]
131133

132134
# Check low and high are numeric
133-
if not isinstance(low, (int, float)) or not isinstance(high, (int, float)):
135+
if not isinstance(low, int | float) or not isinstance(high, int | float):
134136
raise TypeError(
135137
f"Parameter '{name}': low and high must be numeric, "
136138
f"got low={type(low).__name__}, high={type(high).__name__}"
@@ -190,7 +192,7 @@ def _parse_continuous_options(self, name: str, values: tuple):
190192
f"Use 'log' for logarithmic scale."
191193
)
192194
log_scale = True
193-
elif isinstance(third, (int, float)):
195+
elif isinstance(third, int | float):
194196
n_points = int(third)
195197
else:
196198
raise TypeError(
@@ -200,7 +202,7 @@ def _parse_continuous_options(self, name: str, values: tuple):
200202
elif len(values) == 4:
201203
# (low, high, n_points, "log")
202204
third, fourth = values[2], values[3]
203-
if not isinstance(third, (int, float)):
205+
if not isinstance(third, int | float):
204206
raise TypeError(
205207
f"Parameter '{name}': n_points must be numeric, "
206208
f"got {type(third).__name__}"

src/hyperactive/opt/gridsearch/_sk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def _check_param_grid(self, param_grid):
159159
if isinstance(v, np.ndarray) and v.ndim > 1:
160160
raise ValueError("Parameter array should be one-dimensional.")
161161

162-
if isinstance(v, str) or not isinstance(v, (np.ndarray, Sequence)):
162+
if isinstance(v, str) or not isinstance(v, np.ndarray | Sequence):
163163
raise ValueError(
164164
f"Parameter grid for parameter ({name}) needs to"
165165
f" be a list or numpy array, but got ({type(v)})."

src/hyperactive/opt/optuna/_grid_optimizer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def _get_optimizer(self):
103103
for key, space in param_space.items():
104104
if isinstance(space, list):
105105
search_space[key] = space
106-
elif isinstance(space, (tuple,)) and len(space) == 2:
106+
elif isinstance(space, tuple) and len(space) == 2:
107107
# Convert range to discrete list for grid search
108108
low, high = space
109109
if isinstance(low, int) and isinstance(high, int):

src/hyperactive/opt/random_search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def _check_param_distributions(self, param_distributions):
178178
if isinstance(v, np.ndarray) and v.ndim > 1:
179179
raise ValueError("Parameter array should be one-dimensional.")
180180

181-
if isinstance(v, str) or not isinstance(v, (np.ndarray, Sequence)):
181+
if isinstance(v, str) or not isinstance(v, np.ndarray | Sequence):
182182
raise ValueError(
183183
f"Parameter distribution for ({name}) must be a list, numpy "
184184
f"array, or scipy.stats ``rv_frozen``, but got ({type(v)})."

src/hyperactive/tests/test_search_space_adapter.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,11 @@ def test_multiple_categorical_dimensions(self):
182182

183183
assert encoded == {"kernel": [0, 1], "solver": [0, 1, 2]}
184184
assert adapter.categorical_mapping["kernel"] == {0: "rbf", 1: "linear"}
185-
assert adapter.categorical_mapping["solver"] == {0: "lbfgs", 1: "sgd", 2: "adam"}
185+
assert adapter.categorical_mapping["solver"] == {
186+
0: "lbfgs",
187+
1: "sgd",
188+
2: "adam",
189+
}
186190

187191
decoded = adapter.decode({"kernel": 0, "solver": 2})
188192
assert decoded == {"kernel": "rbf", "solver": "adam"}
@@ -228,7 +232,9 @@ def test_validate_continuous_too_few_values_raises(self):
228232

229233
def test_validate_continuous_too_many_values_raises(self):
230234
"""Continuous tuple with > 4 values raises ValueError."""
231-
adapter = SearchSpaceAdapter({"C": (0.1, 10, 50, "log", "extra")}, capabilities={})
235+
adapter = SearchSpaceAdapter(
236+
{"C": (0.1, 10, 50, "log", "extra")}, capabilities={}
237+
)
232238

233239
with pytest.raises(ValueError, match="too many values"):
234240
adapter.validate()
@@ -280,7 +286,7 @@ class TestContinuousDiscretization:
280286

281287
def test_discretize_linear_default_points(self):
282288
"""Linear discretization with default 100 points."""
283-
np = pytest.importorskip("numpy")
289+
pytest.importorskip("numpy")
284290

285291
space = {"C": (0.1, 10.0)}
286292
adapter = SearchSpaceAdapter(space, capabilities={"continuous": False})
@@ -295,7 +301,7 @@ def test_discretize_linear_default_points(self):
295301

296302
def test_discretize_log_scale(self):
297303
"""Log scale discretization."""
298-
np = pytest.importorskip("numpy")
304+
pytest.importorskip("numpy")
299305

300306
space = {"lr": (1e-5, 1e-1, "log")}
301307
adapter = SearchSpaceAdapter(space, capabilities={"continuous": False})

src/hyperactive/tests/test_unified_search_space.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def sklearn_experiment():
1717

1818
@pytest.fixture
1919
def simple_search_space():
20-
"""Simple unified search space format: dict[str, list]."""
20+
"""Provide unified search space format: dict[str, list]."""
2121
return {"C": [0.1, 1, 10], "gamma": [0.01, 0.1, 1]}
2222

2323

@@ -202,7 +202,9 @@ def test_gfo_capability_tags(self):
202202
opt = RandomSearch.create_test_instance()
203203

204204
assert opt.get_tag("capability:discrete") is True
205-
assert opt.get_tag("capability:continuous") is False # GFO needs lists, not tuples
205+
assert (
206+
opt.get_tag("capability:continuous") is False
207+
) # GFO needs lists, not tuples
206208
assert opt.get_tag("capability:categorical") is False # GFO only numeric
207209
assert opt.get_tag("capability:constraints") is True
208210

@@ -272,7 +274,7 @@ def categorical_search_space(self):
272274

273275
@pytest.fixture
274276
def function_experiment(self):
275-
"""Simple function experiment for fast testing."""
277+
"""Create function experiment for fast testing."""
276278
from hyperactive.experiment.func import FunctionExperiment
277279

278280
def objective(params):
@@ -388,20 +390,20 @@ def mixed_search_space(self):
388390

389391
@pytest.fixture
390392
def continuous_function_experiment(self):
391-
"""Simple function experiment for continuous optimization."""
393+
"""Create function experiment for continuous optimization."""
392394
from hyperactive.experiment.func import FunctionExperiment
393395

394396
def objective(params):
395397
# Simple objective: maximize when x and y are in middle of range
396398
x = params["x"]
397399
y = params["y"]
398-
return -(x - 5.0) ** 2 - (y - 0.01) ** 2
400+
return -((x - 5.0) ** 2) - (y - 0.01) ** 2
399401

400402
return FunctionExperiment(objective)
401403

402404
@pytest.fixture
403405
def mixed_function_experiment(self):
404-
"""Function experiment for mixed search space."""
406+
"""Create function experiment for mixed search space."""
405407
from hyperactive.experiment.func import FunctionExperiment
406408

407409
def objective(params):

0 commit comments

Comments
 (0)