Skip to content

Make mypy a bit stricter #27796

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 4 commits into from
Feb 16, 2024
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
3 changes: 1 addition & 2 deletions .github/workflows/reviewdog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ jobs:
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -o pipefail
mypy --config pyproject.toml lib/matplotlib \
--follow-imports silent | \
mypy --config pyproject.toml | \
reviewdog -f=mypy -name=mypy \
-tee -reporter=github-check -filter-mode nofilter

Expand Down
2 changes: 2 additions & 0 deletions lib/matplotlib/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ def matplotlib_fname() -> str: ...
class RcParams(dict[str, Any]):
validate: dict[str, Callable]
def __init__(self, *args, **kwargs) -> None: ...
def _set(self, key: str, val: Any) -> None: ...
def _get(self, key: str) -> Any: ...
def __setitem__(self, key: str, val: Any) -> None: ...
def __getitem__(self, key: str) -> Any: ...
def __iter__(self) -> Generator[str, None, None]: ...
Expand Down
7 changes: 5 additions & 2 deletions lib/matplotlib/_type1font.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@
v1.1, 1993. ISBN 0-201-57044-0.
"""

from __future__ import annotations

import binascii
import functools
import logging
import re
import string
import struct
import typing as T

import numpy as np

Expand Down Expand Up @@ -171,7 +174,7 @@ def value(self):
return float(self.raw)


def _tokenize(data: bytes, skip_ws: bool):
def _tokenize(data: bytes, skip_ws: bool) -> T.Generator[_Token, int, None]:
"""
A generator that produces _Token instances from Type-1 font code.

Expand All @@ -194,7 +197,7 @@ def _tokenize(data: bytes, skip_ws: bool):
hex_re = re.compile(r'^<[0-9a-fA-F\0\t\r\f\n ]*>$')
oct_re = re.compile(r'[0-7]{1,3}')
pos = 0
next_binary = None
next_binary: int | None = None

while pos < len(text):
if next_binary is not None:
Expand Down
10 changes: 5 additions & 5 deletions lib/matplotlib/patheffects.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Normal(AbstractPathEffect): ...
class Stroke(AbstractPathEffect):
def __init__(self, offset: tuple[float, float] = ..., **kwargs) -> None: ...
# rgbFace becomes non-optional
def draw_path(self, renderer: RendererBase, gc: GraphicsContextBase, tpath: Path, affine: Transform, rgbFace: ColorType) -> None: ... # type: ignore
def draw_path(self, renderer: RendererBase, gc: GraphicsContextBase, tpath: Path, affine: Transform, rgbFace: ColorType) -> None: ... # type: ignore[override]

class withStroke(Stroke): ...

Expand All @@ -69,7 +69,7 @@ class SimplePatchShadow(AbstractPathEffect):
**kwargs
) -> None: ...
# rgbFace becomes non-optional
def draw_path(self, renderer: RendererBase, gc: GraphicsContextBase, tpath: Path, affine: Transform, rgbFace: ColorType) -> None: ... # type: ignore
def draw_path(self, renderer: RendererBase, gc: GraphicsContextBase, tpath: Path, affine: Transform, rgbFace: ColorType) -> None: ... # type: ignore[override]

class withSimplePatchShadow(SimplePatchShadow): ...

Expand All @@ -83,13 +83,13 @@ class SimpleLineShadow(AbstractPathEffect):
**kwargs
) -> None: ...
# rgbFace becomes non-optional
def draw_path(self, renderer: RendererBase, gc: GraphicsContextBase, tpath: Path, affine: Transform, rgbFace: ColorType) -> None: ... # type: ignore
def draw_path(self, renderer: RendererBase, gc: GraphicsContextBase, tpath: Path, affine: Transform, rgbFace: ColorType) -> None: ... # type: ignore[override]

class PathPatchEffect(AbstractPathEffect):
patch: Patch
def __init__(self, offset: tuple[float, float] = ..., **kwargs) -> None: ...
# rgbFace becomes non-optional
def draw_path(self, renderer: RendererBase, gc: GraphicsContextBase, tpath: Path, affine: Transform, rgbFace: ColorType) -> None: ... # type: ignore
def draw_path(self, renderer: RendererBase, gc: GraphicsContextBase, tpath: Path, affine: Transform, rgbFace: ColorType) -> None: ... # type: ignore[override]

class TickedStroke(AbstractPathEffect):
def __init__(
Expand All @@ -101,6 +101,6 @@ class TickedStroke(AbstractPathEffect):
**kwargs
) -> None: ...
# rgbFace becomes non-optional
def draw_path(self, renderer: RendererBase, gc: GraphicsContextBase, tpath: Path, affine: Transform, rgbFace: ColorType) -> None: ... # type: ignore
def draw_path(self, renderer: RendererBase, gc: GraphicsContextBase, tpath: Path, affine: Transform, rgbFace: ColorType) -> None: ... # type: ignore[override]

class withTickedStroke(TickedStroke): ...
9 changes: 7 additions & 2 deletions lib/matplotlib/projections/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from .geo import AitoffAxes, HammerAxes, LambertAxes, MollweideAxes
from .polar import PolarAxes
from .geo import (
AitoffAxes as AitoffAxes,
HammerAxes as HammerAxes,
LambertAxes as LambertAxes,
MollweideAxes as MollweideAxes,
)
from .polar import PolarAxes as PolarAxes
from ..axes import Axes

class ProjectionRegistry:
Expand Down
16 changes: 8 additions & 8 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
from matplotlib.artist import Artist
from matplotlib.axes import Axes
from matplotlib.axes import Subplot # noqa: F401
from matplotlib.projections import PolarAxes # type: ignore
from matplotlib.projections import PolarAxes
from matplotlib import mlab # for detrend_none, window_hanning
from matplotlib.scale import get_scale_names # noqa: F401

Expand Down Expand Up @@ -224,7 +224,7 @@ def install_repl_displayhook() -> None:
ip.events.register("post_execute", _draw_all_if_interactive)
_REPL_DISPLAYHOOK = _ReplDisplayHook.IPYTHON

from IPython.core.pylabtools import backend2gui # type: ignore
from IPython.core.pylabtools import backend2gui
# trigger IPython's eventloop integration, if available
ipython_gui_name = backend2gui.get(get_backend())
if ipython_gui_name:
Expand All @@ -235,7 +235,7 @@ def uninstall_repl_displayhook() -> None:
"""Disconnect from the display hook of the current shell."""
global _REPL_DISPLAYHOOK
if _REPL_DISPLAYHOOK is _ReplDisplayHook.IPYTHON:
from IPython import get_ipython # type: ignore
from IPython import get_ipython
ip = get_ipython()
ip.events.unregister("post_execute", _draw_all_if_interactive)
_REPL_DISPLAYHOOK = _ReplDisplayHook.NONE
Expand Down Expand Up @@ -274,7 +274,7 @@ def _get_backend_mod() -> type[matplotlib.backend_bases._Backend]:
# Use rcParams._get("backend") to avoid going through the fallback
# logic (which will (re)import pyplot and then call switch_backend if
# we need to resolve the auto sentinel)
switch_backend(rcParams._get("backend")) # type: ignore[attr-defined]
switch_backend(rcParams._get("backend"))
return cast(type[matplotlib.backend_bases._Backend], _backend_mod)


Expand Down Expand Up @@ -744,7 +744,7 @@ def xkcd(
"xkcd mode is not compatible with text.usetex = True")

stack = ExitStack()
stack.callback(dict.update, rcParams, rcParams.copy()) # type: ignore
stack.callback(dict.update, rcParams, rcParams.copy()) # type: ignore[arg-type]

from matplotlib import patheffects
rcParams.update({
Expand Down Expand Up @@ -2501,10 +2501,10 @@ def polar(*args, **kwargs) -> list[Line2D]:
# requested, ignore rcParams['backend'] and force selection of a backend that
# is compatible with the current running interactive framework.
if (rcParams["backend_fallback"]
and rcParams._get_backend_or_none() in ( # type: ignore
and rcParams._get_backend_or_none() in ( # type: ignore[attr-defined]
set(rcsetup.interactive_bk) - {'WebAgg', 'nbAgg'})
and cbook._get_running_interactive_framework()): # type: ignore
rcParams._set("backend", rcsetup._auto_backend_sentinel) # type: ignore
and cbook._get_running_interactive_framework()):
rcParams._set("backend", rcsetup._auto_backend_sentinel)

# fmt: on

Expand Down
10 changes: 10 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ convention = "numpy"

[tool.mypy]
ignore_missing_imports = true
enable_error_code = [
"ignore-without-code",
"redundant-expr",
"truthy-bool",
]
enable_incomplete_feature = [
"Unpack",
]
Expand All @@ -227,6 +232,11 @@ exclude = [
# stubtest will import and run, opening a figure if not excluded
".*/tinypages"
]
files = [
"lib/matplotlib",
]
follow_imports = "silent"
warn_unreachable = true

[tool.rstcheck]
ignore_directives = [
Expand Down