Skip to content

Commit 00afcc0

Browse files
authored
Improve typing in pyplot (matplotlib#26385)
* TYP: Fix types for path effects property * Add typing to _docstring module This is mainly useful because of its decorators, which previously caused any usage to lose typing. Consequently, there are several _new_ typing errors, many of which are fixed here, but not all. * Add typing to _copy_docstring_and_deprecators As before, this untyped function caused any decorator usage to make the wrapped function untyped. Now, these functions are typed again. This revealed several follow-on type issues, but many of them are expected (as tests tend to call things with incorrect arguments on purpose, use property aliases, etc.) * Add some more typing in pyplot * Add return annotation for pie * Remove return from pyplot funcs annotated as None * TYP: Explicitly re-export some API from pyplot See matplotlib#26372
1 parent 1e6b5ec commit 00afcc0

File tree

27 files changed

+228
-142
lines changed

27 files changed

+228
-142
lines changed

galleries/examples/misc/hyperlinks_sgskip.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
Z = (Z1 - Z2) * 2
3434

3535
im = plt.imshow(Z, interpolation='bilinear', cmap=cm.gray,
36-
origin='lower', extent=[-3, 3, -3, 3])
36+
origin='lower', extent=(-3, 3, -3, 3))
3737

3838
im.set_url('https://www.google.com/')
3939
fig.savefig('image.svg')

galleries/examples/shapes_and_collections/dolphin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
im = plt.imshow(np.random.random((100, 100)),
3434
origin='lower', cmap=cm.winter,
3535
interpolation='spline36',
36-
extent=([-1, 1, -1, 1]))
36+
extent=(-1, 1, -1, 1))
3737
im.set_clip_path(circle)
3838

3939
plt.plot(x, y, 'o', color=(0.9, 0.9, 1.0), alpha=0.8)

galleries/examples/subplots_axes_and_figures/subplots_adjust.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
plt.imshow(np.random.random((100, 100)))
2626

2727
plt.subplots_adjust(bottom=0.1, right=0.8, top=0.9)
28-
cax = plt.axes([0.85, 0.1, 0.075, 0.8])
28+
cax = plt.axes((0.85, 0.1, 0.075, 0.8))
2929
plt.colorbar(cax=cax)
3030

3131
plt.show()

galleries/examples/text_labels_and_annotations/autowrap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import matplotlib.pyplot as plt
1818

1919
fig = plt.figure()
20-
plt.axis([0, 10, 0, 10])
20+
plt.axis((0, 10, 0, 10))
2121
t = ("This is a really long string that I'd rather have wrapped so that it "
2222
"doesn't go outside of the figure, but if it's long enough it will go "
2323
"off the top or bottom!")

galleries/tutorials/pyplot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
# example, to plot the above with red circles, you would issue
7474

7575
plt.plot([1, 2, 3, 4], [1, 4, 9, 16], 'ro')
76-
plt.axis([0, 6, 0, 20])
76+
plt.axis((0, 6, 0, 20))
7777
plt.show()
7878

7979
# %%

lib/matplotlib/_docstring.pyi

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from typing import Any, Callable, TypeVar, overload
2+
3+
4+
_T = TypeVar('_T')
5+
6+
7+
class Substitution:
8+
@overload
9+
def __init__(self, *args: str): ...
10+
@overload
11+
def __init__(self, **kwargs: str): ...
12+
def __call__(self, func: _T) -> _T: ...
13+
def update(self, *args, **kwargs): ... # type: ignore[no-untyped-def]
14+
15+
16+
class _ArtistKwdocLoader(dict[str, str]):
17+
def __missing__(self, key: str) -> str: ...
18+
19+
20+
class _ArtistPropertiesSubstitution(Substitution):
21+
def __init__(self) -> None: ...
22+
def __call__(self, obj: _T) -> _T: ...
23+
24+
25+
def copy(source: Any) -> Callable[[_T], _T]: ...
26+
27+
28+
dedent_interpd: _ArtistPropertiesSubstitution
29+
interpd: _ArtistPropertiesSubstitution

lib/matplotlib/artist.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ def set_path_effects(self, path_effects):
715715
716716
Parameters
717717
----------
718-
path_effects : `.AbstractPathEffect`
718+
path_effects : list of `.AbstractPathEffect`
719719
"""
720720
self._path_effects = path_effects
721721
self.stale = True

lib/matplotlib/artist.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ class Artist:
8383
length: float | None = ...,
8484
randomness: float | None = ...,
8585
) -> None: ...
86-
def set_path_effects(self, path_effects: AbstractPathEffect) -> None: ...
87-
def get_path_effects(self) -> AbstractPathEffect: ...
86+
def set_path_effects(self, path_effects: list[AbstractPathEffect]) -> None: ...
87+
def get_path_effects(self) -> list[AbstractPathEffect]: ...
8888
def get_figure(self) -> Figure | None: ...
8989
def set_figure(self, fig: Figure) -> None: ...
9090
def set_clip_box(self, clipbox: Bbox) -> None: ...

lib/matplotlib/axes/_axes.pyi

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ from matplotlib.legend import Legend
2020
from matplotlib.legend_handler import HandlerBase
2121
from matplotlib.lines import Line2D
2222
from matplotlib.mlab import GaussianKDE
23-
from matplotlib.patches import Rectangle, FancyArrow, Polygon, StepPatch
23+
from matplotlib.patches import Rectangle, FancyArrow, Polygon, StepPatch, Wedge
2424
from matplotlib.quiver import Quiver, QuiverKey, Barbs
2525
from matplotlib.text import Annotation, Text
2626
from matplotlib.transforms import Transform, Bbox
@@ -305,7 +305,7 @@ class Axes(_AxesBase):
305305
autopct: str | Callable[[float], str] | None = ...,
306306
pctdistance: float = ...,
307307
shadow: bool = ...,
308-
labeldistance: float = ...,
308+
labeldistance: float | None = ...,
309309
startangle: float = ...,
310310
radius: float = ...,
311311
counterclock: bool = ...,
@@ -318,7 +318,9 @@ class Axes(_AxesBase):
318318
normalize: bool = ...,
319319
hatch: str | Sequence[str] | None = ...,
320320
data=...,
321-
): ...
321+
) -> tuple[list[Wedge], list[Text]] | tuple[
322+
list[Wedge], list[Text], list[Text]
323+
]: ...
322324
def errorbar(
323325
self,
324326
x: float | ArrayLike,
@@ -564,7 +566,7 @@ class Axes(_AxesBase):
564566
edges: ArrayLike | None = ...,
565567
*,
566568
orientation: Literal["vertical", "horizontal"] = ...,
567-
baseline: float | ArrayLike = ...,
569+
baseline: float | ArrayLike | None = ...,
568570
fill: bool = ...,
569571
data=...,
570572
**kwargs
@@ -736,7 +738,7 @@ class Axes(_AxesBase):
736738
showmeans: bool = ...,
737739
showextrema: bool = ...,
738740
showmedians: bool = ...,
739-
quantiles: Sequence[float] | None = ...,
741+
quantiles: Sequence[float | Sequence[float]] | None = ...,
740742
points: int = ...,
741743
bw_method: Literal["scott", "silverman"]
742744
| float

lib/matplotlib/axes/_base.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class _axis_method_wrapper:
3939

4040
class _AxesBase(martist.Artist):
4141
name: str
42+
patch: Patch
4243
spines: Spines
4344
fmt_xdata: Callable[[float], str] | None
4445
fmt_ydata: Callable[[float], str] | None

lib/matplotlib/backend_bases.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2304,7 +2304,7 @@ def mpl_disconnect(self, cid):
23042304
# ... later
23052305
canvas.mpl_disconnect(cid)
23062306
"""
2307-
return self.callbacks.disconnect(cid)
2307+
self.callbacks.disconnect(cid)
23082308

23092309
# Internal subclasses can override _timer_cls instead of new_timer, though
23102310
# this is not a public API for third-party subclasses.

lib/matplotlib/cbook.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ from typing import (
2020

2121
_T = TypeVar("_T")
2222

23+
def _get_running_interactive_framework() -> str | None: ...
24+
2325
class CallbackRegistry:
2426
exception_handler: Callable[[Exception], Any]
2527
callbacks: dict[Any, dict[int, Any]]
@@ -162,6 +164,7 @@ def normalize_kwargs(
162164
kw: dict[str, Any],
163165
alias_mapping: dict[str, list[str]] | type[Artist] | Artist | None = ...,
164166
) -> dict[str, Any]: ...
167+
def _str_equal(obj: Any, s: str) -> bool: ...
165168

166169
class _OrderedSet(collections.abc.MutableSet):
167170
def __init__(self) -> None: ...
@@ -171,4 +174,5 @@ class _OrderedSet(collections.abc.MutableSet):
171174
def add(self, key) -> None: ...
172175
def discard(self, key) -> None: ...
173176

177+
def _backend_module_name(name: str) -> str: ...
174178
def _format_approx(number: float, precision: int) -> str: ...

lib/matplotlib/collections.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class Collection(artist.Artist, cm.ScalarMappable):
4242
def get_datalim(self, transData: transforms.Transform) -> transforms.Bbox: ...
4343
def set_pickradius(self, pickradius: float) -> None: ...
4444
def get_pickradius(self) -> float: ...
45-
def set_urls(self, urls: Sequence[str]) -> None: ...
45+
def set_urls(self, urls: Sequence[str | None]) -> None: ...
4646
def get_urls(self) -> Sequence[str | None]: ...
4747
def set_hatch(self, hatch: str) -> None: ...
4848
def get_hatch(self) -> str: ...

lib/matplotlib/container.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ from numpy.typing import ArrayLike
1010
class Container(tuple):
1111
def __new__(cls, *args, **kwargs): ...
1212
def __init__(self, kl, label: Any | None = ...) -> None: ...
13-
def remove(self): ...
14-
def get_children(self): ...
13+
def remove(self) -> None: ...
14+
def get_children(self) -> list[Artist]: ...
1515
def get_label(self) -> str | None: ...
1616
def set_label(self, s: Any) -> None: ...
1717
def add_callback(self, func: Callable[[Artist], Any]) -> int: ...

0 commit comments

Comments
 (0)