From c21a83d06d0ee3d0fe6476af73d1d9d0c3670221 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Tue, 9 Jul 2024 12:39:35 -0400 Subject: [PATCH 01/15] TYP: Fix xycoords and friends --- lib/matplotlib/axes/_axes.pyi | 18 +++++------- lib/matplotlib/offsetbox.pyi | 28 ++++-------------- lib/matplotlib/pyplot.py | 13 ++------- lib/matplotlib/text.pyi | 54 +++++++---------------------------- 4 files changed, 26 insertions(+), 87 deletions(-) diff --git a/lib/matplotlib/axes/_axes.pyi b/lib/matplotlib/axes/_axes.pyi index d04e3ad99ddc..dd7ecea64b60 100644 --- a/lib/matplotlib/axes/_axes.pyi +++ b/lib/matplotlib/axes/_axes.pyi @@ -36,6 +36,11 @@ import numpy as np from numpy.typing import ArrayLike from matplotlib.typing import ColorType, MarkerType, LineStyleType +_coords_type_base = ( + str | Artist | Transform | Callable[[RendererBase], Bbox | Transform] +) +_coords_type = _coords_type_base | tuple[_coords_type_base, _coords_type_base] + class Axes(_AxesBase): def get_title(self, loc: Literal["left", "center", "right"] = ...) -> str: ... def set_title( @@ -122,17 +127,8 @@ class Axes(_AxesBase): text: str, xy: tuple[float, float], xytext: tuple[float, float] | None = ..., - xycoords: str - | Artist - | Transform - | Callable[[RendererBase], Bbox | Transform] - | tuple[float, float] = ..., - textcoords: str - | Artist - | Transform - | Callable[[RendererBase], Bbox | Transform] - | tuple[float, float] - | None = ..., + xycoords: _coords_type = ..., + textcoords: _coords_type = ..., arrowprops: dict[str, Any] | None = ..., annotation_clip: bool | None = ..., **kwargs diff --git a/lib/matplotlib/offsetbox.pyi b/lib/matplotlib/offsetbox.pyi index c222a9b2973e..cfd6522352e0 100644 --- a/lib/matplotlib/offsetbox.pyi +++ b/lib/matplotlib/offsetbox.pyi @@ -1,4 +1,5 @@ import matplotlib.artist as martist +from matplotlib.axes._axes import _coords_type from matplotlib.backend_bases import RendererBase, Event, FigureCanvasBase from matplotlib.colors import Colormap, Normalize import matplotlib.text as mtext @@ -219,9 +220,7 @@ class AnnotationBbox(martist.Artist, mtext._AnnotationBase): offsetbox: OffsetBox arrowprops: dict[str, Any] | None xybox: tuple[float, float] - boxcoords: str | tuple[str, str] | martist.Artist | Transform | Callable[ - [RendererBase], Bbox | Transform - ] + boxcoords: _coords_type arrow_patch: FancyArrowPatch | None patch: FancyBboxPatch prop: FontProperties @@ -230,17 +229,8 @@ class AnnotationBbox(martist.Artist, mtext._AnnotationBase): offsetbox: OffsetBox, xy: tuple[float, float], xybox: tuple[float, float] | None = ..., - xycoords: str - | tuple[str, str] - | martist.Artist - | Transform - | Callable[[RendererBase], Bbox | Transform] = ..., - boxcoords: str - | tuple[str, str] - | martist.Artist - | Transform - | Callable[[RendererBase], Bbox | Transform] - | None = ..., + xycoords: _coords_type = ..., + boxcoords: _coords_type = ..., *, frameon: bool = ..., pad: float = ..., @@ -258,17 +248,11 @@ class AnnotationBbox(martist.Artist, mtext._AnnotationBase): @property def anncoords( self, - ) -> str | tuple[str, str] | martist.Artist | Transform | Callable[ - [RendererBase], Bbox | Transform - ]: ... + ) -> _coords_type: ... @anncoords.setter def anncoords( self, - coords: str - | tuple[str, str] - | martist.Artist - | Transform - | Callable[[RendererBase], Bbox | Transform], + coords: _coords_type, ) -> None: ... def get_children(self) -> list[martist.Artist]: ... def set_figure(self, fig: Figure) -> None: ... diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 441af598dbc6..1e6167b6a561 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -2858,17 +2858,8 @@ def annotate( text: str, xy: tuple[float, float], xytext: tuple[float, float] | None = None, - xycoords: str - | Artist - | Transform - | Callable[[RendererBase], Bbox | Transform] - | tuple[float, float] = "data", - textcoords: str - | Artist - | Transform - | Callable[[RendererBase], Bbox | Transform] - | tuple[float, float] - | None = None, + xycoords: _coords_type = "data", + textcoords: _coords_type = None, arrowprops: dict[str, Any] | None = None, annotation_clip: bool | None = None, **kwargs, diff --git a/lib/matplotlib/text.pyi b/lib/matplotlib/text.pyi index 6a83b1bbbed9..ce301d6bac9d 100644 --- a/lib/matplotlib/text.pyi +++ b/lib/matplotlib/text.pyi @@ -1,4 +1,5 @@ from .artist import Artist +from .axes._axes import _coords_type from .backend_bases import RendererBase from .font_manager import FontProperties from .offsetbox import DraggableAnnotation @@ -120,17 +121,11 @@ class OffsetFrom: class _AnnotationBase: xy: tuple[float, float] - xycoords: str | tuple[str, str] | Artist | Transform | Callable[ - [RendererBase], Bbox | Transform - ] + xycoords: _coords_type def __init__( self, xy, - xycoords: str - | tuple[str, str] - | Artist - | Transform - | Callable[[RendererBase], Bbox | Transform] = ..., + xycoords: _coords_type = ..., annotation_clip: bool | None = ..., ) -> None: ... def set_annotation_clip(self, b: bool | None) -> None: ... @@ -147,17 +142,8 @@ class Annotation(Text, _AnnotationBase): text: str, xy: tuple[float, float], xytext: tuple[float, float] | None = ..., - xycoords: str - | tuple[str, str] - | Artist - | Transform - | Callable[[RendererBase], Bbox | Transform] = ..., - textcoords: str - | tuple[str, str] - | Artist - | Transform - | Callable[[RendererBase], Bbox | Transform] - | None = ..., + xycoords: _coords_type = ..., + textcoords: _coords_type = ..., arrowprops: dict[str, Any] | None = ..., annotation_clip: bool | None = ..., **kwargs @@ -165,17 +151,11 @@ class Annotation(Text, _AnnotationBase): @property def xycoords( self, - ) -> str | tuple[str, str] | Artist | Transform | Callable[ - [RendererBase], Bbox | Transform - ]: ... + ) -> _coords_type: ... @xycoords.setter def xycoords( self, - xycoords: str - | tuple[str, str] - | Artist - | Transform - | Callable[[RendererBase], Bbox | Transform], + xycoords: _coords_type, ) -> None: ... @property def xyann(self) -> tuple[float, float]: ... @@ -183,31 +163,19 @@ class Annotation(Text, _AnnotationBase): def xyann(self, xytext: tuple[float, float]) -> None: ... def get_anncoords( self, - ) -> str | tuple[str, str] | Artist | Transform | Callable[ - [RendererBase], Bbox | Transform - ]: ... + ) -> _coords_type: ... def set_anncoords( self, - coords: str - | tuple[str, str] - | Artist - | Transform - | Callable[[RendererBase], Bbox | Transform], + coords: _coords_type, ) -> None: ... @property def anncoords( self, - ) -> str | tuple[str, str] | Artist | Transform | Callable[ - [RendererBase], Bbox | Transform - ]: ... + ) -> _coords_type: ... @anncoords.setter def anncoords( self, - coords: str - | tuple[str, str] - | Artist - | Transform - | Callable[[RendererBase], Bbox | Transform], + coords: _coords_type, ) -> None: ... def update_positions(self, renderer: RendererBase) -> None: ... # Drops `dpi` parameter from superclass From fc4da308ceae42c2c67df22ba3a1801613a5e4fe Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Tue, 9 Jul 2024 13:18:02 -0400 Subject: [PATCH 02/15] Fix organization --- lib/matplotlib/axes/_axes.pyi | 9 ++------- lib/matplotlib/axes/_base.py | 11 +++++++++++ lib/matplotlib/offsetbox.pyi | 2 +- lib/matplotlib/pyplot.py | 2 +- lib/matplotlib/text.pyi | 2 +- 5 files changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/matplotlib/axes/_axes.pyi b/lib/matplotlib/axes/_axes.pyi index dd7ecea64b60..56f46b63e81c 100644 --- a/lib/matplotlib/axes/_axes.pyi +++ b/lib/matplotlib/axes/_axes.pyi @@ -1,4 +1,4 @@ -from matplotlib.axes._base import _AxesBase +from matplotlib.axes._base import _AxesBase, _coords_type from matplotlib.axes._secondary_axes import SecondaryAxis from matplotlib.artist import Artist @@ -22,7 +22,7 @@ from matplotlib.mlab import GaussianKDE from matplotlib.patches import Rectangle, FancyArrow, Polygon, StepPatch, Wedge from matplotlib.quiver import Quiver, QuiverKey, Barbs from matplotlib.text import Annotation, Text -from matplotlib.transforms import Transform, Bbox +from matplotlib.transforms import Transform import matplotlib.tri as mtri import matplotlib.table as mtable import matplotlib.stackplot as mstack @@ -36,11 +36,6 @@ import numpy as np from numpy.typing import ArrayLike from matplotlib.typing import ColorType, MarkerType, LineStyleType -_coords_type_base = ( - str | Artist | Transform | Callable[[RendererBase], Bbox | Transform] -) -_coords_type = _coords_type_base | tuple[_coords_type_base, _coords_type_base] - class Axes(_AxesBase): def get_title(self, loc: Literal["left", "center", "right"] = ...) -> str: ... def set_title( diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 18ff80a51e5a..e8f82bb42e48 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -7,6 +7,7 @@ from operator import attrgetter import re import types +from typing import Callable import numpy as np @@ -14,6 +15,7 @@ from matplotlib import _api, cbook, _docstring, offsetbox import matplotlib.artist as martist import matplotlib.axis as maxis +from matplotlib import backend_bases from matplotlib.cbook import _OrderedSet, _check_1d, index_of import matplotlib.collections as mcoll import matplotlib.colors as mcolors @@ -32,6 +34,15 @@ _log = logging.getLogger(__name__) +_coords_type_base = ( + str | + martist.Artist | + mtransforms.Transform | + Callable[[backend_bases.RendererBase], mtransforms.Bbox | mtransforms.Transform] +) +_coords_type = _coords_type_base | tuple[_coords_type_base, _coords_type_base] + + class _axis_method_wrapper: """ Helper to generate Axes methods wrapping Axis methods. diff --git a/lib/matplotlib/offsetbox.pyi b/lib/matplotlib/offsetbox.pyi index cfd6522352e0..31632a55cd0c 100644 --- a/lib/matplotlib/offsetbox.pyi +++ b/lib/matplotlib/offsetbox.pyi @@ -1,5 +1,5 @@ import matplotlib.artist as martist -from matplotlib.axes._axes import _coords_type +from matplotlib.axes._base import _coords_type from matplotlib.backend_bases import RendererBase, Event, FigureCanvasBase from matplotlib.colors import Colormap, Normalize import matplotlib.text as mtext diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 1e6167b6a561..6f7c268ac261 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -94,7 +94,7 @@ import matplotlib.artist import matplotlib.backend_bases from matplotlib.axis import Tick - from matplotlib.axes._base import _AxesBase + from matplotlib.axes._base import _AxesBase, _coords_type from matplotlib.backend_bases import RendererBase, Event from matplotlib.cm import ScalarMappable from matplotlib.contour import ContourSet, QuadContourSet diff --git a/lib/matplotlib/text.pyi b/lib/matplotlib/text.pyi index ce301d6bac9d..caa12279e557 100644 --- a/lib/matplotlib/text.pyi +++ b/lib/matplotlib/text.pyi @@ -1,5 +1,5 @@ from .artist import Artist -from .axes._axes import _coords_type +from .axes._base import _coords_type from .backend_bases import RendererBase from .font_manager import FontProperties from .offsetbox import DraggableAnnotation From 4682f444ed0b0a4c67db917bb19e46d639529a79 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Tue, 9 Jul 2024 13:47:26 -0400 Subject: [PATCH 03/15] Fix linting --- lib/matplotlib/axes/_axes.pyi | 6 +++--- lib/matplotlib/axes/_base.py | 4 ++-- lib/matplotlib/axes/_base.pyi | 3 +++ lib/matplotlib/offsetbox.pyi | 12 ++++++------ lib/matplotlib/pyplot.py | 9 ++++----- lib/matplotlib/text.pyi | 22 +++++++++++----------- 6 files changed, 29 insertions(+), 27 deletions(-) diff --git a/lib/matplotlib/axes/_axes.pyi b/lib/matplotlib/axes/_axes.pyi index 56f46b63e81c..09c2e9bbcf91 100644 --- a/lib/matplotlib/axes/_axes.pyi +++ b/lib/matplotlib/axes/_axes.pyi @@ -1,4 +1,4 @@ -from matplotlib.axes._base import _AxesBase, _coords_type +from matplotlib.axes._base import _AxesBase, coords_type from matplotlib.axes._secondary_axes import SecondaryAxis from matplotlib.artist import Artist @@ -122,8 +122,8 @@ class Axes(_AxesBase): text: str, xy: tuple[float, float], xytext: tuple[float, float] | None = ..., - xycoords: _coords_type = ..., - textcoords: _coords_type = ..., + xycoords: coords_type = ..., + textcoords: coords_type | None = ..., arrowprops: dict[str, Any] | None = ..., annotation_clip: bool | None = ..., **kwargs diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index e8f82bb42e48..a607214fa6c6 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -34,13 +34,13 @@ _log = logging.getLogger(__name__) -_coords_type_base = ( +coords_type_base = ( str | martist.Artist | mtransforms.Transform | Callable[[backend_bases.RendererBase], mtransforms.Bbox | mtransforms.Transform] ) -_coords_type = _coords_type_base | tuple[_coords_type_base, _coords_type_base] +coords_type = coords_type_base | tuple[coords_type_base, coords_type_base] class _axis_method_wrapper: diff --git a/lib/matplotlib/axes/_base.pyi b/lib/matplotlib/axes/_base.pyi index 751dcd248a5c..2f788270f0a2 100644 --- a/lib/matplotlib/axes/_base.pyi +++ b/lib/matplotlib/axes/_base.pyi @@ -29,6 +29,9 @@ from numpy.typing import ArrayLike from typing import Any, Literal, TypeVar, overload from matplotlib.typing import ColorType +coords_type_base = str | Artist | Transform | Callable[[RendererBase], Bbox | Transform] +coords_type = coords_type_base | tuple[coords_type_base, coords_type_base] + _T = TypeVar("_T", bound=Artist) class _axis_method_wrapper: diff --git a/lib/matplotlib/offsetbox.pyi b/lib/matplotlib/offsetbox.pyi index 31632a55cd0c..ef12da446e59 100644 --- a/lib/matplotlib/offsetbox.pyi +++ b/lib/matplotlib/offsetbox.pyi @@ -1,5 +1,5 @@ import matplotlib.artist as martist -from matplotlib.axes._base import _coords_type +from matplotlib.axes._base import coords_type from matplotlib.backend_bases import RendererBase, Event, FigureCanvasBase from matplotlib.colors import Colormap, Normalize import matplotlib.text as mtext @@ -220,7 +220,7 @@ class AnnotationBbox(martist.Artist, mtext._AnnotationBase): offsetbox: OffsetBox arrowprops: dict[str, Any] | None xybox: tuple[float, float] - boxcoords: _coords_type + boxcoords: coords_type arrow_patch: FancyArrowPatch | None patch: FancyBboxPatch prop: FontProperties @@ -229,8 +229,8 @@ class AnnotationBbox(martist.Artist, mtext._AnnotationBase): offsetbox: OffsetBox, xy: tuple[float, float], xybox: tuple[float, float] | None = ..., - xycoords: _coords_type = ..., - boxcoords: _coords_type = ..., + xycoords: coords_type = ..., + boxcoords: coords_type = ..., *, frameon: bool = ..., pad: float = ..., @@ -248,11 +248,11 @@ class AnnotationBbox(martist.Artist, mtext._AnnotationBase): @property def anncoords( self, - ) -> _coords_type: ... + ) -> coords_type: ... @anncoords.setter def anncoords( self, - coords: _coords_type, + coords: coords_type, ) -> None: ... def get_children(self) -> list[martist.Artist]: ... def set_figure(self, fig: Figure) -> None: ... diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 6f7c268ac261..26d06e5b5996 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -94,8 +94,8 @@ import matplotlib.artist import matplotlib.backend_bases from matplotlib.axis import Tick - from matplotlib.axes._base import _AxesBase, _coords_type - from matplotlib.backend_bases import RendererBase, Event + from matplotlib.axes._base import _AxesBase, coords_type + from matplotlib.backend_bases import Event from matplotlib.cm import ScalarMappable from matplotlib.contour import ContourSet, QuadContourSet from matplotlib.collections import ( @@ -119,7 +119,6 @@ from matplotlib.patches import FancyArrow, StepPatch, Wedge from matplotlib.quiver import Barbs, Quiver, QuiverKey from matplotlib.scale import ScaleBase - from matplotlib.transforms import Transform, Bbox from matplotlib.typing import ColorType, LineStyleType, MarkerType, HashableList from matplotlib.widgets import SubplotTool @@ -2858,8 +2857,8 @@ def annotate( text: str, xy: tuple[float, float], xytext: tuple[float, float] | None = None, - xycoords: _coords_type = "data", - textcoords: _coords_type = None, + xycoords: coords_type = "data", + textcoords: coords_type | None = None, arrowprops: dict[str, Any] | None = None, annotation_clip: bool | None = None, **kwargs, diff --git a/lib/matplotlib/text.pyi b/lib/matplotlib/text.pyi index caa12279e557..93cfdd61014a 100644 --- a/lib/matplotlib/text.pyi +++ b/lib/matplotlib/text.pyi @@ -1,5 +1,5 @@ from .artist import Artist -from .axes._base import _coords_type +from .axes._base import coords_type from .backend_bases import RendererBase from .font_manager import FontProperties from .offsetbox import DraggableAnnotation @@ -121,11 +121,11 @@ class OffsetFrom: class _AnnotationBase: xy: tuple[float, float] - xycoords: _coords_type + xycoords: coords_type def __init__( self, xy, - xycoords: _coords_type = ..., + xycoords: coords_type = ..., annotation_clip: bool | None = ..., ) -> None: ... def set_annotation_clip(self, b: bool | None) -> None: ... @@ -142,8 +142,8 @@ class Annotation(Text, _AnnotationBase): text: str, xy: tuple[float, float], xytext: tuple[float, float] | None = ..., - xycoords: _coords_type = ..., - textcoords: _coords_type = ..., + xycoords: coords_type = ..., + textcoords: coords_type | None = ..., arrowprops: dict[str, Any] | None = ..., annotation_clip: bool | None = ..., **kwargs @@ -151,11 +151,11 @@ class Annotation(Text, _AnnotationBase): @property def xycoords( self, - ) -> _coords_type: ... + ) -> coords_type: ... @xycoords.setter def xycoords( self, - xycoords: _coords_type, + xycoords: coords_type, ) -> None: ... @property def xyann(self) -> tuple[float, float]: ... @@ -163,19 +163,19 @@ class Annotation(Text, _AnnotationBase): def xyann(self, xytext: tuple[float, float]) -> None: ... def get_anncoords( self, - ) -> _coords_type: ... + ) -> coords_type: ... def set_anncoords( self, - coords: _coords_type, + coords: coords_type, ) -> None: ... @property def anncoords( self, - ) -> _coords_type: ... + ) -> coords_type: ... @anncoords.setter def anncoords( self, - coords: _coords_type, + coords: coords_type, ) -> None: ... def update_positions(self, renderer: RendererBase) -> None: ... # Drops `dpi` parameter from superclass From 316d882f6ec81aad6ab0670b4b53beec6478efcb Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Tue, 9 Jul 2024 13:54:59 -0400 Subject: [PATCH 04/15] Fix import --- lib/matplotlib/axes/_base.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index a607214fa6c6..87e967d6809d 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -7,7 +7,7 @@ from operator import attrgetter import re import types -from typing import Callable +from typing import Callable, Type import numpy as np @@ -34,13 +34,13 @@ _log = logging.getLogger(__name__) -coords_type_base = ( - str | - martist.Artist | - mtransforms.Transform | +_coords_type_base = ( + Type[str] | + martist.Artist | + mtransforms.Transform | Callable[[backend_bases.RendererBase], mtransforms.Bbox | mtransforms.Transform] ) -coords_type = coords_type_base | tuple[coords_type_base, coords_type_base] +_coords_type = _coords_type_base | tuple[_coords_type_base, _coords_type_base] class _axis_method_wrapper: From 8b370c20e93ab4ced2a3cadea9f87cb0ac261224 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Tue, 9 Jul 2024 13:59:33 -0400 Subject: [PATCH 05/15] Fix linting --- lib/matplotlib/axes/_base.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 87e967d6809d..dc538bde3e26 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -35,10 +35,10 @@ _coords_type_base = ( - Type[str] | - martist.Artist | - mtransforms.Transform | - Callable[[backend_bases.RendererBase], mtransforms.Bbox | mtransforms.Transform] + Type[str] + | martist.Artist + | mtransforms.Transform + | Callable[[backend_bases.RendererBase], mtransforms.Bbox | mtransforms.Transform] ) _coords_type = _coords_type_base | tuple[_coords_type_base, _coords_type_base] From f4473a94404457190474276fbae1096bcb2a03d8 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Tue, 9 Jul 2024 14:13:45 -0400 Subject: [PATCH 06/15] Fix for earlier Pythons --- lib/matplotlib/axes/_base.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index dc538bde3e26..1c0c7d3fb661 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -7,7 +7,7 @@ from operator import attrgetter import re import types -from typing import Callable, Type +from typing import Callable, Union import numpy as np @@ -34,12 +34,12 @@ _log = logging.getLogger(__name__) -_coords_type_base = ( - Type[str] - | martist.Artist - | mtransforms.Transform - | Callable[[backend_bases.RendererBase], mtransforms.Bbox | mtransforms.Transform] -) +_coords_type_base = Union[ + str, + martist.Artist, + mtransforms.Transform, + Callable[[backend_bases.RendererBase], mtransforms.Bbox | mtransforms.Transform] +] _coords_type = _coords_type_base | tuple[_coords_type_base, _coords_type_base] From 0a174e975e8c80bb6ca8ec6f8fa7100d93b96585 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Tue, 9 Jul 2024 14:21:08 -0400 Subject: [PATCH 07/15] Fix again for older Pythons --- lib/matplotlib/axes/_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 1c0c7d3fb661..25308e3170ef 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -38,7 +38,7 @@ str, martist.Artist, mtransforms.Transform, - Callable[[backend_bases.RendererBase], mtransforms.Bbox | mtransforms.Transform] + Callable[[backend_bases.RendererBase], Union[mtransforms.Bbox, mtransforms.Transform]] ] _coords_type = _coords_type_base | tuple[_coords_type_base, _coords_type_base] From 851125d905795c5fa9aae78c99f9d4d258959677 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Tue, 9 Jul 2024 14:21:37 -0400 Subject: [PATCH 08/15] Fix long line --- lib/matplotlib/axes/_base.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 25308e3170ef..c81d7625d800 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -38,7 +38,10 @@ str, martist.Artist, mtransforms.Transform, - Callable[[backend_bases.RendererBase], Union[mtransforms.Bbox, mtransforms.Transform]] + Callable[ + [backend_bases.RendererBase], + Union[mtransforms.Bbox, mtransforms.Transform] + ] ] _coords_type = _coords_type_base | tuple[_coords_type_base, _coords_type_base] From 151985666538209f0cd55429294345271f031229 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Tue, 9 Jul 2024 14:23:42 -0400 Subject: [PATCH 09/15] Trailing whitespace --- lib/matplotlib/axes/_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index c81d7625d800..2bce0ab29c9d 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -39,7 +39,7 @@ martist.Artist, mtransforms.Transform, Callable[ - [backend_bases.RendererBase], + [backend_bases.RendererBase], Union[mtransforms.Bbox, mtransforms.Transform] ] ] From ec232b5cdb56043681d83299cf4010d1d431f1cd Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Tue, 9 Jul 2024 14:24:32 -0400 Subject: [PATCH 10/15] Fix older Pythons again --- lib/matplotlib/axes/_base.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 2bce0ab29c9d..a55f4cfea885 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -43,7 +43,10 @@ Union[mtransforms.Bbox, mtransforms.Transform] ] ] -_coords_type = _coords_type_base | tuple[_coords_type_base, _coords_type_base] +_coords_type = Union[ + _coords_type_base, + tuple[_coords_type_base, _coords_type_base] +] class _axis_method_wrapper: From 58d436121681da95b983c6bb9a893deabdee0b08 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Tue, 9 Jul 2024 14:31:19 -0400 Subject: [PATCH 11/15] Fix mypy stub check --- lib/matplotlib/offsetbox.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/offsetbox.pyi b/lib/matplotlib/offsetbox.pyi index ef12da446e59..a78aac8ce01d 100644 --- a/lib/matplotlib/offsetbox.pyi +++ b/lib/matplotlib/offsetbox.pyi @@ -230,7 +230,7 @@ class AnnotationBbox(martist.Artist, mtext._AnnotationBase): xy: tuple[float, float], xybox: tuple[float, float] | None = ..., xycoords: coords_type = ..., - boxcoords: coords_type = ..., + boxcoords: coords_type | None = ..., *, frameon: bool = ..., pad: float = ..., From ec2ccbe8f61b663c408632f221590a95dcd31ec4 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Tue, 9 Jul 2024 14:45:55 -0400 Subject: [PATCH 12/15] Fix mypy stubtests --- lib/matplotlib/axes/_base.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index a55f4cfea885..e6070e132a9e 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -7,7 +7,7 @@ from operator import attrgetter import re import types -from typing import Callable, Union +from typing import Callable, Union, TypeAlias import numpy as np @@ -34,7 +34,7 @@ _log = logging.getLogger(__name__) -_coords_type_base = Union[ +coords_type_base: TypeAlias = Union[ str, martist.Artist, mtransforms.Transform, @@ -43,9 +43,9 @@ Union[mtransforms.Bbox, mtransforms.Transform] ] ] -_coords_type = Union[ - _coords_type_base, - tuple[_coords_type_base, _coords_type_base] +coords_type: TypeAlias = Union[ + coords_type_base, + tuple[coords_type_base, coords_type_base] ] From e8be3e596f0f2e89454a903bd212005a7e5df0f9 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Tue, 9 Jul 2024 14:50:06 -0400 Subject: [PATCH 13/15] Fix stubtest for Python 3.9 --- lib/matplotlib/axes/_base.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index e6070e132a9e..570dc812f30d 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -7,7 +7,7 @@ from operator import attrgetter import re import types -from typing import Callable, Union, TypeAlias +from typing import Callable, Union import numpy as np @@ -34,7 +34,7 @@ _log = logging.getLogger(__name__) -coords_type_base: TypeAlias = Union[ +coords_type_base = Union[ str, martist.Artist, mtransforms.Transform, @@ -43,7 +43,7 @@ Union[mtransforms.Bbox, mtransforms.Transform] ] ] -coords_type: TypeAlias = Union[ +coords_type = Union[ coords_type_base, tuple[coords_type_base, coords_type_base] ] From 4db147a1f6acbddda766848d043747a533093010 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Mon, 16 Sep 2024 11:36:13 -0400 Subject: [PATCH 14/15] Handle suggestions in PR --- lib/matplotlib/axes/_axes.pyi | 5 +++-- lib/matplotlib/axes/_base.py | 17 ----------------- lib/matplotlib/axes/_base.pyi | 3 --- lib/matplotlib/offsetbox.pyi | 11 ++++++----- lib/matplotlib/pyplot.py | 8 ++++---- lib/matplotlib/text.pyi | 23 +++++++++++------------ lib/matplotlib/typing.py | 19 ++++++++++++++++++- 7 files changed, 42 insertions(+), 44 deletions(-) diff --git a/lib/matplotlib/axes/_axes.pyi b/lib/matplotlib/axes/_axes.pyi index 09c2e9bbcf91..b78f4c59b2a0 100644 --- a/lib/matplotlib/axes/_axes.pyi +++ b/lib/matplotlib/axes/_axes.pyi @@ -23,6 +23,7 @@ from matplotlib.patches import Rectangle, FancyArrow, Polygon, StepPatch, Wedge from matplotlib.quiver import Quiver, QuiverKey, Barbs from matplotlib.text import Annotation, Text from matplotlib.transforms import Transform +from matplotlib.typing import CoordsType import matplotlib.tri as mtri import matplotlib.table as mtable import matplotlib.stackplot as mstack @@ -122,8 +123,8 @@ class Axes(_AxesBase): text: str, xy: tuple[float, float], xytext: tuple[float, float] | None = ..., - xycoords: coords_type = ..., - textcoords: coords_type | None = ..., + xycoords: CoordsType = ..., + textcoords: CoordsType | None = ..., arrowprops: dict[str, Any] | None = ..., annotation_clip: bool | None = ..., **kwargs diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 570dc812f30d..18ff80a51e5a 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -7,7 +7,6 @@ from operator import attrgetter import re import types -from typing import Callable, Union import numpy as np @@ -15,7 +14,6 @@ from matplotlib import _api, cbook, _docstring, offsetbox import matplotlib.artist as martist import matplotlib.axis as maxis -from matplotlib import backend_bases from matplotlib.cbook import _OrderedSet, _check_1d, index_of import matplotlib.collections as mcoll import matplotlib.colors as mcolors @@ -34,21 +32,6 @@ _log = logging.getLogger(__name__) -coords_type_base = Union[ - str, - martist.Artist, - mtransforms.Transform, - Callable[ - [backend_bases.RendererBase], - Union[mtransforms.Bbox, mtransforms.Transform] - ] -] -coords_type = Union[ - coords_type_base, - tuple[coords_type_base, coords_type_base] -] - - class _axis_method_wrapper: """ Helper to generate Axes methods wrapping Axis methods. diff --git a/lib/matplotlib/axes/_base.pyi b/lib/matplotlib/axes/_base.pyi index 2f788270f0a2..751dcd248a5c 100644 --- a/lib/matplotlib/axes/_base.pyi +++ b/lib/matplotlib/axes/_base.pyi @@ -29,9 +29,6 @@ from numpy.typing import ArrayLike from typing import Any, Literal, TypeVar, overload from matplotlib.typing import ColorType -coords_type_base = str | Artist | Transform | Callable[[RendererBase], Bbox | Transform] -coords_type = coords_type_base | tuple[coords_type_base, coords_type_base] - _T = TypeVar("_T", bound=Artist) class _axis_method_wrapper: diff --git a/lib/matplotlib/offsetbox.pyi b/lib/matplotlib/offsetbox.pyi index a78aac8ce01d..d56d1fb62717 100644 --- a/lib/matplotlib/offsetbox.pyi +++ b/lib/matplotlib/offsetbox.pyi @@ -8,6 +8,7 @@ from matplotlib.font_manager import FontProperties from matplotlib.image import BboxImage from matplotlib.patches import FancyArrowPatch, FancyBboxPatch from matplotlib.transforms import Bbox, BboxBase, Transform +from matplotlib.typing import CoordsType import numpy as np from numpy.typing import ArrayLike @@ -220,7 +221,7 @@ class AnnotationBbox(martist.Artist, mtext._AnnotationBase): offsetbox: OffsetBox arrowprops: dict[str, Any] | None xybox: tuple[float, float] - boxcoords: coords_type + boxcoords: CoordsType arrow_patch: FancyArrowPatch | None patch: FancyBboxPatch prop: FontProperties @@ -229,8 +230,8 @@ class AnnotationBbox(martist.Artist, mtext._AnnotationBase): offsetbox: OffsetBox, xy: tuple[float, float], xybox: tuple[float, float] | None = ..., - xycoords: coords_type = ..., - boxcoords: coords_type | None = ..., + xycoords: CoordsType = ..., + boxcoords: CoordsType | None = ..., *, frameon: bool = ..., pad: float = ..., @@ -248,11 +249,11 @@ class AnnotationBbox(martist.Artist, mtext._AnnotationBase): @property def anncoords( self, - ) -> coords_type: ... + ) -> CoordsType: ... @anncoords.setter def anncoords( self, - coords: coords_type, + coords: CoordsType, ) -> None: ... def get_children(self) -> list[martist.Artist]: ... def set_figure(self, fig: Figure) -> None: ... diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 26d06e5b5996..f18599756043 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -94,7 +94,7 @@ import matplotlib.artist import matplotlib.backend_bases from matplotlib.axis import Tick - from matplotlib.axes._base import _AxesBase, coords_type + from matplotlib.axes._base import _AxesBase from matplotlib.backend_bases import Event from matplotlib.cm import ScalarMappable from matplotlib.contour import ContourSet, QuadContourSet @@ -119,7 +119,7 @@ from matplotlib.patches import FancyArrow, StepPatch, Wedge from matplotlib.quiver import Barbs, Quiver, QuiverKey from matplotlib.scale import ScaleBase - from matplotlib.typing import ColorType, LineStyleType, MarkerType, HashableList + from matplotlib.typing import ColorType, CoordsType, LineStyleType, MarkerType, HashableList from matplotlib.widgets import SubplotTool _P = ParamSpec('_P') @@ -2857,8 +2857,8 @@ def annotate( text: str, xy: tuple[float, float], xytext: tuple[float, float] | None = None, - xycoords: coords_type = "data", - textcoords: coords_type | None = None, + xycoords: CoordsType = "data", + textcoords: CoordsType | None = None, arrowprops: dict[str, Any] | None = None, annotation_clip: bool | None = None, **kwargs, diff --git a/lib/matplotlib/text.pyi b/lib/matplotlib/text.pyi index 93cfdd61014a..d767a1b66a3c 100644 --- a/lib/matplotlib/text.pyi +++ b/lib/matplotlib/text.pyi @@ -1,5 +1,4 @@ from .artist import Artist -from .axes._base import coords_type from .backend_bases import RendererBase from .font_manager import FontProperties from .offsetbox import DraggableAnnotation @@ -17,7 +16,7 @@ from .transforms import ( from collections.abc import Callable, Iterable from typing import Any, Literal -from .typing import ColorType +from .typing import ColorType, CoordsType class Text(Artist): zorder: float @@ -121,11 +120,11 @@ class OffsetFrom: class _AnnotationBase: xy: tuple[float, float] - xycoords: coords_type + xycoords: CoordsType def __init__( self, xy, - xycoords: coords_type = ..., + xycoords: CoordsType = ..., annotation_clip: bool | None = ..., ) -> None: ... def set_annotation_clip(self, b: bool | None) -> None: ... @@ -142,8 +141,8 @@ class Annotation(Text, _AnnotationBase): text: str, xy: tuple[float, float], xytext: tuple[float, float] | None = ..., - xycoords: coords_type = ..., - textcoords: coords_type | None = ..., + xycoords: CoordsType = ..., + textcoords: CoordsType | None = ..., arrowprops: dict[str, Any] | None = ..., annotation_clip: bool | None = ..., **kwargs @@ -151,11 +150,11 @@ class Annotation(Text, _AnnotationBase): @property def xycoords( self, - ) -> coords_type: ... + ) -> CoordsType: ... @xycoords.setter def xycoords( self, - xycoords: coords_type, + xycoords: CoordsType, ) -> None: ... @property def xyann(self) -> tuple[float, float]: ... @@ -163,19 +162,19 @@ class Annotation(Text, _AnnotationBase): def xyann(self, xytext: tuple[float, float]) -> None: ... def get_anncoords( self, - ) -> coords_type: ... + ) -> CoordsType: ... def set_anncoords( self, - coords: coords_type, + coords: CoordsType, ) -> None: ... @property def anncoords( self, - ) -> coords_type: ... + ) -> CoordsType: ... @anncoords.setter def anncoords( self, - coords: coords_type, + coords: CoordsType, ) -> None: ... def update_positions(self, renderer: RendererBase) -> None: ... # Drops `dpi` parameter from superclass diff --git a/lib/matplotlib/typing.py b/lib/matplotlib/typing.py index 02059be94ba2..8e4741d3fef8 100644 --- a/lib/matplotlib/typing.py +++ b/lib/matplotlib/typing.py @@ -11,11 +11,14 @@ """ from collections.abc import Hashable, Sequence import pathlib -from typing import Any, Literal, TypeVar, Union +from typing import Any, Callable, Literal, TypeVar, Union from . import path from ._enums import JoinStyle, CapStyle +from .artist import Artist +from .backend_bases import RendererBase from .markers import MarkerStyle +from .transforms import Bbox, Transform # The following are type aliases. Once python 3.9 is dropped, they should be annotated # using ``typing.TypeAlias`` and Unions should be converted to using ``|`` syntax. @@ -48,6 +51,20 @@ JoinStyleType = Union[JoinStyle, Literal["miter", "round", "bevel"]] CapStyleType = Union[CapStyle, Literal["butt", "projecting", "round"]] +CoordsBaseType = Union[ + str, + Artist, + Transform, + Callable[ + [RendererBase], + Union[Bbox, Transform] + ] +] +CoordsType = Union[ + CoordsBaseType, + tuple[CoordsBaseType, CoordsBaseType] +] + RcStyleType = Union[ str, dict[str, Any], From 5fd42ee8b8d4cc50b89e198fc8fbfe2e421a15e3 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Mon, 16 Sep 2024 11:54:53 -0400 Subject: [PATCH 15/15] Fix lint and stubtest --- lib/matplotlib/axes/_axes.pyi | 2 +- lib/matplotlib/offsetbox.pyi | 1 - lib/matplotlib/pyplot.py | 8 +++++++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/axes/_axes.pyi b/lib/matplotlib/axes/_axes.pyi index e8dd396c87f8..78a1f146e27f 100644 --- a/lib/matplotlib/axes/_axes.pyi +++ b/lib/matplotlib/axes/_axes.pyi @@ -1,4 +1,4 @@ -from matplotlib.axes._base import _AxesBase, coords_type +from matplotlib.axes._base import _AxesBase from matplotlib.axes._secondary_axes import SecondaryAxis from matplotlib.artist import Artist diff --git a/lib/matplotlib/offsetbox.pyi b/lib/matplotlib/offsetbox.pyi index 7086b13f5e19..3b1520e17138 100644 --- a/lib/matplotlib/offsetbox.pyi +++ b/lib/matplotlib/offsetbox.pyi @@ -1,5 +1,4 @@ import matplotlib.artist as martist -from matplotlib.axes._base import coords_type from matplotlib.backend_bases import RendererBase, Event, FigureCanvasBase from matplotlib.colors import Colormap, Normalize import matplotlib.text as mtext diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index df98dae911b4..f70808e89d04 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -120,7 +120,13 @@ from matplotlib.patches import FancyArrow, StepPatch, Wedge from matplotlib.quiver import Barbs, Quiver, QuiverKey from matplotlib.scale import ScaleBase - from matplotlib.typing import ColorType, CoordsType, LineStyleType, MarkerType, HashableList + from matplotlib.typing import ( + ColorType, + CoordsType, + HashableList, + LineStyleType, + MarkerType, + ) from matplotlib.widgets import SubplotTool _P = ParamSpec('_P')