Skip to content

Commit a8fad39

Browse files
committed
TYP: Use new syntax for Generic types
No longer do we need to define private `TypeVar` variables with semi-unique names, but instead a function/class/method-local type can be created. As these are now local, I have dropped the leading underscores.
1 parent 331fed4 commit a8fad39

18 files changed

Lines changed: 141 additions & 185 deletions

ci/mypy-stubtest-allowlist.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ matplotlib\.figure\.Figure\.set_tight_layout
3838
matplotlib\.tri\..*TriInterpolator\.__call__
3939
matplotlib\.tri\..*TriInterpolator\.gradient
4040

41-
# TypeVar used only in type hints
42-
matplotlib\.backend_bases\.FigureCanvasBase\._T
43-
matplotlib\.backend_managers\.ToolManager\._T
44-
matplotlib\.spines\.Spine\._T
45-
4641
# Parameter inconsistency due to 3.10 deprecation
4742
matplotlib\.figure\.FigureBase\.get_figure
4843

lib/matplotlib/_api/__init__.pyi

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections.abc import Callable, Generator, Iterable, Mapping, Sequence
2-
from typing import Any, TypeVar, overload
2+
from typing import Any, overload
33
from typing import Self
44

55
from numpy.typing import NDArray
@@ -16,15 +16,13 @@ from .deprecation import ( # noqa: F401, re-exported API
1616
MatplotlibDeprecationWarning as MatplotlibDeprecationWarning,
1717
)
1818

19-
_T = TypeVar("_T")
20-
2119
class _Unset: ...
2220
UNSET = _Unset()
2321

24-
class classproperty(Any):
22+
class classproperty[T](Any):
2523
def __init__(
2624
self,
27-
fget: Callable[[_T], Any],
25+
fget: Callable[[T], Any],
2826
fset: None = ...,
2927
fdel: None = ...,
3028
doc: str | None = None,
@@ -34,22 +32,22 @@ class classproperty(Any):
3432
@overload
3533
def __get__(self, instance: object, owner: type[object]) -> Any: ...
3634
@property
37-
def fget(self) -> Callable[[_T], Any]: ...
35+
def fget(self) -> Callable[[T], Any]: ...
3836

3937
def check_isinstance(
4038
types: type | tuple[type | None, ...], /, **kwargs: Any
4139
) -> None: ...
4240
def list_suggestion_error_msg(name: str, potential: Any, values: Sequence[Any]) -> str: ...
4341
def check_in_list(values: Sequence[Any], /, **kwargs: Any) -> None: ...
4442
def check_shape(shape: tuple[int | None, ...], /, **kwargs: NDArray) -> None: ...
45-
def getitem_checked(mapping: Mapping[Any, _T], /, _error_cls: type[Exception] = ..., **kwargs: Any) -> _T: ...
43+
def getitem_checked[T](mapping: Mapping[Any, T], /, _error_cls: type[Exception] = ..., **kwargs: Any) -> T: ...
4644
def caching_module_getattr(cls: type) -> Callable[[str], Any]: ...
4745
@overload
48-
def define_aliases(
46+
def define_aliases[T](
4947
alias_d: dict[str, list[str]], cls: None = ...
50-
) -> Callable[[type[_T]], type[_T]]: ...
48+
) -> Callable[[type[T]], type[T]]: ...
5149
@overload
52-
def define_aliases(alias_d: dict[str, list[str]], cls: type[_T]) -> type[_T]: ...
50+
def define_aliases[T](alias_d: dict[str, list[str]], cls: type[T]) -> type[T]: ...
5351
def select_matching_signature(
5452
funcs: list[Callable], *args: Any, **kwargs: Any
5553
) -> Any: ...
Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
from collections.abc import Callable
22
import contextlib
3-
from typing import Any, Literal, ParamSpec, TypedDict, TypeVar, Unpack, overload
4-
5-
_P = ParamSpec("_P")
6-
_R = TypeVar("_R")
7-
_T = TypeVar("_T")
3+
from typing import Any, Literal, TypedDict, Unpack, overload
84

95
class MatplotlibDeprecationWarning(DeprecationWarning): ...
106

@@ -20,9 +16,9 @@ class NamedDeprecationKwargs(DeprecationKwargs, total=False):
2016
name: str
2117

2218
def warn_deprecated(since: str, **kwargs: Unpack[NamedDeprecationKwargs]) -> None: ...
23-
def deprecated(
19+
def deprecated[T](
2420
since: str, **kwargs: Unpack[NamedDeprecationKwargs]
25-
) -> Callable[[_T], _T]: ...
21+
) -> Callable[[T], T]: ...
2622

2723
class deprecate_privatize_attribute(Any):
2824
def __init__(self, since: str, **kwargs: Unpack[NamedDeprecationKwargs]): ...
@@ -31,42 +27,42 @@ class deprecate_privatize_attribute(Any):
3127
DECORATORS: dict[Callable, Callable] = ...
3228

3329
@overload
34-
def rename_parameter(
30+
def rename_parameter[**P, R](
3531
since: str, old: str, new: str, func: None = ...
36-
) -> Callable[[Callable[_P, _R]], Callable[_P, _R]]: ...
32+
) -> Callable[[Callable[P, R]], Callable[P, R]]: ...
3733
@overload
38-
def rename_parameter(
39-
since: str, old: str, new: str, func: Callable[_P, _R]
40-
) -> Callable[_P, _R]: ...
34+
def rename_parameter[**P, R](
35+
since: str, old: str, new: str, func: Callable[P, R]
36+
) -> Callable[P, R]: ...
4137

4238
class _deprecated_parameter_class: ...
4339

4440
_deprecated_parameter: _deprecated_parameter_class
4541

4642
@overload
47-
def delete_parameter(
43+
def delete_parameter[**P, R](
4844
since: str, name: str, func: None = ..., **kwargs: Unpack[DeprecationKwargs]
49-
) -> Callable[[Callable[_P, _R]], Callable[_P, _R]]: ...
45+
) -> Callable[[Callable[P, R]], Callable[P, R]]: ...
5046
@overload
51-
def delete_parameter(
52-
since: str, name: str, func: Callable[_P, _R], **kwargs: Unpack[DeprecationKwargs]
53-
) -> Callable[_P, _R]: ...
47+
def delete_parameter[**P, R](
48+
since: str, name: str, func: Callable[P, R], **kwargs: Unpack[DeprecationKwargs]
49+
) -> Callable[P, R]: ...
5450
@overload
55-
def make_keyword_only(
51+
def make_keyword_only[**P, R](
5652
since: str, name: str, func: None = ...
57-
) -> Callable[[Callable[_P, _R]], Callable[_P, _R]]: ...
53+
) -> Callable[[Callable[P, R]], Callable[P, R]]: ...
5854
@overload
59-
def make_keyword_only(
60-
since: str, name: str, func: Callable[_P, _R]
61-
) -> Callable[_P, _R]: ...
62-
def deprecate_method_override(
63-
method: Callable[_P, _R],
55+
def make_keyword_only[**P, R](
56+
since: str, name: str, func: Callable[P, R]
57+
) -> Callable[P, R]: ...
58+
def deprecate_method_override[**P, R](
59+
method: Callable[P, R],
6460
obj: object | type,
6561
*,
6662
allow_empty: bool = ...,
6763
since: str,
6864
**kwargs: Unpack[NamedDeprecationKwargs]
69-
) -> Callable[_P, _R]: ...
65+
) -> Callable[P, R]: ...
7066
def suppress_matplotlib_deprecation_warning() -> (
7167
contextlib.AbstractContextManager[None]
7268
): ...

lib/matplotlib/_docstring.pyi

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
from collections.abc import Callable
2-
from typing import Any, TypeVar, overload
2+
from typing import Any, overload
33

44

5-
_T = TypeVar('_T')
6-
7-
8-
def kwarg_doc(text: str) -> Callable[[_T], _T]: ...
5+
def kwarg_doc[T](text: str) -> Callable[[T], T]: ...
96

107

118
class Substitution:
129
@overload
1310
def __init__(self, *args: str): ...
1411
@overload
1512
def __init__(self, **kwargs: str): ...
16-
def __call__(self, func: _T) -> _T: ...
13+
def __call__[T](self, func: T) -> T: ...
1714

1815

1916
class _ArtistKwdocLoader(dict[str, str]):
@@ -23,10 +20,10 @@ class _ArtistKwdocLoader(dict[str, str]):
2320
class _ArtistPropertiesSubstitution:
2421
def __init__(self) -> None: ...
2522
def register(self, **kwargs) -> None: ...
26-
def __call__(self, obj: _T) -> _T: ...
23+
def __call__[T](self, obj: T) -> T: ...
2724

2825

29-
def copy(source: Any) -> Callable[[_T], _T]: ...
26+
def copy[T](source: Any) -> Callable[[T], T]: ...
3027

3128

3229
dedent_interpd: _ArtistPropertiesSubstitution

lib/matplotlib/artist.pyi

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@ from .transforms import (
1515
import numpy as np
1616

1717
from collections.abc import Callable, Iterable
18-
from typing import Any, Literal, NamedTuple, TextIO, overload, TypeVar
18+
from typing import Any, Literal, NamedTuple, TextIO, overload
1919
from numpy.typing import ArrayLike
2020

21-
_T_Artist = TypeVar("_T_Artist", bound=Artist)
22-
2321
def allow_rasterization(draw): ...
2422

2523
class _XYPair(NamedTuple):
@@ -143,11 +141,11 @@ class Artist:
143141
) -> list[Artist]: ...
144142

145143
@overload
146-
def findobj(
144+
def findobj[T: Artist](
147145
self,
148-
match: type[_T_Artist],
146+
match: type[T],
149147
include_self: bool = ...,
150-
) -> list[_T_Artist]: ...
148+
) -> list[T]: ...
151149

152150
def get_cursor_data(self, event: MouseEvent) -> Any: ...
153151
def format_cursor_data(self, data: Any) -> str: ...

lib/matplotlib/axes/__init__.pyi

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
from typing import TypeVar
2-
31
from ._axes import Axes as Axes
42

53

6-
_T = TypeVar("_T")
7-
84
# Backcompat.
95
Subplot = Axes
106

@@ -13,4 +9,4 @@ class _SubplotBaseMeta(type):
139

1410
class SubplotBase(metaclass=_SubplotBaseMeta): ...
1511

16-
def subplot_class_factory(cls: type[_T]) -> type[_T]: ...
12+
def subplot_class_factory[T](cls: type[T]) -> type[T]: ...

lib/matplotlib/axes/_base.pyi

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ from cycler import Cycler
2727

2828
import numpy as np
2929
from numpy.typing import ArrayLike
30-
from typing import Any, Literal, TypeVar, overload
30+
from typing import Any, Literal, overload
3131
from matplotlib.typing import ColorType
3232

33-
_T = TypeVar("_T", bound=Artist)
34-
3533
class _axis_method_wrapper:
3634
attr_name: str
3735
method_name: str
@@ -136,7 +134,7 @@ class _AxesBase(martist.Artist):
136134
def clear(self) -> None: ...
137135
def cla(self) -> None: ...
138136

139-
class ArtistList(Sequence[_T]):
137+
class ArtistList[T](Sequence[T]):
140138
def __init__(
141139
self,
142140
axes: _AxesBase,
@@ -145,21 +143,21 @@ class _AxesBase(martist.Artist):
145143
invalid_types: type | Iterable[type] | None = ...,
146144
) -> None: ...
147145
def __len__(self) -> int: ...
148-
def __iter__(self) -> Iterator[_T]: ...
146+
def __iter__(self) -> Iterator[T]: ...
149147
@overload
150-
def __getitem__(self, key: int) -> _T: ...
148+
def __getitem__(self, key: int) -> T: ...
151149
@overload
152-
def __getitem__(self, key: slice) -> list[_T]: ...
150+
def __getitem__(self, key: slice) -> list[T]: ...
153151

154152
@overload
155-
def __add__(self, other: _AxesBase.ArtistList[_T]) -> list[_T]: ...
153+
def __add__(self, other: _AxesBase.ArtistList[T]) -> list[T]: ...
156154
@overload
157155
def __add__(self, other: list[Any]) -> list[Any]: ...
158156
@overload
159157
def __add__(self, other: tuple[Any]) -> tuple[Any]: ...
160158

161159
@overload
162-
def __radd__(self, other: _AxesBase.ArtistList[_T]) -> list[_T]: ...
160+
def __radd__(self, other: _AxesBase.ArtistList[T]) -> list[T]: ...
163161
@overload
164162
def __radd__(self, other: list[Any]) -> list[Any]: ...
165163
@overload

lib/matplotlib/backend_bases.pyi

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ from matplotlib.text import Text, TextToPath
1919
from matplotlib.transforms import Bbox, BboxBase, Transform, TransformedPath
2020

2121
from collections.abc import Callable, Iterable, Sequence
22-
from typing import Any, IO, Literal, NamedTuple, TypeVar, overload
22+
from typing import Any, IO, Literal, NamedTuple, overload
2323
from numpy.typing import ArrayLike
2424
from .typing import (
2525
CapStyleType,
@@ -362,7 +362,6 @@ class FigureCanvasBase:
362362
@classmethod
363363
def get_default_filetype(cls) -> str: ...
364364
def get_default_filename(self) -> str: ...
365-
_T = TypeVar("_T", bound=FigureCanvasBase)
366365

367366
@overload
368367
def mpl_connect(

lib/matplotlib/backend_managers.pyi

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ from matplotlib.backend_bases import FigureCanvasBase
33
from matplotlib.figure import Figure
44

55
from collections.abc import Callable, Iterable
6-
from typing import Any, TypeVar
6+
from typing import Any
77

88
class ToolEvent:
99
name: str
@@ -48,8 +48,7 @@ class ToolManager:
4848
def get_tool_keymap(self, name: str) -> list[str]: ...
4949
def update_keymap(self, name: str, key: str | Iterable[str]) -> None: ...
5050
def remove_tool(self, name: str) -> None: ...
51-
_T = TypeVar("_T", bound=backend_tools.ToolBase)
52-
def add_tool(self, name: str, tool: type[_T], *args, **kwargs) -> _T: ...
51+
def add_tool[T: backend_tools.ToolBase](self, name: str, tool: type[T], *args, **kwargs) -> T: ...
5352
def trigger_tool(
5453
self,
5554
name: str | backend_tools.ToolBase,

0 commit comments

Comments
 (0)