Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7bffaa2

Browse files
committed
Better signature and docstring for Artist.set
1 parent831a13c commit7bffaa2

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

‎doc/api/axes_api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,3 +606,4 @@ Other
606606
Axes.get_default_bbox_extra_artists
607607
Axes.get_transformed_clip_path_and_affine
608608
Axes.has_data
609+
Axes.set

‎lib/matplotlib/artist.py

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
importcontextlib
33
fromfunctoolsimportwraps
44
importinspect
5+
frominspectimportSignature,Parameter
56
importlogging
67
fromnumbersimportNumber
78
importre
@@ -10,7 +11,7 @@
1011
importnumpyasnp
1112

1213
importmatplotlibasmpl
13-
from .import_api,cbook
14+
from .import_api,cbook,docstring
1415
from .pathimportPath
1516
from .transformsimport (Bbox,IdentityTransform,Transform,TransformedBbox,
1617
TransformedPatchPath,TransformedPath)
@@ -84,6 +85,12 @@ def _stale_axes_callback(self, val):
8485
_XYPair=namedtuple("_XYPair","x y")
8586

8687

88+
class_Unset:
89+
def__repr__(self):
90+
return"<UNSET>"
91+
UNSET=_Unset()
92+
93+
8794
classArtist:
8895
"""
8996
Abstract base class for objects that render into a FigureCanvas.
@@ -93,6 +100,51 @@ class Artist:
93100

94101
zorder=0
95102

103+
def__init_subclass__(cls):
104+
# Inject custom set() methods into the subclass with signature and
105+
# docstring based on the subclasses' properties.
106+
# docstring based on the subclasses' properties.
107+
108+
if'set'incls.__dict__:
109+
return# don't overwrite set if the subclass defines set itself.
110+
111+
defset(self,**kwargs):
112+
Artist.set(self,**kwargs)
113+
114+
cls.set=set
115+
cls._update_set_signature_and_docstring()
116+
117+
_PROPERTIES_EXCLUDED_FROM_SET= [
118+
'navigate_mode',# not a user-facing function
119+
'figure',# changing the figure is such a profound operation
120+
# that we don't want this in set()
121+
'3d_properties',# cannot be used as a keyword do to leading digit
122+
]
123+
124+
@classmethod
125+
def_update_set_signature_and_docstring(cls):
126+
"""
127+
Update the signature of the set function to list all properties
128+
as keyword arguments.
129+
130+
Property aliases are not listed in the signature for brevity, but
131+
are still accepted as keyword arguments.
132+
"""
133+
cls.set.__signature__=Signature(
134+
[Parameter("self",Parameter.POSITIONAL_OR_KEYWORD),
135+
*[Parameter(prop,Parameter.KEYWORD_ONLY,default=UNSET)
136+
forpropinArtistInspector(cls).get_setters()
137+
ifpropnotinArtist._PROPERTIES_EXCLUDED_FROM_SET]])
138+
139+
cls.set.__doc__="\n".join([
140+
"Set multiple properties at once.",
141+
"",
142+
"Supported properties are:",
143+
"",
144+
f"%({cls.__name__}:kwdoc)s"])
145+
docstring.dedent_interpd(cls.set)
146+
147+
96148
def__init__(self):
97149
self._stale=True
98150
self.stale_callback=None
@@ -1096,7 +1148,9 @@ def properties(self):
10961148
returnArtistInspector(self).properties()
10971149

10981150
defset(self,**kwargs):
1099-
"""A property batch setter. Pass *kwargs* to set properties."""
1151+
# docstring and signature are auto-generated via
1152+
# Artist._update_set_signature_and_docstring() at the end of the
1153+
# module.
11001154
kwargs=cbook.normalize_kwargs(kwargs,self)
11011155
returnself.update(kwargs)
11021156

@@ -1656,3 +1710,7 @@ def kwdoc(artist):
16561710
return ('\n'.join(ai.pprint_setters_rest(leadingspace=4))
16571711
ifmpl.rcParams['docstring.hardcopy']else
16581712
'Properties:\n'+'\n'.join(ai.pprint_setters(leadingspace=4)))
1713+
1714+
# We defer this to the end of them module, because it needs ArtistInspector
1715+
# to be defined.
1716+
Artist._update_set_signature_and_docstring()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp