From dc8f66e98d3fb5db2242418e5ef2bef62c10a7de Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Tue, 17 Dec 2024 16:05:08 -0800 Subject: [PATCH 1/5] Type annotation add_subplot for projection="3d" --- lib/matplotlib/figure.pyi | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/matplotlib/figure.pyi b/lib/matplotlib/figure.pyi index c048ad556036..765a794d9b4b 100644 --- a/lib/matplotlib/figure.pyi +++ b/lib/matplotlib/figure.pyi @@ -1,3 +1,7 @@ +from typing import TYPE_CHECKING +if TYPE_CHECKING: + import mpl_toolkits + from collections.abc import Callable, Hashable, Iterable, Sequence import os from typing import Any, IO, Literal, TypeVar, overload @@ -87,6 +91,8 @@ class FigureBase(Artist): # TODO: docstring indicates SubplotSpec a valid arg, but none of the listed signatures appear to be that @overload + def add_subplot(self, *args, projection="3d", **kwargs) -> mpl_toolkits.mplot3d.Axes3D: ... + @overload def add_subplot( self, nrows: int, ncols: int, index: int | tuple[int, int], **kwargs ) -> Axes: ... From ef1be0a25faae8e401da003244a8fb0218ee672e Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Tue, 17 Dec 2024 16:13:00 -0800 Subject: [PATCH 2/5] Try Literal approach --- lib/matplotlib/figure.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/figure.pyi b/lib/matplotlib/figure.pyi index 765a794d9b4b..6f422bf8deb4 100644 --- a/lib/matplotlib/figure.pyi +++ b/lib/matplotlib/figure.pyi @@ -91,7 +91,7 @@ class FigureBase(Artist): # TODO: docstring indicates SubplotSpec a valid arg, but none of the listed signatures appear to be that @overload - def add_subplot(self, *args, projection="3d", **kwargs) -> mpl_toolkits.mplot3d.Axes3D: ... + def add_subplot(self, *args, projection: Literal["3d"], **kwargs) -> mpl_toolkits.mplot3d.Axes3D: ... @overload def add_subplot( self, nrows: int, ncols: int, index: int | tuple[int, int], **kwargs From 7299ae4b2d5c8eeb34f2873994e6bd8973b776b1 Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Tue, 17 Dec 2024 16:16:52 -0800 Subject: [PATCH 3/5] Tidy up Axes3D import --- lib/matplotlib/figure.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/figure.pyi b/lib/matplotlib/figure.pyi index 6f422bf8deb4..82b115833c1c 100644 --- a/lib/matplotlib/figure.pyi +++ b/lib/matplotlib/figure.pyi @@ -1,6 +1,6 @@ from typing import TYPE_CHECKING if TYPE_CHECKING: - import mpl_toolkits + from mpl_toolkits.mplot3d import Axes3D from collections.abc import Callable, Hashable, Iterable, Sequence import os @@ -91,7 +91,7 @@ class FigureBase(Artist): # TODO: docstring indicates SubplotSpec a valid arg, but none of the listed signatures appear to be that @overload - def add_subplot(self, *args, projection: Literal["3d"], **kwargs) -> mpl_toolkits.mplot3d.Axes3D: ... + def add_subplot(self, *args, projection: Literal["3d"], **kwargs) -> Axes3D: ... @overload def add_subplot( self, nrows: int, ncols: int, index: int | tuple[int, int], **kwargs From 81f6ba34e4889b36e32bd621f931db46cefd3939 Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Thu, 1 May 2025 09:45:31 -0700 Subject: [PATCH 4/5] Incorporate feedback from @QuLogic --- lib/matplotlib/figure.pyi | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/figure.pyi b/lib/matplotlib/figure.pyi index 82b115833c1c..5c46c826e415 100644 --- a/lib/matplotlib/figure.pyi +++ b/lib/matplotlib/figure.pyi @@ -1,10 +1,6 @@ -from typing import TYPE_CHECKING -if TYPE_CHECKING: - from mpl_toolkits.mplot3d import Axes3D - from collections.abc import Callable, Hashable, Iterable, Sequence import os -from typing import Any, IO, Literal, TypeVar, overload +from typing import Any, IO, Literal, TypeVar, overload, TYPE_CHECKING import numpy as np from numpy.typing import ArrayLike @@ -29,6 +25,9 @@ from matplotlib.lines import Line2D from matplotlib.patches import Rectangle, Patch from matplotlib.text import Text from matplotlib.transforms import Affine2D, Bbox, BboxBase, Transform +if TYPE_CHECKING: + from mpl_toolkits.mplot3d import Axes3D + from .typing import ColorType, HashableList _T = TypeVar("_T") From 56fcafb2cf66b8d713af731461b4d62f1b2175d2 Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Thu, 1 May 2025 10:27:43 -0700 Subject: [PATCH 5/5] Remove guard as per @timhoffm's suggestion --- lib/matplotlib/figure.pyi | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/figure.pyi b/lib/matplotlib/figure.pyi index 5c46c826e415..e7c5175d8af9 100644 --- a/lib/matplotlib/figure.pyi +++ b/lib/matplotlib/figure.pyi @@ -1,6 +1,6 @@ from collections.abc import Callable, Hashable, Iterable, Sequence import os -from typing import Any, IO, Literal, TypeVar, overload, TYPE_CHECKING +from typing import Any, IO, Literal, TypeVar, overload import numpy as np from numpy.typing import ArrayLike @@ -25,8 +25,7 @@ from matplotlib.lines import Line2D from matplotlib.patches import Rectangle, Patch from matplotlib.text import Text from matplotlib.transforms import Affine2D, Bbox, BboxBase, Transform -if TYPE_CHECKING: - from mpl_toolkits.mplot3d import Axes3D +from mpl_toolkits.mplot3d import Axes3D from .typing import ColorType, HashableList