Skip to content

mypy: Fix all no-untyped-call and no-any-return #368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,13 @@ reportUnsupportedDunderAll = "error"
[tool.mypy]
python_version = "3.9" # Target oldest supported Python version
strict = true
check_untyped_defs = true # Strict check on all defs
show_column_numbers = true
warn_unused_ignores = false # Change from pandas
# Partial stubs are acceptable
disallow_any_generics = false
disallow_incomplete_defs = false
disallow_untyped_defs = false
# Allow dynamic typing in our own code
warn_return_any = false # TODO
disallow_untyped_calls = false # TODO
check_untyped_defs = true
# Suppressing errors
disable_error_code = [
# Not all imports in these stubs are gonna be typed
Expand Down
34 changes: 26 additions & 8 deletions stubs/matplotlib/colors.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -128,24 +128,42 @@ class CenteredNorm(Normalize):
def __call__(self, value, clip: bool = ...): ...

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

@make_norm_from_scale(functools.partial(LogScale, nonpositive="mask"))
class FuncNorm(Normalize):
def __init__(
self,
functions: tuple[Callable, Callable],
vmin: float | None = ...,
vmax: float | None = ...,
clip: bool = ...,
) -> None: ...

class LogNorm(Normalize): ...

@make_norm_from_scale(
SymmetricalLogScale,
init=lambda linthresh, linscale=1, vmin=None, vmax=None, clip=False, *, base=10: None,
)
class SymLogNorm(Normalize):
def __init__(
self,
linthresh: float,
linscale: float = ...,
vmin: float | None = ...,
vmax: float | None = ...,
clip: bool = ...,
*,
base: float = ...,
) -> None: ...
@property
def linthresh(self): ...
@linthresh.setter
def linthresh(self, value): ...

@make_norm_from_scale(AsinhScale, init=lambda linear_width=1, vmin=None, vmax=None, clip=False: None)
class AsinhNorm(Normalize):
def __init__(
self,
linear_width: float = ...,
vmin: float | None = ...,
vmax: float | None = ...,
clip: bool = ...,
) -> None: ...
@property
def linear_width(self): ...
@linear_width.setter
Expand Down
2 changes: 1 addition & 1 deletion stubs/matplotlib/transforms.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class Transform(TransformNode):
output_dims = ...
is_separable = ...
has_inverse = ...
def __init_subclass__(cls): ...
def __init_subclass__(cls) -> None: ...
def __add__(self, other: Transform) -> Transform: ...
@property
def depth(self) -> int: ...
Expand Down
4 changes: 2 additions & 2 deletions stubs/sympy-stubs/assumptions/assume.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ class Predicate(Boolean, metaclass=PredicateMeta):
@property
def name(self) -> str: ...
@classmethod
def register(cls, *types, **kwargs): ...
def register(cls, *types: type, **kwargs: object) -> Callable[..., None]: ...
@classmethod
def register_many(cls, *types, **kwargs) -> Callable[..., None]: ...
def register_many(cls, *types: type, **kwargs: object) -> Callable[..., None]: ...
def __call__(self, *args) -> AppliedPredicate: ...
def eval(self, args, assumptions=...) -> None: ...

Expand Down
8 changes: 4 additions & 4 deletions tests/run_hygiene.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
from pathlib import Path


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


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


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


def main():
def main() -> None:
os.chdir(Path(__file__).parent.parent)

install_requirements()
Expand Down
8 changes: 4 additions & 4 deletions tests/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@
from pathlib import Path


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


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


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


def main():
def main() -> None:
os.chdir(Path(__file__).parent.parent)

install_requirements()
Expand Down
3 changes: 2 additions & 1 deletion utils/count_ids.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/python
from __future__ import annotations

__doc__ = """Count IDs.

Expand All @@ -23,7 +24,7 @@
import docopt


def count(root, suffix, regex, uniq):
def count(root: str | None, suffix: str | None, regex: str | re.Pattern[str] | None, uniq: bool) -> None:
if root is None:
root = "."
filepat = "*" if suffix is None else "*." + suffix[suffix.find(".") + 1 :]
Expand Down
20 changes: 11 additions & 9 deletions utils/validate_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def import_dual(m: str, stub_path: str) -> tuple:

"""

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

def __init__(
self, file: str, module: str, name: str, object_: object, type_: ItemType, children: dict[str, Item] | None = None
):
) -> None:
self.file = file
self.module = module
self.name = name
Expand All @@ -109,25 +109,25 @@ def __init__(
self.done = False
self.analog: Item | None = None

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

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

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

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

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

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


Expand All @@ -144,7 +144,9 @@ def isfrompackage(v: object, path: str) -> bool:
def isfrommodule(v: object, module: str, default: bool = True) -> bool:
try:
# Make sure it came from this module
return v.__dict__["__module__"] == module
__module__ = v.__dict__["__module__"]
assert isinstance(__module__, str)
return module == __module__
except Exception:
return default

Expand Down