Skip to content

Commit 3dd7753

Browse files
committed
Fix 101 mypy issues
1 parent 98c1b6f commit 3dd7753

File tree

12 files changed

+467
-476
lines changed

12 files changed

+467
-476
lines changed

autosklearn/automl.py

Lines changed: 394 additions & 456 deletions
Large diffs are not rendered by default.

autosklearn/data/validation.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# -*- encoding: utf-8 -*-
2-
from typing import List, Optional, Tuple, Union
1+
from typing import overload
32

43
import logging
54

@@ -16,7 +15,7 @@
1615

1716
def convert_if_sparse(
1817
y: SUPPORTED_TARGET_TYPES,
19-
) -> Union[np.ndarray, List, pd.DataFrame, pd.Series]:
18+
) -> np.ndarray | list | pd.DataFrame | pd.Series:
2019
"""If the labels `y` are sparse, it will convert it to its dense representation
2120
2221
Parameters
@@ -77,9 +76,9 @@ class InputValidator(BaseEstimator):
7776

7877
def __init__(
7978
self,
80-
feat_type: Optional[List[str]] = None,
79+
feat_type: list[str] | None = None,
8180
is_classification: bool = False,
82-
logger_port: Optional[int] = None,
81+
logger_port: int | None = None,
8382
allow_string_features: bool = True,
8483
) -> None:
8584
self.feat_type = feat_type
@@ -108,8 +107,8 @@ def fit(
108107
self,
109108
X_train: SUPPORTED_FEAT_TYPES,
110109
y_train: SUPPORTED_TARGET_TYPES,
111-
X_test: Optional[SUPPORTED_FEAT_TYPES] = None,
112-
y_test: Optional[SUPPORTED_TARGET_TYPES] = None,
110+
X_test: SUPPORTED_FEAT_TYPES | None = None,
111+
y_test: SUPPORTED_TARGET_TYPES | None = None,
113112
) -> BaseEstimator:
114113
"""
115114
Validates and fit a categorical encoder (if needed) to the features, and
@@ -172,11 +171,27 @@ def fit(
172171

173172
return self
174173

174+
@overload
175175
def transform(
176176
self,
177177
X: SUPPORTED_FEAT_TYPES,
178-
y: Optional[Union[List, pd.Series, pd.DataFrame, np.ndarray]] = None,
179-
) -> Tuple[Union[np.ndarray, pd.DataFrame, spmatrix], Optional[np.ndarray]]:
178+
y: None = None,
179+
) -> tuple[np.ndarray | pd.DataFrame | spmatrix, None]:
180+
...
181+
182+
@overload
183+
def transform(
184+
self,
185+
X: SUPPORTED_FEAT_TYPES,
186+
y: list | pd.Series | pd.DataFrame | np.ndarray,
187+
) -> tuple[np.ndarray | pd.DataFrame | spmatrix, np.ndarray]:
188+
...
189+
190+
def transform(
191+
self,
192+
X: SUPPORTED_FEAT_TYPES,
193+
y: list | pd.Series | pd.DataFrame | np.ndarray | None = None,
194+
) -> tuple[np.ndarray | pd.DataFrame | spmatrix, np.ndarray | None]:
180195
"""
181196
Transform the given target or features to a numpy array
182197

autosklearn/data/xy_data_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def __init__(
6464
else:
6565
raise ValueError(
6666
"Unsupported feat_type provided. We expect the user to "
67-
"provide a Dict[str, str] mapping from column to categorical/ "
67+
"provide a Dict[str | int, str] mapping from column to categorical/ "
6868
"numerical."
6969
)
7070

autosklearn/evaluation/__init__.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- encoding: utf-8 -*-
21
from __future__ import annotations
32

43
from typing import (
@@ -12,6 +11,7 @@
1211
Type,
1312
Union,
1413
cast,
14+
overload,
1515
)
1616

1717
import functools
@@ -98,14 +98,27 @@ def fit_predict_try_except_decorator(
9898
queue.close()
9999

100100

101-
def get_cost_of_crash(metrics: Sequence[Scorer]) -> List[float] | float:
101+
@overload
102+
def get_cost_of_crash(metrics: Sequence[Scorer]) -> List[float]:
103+
...
104+
105+
106+
@overload
107+
def get_cost_of_crash(metrics: Scorer) -> float:
108+
...
109+
110+
111+
def get_cost_of_crash(metrics: Sequence[Scorer] | Scorer) -> List[float] | float:
102112
"""Return the cost of crash.
103113
104114
Return value can be either a list (multi-objective optimization) or a
105115
raw float (single objective) because SMAC assumes different types in the
106116
two different cases.
107117
"""
108118
costs = []
119+
if isinstance(metrics, Scorer):
120+
metrics = [metrics]
121+
109122
for metric in metrics:
110123
if not isinstance(metric, Scorer):
111124
raise ValueError("The metric {metric} must be an instance of Scorer")

autosklearn/evaluation/abstract_evaluator.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
SUPPORTED_TARGET_TYPES,
3030
SUPPORTED_XDATA_TYPES,
3131
)
32+
from autosklearn.data.validation import SUPPORTED_FEAT_TYPES
3233
from autosklearn.metrics import Scorer, calculate_losses
3334
from autosklearn.pipeline.components.base import ThirdPartyComponents, _addons
3435
from autosklearn.pipeline.implementations.util import (
@@ -164,8 +165,8 @@ def get_additional_run_info(self) -> Optional[TYPE_ADDITIONAL_INFO]:
164165
def _fit_and_suppress_warnings(
165166
logger: Union[logging.Logger, PicklableClientLogger],
166167
model: BaseEstimator,
167-
X: np.ndarray,
168-
y: np.ndarray,
168+
X: SUPPORTED_FEAT_TYPES,
169+
y: SUPPORTED_TARGET_TYPES,
169170
) -> BaseEstimator:
170171
def send_warnings_to_log(
171172
message: Union[Warning, str],

autosklearn/evaluation/train_evaluator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def _fit_with_budget(
115115
budget_type: Optional[str],
116116
logger: Union[logging.Logger, PicklableClientLogger],
117117
model: BaseEstimator,
118-
train_indices: List[int],
118+
train_indices: Sequence[int] | np.ndarray,
119119
task_type: int,
120120
) -> None:
121121
if (

autosklearn/metrics/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ def make_scorer(
431431

432432
def _validate_metrics(
433433
metrics: Sequence[Scorer],
434-
scoring_functions: Optional[List[Scorer]] = None,
434+
scoring_functions: Optional[Sequence[Scorer]] = None,
435435
) -> None:
436436
"""
437437
Validate metrics given to Auto-sklearn. Raises an Exception in case of a problem.

autosklearn/pipeline/components/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ def get_estimator(self):
338338
return self.estimator
339339

340340

341-
class AutoSklearnChoice(object):
341+
class AutoSklearnChoice:
342342
def __init__(self, dataset_properties, random_state=None):
343343
"""
344344
Parameters
@@ -370,6 +370,7 @@ def __init__(self, dataset_properties, random_state=None):
370370
# self.set_hyperparameters(self.configuration)
371371
self.choice = None
372372

373+
@classmethod
373374
def get_components(cls):
374375
raise NotImplementedError()
375376

autosklearn/pipeline/components/data_preprocessing/categorical_encoding/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def add_ohe(ohe: "OHEChoice") -> None:
3030

3131
class OHEChoice(AutoSklearnChoice):
3232
@classmethod
33-
def get_components(cls: BaseEstimator) -> Dict[str, BaseEstimator]:
33+
def get_components(cls) -> Dict[str, BaseEstimator]:
3434
components: Dict[str, BaseEstimator] = OrderedDict()
3535
components.update(_ohes)
3636
components.update(additional_components.components)

autosklearn/pipeline/components/data_preprocessing/minority_coalescense/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def add_mc(mc: BaseEstimator) -> None:
3030

3131
class CoalescenseChoice(AutoSklearnChoice):
3232
@classmethod
33-
def get_components(cls: BaseEstimator) -> Dict[str, BaseEstimator]:
33+
def get_components(cls) -> Dict[str, BaseEstimator]:
3434
components: Dict[str, BaseEstimator] = OrderedDict()
3535
components.update(_mcs)
3636
components.update(additional_components.components)

0 commit comments

Comments
 (0)