Skip to content

Commit bf29a74

Browse files
committed
[TYP] Change typing for texts to Any/object
1 parent dce9ea4 commit bf29a74

File tree

8 files changed

+37
-31
lines changed

8 files changed

+37
-31
lines changed

lib/matplotlib/artist.pyi

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ from .figure import Figure, SubFigure
44
from .path import Path
55
from .patches import Patch
66
from .patheffects import AbstractPathEffect
7+
from .text import Text
78
from .transforms import (
89
BboxBase,
910
Bbox,
1011
Transform,
1112
TransformedPatchPath,
1213
TransformedPath,
1314
)
15+
from .typing import StrLike
1416

1517
import numpy as np
1618

@@ -117,8 +119,8 @@ class Artist:
117119
def set_visible(self, b: bool) -> None: ...
118120
def set_animated(self, b: bool) -> None: ...
119121
def set_in_layout(self, in_layout: bool) -> None: ...
120-
def get_label(self) -> object: ...
121-
def set_label(self, s: object) -> None: ...
122+
def get_label(self) -> str | Text | None: ...
123+
def set_label(self, s: StrLike | None) -> None: ...
122124
def get_zorder(self) -> float: ...
123125
def set_zorder(self, level: float) -> None: ...
124126
@property

lib/matplotlib/axes/_axes.pyi

+9-9
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ from collections.abc import Callable, Sequence
3434
from typing import Any, Literal, overload
3535
import numpy as np
3636
from numpy.typing import ArrayLike
37-
from matplotlib.typing import ColorType, MarkerType, LineStyleType
37+
from matplotlib.typing import ColorType, MarkerType, LineStyleType, StrLike
3838

3939
class Axes(_AxesBase):
4040
def get_title(self, loc: Literal["left", "center", "right"] = ...) -> str: ...
4141
def set_title(
4242
self,
43-
label: str,
43+
label: StrLike,
4444
fontdict: dict[str, Any] | None = ...,
4545
loc: Literal["left", "center", "right"] | None = ...,
4646
pad: float | None = ...,
@@ -111,13 +111,13 @@ class Axes(_AxesBase):
111111
self,
112112
x: float,
113113
y: float,
114-
s: str,
114+
s: StrLike,
115115
fontdict: dict[str, Any] | None = ...,
116116
**kwargs
117117
) -> Text: ...
118118
def annotate(
119119
self,
120-
text: str,
120+
text: StrLike,
121121
xy: tuple[float, float],
122122
xytext: tuple[float, float] | None = ...,
123123
xycoords: str
@@ -164,7 +164,7 @@ class Axes(_AxesBase):
164164
xmax: float | ArrayLike,
165165
colors: ColorType | Sequence[ColorType] | None = ...,
166166
linestyles: LineStyleType = ...,
167-
label: str = ...,
167+
label: StrLike = ...,
168168
*,
169169
data=...,
170170
**kwargs
@@ -176,7 +176,7 @@ class Axes(_AxesBase):
176176
ymax: float | ArrayLike,
177177
colors: ColorType | Sequence[ColorType] | None = ...,
178178
linestyles: LineStyleType = ...,
179-
label: str = ...,
179+
label: StrLike = ...,
180180
*,
181181
data=...,
182182
**kwargs
@@ -289,7 +289,7 @@ class Axes(_AxesBase):
289289
markerfmt: str | None = ...,
290290
basefmt: str | None = ...,
291291
bottom: float = ...,
292-
label: str | None = ...,
292+
label: StrLike | None = ...,
293293
orientation: Literal["vertical", "horizontal"] = ...,
294294
data=...,
295295
) -> StemContainer: ...
@@ -443,7 +443,7 @@ class Axes(_AxesBase):
443443
self, x: float, y: float, dx: float, dy: float, **kwargs
444444
) -> FancyArrow: ...
445445
def quiverkey(
446-
self, Q: Quiver, X: float, Y: float, U: float, label: str, **kwargs
446+
self, Q: Quiver, X: float, Y: float, U: float, label: StrLike, **kwargs
447447
) -> QuiverKey: ...
448448
def quiver(self, *args, data=..., **kwargs) -> Quiver: ...
449449
def barbs(self, *args, data=..., **kwargs) -> Barbs: ...
@@ -549,7 +549,7 @@ class Axes(_AxesBase):
549549
rwidth: float | None = ...,
550550
log: bool = ...,
551551
color: ColorType | Sequence[ColorType] | None = ...,
552-
label: str | Sequence[str] | None = ...,
552+
label: StrLike | Sequence[StrLike] | None = ...,
553553
stacked: bool = ...,
554554
*,
555555
data=...,

lib/matplotlib/offsetbox.pyi

+4-3
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 StrLike
1011

1112
import numpy as np
1213
from numpy.typing import ArrayLike
@@ -117,12 +118,12 @@ class TextArea(OffsetBox):
117118
offset_transform: Transform
118119
def __init__(
119120
self,
120-
s: str,
121+
s: StrLike,
121122
*,
122123
textprops: dict[str, Any] | None = ...,
123124
multilinebaseline: bool = ...,
124125
) -> None: ...
125-
def set_text(self, s: str) -> None: ...
126+
def set_text(self, s: StrLike) -> None: ...
126127
def get_text(self) -> str: ...
127128
def set_multilinebaseline(self, t: bool) -> None: ...
128129
def get_multilinebaseline(self) -> bool: ...
@@ -180,7 +181,7 @@ class AnchoredText(AnchoredOffsetbox):
180181
txt: TextArea
181182
def __init__(
182183
self,
183-
s: str,
184+
s: StrLike,
184185
loc: str,
185186
*,
186187
pad: float = ...,

lib/matplotlib/pyplot.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3951,7 +3951,7 @@ def table(
39513951
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
39523952
@_copy_docstring_and_deprecators(Axes.text)
39533953
def text(
3954-
x: float, y: float, s: str, fontdict: dict[str, Any] | None = None, **kwargs
3954+
x: float, y: float, s: Any, fontdict: dict[str, Any] | None = None, **kwargs
39553955
) -> Text:
39563956
return gca().text(x, y, s, fontdict=fontdict, **kwargs)
39573957

lib/matplotlib/table.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ def __init__(self, xy, width, height, *,
7676
The cell facecolor.
7777
fill : bool, default: True
7878
Whether the cell background is filled.
79-
text : str, optional
79+
text : str, default: ''
8080
The cell text.
81-
loc : {'right', 'center', 'left'}
81+
loc : {'center', 'left', 'right'}, default: 'right'
8282
The alignment of the text within the cell.
8383
fontproperties : dict, optional
8484
A dict defining the font properties of the text. Supported keys and
8585
values are the keyword arguments accepted by `.FontProperties`.
8686
visible_edges : {'closed', 'open', 'horizontal', 'vertical'} or \
87-
substring of 'BRTL'
87+
substring of 'BRTL', default: 'closed'
8888
The cell edges to be drawn with a line: a substring of 'BRTL'
8989
(bottom, right, top, left), or one of 'open' (no edges drawn),
9090
'closed' (all edges drawn), 'horizontal' (bottom and top),
@@ -684,7 +684,7 @@ def table(ax,
684684
cellColours : 2D list of colors, optional
685685
The background colors of the cells.
686686
687-
cellLoc : {'right', 'center', 'left'}
687+
cellLoc : {'center', 'left', 'right'}, default: 'right'
688688
The alignment of the text within the cells.
689689
690690
colWidths : list of float, optional
@@ -697,7 +697,7 @@ def table(ax,
697697
rowColours : list of colors, optional
698698
The colors of the row header cells.
699699
700-
rowLoc : {'left', 'center', 'right'}
700+
rowLoc : {'center', 'left', 'right'}, default: 'center'
701701
The text alignment of the row header cells.
702702
703703
colLabels : list of str, optional
@@ -706,7 +706,7 @@ def table(ax,
706706
colColours : list of colors, optional
707707
The colors of the column header cells.
708708
709-
colLoc : {'center', 'left', 'right'}
709+
colLoc : {'center', 'left', 'right'}, default: 'center'
710710
The text alignment of the column header cells.
711711
712712
loc : str, default: 'bottom'
@@ -717,9 +717,9 @@ def table(ax,
717717
A bounding box to draw the table into. If this is not *None*, this
718718
overrides *loc*.
719719
720-
edges : {'closed', 'open', 'horizontal', 'vertical'} or substring of 'BRTL'
721-
The cell edges to be drawn with a line. See also
722-
`~.Cell.visible_edges`.
720+
edges : {'closed', 'open', 'horizontal', 'vertical'} or substring of 'BRTL', \
721+
default: 'closed'
722+
The cell edges to be drawn with a line. See also `~.Cell.visible_edges`.
723723
724724
Returns
725725
-------

lib/matplotlib/table.pyi

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ from .patches import Rectangle
55
from .path import Path
66
from .text import Text
77
from .transforms import Bbox
8-
from .typing import ColorType
8+
from .typing import ColorType, StrLike
99

1010
from collections.abc import Sequence
1111
from typing import Any, Literal
@@ -21,7 +21,7 @@ class Cell(Rectangle):
2121
edgecolor: ColorType = ...,
2222
facecolor: ColorType = ...,
2323
fill: bool = ...,
24-
text: str = ...,
24+
text: StrLike = ...,
2525
loc: Literal["left", "center", "right"] = ...,
2626
fontproperties: dict[str, Any] | None = ...,
2727
visible_edges: str | None = ...
@@ -68,7 +68,7 @@ class Table(Artist):
6868

6969
def table(
7070
ax: Axes,
71-
cellText: Sequence[Sequence[str]] | None = ...,
71+
cellText: Sequence[Sequence[Any]] | None = ...,
7272
cellColours: Sequence[Sequence[ColorType]] | None = ...,
7373
cellLoc: Literal["left", "center", "right"] = ...,
7474
colWidths: Sequence[float] | None = ...,

lib/matplotlib/text.pyi

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ 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, StrLike
2020

2121
class Text(Artist):
2222
zorder: float
2323
def __init__(
2424
self,
2525
x: float = ...,
2626
y: float = ...,
27-
text: Any = ...,
27+
text: StrLike = ...,
2828
*,
2929
color: ColorType | None = ...,
3030
verticalalignment: Literal[
@@ -65,7 +65,7 @@ class Text(Artist):
6565
def get_horizontalalignment(self) -> Literal["left", "center", "right"]: ...
6666
def get_unitless_position(self) -> tuple[float, float]: ...
6767
def get_position(self) -> tuple[float, float]: ...
68-
def get_text(self) -> str: ...
68+
def get_text(self) -> str | None: ...
6969
def get_verticalalignment(
7070
self,
7171
) -> Literal["bottom", "baseline", "center", "center_baseline", "top"]: ...
@@ -97,7 +97,7 @@ class Text(Artist):
9797
def set_verticalalignment(
9898
self, align: Literal["bottom", "baseline", "center", "center_baseline", "top"]
9999
) -> None: ...
100-
def set_text(self, s: Any) -> None: ...
100+
def set_text(self, s: StrLike) -> None: ...
101101
def set_fontproperties(self, fp: FontProperties | str | Path | None) -> None: ...
102102
def set_usetex(self, usetex: bool | None) -> None: ...
103103
def get_usetex(self) -> bool: ...

lib/matplotlib/typing.py

+3
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,6 @@
5858
_HT = TypeVar("_HT", bound=Hashable)
5959
HashableList = list[Union[_HT, "HashableList[_HT]"]]
6060
"""A nested list of Hashable values."""
61+
62+
# Many methods accept arbitrary objects which are then converted to strings
63+
StrLike = Any

0 commit comments

Comments
 (0)