Skip to content

Commit 8df07c3

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

File tree

9 files changed

+29
-24
lines changed

9 files changed

+29
-24
lines changed

lib/matplotlib/artist.pyi

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ from .transforms import (
1111
TransformedPatchPath,
1212
TransformedPath,
1313
)
14+
from .typing import StrLike
1415

1516
import numpy as np
1617

@@ -117,8 +118,8 @@ class Artist:
117118
def set_visible(self, b: bool) -> None: ...
118119
def set_animated(self, b: bool) -> None: ...
119120
def set_in_layout(self, in_layout: bool) -> None: ...
120-
def get_label(self) -> object: ...
121-
def set_label(self, s: object) -> None: ...
121+
def get_label(self) -> str | None: ...
122+
def set_label(self, s: StrLike) -> None: ...
122123
def get_zorder(self) -> float: ...
123124
def set_zorder(self, level: float) -> None: ...
124125
@property

lib/matplotlib/axes/_axes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -644,8 +644,8 @@ def text(self, x, y, s, fontdict=None, **kwargs):
644644
coordinates. The coordinate system can be changed using the
645645
*transform* parameter.
646646
647-
s : str
648-
The text.
647+
s : object
648+
The text. Will be converted to `str`, see `.Text.set_text`.
649649
650650
fontdict : dict, default: None
651651

lib/matplotlib/axes/_axes.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class Axes(_AxesBase):
111111
self,
112112
x: float,
113113
y: float,
114-
s: str,
114+
s: Any,
115115
fontdict: dict[str, Any] | None = ...,
116116
**kwargs
117117
) -> Text: ...

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

+3-3
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[
@@ -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)