Skip to content

Commit a009473

Browse files
committed
Missing return types for Figure
Required changes for Figure.figure type hint
1 parent a844eca commit a009473

3 files changed

Lines changed: 19 additions & 16 deletions

File tree

‎lib/matplotlib/backend_bases.pyi‎

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

3939
class RendererBase:
4040
def __init__(self) -> None: ...
41-
def open_group(self, s: str, gid: int | None = ...) -> None: ...
41+
def open_group(self, s: str, gid: str | None = ...) -> None: ...
4242
def close_group(self, s: str) -> None: ...
4343
def draw_path(
4444
self,

‎lib/matplotlib/figure.pyi‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ from matplotlib.colors import Colormap, Normalize
1616
from matplotlib.colorbar import Colorbar
1717
from matplotlib.cm import ScalarMappable
1818
from matplotlib.gridspec import GridSpec, SubplotSpec
19-
from matplotlib.image import _ImageBase
19+
from matplotlib.image import _ImageBase, FigureImage
2020
from matplotlib.layout_engine import (
2121
ConstrainedLayoutEngine,
2222
LayoutEngine,
@@ -70,7 +70,10 @@ class SubplotParams:
7070
) -> None: ...
7171

7272
class FigureBase(Artist):
73-
figure: FigureBase | None
73+
# Small slight-of-hand as FigureBase is not instantiated directly
74+
# hinting as Figure means e.g. plt.figure can be type hinted more easily
75+
# FigureBase is never instantiated directly in internal code at the very least
76+
figure: Figure
7477
artists: list[Artist]
7578
lines: list[Line2D]
7679
patches: list[Patch]
@@ -152,15 +155,15 @@ class FigureBase(Artist):
152155
*,
153156
sharex: bool | Literal["none", "all", "row", "col"] = ...,
154157
sharey: bool | Literal["none", "all", "row", "col"] = ...,
155-
squeeze: Literal[True] = ...,
158+
squeeze: bool = ...,
156159
width_ratios: ArrayLike | None = ...,
157160
height_ratios: ArrayLike | None = ...,
158161
subplot_kw: dict[str, Any] | None = ...,
159162
gridspec_kw: dict[str, Any] | None = ...
160163
) -> np.ndarray | SubplotBase | Axes: ...
161164
def delaxes(self, ax: Axes) -> None: ...
162165
def clear(self, keep_observers: bool = ...) -> None: ...
163-
def clf(self, keep_observers: bool = ...): ...
166+
def clf(self, keep_observers: bool = ...) -> None: ...
164167

165168
@overload
166169
def legend(self) -> Legend: ...
@@ -201,7 +204,7 @@ class FigureBase(Artist):
201204
def align_xlabels(self, axs: Iterable[Axes] | None = ...) -> None: ...
202205
def align_ylabels(self, axs: Iterable[Axes] | None = ...) -> None: ...
203206
def align_labels(self, axs: Iterable[Axes] | None = ...) -> None: ...
204-
def add_gridspec(self, nrows: int = ..., ncols: int = ..., **kwargs): ...
207+
def add_gridspec(self, nrows: int = ..., ncols: int = ..., **kwargs) -> GridSpec: ...
205208
@overload
206209
def subfigures(
207210
self,
@@ -232,7 +235,7 @@ class FigureBase(Artist):
232235
def _gci(self) -> ScalarMappable | None: ...
233236
def _process_projection_requirements(
234237
self, *args, axes_class=None, polar=False, projection=None, **kwargs
235-
): ...
238+
) -> tuple[type[Axes], dict[str, Any]]: ...
236239
def get_default_bbox_extra_artists(self) -> list[Artist]: ...
237240
def get_tightbbox(
238241
self,
@@ -256,7 +259,7 @@ class FigureBase(Artist):
256259
) -> dict[Any, Axes]: ...
257260

258261
class SubFigure(FigureBase):
259-
figure: FigureBase
262+
figure: Figure
260263
subplotpars: SubplotParams
261264
dpi_scale_trans: Affine2D
262265
canvas: FigureCanvasBase
@@ -355,7 +358,7 @@ class Figure(FigureBase):
355358
origin: Literal["upper", "lower"] | None = ...,
356359
resize: bool = ...,
357360
**kwargs
358-
): ...
361+
) -> FigureImage: ...
359362
def set_size_inches(
360363
self, w: float | tuple[float, float], h: float | None = ..., forward: bool = ...
361364
) -> None: ...
@@ -369,7 +372,7 @@ class Figure(FigureBase):
369372
def clear(self, keep_observers: bool = ...) -> None: ...
370373
def draw_without_rendering(self) -> None: ...
371374
def draw_artist(self, a: Artist) -> None: ...
372-
def add_axobserver(self, func: Callable[[Figure], Any]): ...
375+
def add_axobserver(self, func: Callable[[Figure], Any]) -> None: ...
373376
def savefig(
374377
self,
375378
fname: str | os.PathLike | io.FileIO,
@@ -386,7 +389,7 @@ class Figure(FigureBase):
386389
mouse_pop: MouseButton = ...,
387390
mouse_stop: MouseButton = ...,
388391
) -> list[tuple[int, int]]: ...
389-
def waitforbuttonpress(self, timeout: float = ...): ...
392+
def waitforbuttonpress(self, timeout: float = ...) -> None | bool: ...
390393
def tight_layout(
391394
self,
392395
*,

‎lib/matplotlib/pyplot.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
from matplotlib.figure import SubFigure
118118
from matplotlib.legend import Legend
119119
from matplotlib.mlab import GaussianKDE
120-
from matplotlib.image import AxesImage
120+
from matplotlib.image import AxesImage, FigureImage
121121
from matplotlib.patches import FancyArrow, StepPatch
122122
from matplotlib.quiver import Barbs, Quiver, QuiverKey
123123
from matplotlib.scale import ScaleBase
@@ -755,7 +755,7 @@ def figure(
755755
FigureClass: Type[Figure] = Figure,
756756
clear: bool = False,
757757
**kwargs
758-
):
758+
) -> Figure:
759759
"""
760760
Create a new figure, or activate an existing figure.
761761
@@ -2346,7 +2346,7 @@ def matshow(A: ArrayLike, fignum: None | int = None, **kwargs) -> AxesImage:
23462346
# Extract actual aspect ratio of array and make appropriately sized
23472347
# figure.
23482348
fig = figure(fignum, figsize=figaspect(A))
2349-
ax = fig.add_axes([0.15, 0.09, 0.775, 0.775])
2349+
ax = fig.add_axes((0.15, 0.09, 0.775, 0.775))
23502350
im = ax.matshow(A, **kwargs)
23512351
sci(im)
23522352
return im
@@ -2402,7 +2402,7 @@ def figimage(
24022402
origin: Literal["upper", "lower"] | None = None,
24032403
resize: bool = False,
24042404
**kwargs,
2405-
):
2405+
) -> FigureImage:
24062406
return gcf().figimage(
24072407
X,
24082408
xo=xo,
@@ -2493,7 +2493,7 @@ def tight_layout(
24932493

24942494
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
24952495
@_copy_docstring_and_deprecators(Figure.waitforbuttonpress)
2496-
def waitforbuttonpress(timeout: float = -1):
2496+
def waitforbuttonpress(timeout: float = -1) -> None | bool:
24972497
return gcf().waitforbuttonpress(timeout=timeout)
24982498

24992499

0 commit comments

Comments
 (0)