Skip to content

Commit 994e586

Browse files
authored
mypy: Fix import-untyped and most misc issues (#378)
1 parent e049074 commit 994e586

File tree

13 files changed

+143
-94
lines changed

13 files changed

+143
-94
lines changed

pyproject.toml

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ tests = [
2121
"scipy-stubs",
2222
"typing_extensions",
2323

24-
# Untyped libraries, used to prevent "reportMissingImports" and get inferred typing
24+
# Untyped libraries and partial stubs. Used to prevent "reportMissingImports" and get inferred typing
2525
"joblib",
2626
"networkx",
2727
"PyOpenGL",
@@ -142,15 +142,16 @@ python_version = "3.9" # Target oldest supported Python version
142142
strict = true
143143
check_untyped_defs = true # Strict check on all defs
144144
show_column_numbers = true
145+
# Not all imports in these stubs are gonna be typed
146+
# Don't infer symbols from untyped packages as Any
147+
follow_untyped_imports = true
145148
warn_unused_ignores = false # Change from pandas
146149
# Partial stubs are acceptable
147150
disallow_any_generics = false
148151
disallow_incomplete_defs = false
149152
disallow_untyped_defs = false
150153
# Suppressing errors
151154
disable_error_code = [
152-
# Not all imports in these stubs are gonna be typed
153-
"import-untyped",
154155
# mypy's overload implementation differs from pyright
155156
# `assert-type` issues is tests mostly comme from checking overloads
156157
# Since this project is specific to Pylance, just ignore them
@@ -159,6 +160,18 @@ disable_error_code = [
159160
# as they are inherited from the implementation.
160161
"override",
161162
# TODO
162-
"assignment", # 773 errors in 172 files
163-
"misc", # 692 errors in 132 files
163+
"assignment", # 744 errors in 155 files
164164
]
165+
166+
[[tool.mypy.overrides]]
167+
# These modules are to be removed soon, not worth solving many issues
168+
module = ["matplotlib.*", "networkx.*"]
169+
disable_error_code = [
170+
"assignment",
171+
"misc",
172+
]
173+
[[tool.mypy.overrides]]
174+
module = ["sympy.*", "skimage.*", "sklearn.*"]
175+
# TODO: Too many untyped decorators still left
176+
# https://github.com/python/mypy/issues/19148
177+
disable_error_code = ["misc"]

stubs/skimage/__init__.pyi

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ from ._shared.version_requirements import ensure_python_version as ensure_python
44

55
__version__: str = ...
66
submodules: list = ...
7-
__getattr__ = ...
87
__lazy_dir__ = ...
98

109
def __dir__(): ...

stubs/skimage/data/__init__.pyi

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,88 @@
1-
from .._shared import lazy as lazy
1+
from ._binary_blobs import binary_blobs
2+
from ._fetchers import (
3+
astronaut,
4+
brain,
5+
brick,
6+
camera,
7+
cat,
8+
cell,
9+
cells3d,
10+
checkerboard,
11+
chelsea,
12+
clock,
13+
coffee,
14+
coins,
15+
colorwheel,
16+
data_dir,
17+
download_all,
18+
eagle,
19+
file_hash,
20+
grass,
21+
gravel,
22+
horse,
23+
hubble_deep_field,
24+
human_mitosis,
25+
immunohistochemistry,
26+
kidney,
27+
lbp_frontal_face_cascade_filename,
28+
lfw_subset,
29+
lily,
30+
logo,
31+
microaneurysms,
32+
moon,
33+
nickel_solidification,
34+
page,
35+
palisades_of_vogt as palisades_of_vogt,
36+
protein_transport,
37+
retina,
38+
rocket,
39+
shepp_logan_phantom,
40+
skin,
41+
stereo_motorcycle,
42+
text,
43+
vortex,
44+
)
245

3-
__getattr__ = ...
4-
__dir__ = ...
5-
__all__ = ... # pyright: ignore[reportUnsupportedDunderAll] # TODO
46+
__all__ = [
47+
"astronaut",
48+
"binary_blobs",
49+
"brain",
50+
"brick",
51+
"camera",
52+
"cat",
53+
"cell",
54+
"cells3d",
55+
"checkerboard",
56+
"chelsea",
57+
"clock",
58+
"coffee",
59+
"coins",
60+
"colorwheel",
61+
"data_dir",
62+
"download_all",
63+
"eagle",
64+
"file_hash",
65+
"grass",
66+
"gravel",
67+
"horse",
68+
"hubble_deep_field",
69+
"human_mitosis",
70+
"immunohistochemistry",
71+
"kidney",
72+
"lbp_frontal_face_cascade_filename",
73+
"lfw_subset",
74+
"lily",
75+
"logo",
76+
"microaneurysms",
77+
"moon",
78+
"nickel_solidification",
79+
"page",
80+
"protein_transport",
81+
"retina",
82+
"rocket",
83+
"shepp_logan_phantom",
84+
"skin",
85+
"stereo_motorcycle",
86+
"text",
87+
"vortex",
88+
]

stubs/skimage/data/_fetchers.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from os import PathLike
22

3+
def file_hash(fname, alg="sha256") -> str: ...
4+
35
legacy_data_dir = ...
46
skimage_distribution_dir = ...
57

@@ -46,6 +48,7 @@ def hubble_deep_field(): ...
4648
def retina(): ...
4749
def shepp_logan_phantom(): ...
4850
def colorwheel(): ...
51+
def palisades_of_vogt(): ...
4952
def rocket(): ...
5053
def stereo_motorcycle(): ...
5154
def lfw_subset(): ...

stubs/sklearn/gaussian_process/kernels.pyi

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,6 @@ class Hyperparameter(NamedTuple):
2323
value_type: str
2424
name: str
2525

26-
# A raw namedtuple is very memory efficient as it packs the attributes
27-
# in a struct to get rid of the __dict__ of attributes in particular it
28-
# does not copy the string for the keys on each instance.
29-
# By deriving a namedtuple class just to introduce the __init__ method we
30-
# would also reintroduce the __dict__ on the instance. By telling the
31-
# Python interpreter that this subclass uses static __slots__ instead of
32-
# dynamic attributes. Furthermore we don't need any additional slot in the
33-
# subclass so we set __slots__ to the empty tuple.
34-
__slots__ = ...
35-
36-
def __new__(
37-
cls,
38-
name: str,
39-
value_type: str,
40-
bounds: ndarray | str | tuple[float, int] | tuple[float, float],
41-
n_elements: int = 1,
42-
fixed=None,
43-
) -> Self: ...
44-
45-
# This is mainly a testing utility to check that two hyperparameters
46-
# are equal.
47-
def __eq__(self, other) -> bool: ...
48-
4926
class Kernel(metaclass=ABCMeta):
5027
def get_params(self, deep: bool = True) -> dict: ...
5128
def set_params(self, **params) -> Self: ...

stubs/sklearn/metrics/_regression.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ def mean_squared_error(
5656
sample_weight: None | ArrayLike = None,
5757
multioutput: ArrayLike | Literal["raw_values", "uniform_average"] = "uniform_average",
5858
) -> ndarray | Float: ...
59+
@overload
5960
@deprecated(
6061
"`squared` is deprecated in 1.4 and will be removed in 1.6. Use `root_mean_squared_error` instead to calculate the root mean squared error."
6162
)
62-
@overload
6363
def mean_squared_error(
6464
y_true: MatrixLike | ArrayLike,
6565
y_pred: MatrixLike | ArrayLike,
@@ -76,10 +76,10 @@ def mean_squared_log_error(
7676
sample_weight: None | ArrayLike = None,
7777
multioutput: ArrayLike | Literal["raw_values", "uniform_average"] = "uniform_average",
7878
) -> float | ndarray: ...
79+
@overload
7980
@deprecated(
8081
"`squared` is deprecated in 1.4 and will be removed in 1.6. Use `root_mean_squared_log_error` instead to calculate the root mean squared logarithmic error."
8182
)
82-
@overload
8383
def mean_squared_log_error(
8484
y_true: MatrixLike | ArrayLike,
8585
y_pred: MatrixLike | ArrayLike,

stubs/sklearn/preprocessing/_data.pyi

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,10 @@ class StandardScaler(OneToOneFeatureMixin, TransformerMixin, BaseEstimator):
112112
def transform(self, X: spmatrix, copy: None | bool = None) -> spmatrix: ...
113113
@overload
114114
def transform(self, X: ArrayLike, copy: None | bool = None) -> ndarray: ...
115-
def transform(self, X: MatrixLike, copy: None | bool = None) -> ndarray | spmatrix: ...
116115
@overload
117116
def inverse_transform(self, X: spmatrix, copy: None | bool = None) -> spmatrix: ...
118117
@overload
119118
def inverse_transform(self, X: ArrayLike, copy: None | bool = None) -> ndarray: ...
120-
def inverse_transform(self, X: MatrixLike | ArrayLike, copy: None | bool = None) -> ndarray | spmatrix: ...
121119

122120
class MaxAbsScaler(OneToOneFeatureMixin, TransformerMixin, BaseEstimator):
123121
n_samples_seen_: int = ...
@@ -135,12 +133,10 @@ class MaxAbsScaler(OneToOneFeatureMixin, TransformerMixin, BaseEstimator):
135133
def transform(self, X: spmatrix) -> spmatrix: ...
136134
@overload
137135
def transform(self, X: ArrayLike) -> ndarray: ...
138-
def transform(self, X: MatrixLike | ArrayLike) -> ndarray | spmatrix: ...
139136
@overload
140137
def inverse_transform(self, X: spmatrix) -> spmatrix: ...
141138
@overload
142139
def inverse_transform(self, X: ArrayLike) -> ndarray: ...
143-
def inverse_transform(self, X: MatrixLike | ArrayLike) -> ndarray | spmatrix: ...
144140

145141
def maxabs_scale(X: MatrixLike | ArrayLike, *, axis: Int = 0, copy: bool = True): ...
146142

@@ -166,12 +162,10 @@ class RobustScaler(OneToOneFeatureMixin, TransformerMixin, BaseEstimator):
166162
def transform(self, X: spmatrix) -> spmatrix: ...
167163
@overload
168164
def transform(self, X: ArrayLike) -> ndarray: ...
169-
def transform(self, X: MatrixLike | ArrayLike) -> ndarray | spmatrix: ...
170165
@overload
171166
def inverse_transform(self, X: spmatrix) -> spmatrix: ...
172167
@overload
173168
def inverse_transform(self, X: ArrayLike) -> ndarray: ...
174-
def inverse_transform(self, X: MatrixLike | ArrayLike) -> ndarray | spmatrix: ...
175169

176170
@overload
177171
def robust_scale(
@@ -186,7 +180,7 @@ def robust_scale(
186180
) -> spmatrix: ...
187181
@overload
188182
def robust_scale(
189-
X: ndarray,
183+
X: ArrayLike,
190184
*,
191185
axis: Int = 0,
192186
with_centering: bool = True,
@@ -195,16 +189,6 @@ def robust_scale(
195189
copy: bool = True,
196190
unit_variance: bool = False,
197191
) -> ndarray: ...
198-
def robust_scale(
199-
X: MatrixLike,
200-
*,
201-
axis: Int = 0,
202-
with_centering: bool = True,
203-
with_scaling: bool = True,
204-
quantile_range: tuple[float, float] = ...,
205-
copy: bool = True,
206-
unit_variance: bool = False,
207-
) -> ndarray | spmatrix: ...
208192
@overload
209193
def normalize(
210194
X: spmatrix,
@@ -241,14 +225,6 @@ def normalize(
241225
copy: bool = True,
242226
return_norm: Literal[False] = False,
243227
) -> ndarray: ...
244-
def normalize(
245-
X: MatrixLike | ArrayLike,
246-
norm: Literal["l1", "l2", "max"] = "l2",
247-
*,
248-
axis: int = 1,
249-
copy: bool = True,
250-
return_norm: bool = False,
251-
) -> csr_matrix | tuple[ndarray | spmatrix, ndarray] | ndarray: ...
252228

253229
class Normalizer(OneToOneFeatureMixin, TransformerMixin, BaseEstimator):
254230
feature_names_in_: ndarray = ...
@@ -262,7 +238,6 @@ class Normalizer(OneToOneFeatureMixin, TransformerMixin, BaseEstimator):
262238
def transform(self, X: spmatrix, copy: None | bool = None) -> spmatrix: ...
263239
@overload
264240
def transform(self, X: ArrayLike, copy: None | bool = None) -> ndarray: ...
265-
def transform(self, X: MatrixLike | ArrayLike, copy: None | bool = None) -> ndarray | spmatrix: ...
266241

267242
def binarize(X: MatrixLike | ArrayLike, *, threshold: Float = 0.0, copy: bool = True) -> ndarray | spmatrix: ...
268243

@@ -278,7 +253,6 @@ class Binarizer(OneToOneFeatureMixin, TransformerMixin, BaseEstimator):
278253
def transform(self, X: spmatrix, copy: None | bool = None) -> spmatrix: ...
279254
@overload
280255
def transform(self, X: ArrayLike, copy: None | bool = None) -> ndarray: ...
281-
def transform(self, X: MatrixLike | ArrayLike, copy: None | bool = None) -> ndarray | spmatrix: ...
282256

283257
class KernelCenterer(ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator):
284258
feature_names_in_: ndarray = ...
@@ -294,7 +268,6 @@ class KernelCenterer(ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEsti
294268
def add_dummy_feature(X: spmatrix, value: Float = 1.0) -> spmatrix: ...
295269
@overload
296270
def add_dummy_feature(X: ArrayLike, value: Float = 1.0) -> ndarray: ...
297-
def add_dummy_feature(X: MatrixLike | ArrayLike, value: Float = 1.0) -> ndarray | spmatrix: ...
298271

299272
class QuantileTransformer(OneToOneFeatureMixin, TransformerMixin, BaseEstimator):
300273
feature_names_in_: ndarray = ...
@@ -324,12 +297,10 @@ class QuantileTransformer(OneToOneFeatureMixin, TransformerMixin, BaseEstimator)
324297
def transform(self, X: spmatrix) -> spmatrix: ...
325298
@overload
326299
def transform(self, X: ArrayLike) -> ndarray: ...
327-
def transform(self, X: MatrixLike | ArrayLike) -> ndarray | spmatrix: ...
328300
@overload
329301
def inverse_transform(self, X: spmatrix) -> spmatrix: ...
330302
@overload
331303
def inverse_transform(self, X: ArrayLike) -> ndarray: ...
332-
def inverse_transform(self, X: MatrixLike | ArrayLike) -> ndarray | spmatrix: ...
333304

334305
@overload
335306
def quantile_transform(
@@ -355,17 +326,6 @@ def quantile_transform(
355326
random_state: RandomState | None | Int = None,
356327
copy: bool = True,
357328
) -> ndarray: ...
358-
def quantile_transform(
359-
X: MatrixLike | ArrayLike,
360-
*,
361-
axis: Int = 0,
362-
n_quantiles: Int = 1000,
363-
output_distribution: Literal["uniform", "normal"] = "uniform",
364-
ignore_implicit_zeros: bool = False,
365-
subsample: Int = ...,
366-
random_state: RandomState | None | Int = None,
367-
copy: bool = True,
368-
) -> ndarray | spmatrix: ...
369329

370330
class PowerTransformer(OneToOneFeatureMixin, TransformerMixin, BaseEstimator):
371331
feature_names_in_: ndarray = ...

stubs/sklearn/utils/_isfinite.pyi

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
from enum import IntEnum
2+
from typing import Final
23

34
import numpy as np
45

5-
class FiniteStatus(IntEnum): ...
6+
__test__: dict
7+
8+
class FiniteStatus(IntEnum):
9+
all_finite = 0
10+
has_nan = 1
11+
has_infinite = 2
12+
13+
all_finite: Final = FiniteStatus.all_finite
14+
has_nan: Final = FiniteStatus.has_nan
15+
has_infinite: Final = FiniteStatus.has_infinite
616

717
def cy_isfinite(a: np.ndarray, allow_nan: bool = False) -> bool: ...
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from sympy.core._print_helpers import Printable as Printable
2+
3+
DefaultPrinting = Printable

stubs/transformers-stubs/models/auto/configuration_auto.pyi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ from typing import Any, NoReturn, TypeVar
55

66
from transformers.configuration_utils import PretrainedConfig
77

8-
_F = TypeVar("_F", bound=Callable[..., Any])
8+
_CallableT = TypeVar("_CallableT", bound=Callable)
99

1010
CONFIG_MAPPING_NAMES: OrderedDict[str, str]
1111
MODEL_NAMES_MAPPING: OrderedDict[str, str]
@@ -35,7 +35,9 @@ class _LazyLoadAllMappings(OrderedDict[str, str]):
3535
def __iter__(self) -> Iterator[str]: ...
3636
def __contains__(self, item: object) -> bool: ...
3737

38-
def replace_list_option_in_docstrings(config_to_class=None, use_model_types: bool = True) -> Callable[[_F], _F]: ...
38+
def replace_list_option_in_docstrings(
39+
config_to_class=None, use_model_types: bool = True
40+
) -> Callable[[_CallableT], _CallableT]: ...
3941

4042
class AutoConfig:
4143
def __init__(self) -> None: ...

0 commit comments

Comments
 (0)