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

Commit00c92c2

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

File tree

3 files changed

+101
-2
lines changed

3 files changed

+101
-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: 65 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,56 @@ 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+
107+
ifnothasattr(cls.set,'_autogenerated_signature'):
108+
# Don't overwrite cls.set if the subclass or one of its parents
109+
# has defined a set method set itself.
110+
# If there was no explicit definition, cls.set is inherited from
111+
# the hierarchy of auto-generated set methods, which hold the
112+
# flag _autogenerated_signature.
113+
return
114+
115+
defset(self,**kwargs):
116+
Artist.set(self,**kwargs)
117+
118+
cls.set=set
119+
cls.set.__qualname__=f"{cls.__qualname__}.set"
120+
cls._update_set_signature_and_docstring()
121+
122+
_PROPERTIES_EXCLUDED_FROM_SET= [
123+
'navigate_mode',# not a user-facing function
124+
'figure',# changing the figure is such a profound operation
125+
# that we don't want this in set()
126+
'3d_properties',# cannot be used as a keyword do to leading digit
127+
]
128+
129+
@classmethod
130+
def_update_set_signature_and_docstring(cls):
131+
"""
132+
Update the signature of the set function to list all properties
133+
as keyword arguments.
134+
135+
Property aliases are not listed in the signature for brevity, but
136+
are still accepted as keyword arguments.
137+
"""
138+
cls.set.__signature__=Signature(
139+
[Parameter("self",Parameter.POSITIONAL_OR_KEYWORD),
140+
*[Parameter(prop,Parameter.KEYWORD_ONLY,default=UNSET)
141+
forpropinArtistInspector(cls).get_setters()
142+
ifpropnotinArtist._PROPERTIES_EXCLUDED_FROM_SET]])
143+
cls.set._autogenerated_signature=True
144+
145+
cls.set.__doc__="\n".join([
146+
"Set multiple properties at once.",
147+
"",
148+
"Supported properties are:",
149+
"",
150+
f"%({cls.__name__}:kwdoc)s"])
151+
docstring.dedent_interpd(cls.set)
152+
96153
def__init__(self):
97154
self._stale=True
98155
self.stale_callback=None
@@ -1096,7 +1153,9 @@ def properties(self):
10961153
returnArtistInspector(self).properties()
10971154

10981155
defset(self,**kwargs):
1099-
"""A property batch setter. Pass *kwargs* to set properties."""
1156+
# docstring and signature are auto-generated via
1157+
# Artist._update_set_signature_and_docstring() at the end of the
1158+
# module.
11001159
kwargs=cbook.normalize_kwargs(kwargs,self)
11011160
returnself.update(kwargs)
11021161

@@ -1656,3 +1715,7 @@ def kwdoc(artist):
16561715
return ('\n'.join(ai.pprint_setters_rest(leadingspace=4))
16571716
ifmpl.rcParams['docstring.hardcopy']else
16581717
'Properties:\n'+'\n'.join(ai.pprint_setters(leadingspace=4)))
1718+
1719+
# We defer this to the end of them module, because it needs ArtistInspector
1720+
# to be defined.
1721+
Artist._update_set_signature_and_docstring()

‎lib/matplotlib/tests/test_artist.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,38 @@ def func(artist):
340340
art.remove_callback(oid)
341341
art.pchanged()# must not call the callback anymore
342342
assertfunc.counter==2
343+
344+
345+
deftest_set_signature():
346+
"""Test autogenerated ``set()`` for Artist subclasses."""
347+
classMyArtist1(martist.Artist):
348+
defset_myparam1(self,val):
349+
pass
350+
351+
asserthasattr(MyArtist1.set,'_autogenerated_signature')
352+
assert'myparam1'inMyArtist1.set.__doc__
353+
354+
classMyArtist2(MyArtist1):
355+
defset_myparam2(self,val):
356+
pass
357+
358+
asserthasattr(MyArtist2.set,'_autogenerated_signature')
359+
assert'myparam1'inMyArtist2.set.__doc__
360+
assert'myparam2'inMyArtist2.set.__doc__
361+
362+
363+
deftest_set_is_overwritten():
364+
"""set() defined in Artist subclasses should not be overwritten."""
365+
classMyArtist3(martist.Artist):
366+
367+
defset(self,**kwargs):
368+
"""Not overwritten."""
369+
pass
370+
371+
assertnothasattr(MyArtist3.set,'_autogenerated_signature')
372+
assertMyArtist3.set.__doc__=="Not overwritten."
373+
374+
classMyArtist4(MyArtist3):
375+
pass
376+
377+
assertMyArtist4.setisMyArtist3.set

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp