Skip to content

Commit 1321839

Browse files
authored
mypy: Fix all no-untyped-call and no-any-return (#368)
1 parent d7d8ff9 commit 1321839

File tree

8 files changed

+51
-33
lines changed

8 files changed

+51
-33
lines changed

pyproject.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,13 @@ reportUnsupportedDunderAll = "error"
137137
[tool.mypy]
138138
python_version = "3.9" # Target oldest supported Python version
139139
strict = true
140+
check_untyped_defs = true # Strict check on all defs
140141
show_column_numbers = true
141142
warn_unused_ignores = false # Change from pandas
142143
# Partial stubs are acceptable
143144
disallow_any_generics = false
144145
disallow_incomplete_defs = false
145146
disallow_untyped_defs = false
146-
# Allow dynamic typing in our own code
147-
warn_return_any = false # TODO
148-
disallow_untyped_calls = false # TODO
149-
check_untyped_defs = true
150147
# Suppressing errors
151148
disable_error_code = [
152149
# Not all imports in these stubs are gonna be typed

stubs/matplotlib/colors.pyi

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,24 +128,42 @@ class CenteredNorm(Normalize):
128128
def __call__(self, value, clip: bool = ...): ...
129129

130130
def make_norm_from_scale(scale_cls, base_norm_cls=..., *, init=...): ...
131-
@make_norm_from_scale(FuncScale, init=lambda functions, vmin=None, vmax=None, clip=False: None)
132-
class FuncNorm(Normalize): ...
133131

134-
@make_norm_from_scale(functools.partial(LogScale, nonpositive="mask"))
132+
class FuncNorm(Normalize):
133+
def __init__(
134+
self,
135+
functions: tuple[Callable, Callable],
136+
vmin: float | None = ...,
137+
vmax: float | None = ...,
138+
clip: bool = ...,
139+
) -> None: ...
140+
135141
class LogNorm(Normalize): ...
136142

137-
@make_norm_from_scale(
138-
SymmetricalLogScale,
139-
init=lambda linthresh, linscale=1, vmin=None, vmax=None, clip=False, *, base=10: None,
140-
)
141143
class SymLogNorm(Normalize):
144+
def __init__(
145+
self,
146+
linthresh: float,
147+
linscale: float = ...,
148+
vmin: float | None = ...,
149+
vmax: float | None = ...,
150+
clip: bool = ...,
151+
*,
152+
base: float = ...,
153+
) -> None: ...
142154
@property
143155
def linthresh(self): ...
144156
@linthresh.setter
145157
def linthresh(self, value): ...
146158

147-
@make_norm_from_scale(AsinhScale, init=lambda linear_width=1, vmin=None, vmax=None, clip=False: None)
148159
class AsinhNorm(Normalize):
160+
def __init__(
161+
self,
162+
linear_width: float = ...,
163+
vmin: float | None = ...,
164+
vmax: float | None = ...,
165+
clip: bool = ...,
166+
) -> None: ...
149167
@property
150168
def linear_width(self): ...
151169
@linear_width.setter

stubs/matplotlib/transforms.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ class Transform(TransformNode):
199199
output_dims = ...
200200
is_separable = ...
201201
has_inverse = ...
202-
def __init_subclass__(cls): ...
202+
def __init_subclass__(cls) -> None: ...
203203
def __add__(self, other: Transform) -> Transform: ...
204204
@property
205205
def depth(self) -> int: ...

stubs/sympy-stubs/assumptions/assume.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ class Predicate(Boolean, metaclass=PredicateMeta):
3535
@property
3636
def name(self) -> str: ...
3737
@classmethod
38-
def register(cls, *types, **kwargs): ...
38+
def register(cls, *types: type, **kwargs: object) -> Callable[..., None]: ...
3939
@classmethod
40-
def register_many(cls, *types, **kwargs) -> Callable[..., None]: ...
40+
def register_many(cls, *types: type, **kwargs: object) -> Callable[..., None]: ...
4141
def __call__(self, *args) -> AppliedPredicate: ...
4242
def eval(self, args, assumptions=...) -> None: ...
4343

tests/run_hygiene.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@
44
from pathlib import Path
55

66

7-
def install_requirements():
7+
def install_requirements() -> None:
88
print("\nInstalling requirements...")
99
subprocess.check_call((sys.executable, "-m", "pip", "install", "pip>=25.1"))
1010
subprocess.check_call((sys.executable, "-m", "pip", "install", "--upgrade", "--group", "hygiene"))
1111

1212

13-
def run_ruff_fix():
13+
def run_ruff_fix() -> subprocess.CompletedProcess[bytes]:
1414
print("\nRunning Ruff check --fix...")
1515
return subprocess.run((sys.executable, "-m", "ruff", "check", "--fix"))
1616

1717

18-
def run_ruff_format():
18+
def run_ruff_format() -> subprocess.CompletedProcess[bytes]:
1919
print("\nRunning Ruff format...")
2020
return subprocess.run((sys.executable, "-m", "ruff", "format"))
2121

2222

23-
def main():
23+
def main() -> None:
2424
os.chdir(Path(__file__).parent.parent)
2525

2626
install_requirements()

tests/run_tests.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,26 @@
44
from pathlib import Path
55

66

7-
def install_requirements():
7+
def install_requirements() -> None:
88
print("\nInstalling requirements...")
99
subprocess.check_call((sys.executable, "-m", "pip", "install", "pip>=25.1"))
1010
subprocess.check_call((sys.executable, "-m", "pip", "install", "--upgrade", "--group", "tests"))
1111

1212

13-
def run_pyright():
13+
def run_pyright() -> subprocess.CompletedProcess[bytes]:
1414
print("\nRunning Pyright...")
1515
# https://github.com/RobertCraigie/pyright-python#keeping-pyright-and-pylance-in-sync
1616
os.environ.pop("PYRIGHT_PYTHON_FORCE_VERSION", None)
1717
os.environ["PYRIGHT_PYTHON_PYLANCE_VERSION"] = "latest-prerelease"
1818
return subprocess.run((sys.executable, "-m", "pyright"))
1919

2020

21-
def run_mypy():
21+
def run_mypy() -> subprocess.CompletedProcess[bytes]:
2222
print("\nRunning mypy...")
2323
return subprocess.run((sys.executable, "-m", "mypy", "."))
2424

2525

26-
def main():
26+
def main() -> None:
2727
os.chdir(Path(__file__).parent.parent)
2828

2929
install_requirements()

utils/count_ids.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/bin/python
2+
from __future__ import annotations
23

34
__doc__ = """Count IDs.
45
@@ -23,7 +24,7 @@
2324
import docopt
2425

2526

26-
def count(root, suffix, regex, uniq):
27+
def count(root: str | None, suffix: str | None, regex: str | re.Pattern[str] | None, uniq: bool) -> None:
2728
if root is None:
2829
root = "."
2930
filepat = "*" if suffix is None else "*." + suffix[suffix.find(".") + 1 :]

utils/validate_stubs.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def import_dual(m: str, stub_path: str) -> tuple:
6666
6767
"""
6868

69-
def _clean(m):
69+
def _clean(m: str) -> None:
7070
to_del = [k for k in sys.modules.keys() if k == m or k.startswith(m + ".")]
7171
for k in to_del:
7272
del sys.modules[k]
@@ -99,7 +99,7 @@ class ItemType(Enum):
9999

100100
def __init__(
101101
self, file: str, module: str, name: str, object_: object, type_: ItemType, children: dict[str, Item] | None = None
102-
):
102+
) -> None:
103103
self.file = file
104104
self.module = module
105105
self.name = name
@@ -109,25 +109,25 @@ def __init__(
109109
self.done = False
110110
self.analog: Item | None = None
111111

112-
def ismodule(self):
112+
def ismodule(self) -> bool:
113113
return self.type_ == Item.ItemType.MODULE
114114

115-
def isclass(self):
115+
def isclass(self) -> bool:
116116
return self.type_ == Item.ItemType.CLASS
117117

118-
def isfunction(self):
118+
def isfunction(self) -> bool:
119119
return self.type_ == Item.ItemType.FUNCTION
120120

121121
@staticmethod
122-
def make_function(file: str, module: str, name: str, object_: object):
122+
def make_function(file: str, module: str, name: str, object_: object) -> Item:
123123
return Item(file, module, name, object_, Item.ItemType.FUNCTION)
124124

125125
@staticmethod
126-
def make_class(file: str, module: str, name: str, object_: object, children: dict[str, Item]):
126+
def make_class(file: str, module: str, name: str, object_: object, children: dict[str, Item]) -> Item:
127127
return Item(file, module, name, object_, Item.ItemType.CLASS, children)
128128

129129
@staticmethod
130-
def make_module(file: str, module: str, name: str, object_: object, children: dict[str, Item]):
130+
def make_module(file: str, module: str, name: str, object_: object, children: dict[str, Item]) -> Item:
131131
return Item(file, module, name, object_, Item.ItemType.MODULE, children)
132132

133133

@@ -144,7 +144,9 @@ def isfrompackage(v: object, path: str) -> bool:
144144
def isfrommodule(v: object, module: str, default: bool = True) -> bool:
145145
try:
146146
# Make sure it came from this module
147-
return v.__dict__["__module__"] == module
147+
__module__ = v.__dict__["__module__"]
148+
assert isinstance(__module__, str)
149+
return module == __module__
148150
except Exception:
149151
return default
150152

0 commit comments

Comments
 (0)