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

Skip to content

Commit 8edf04f

Browse files
committed
Missing return types for Figure
Required changes for Figure.figure type hint
1 parent cb7f649 commit 8edf04f

File tree

5 files changed

+20
-19
lines changed

5 files changed

+20
-19
lines changed

lib/matplotlib/artist.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ from .axes._base import _AxesBase
44
from .backend_bases import RendererBase, MouseEvent
55
from .cm import ScalarMappable
66
from .colors import BoundaryNorm
7-
from .figure import Figure, FigureBase
7+
from .figure import Figure, SubFigure
88
from .path import Path
99
from .patches import Patch
1010
from .patheffects import AbstractPathEffect
@@ -35,7 +35,7 @@ class Artist:
3535
zorder: float
3636
def __init_subclass__(cls): ...
3737
stale_callback: Callable[[Artist, bool], None] | None
38-
figure: FigureBase | None
38+
figure: Figure | SubFigure | None
3939
clipbox: Bbox | None
4040
def __init__(self) -> None: ...
4141
def remove(self) -> None: ...

lib/matplotlib/backend_bases.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def get_registered_canvas_class(format: str) -> type[FigureCanvasBase]: ...
3636

3737
class RendererBase:
3838
def __init__(self) -> None: ...
39-
def open_group(self, s: str, gid: int | None = ...) -> None: ...
39+
def open_group(self, s: str, gid: str | None = ...) -> None: ...
4040
def close_group(self, s: str) -> None: ...
4141
def draw_path(
4242
self,

lib/matplotlib/figure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ def __init__(self, **kwargs):
188188
# axis._get_tick_boxes_siblings
189189
self._align_label_groups = {"x": cbook.Grouper(), "y": cbook.Grouper()}
190190

191-
self.figure = self
192191
self._localaxes = [] # track all axes
193192
self.artists = []
194193
self.lines = []
@@ -2458,6 +2457,7 @@ def __init__(self,
24582457
%(Figure:kwdoc)s
24592458
"""
24602459
super().__init__(**kwargs)
2460+
self.figure = self
24612461
self._layout_engine = None
24622462

24632463
if layout is not None:

lib/matplotlib/figure.pyi

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ from matplotlib.colors import Colormap, Normalize
1515
from matplotlib.colorbar import Colorbar
1616
from matplotlib.cm import ScalarMappable
1717
from matplotlib.gridspec import GridSpec, SubplotSpec
18-
from matplotlib.image import _ImageBase
18+
from matplotlib.image import _ImageBase, FigureImage
1919
from matplotlib.layout_engine import (
2020
ConstrainedLayoutEngine,
2121
LayoutEngine,
@@ -69,7 +69,6 @@ class SubplotParams:
6969
) -> None: ...
7070

7171
class FigureBase(Artist):
72-
figure: FigureBase | None
7372
artists: list[Artist]
7473
lines: list[Line2D]
7574
patches: list[Patch]
@@ -154,15 +153,15 @@ class FigureBase(Artist):
154153
*,
155154
sharex: bool | Literal["none", "all", "row", "col"] = ...,
156155
sharey: bool | Literal["none", "all", "row", "col"] = ...,
157-
squeeze: Literal[True] = ...,
156+
squeeze: bool = ...,
158157
width_ratios: ArrayLike | None = ...,
159158
height_ratios: ArrayLike | None = ...,
160159
subplot_kw: dict[str, Any] | None = ...,
161160
gridspec_kw: dict[str, Any] | None = ...
162161
) -> np.ndarray | SubplotBase | Axes: ...
163162
def delaxes(self, ax: Axes) -> None: ...
164163
def clear(self, keep_observers: bool = ...) -> None: ...
165-
def clf(self, keep_observers: bool = ...): ...
164+
def clf(self, keep_observers: bool = ...) -> None: ...
166165

167166
@overload
168167
def legend(self) -> Legend: ...
@@ -203,7 +202,7 @@ class FigureBase(Artist):
203202
def align_xlabels(self, axs: Iterable[Axes] | None = ...) -> None: ...
204203
def align_ylabels(self, axs: Iterable[Axes] | None = ...) -> None: ...
205204
def align_labels(self, axs: Iterable[Axes] | None = ...) -> None: ...
206-
def add_gridspec(self, nrows: int = ..., ncols: int = ..., **kwargs): ...
205+
def add_gridspec(self, nrows: int = ..., ncols: int = ..., **kwargs) -> GridSpec: ...
207206
@overload
208207
def subfigures(
209208
self,
@@ -234,7 +233,7 @@ class FigureBase(Artist):
234233
def _gci(self) -> ScalarMappable | None: ...
235234
def _process_projection_requirements(
236235
self, *args, axes_class=None, polar=False, projection=None, **kwargs
237-
): ...
236+
) -> tuple[type[Axes], dict[str, Any]]: ...
238237
def get_default_bbox_extra_artists(self) -> list[Artist]: ...
239238
def get_tightbbox(
240239
self,
@@ -258,7 +257,7 @@ class FigureBase(Artist):
258257
) -> dict[Any, Axes]: ...
259258

260259
class SubFigure(FigureBase):
261-
figure: FigureBase
260+
figure: Figure
262261
subplotpars: SubplotParams
263262
dpi_scale_trans: Affine2D
264263
canvas: FigureCanvasBase
@@ -295,6 +294,7 @@ class SubFigure(FigureBase):
295294
def get_axes(self) -> list[Axes]: ...
296295

297296
class Figure(FigureBase):
297+
figure: Figure
298298
bbox_inches: Bbox
299299
dpi_scale_trans: Affine2D
300300
bbox: Bbox
@@ -357,7 +357,7 @@ class Figure(FigureBase):
357357
origin: Literal["upper", "lower"] | None = ...,
358358
resize: bool = ...,
359359
**kwargs
360-
): ...
360+
) -> FigureImage: ...
361361
def set_size_inches(
362362
self, w: float | tuple[float, float], h: float | None = ..., forward: bool = ...
363363
) -> None: ...
@@ -371,7 +371,7 @@ class Figure(FigureBase):
371371
def clear(self, keep_observers: bool = ...) -> None: ...
372372
def draw_without_rendering(self) -> None: ...
373373
def draw_artist(self, a: Artist) -> None: ...
374-
def add_axobserver(self, func: Callable[[Figure], Any]): ...
374+
def add_axobserver(self, func: Callable[[Figure], Any]) -> None: ...
375375
def savefig(
376376
self,
377377
fname: str | os.PathLike | IO,
@@ -388,7 +388,7 @@ class Figure(FigureBase):
388388
mouse_pop: MouseButton = ...,
389389
mouse_stop: MouseButton = ...,
390390
) -> list[tuple[int, int]]: ...
391-
def waitforbuttonpress(self, timeout: float = ...): ...
391+
def waitforbuttonpress(self, timeout: float = ...) -> None | bool: ...
392392
def tight_layout(
393393
self,
394394
*,

lib/matplotlib/pyplot.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
from matplotlib.figure import SubFigure
117117
from matplotlib.legend import Legend
118118
from matplotlib.mlab import GaussianKDE
119-
from matplotlib.image import AxesImage
119+
from matplotlib.image import AxesImage, FigureImage
120120
from matplotlib.patches import FancyArrow, StepPatch
121121
from matplotlib.quiver import Barbs, Quiver, QuiverKey
122122
from matplotlib.scale import ScaleBase
@@ -754,7 +754,7 @@ def figure(
754754
FigureClass: Type[Figure] = Figure,
755755
clear: bool = False,
756756
**kwargs
757-
):
757+
) -> Figure:
758758
"""
759759
Create a new figure, or activate an existing figure.
760760
@@ -857,6 +857,7 @@ def figure(
857857
in the matplotlibrc file.
858858
"""
859859
if isinstance(num, FigureBase):
860+
# type narrowed to `Figure | SubFigure` by combination of input and isinstance
860861
if num.canvas.manager is None:
861862
raise ValueError("The passed figure is not managed by pyplot")
862863
_pylab_helpers.Gcf.set_active(num.canvas.manager)
@@ -2347,7 +2348,7 @@ def matshow(A: ArrayLike, fignum: None | int = None, **kwargs) -> AxesImage:
23472348
# Extract actual aspect ratio of array and make appropriately sized
23482349
# figure.
23492350
fig = figure(fignum, figsize=figaspect(A))
2350-
ax = fig.add_axes([0.15, 0.09, 0.775, 0.775])
2351+
ax = fig.add_axes((0.15, 0.09, 0.775, 0.775))
23512352
im = ax.matshow(A, **kwargs)
23522353
sci(im)
23532354
return im
@@ -2403,7 +2404,7 @@ def figimage(
24032404
origin: Literal["upper", "lower"] | None = None,
24042405
resize: bool = False,
24052406
**kwargs,
2406-
):
2407+
) -> FigureImage:
24072408
return gcf().figimage(
24082409
X,
24092410
xo=xo,
@@ -2494,7 +2495,7 @@ def tight_layout(
24942495

24952496
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
24962497
@_copy_docstring_and_deprecators(Figure.waitforbuttonpress)
2497-
def waitforbuttonpress(timeout: float = -1):
2498+
def waitforbuttonpress(timeout: float = -1) -> None | bool:
24982499
return gcf().waitforbuttonpress(timeout=timeout)
24992500

25002501

0 commit comments

Comments
 (0)