Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 427392e

Browse files
authored
Merge pull request #26407 from QuLogic/better-typing
Improve some smaller typing issues
2 parents 54f1612 + c70b9b3 commit 427392e

34 files changed

+155
-122
lines changed

lib/matplotlib/__init__.pyi

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,16 @@ from packaging.version import Version
3939
from matplotlib._api import MatplotlibDeprecationWarning
4040
from typing import Any, NamedTuple
4141

42-
__bibtex__: str
43-
4442
class _VersionInfo(NamedTuple):
4543
major: int
4644
minor: int
4745
micro: int
4846
releaselevel: str
4947
serial: int
5048

51-
class __getattr__:
52-
__version__: str
53-
__version_info__: _VersionInfo
49+
__bibtex__: str
50+
__version__: str
51+
__version_info__: _VersionInfo
5452

5553
def set_loglevel(level: str) -> None: ...
5654

lib/matplotlib/animation.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class Animation:
162162
def save(
163163
self,
164164
filename: str | Path,
165-
writer: MovieWriter | str | None = ...,
165+
writer: AbstractMovieWriter | str | None = ...,
166166
fps: int | None = ...,
167167
dpi: float | None = ...,
168168
codec: str | None = ...,
@@ -183,6 +183,7 @@ class Animation:
183183
embed_frames: bool = ...,
184184
default_mode: str | None = ...,
185185
) -> str: ...
186+
def _repr_html_(self) -> str: ...
186187
def pause(self) -> None: ...
187188
def resume(self) -> None: ...
188189

lib/matplotlib/artist.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,8 +757,7 @@ def set_clip_box(self, clipbox):
757757
758758
Parameters
759759
----------
760-
clipbox : `.Bbox`
761-
760+
clipbox : `.BboxBase` or None
762761
Typically would be created from a `.TransformedBbox`. For
763762
instance ``TransformedBbox(Bbox([[0, 0], [1, 1]]), ax.transAxes)``
764763
is the default clipping for an artist added to an Axes.

lib/matplotlib/artist.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ from .path import Path
55
from .patches import Patch
66
from .patheffects import AbstractPathEffect
77
from .transforms import (
8+
BboxBase,
89
Bbox,
910
Transform,
1011
TransformedPatchPath,
@@ -27,10 +28,9 @@ class _Unset: ...
2728

2829
class Artist:
2930
zorder: float
30-
def __init_subclass__(cls): ...
3131
stale_callback: Callable[[Artist, bool], None] | None
3232
figure: Figure | SubFigure | None
33-
clipbox: Bbox | None
33+
clipbox: BboxBase | None
3434
def __init__(self) -> None: ...
3535
def remove(self) -> None: ...
3636
def have_units(self) -> bool: ...
@@ -51,7 +51,7 @@ class Artist:
5151
def remove_callback(self, oid: int) -> None: ...
5252
def pchanged(self) -> None: ...
5353
def is_transform_set(self) -> bool: ...
54-
def set_transform(self, t: Transform) -> None: ...
54+
def set_transform(self, t: Transform | None) -> None: ...
5555
def get_transform(self) -> Transform: ...
5656
def get_children(self) -> list[Artist]: ...
5757
# TODO can these dicts be type narrowed? e.g. str keys
@@ -87,7 +87,7 @@ class Artist:
8787
def get_path_effects(self) -> list[AbstractPathEffect]: ...
8888
def get_figure(self) -> Figure | None: ...
8989
def set_figure(self, fig: Figure) -> None: ...
90-
def set_clip_box(self, clipbox: Bbox) -> None: ...
90+
def set_clip_box(self, clipbox: BboxBase | None) -> None: ...
9191
def set_clip_path(
9292
self,
9393
path: Patch | Path | TransformedPath | TransformedPatchPath | None,

lib/matplotlib/axes/_axes.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Axes(_AxesBase):
5252
def get_legend_handles_labels(
5353
self, legend_handler_map: dict[type, HandlerBase] | None = ...
5454
) -> tuple[list[Artist], list[Any]]: ...
55-
legend_: Legend
55+
legend_: Legend | None
5656

5757
@overload
5858
def legend(self) -> Legend: ...
@@ -433,7 +433,7 @@ class Axes(_AxesBase):
433433
alpha: float | None = ...,
434434
linewidths: float | None = ...,
435435
edgecolors: Literal["face", "none"] | ColorType = ...,
436-
reduce_C_function: Callable[[np.ndarray], float] = ...,
436+
reduce_C_function: Callable[[np.ndarray | list[float]], float] = ...,
437437
mincnt: int | None = ...,
438438
marginals: bool = ...,
439439
*,

lib/matplotlib/axes/_base.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ class _AxesBase(martist.Artist):
131131
self,
132132
axes: _AxesBase,
133133
prop_name: str,
134-
valid_types: list[type] | None = ...,
135-
invalid_types: tuple[type] | None = ...,
134+
valid_types: type | Iterable[type] | None = ...,
135+
invalid_types: type | Iterable[type] | None = ...,
136136
) -> None: ...
137137
def __len__(self) -> int: ...
138138
def __iter__(self) -> Iterator[Artist]: ...

lib/matplotlib/backend_bases.pyi

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ from matplotlib.font_manager import FontProperties
1616
from matplotlib.path import Path
1717
from matplotlib.texmanager import TexManager
1818
from matplotlib.text import Text
19-
from matplotlib.transforms import Transform, TransformedPath, Bbox
19+
from matplotlib.transforms import Bbox, BboxBase, Transform, TransformedPath
2020

2121
from collections.abc import Callable, Iterable, Sequence
2222
from typing import Any, IO, Literal, NamedTuple, TypeVar
@@ -168,7 +168,7 @@ class GraphicsContextBase:
168168
def set_url(self, url: str | None) -> None: ...
169169
def set_gid(self, id: int | None) -> None: ...
170170
def set_snap(self, snap: bool | None) -> None: ...
171-
def set_hatch(self, hatch: str) -> None: ...
171+
def set_hatch(self, hatch: str | None) -> None: ...
172172
def get_hatch(self) -> str | None: ...
173173
def get_hatch_path(self, density: float = ...) -> Path: ...
174174
def get_hatch_color(self) -> ColorType: ...
@@ -323,7 +323,7 @@ class FigureCanvasBase:
323323
@classmethod
324324
def new_manager(cls, figure: Figure, num: int | str): ...
325325
def is_saving(self) -> bool: ...
326-
def blit(self, bbox: Bbox | None = ...) -> None: ...
326+
def blit(self, bbox: BboxBase | None = ...) -> None: ...
327327
def inaxes(self, xy: tuple[float, float]) -> Axes | None: ...
328328
def grab_mouse(self, ax: Axes) -> None: ...
329329
def release_mouse(self, ax: Axes) -> None: ...
@@ -386,8 +386,8 @@ class FigureManagerBase:
386386
num: int | str
387387
key_press_handler_id: int | None
388388
button_press_handler_id: int | None
389-
toolmanager: ToolManager
390-
toolbar: NavigationToolbar2
389+
toolmanager: ToolManager | None
390+
toolbar: NavigationToolbar2 | ToolContainerBase | None
391391
def __init__(self, canvas: FigureCanvasBase, num: int | str) -> None: ...
392392
@classmethod
393393
def create_with_canvas(
@@ -429,7 +429,7 @@ class NavigationToolbar2:
429429

430430
class _PanInfo(NamedTuple):
431431
button: MouseButton
432-
axes: Axes
432+
axes: list[Axes]
433433
cid: int
434434
def press_pan(self, event: Event) -> None: ...
435435
def drag_pan(self, event: Event) -> None: ...
@@ -439,7 +439,7 @@ class NavigationToolbar2:
439439
class _ZoomInfo(NamedTuple):
440440
direction: Literal["in", "out"]
441441
start_xy: tuple[float, float]
442-
axes: Axes
442+
axes: list[Axes]
443443
cid: int
444444
cbar: Colorbar
445445
def press_zoom(self, event: Event) -> None: ...

lib/matplotlib/backend_managers.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ class ToolManager:
5252
def add_tool(self, name: str, tool: type[_T], *args, **kwargs) -> _T: ...
5353
def trigger_tool(
5454
self,
55-
name: str,
55+
name: str | backend_tools.ToolBase,
5656
sender: Any | None = ...,
5757
canvasevent: ToolEvent | None = ...,
5858
data: Any | None = ...,
5959
) -> None: ...
6060
@property
6161
def tools(self) -> dict[str, backend_tools.ToolBase]: ...
6262
def get_tool(
63-
self, name: str, warn: bool = ...
63+
self, name: str | backend_tools.ToolBase, warn: bool = ...
6464
) -> backend_tools.ToolBase | None: ...

lib/matplotlib/colorbar.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Colorbar:
2323
n_rasterize: int
2424
mappable: cm.ScalarMappable
2525
ax: Axes
26-
alpha: float
26+
alpha: float | None
2727
cmap: colors.Colormap
2828
norm: colors.Normalize
2929
values: Sequence[float] | None

lib/matplotlib/colors.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ class Colormap:
9797
def is_gray(self) -> bool: ...
9898
def resampled(self, lutsize: int) -> Colormap: ...
9999
def reversed(self, name: str | None = ...) -> Colormap: ...
100+
def _repr_html_(self) -> str: ...
101+
def _repr_png_(self) -> bytes: ...
100102
def copy(self) -> Colormap: ...
101103

102104
class LinearSegmentedColormap(Colormap):

lib/matplotlib/figure.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2213,7 +2213,7 @@ def __init__(self, parent, subplotspec, *,
22132213
self._axobservers = parent._axobservers
22142214
self.canvas = parent.canvas
22152215
self.transFigure = parent.transFigure
2216-
self.bbox_relative = None
2216+
self.bbox_relative = Bbox.null()
22172217
self._redo_transform_rel_fig()
22182218
self.figbbox = self._parent.figbbox
22192219
self.bbox = TransformedBbox(self.bbox_relative,
@@ -2278,11 +2278,8 @@ def _redo_transform_rel_fig(self, bbox=None):
22782278
dy = hr[self._subplotspec.rowspan].sum() / hr.sum()
22792279
x0 = wr[:self._subplotspec.colspan.start].sum() / wr.sum()
22802280
y0 = 1 - hr[:self._subplotspec.rowspan.stop].sum() / hr.sum()
2281-
if self.bbox_relative is None:
2282-
self.bbox_relative = Bbox.from_bounds(x0, y0, dx, dy)
2283-
else:
2284-
self.bbox_relative.p0 = (x0, y0)
2285-
self.bbox_relative.p1 = (x0 + dx, y0 + dy)
2281+
self.bbox_relative.p0 = (x0, y0)
2282+
self.bbox_relative.p1 = (x0 + dx, y0 + dy)
22862283

22872284
def get_constrained_layout(self):
22882285
"""

lib/matplotlib/figure.pyi

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ from matplotlib.legend import Legend
1818
from matplotlib.lines import Line2D
1919
from matplotlib.patches import Rectangle, Patch
2020
from matplotlib.text import Text
21-
from matplotlib.transforms import Affine2D, Bbox, Transform
21+
from matplotlib.transforms import Affine2D, Bbox, BboxBase, Transform
2222

2323
import numpy as np
2424
from numpy.typing import ArrayLike
@@ -249,8 +249,8 @@ class SubFigure(FigureBase):
249249
canvas: FigureCanvasBase
250250
transFigure: Transform
251251
bbox_relative: Bbox
252-
figbbox: Bbox
253-
bbox: Bbox
252+
figbbox: BboxBase
253+
bbox: BboxBase
254254
transSubfigure: Transform
255255
patch: Rectangle
256256
def __init__(
@@ -283,8 +283,8 @@ class Figure(FigureBase):
283283
figure: Figure
284284
bbox_inches: Bbox
285285
dpi_scale_trans: Affine2D
286-
bbox: Bbox
287-
figbbox: Bbox
286+
bbox: BboxBase
287+
figbbox: BboxBase
288288
transFigure: Transform
289289
transSubfigure: Transform
290290
patch: Rectangle
@@ -315,6 +315,7 @@ class Figure(FigureBase):
315315
**kwargs
316316
) -> None: ...
317317
def get_layout_engine(self) -> LayoutEngine | None: ...
318+
def _repr_html_(self) -> str | None: ...
318319
def show(self, warn: bool = ...) -> None: ...
319320
@property # type: ignore[misc]
320321
def axes(self) -> list[Axes]: ... # type: ignore[override]

lib/matplotlib/font_manager.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class FontEntry:
3535
weight: str | int = ...
3636
stretch: str = ...
3737
size: str = ...
38+
def _repr_html_(self) -> str: ...
39+
def _repr_png_(self) -> bytes: ...
3840

3941
def ttfFontProperty(font: ft2font.FT2Font) -> FontEntry: ...
4042
def afmFontProperty(fontpath: str, font: AFM) -> FontEntry: ...

lib/matplotlib/gridspec.pyi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ class GridSpecBase:
3434
) -> tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: ...
3535
@staticmethod
3636
def _check_gridspec_exists(figure, nrows, ncols): ...
37-
def __getitem__(self, key: tuple[int | slice, int | slice] | slice | int) -> SubplotSpec: ...
37+
def __getitem__(
38+
self, key: tuple[int | slice, int | slice] | slice | int
39+
) -> SubplotSpec: ...
3840
@overload
3941
def subplots(
4042
self,
@@ -105,7 +107,7 @@ class GridSpecFromSubplotSpec(GridSpecBase):
105107
class SubplotSpec:
106108
num1: int
107109
def __init__(
108-
self, gridspec: GridSpec, num1: int, num2: int | None = ...
110+
self, gridspec: GridSpecBase, num1: int, num2: int | None = ...
109111
) -> None: ...
110112
@staticmethod
111113
def _from_subplot_args(figure, args): ...

0 commit comments

Comments
 (0)