8000 TYP: Add CoordsType and use in relevant locations(#28532) · matplotlib/matplotlib@fec6863 · GitHub
[go: up one dir, main page]

Skip to content

Commit fec6863

Browse files
authored
TYP: Add CoordsType and use in relevant locations(#28532)
* TYP: Fix xycoords and friends * Fix organization * Fix linting * Fix import * Fix linting * Fix for earlier Pythons * Fix again for older Pythons * Fix long line * Trailing whitespace * Fix older Pythons again * Fix mypy stub check * Fix mypy stubtests * Fix stubtest for Python 3.9 * Handle suggestions in PR * Fix lint and stubtest
1 parent 9674b53 commit fec6863

File tree

5 files changed

+49
-93
lines changed

5 files changed

+49
-93
lines changed

lib/matplotlib/axes/_axes.pyi

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ from matplotlib.mlab import GaussianKDE
2222
from matplotlib.patches import Rectangle, FancyArrow, Polygon, StepPatch, Wedge
2323
from matplotlib.quiver import Quiver, QuiverKey, Barbs
2424
from matplotlib.text import Annotation, Text
25-
from matplotlib.transforms import Transform, Bbox
25+
from matplotlib.transforms import Transform
26+
from matplotlib.typing import CoordsType
2627
import matplotlib.tri as mtri
2728
import matplotlib.table as mtable
2829
import matplotlib.stackplot as mstack
@@ -122,17 +123,8 @@ class Axes(_AxesBase):
122123
text: str,
123124
xy: tuple[float, float],
124125
xytext: tuple[float, float] | None = ...,
125-
xycoords: str
126-
| Artist
127-
| Transform
128-
| Callable[[RendererBase], Bbox | Transform]
129-
| tuple[float, float] = ...,
130-
textcoords: str
131-
| Artist
132-
| Transform
133-
| Callable[[RendererBase], Bbox | Transform]
134-
| tuple[float, float]
135-
| None = ...,
126+
xycoords: CoordsType = ...,
127+
textcoords: CoordsType | None = ...,
136128
arrowprops: dict[str, Any] | None = ...,
137129
annotation_clip: bool | None = ...,
138130
**kwargs

lib/matplotlib/offsetbox.pyi

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ from matplotlib.font_manager import FontProperties
77
from matplotlib.image import BboxImage
88
from matplotlib.patches import FancyArrowPatch, FancyBboxPatch
99
from matplotlib.transforms import Bbox, BboxBase, Transform
10+
from matplotlib.typing import CoordsType
1011

1112
import numpy as np
1213
from numpy.typing import ArrayLike
@@ -219,9 +220,7 @@ class AnnotationBbox(martist.Artist, mtext._AnnotationBase):
219220
offsetbox: OffsetBox
220221
arrowprops: dict[str, Any] | None
221222
xybox: tuple[float, float]
222-
boxcoords: str | tuple[str, str] | martist.Artist | Transform | Callable[
223-
[RendererBase], Bbox | Transform
224-
]
223+
boxcoords: CoordsType
225224
arrow_patch: FancyArrowPatch | None
226225
patch: FancyBboxPatch
227226
prop: FontProperties
@@ -230,17 +229,8 @@ class AnnotationBbox(martist.Artist, mtext._AnnotationBase):
230229
offsetbox: OffsetBox,
231230
xy: tuple[float, float],
232231
xybox: tuple[float, float] | None = ...,
233-
xycoords: str
234-
| tuple[str, str]
235-
| martist.Artist
236-
| Transform
237-
| Callable[[RendererBase], Bbox | Transform] = ...,
238-
boxcoords: str
239-
| tuple[str, str]
240-
| martist.Artist
241-
| Transform
242-
| Callable[[RendererBase], Bbox | Transform]
243-
| None = ...,
232+
xycoords: CoordsType = ...,
233+
boxcoords: CoordsType | None = ...,
244234
*,
245235
frameon: bool = ...,
246236
pad: float = ...,
@@ -258,17 +248,11 @@ class AnnotationBbox(martist.Artist, mtext._AnnotationBase):
258248
@property
259249
def anncoords(
260250
self,
261-
) -> str | tuple[str, str] | martist.Artist | Transform | Callable[
262-
[RendererBase], Bbox | Transform
263-
]: ...
251+
) -> CoordsType: ...
264252
@anncoords.setter
265253
def anncoords(
266254
self,
267-
coords: str
268-
| tuple[str, str]
269-
| martist.Artist
270-
| Transform
271-
| Callable[[RendererBase], Bbox | Transform],
255+
coords: CoordsType,
272256
) -> None: ...
273257
def get_children(self) -> list[martist.Artist]: ...
274258
def set_figure(self, fig: Figure | SubFigure) -> None: ...

lib/matplotlib/pyplot.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
import matplotlib.backend_bases
9797
from matplotlib.axis import Tick
9898
from matplotlib.axes._base import _AxesBase
99-
from matplotlib.backend_bases import RendererBase, Event
99+
from matplotlib.backend_bases import Event
100100
from matplotlib.cm import ScalarMappable
101101
from matplotlib.contour import ContourSet, QuadContourSet
102102
from matplotlib.collections import (
@@ -120,8 +120,13 @@
120120
from matplotlib.patches import FancyArrow, StepPatch, Wedge
121121
from matplotlib.quiver import Barbs, Quiver, QuiverKey
122122
from matplotlib.scale import ScaleBase
123-
from matplotlib.transforms import Transform, Bbox
124-
from matplotlib.typing import ColorType, LineStyleType, MarkerType, HashableList
123+
from matplotlib.typing import (
124+
ColorType,
125+
CoordsType,
126+
HashableList,
127+
LineStyleType,
128+
MarkerType,
129+
)
125130
from matplotlib.widgets import SubplotTool
126131

127132
_P = ParamSpec('_P')
@@ -2860,17 +2865,8 @@ def annotate(
28602865
text: str,
28612866
xy: tuple[float, float],
28622867
xytext: tuple[float, float] | None = None,
2863-
xycoords: str
2864-
| Artist
2865-
| Transform
2866-
| Callable[[RendererBase], Bbox | Transform]
2867-
| tuple[float, float] = "data",
2868-
textcoords: str
2869-
| Artist
2870-
| Transform
2871-
| Callable[[RendererBase], Bbox | Transform]
2872-
| tuple[float, float]
2873-
| None = None,
2868+
xycoords: CoordsType = "data",
2869+
textcoords: CoordsType | None = None,
28742870
arrowprops: dict[str, Any] | None = None,
28752871
annotation_clip: bool | None = None,
28762872
**kwargs,

lib/matplotlib/text.pyi

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ from .transforms import (
1616

1717
from collections.abc import Callable, Iterable
1818
from typing import Any, Literal
19-
from .typing import ColorType
19+
from .typing import ColorType, CoordsType
2020

2121
class Text(Artist):
2222
zorder: float
@@ -120,17 +120,11 @@ class OffsetFrom:
120120

121121
class _AnnotationBase:
122122
xy: tuple[float, float]
123-
xycoords: str | tuple[str, str] | Artist | Transform | Callable[
124-
[RendererBase], Bbox | Transform
125-
]
123+
xycoords: CoordsType
126124
def __init__(
127125
self,
128126
xy,
129-
xycoords: str
130-
| tuple[str, str]
131-
| Artist
132-
| Transform
133-
| Callable[[RendererBase], Bbox | Transform] = ...,
127+
xycoords: CoordsType = ...,
134128
annotation_clip: bool | None = ...,
135129
) -> None: ...
136130
def set_annotation_clip(self, b: bool | None) -> None: ...
@@ -147,67 +141,40 @@ class Annotation(Text, _AnnotationBase):
147141
text: str,
148142
xy: tuple[float, float],
149143
xytext: tuple[float, float] | None = ...,
150-
xycoords: str
151-
| tuple[str, str]
152-
| Artist
153-
| Transform
154-
| Callable[[RendererBase], Bbox | Transform] = ...,
155-
textcoords: str
156-
| tuple[str, str]
157-
| Artist
158-
| Transform
159-
| Callable[[RendererBase], Bbox | Transform]
160-
| None = ...,
144+
xycoords: CoordsType = ...,
145+
textcoords: CoordsType | None = ...,
161146
arrowprops: dict[str, Any] | None = ...,
162147
annotation_clip: bool | None = ...,
163148
**kwargs
164149
) -> None: ...
165150
@property
166151
def xycoords(
167152
self,
168-
) -> str | tuple[str, str] | Artist | Transform | Callable[
169-
[RendererBase], Bbox | Transform
170-
]: ...
153+
) -> CoordsType: ...
171154
@xycoords.setter
172155
def xycoords(
173156
self,
174-
xycoords: str
175-
| tuple[str, str]
176-
| Artist
177-
| Transform
178-
| Callable[[RendererBase], Bbox | Transform],
157+
xycoords: CoordsType,
179158
) -> None: ...
180159
@property
181160
def xyann(self) -> tuple[float, float]: ...
182161
@xyann.setter
183162
def xyann(self, xytext: tuple[float, float]) -> None: ...
184163
def get_anncoords(
185164
self,
186-
) -> str | tuple[str, str] | Artist | Transform | Callable[
187-
[RendererBase], Bbox | Transform
188-
]: ...
165+
) -> CoordsType: ...
189166
def set_anncoords(
190167
self,
191-
coords: str
192-
| tuple[str, str]
193-
| Artist
194-
| Transform
195-
| Callable[[RendererBase], Bbox | Transform],
168+
coords: CoordsType,
196169
) -> None: ...
197170
@property
198171
def anncoords(
199172
self,
200-
) -> str | tuple[str, str] | Artist | Transform | Callable[
201-
[RendererBase], Bbox | Transform
202-
]: ...
173+
) -> CoordsType: ...
203174
@anncoords.setter
204175
def anncoords(
205176
self,
206-
coords: str
207-
| tuple[str, str]
208-
| Artist
209-
| Transform
210-
| Callable[[RendererBase], Bbox | Transform],
177+
coords: CoordsType,
211178
) -> None: ...
212179
def update_positions(self, renderer: RendererBase) -> None: ...
213180
# Drops `dpi` parameter from superclass

lib/matplotlib/typing.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111
"""
1212
from collections.abc import Hashable, Sequence
1313
import pathlib
14-
from typing import Any, Literal, TypeAlias, TypeVar
14+
from typing import Any, Callable, Literal, TypeAlias, TypeVar, Union
1515

1616
from . import path
1717
from ._enums import JoinStyle, CapStyle
18+
from .artist import Artist
19+
from .backend_bases import RendererBase
1820
from .markers import MarkerStyle
21+
from .transforms import Bbox, Transform
1922

2023
RGBColorType: TypeAlias = tuple[float, float, float] | str
2124
RGBAColorType: TypeAlias = (
@@ -49,6 +52,20 @@
4952
JoinStyleType: TypeAlias = JoinStyle | Literal["miter", "round", "bevel"]
5053
CapStyleType: TypeAlias = CapStyle | Literal["butt", "projecting", "round"]
5154

55+
CoordsBaseType = Union[
56+
str,
57+
Artist,
58+
Transform,
59+
Callable[
60+
[RendererBase],
61+
Union[Bbox, Transform]
62+
]
63+
]
64+
CoordsType = Union[
65+
CoordsBaseType,
66+
tuple[CoordsBaseType, CoordsBaseType]
67+
]
68+
5269
RcStyleType: TypeAlias = (
5370
str |
5471
dict[str, Any] |

0 commit comments

Comments
 (0)
0