Skip to content

Commit 0dc472b

Browse files
committed
Harmonize docstrings for boxstyle/connectionstyle/arrowstyle.
- Rely on `__init_subclass__` to avoid the need for the out-of-order `interpd.update`/`dedent_interpd`. - Use consistent wording for all setters, and add ACCEPTS list in all cases. - Move get_boxstyle right next to set_boxstyle (consistently with the other setters/getters). - Make the type check in the setters consistent in all cases (check for str, not for forcing inheritance from the private _Base). - Support `set_connectionstyle()` as equivalent to `set_connectionstyle(None)`, consistently with the other two setters.
1 parent a30b461 commit 0dc472b

File tree

1 file changed

+93
-77
lines changed

1 file changed

+93
-77
lines changed

lib/matplotlib/patches.py

+93-77
Original file line numberDiff line numberDiff line change
@@ -2166,22 +2166,32 @@ def draw_bbox(bbox, renderer, color='k', trans=None):
21662166
r.draw(renderer)
21672167

21682168

2169-
def _simpleprint_styles(_styles):
2170-
"""
2171-
A helper function for the _Style class. Given the dictionary of
2172-
{stylename: styleclass}, return a string rep of the list of keys.
2173-
Used to update the documentation.
2174-
"""
2175-
return "[{}]".format("|".join(map(" '{}' ".format, _styles)))
2176-
2177-
21782169
class _Style:
21792170
"""
21802171
A base class for the Styles. It is meant to be a container class,
21812172
where actual styles are declared as subclass of it, and it
21822173
provides some helper functions.
21832174
"""
21842175

2176+
def __init_subclass__(cls):
2177+
# Automatically perform docstring interpolation on the subclasses:
2178+
# This allows listing the supported styles via
2179+
# - %(BoxStyle:table)s
2180+
# - %(ConnectionStyle:table)s
2181+
# - %(ArrowStyle:table)s
2182+
# and additionally adding .. ACCEPTS: blocks via
2183+
# - %(BoxStyle:table_and_accepts)s
2184+
# - %(ConnectionStyle:table_and_accepts)s
2185+
# - %(ArrowStyle:table_and_accepts)s
2186+
_docstring.interpd.update({
2187+
f"{cls.__name__}:table": cls.pprint_styles(),
2188+
f"{cls.__name__}:table_and_accepts": (
2189+
cls.pprint_styles()
2190+
+ "\n\n .. ACCEPTS: ["
2191+
+ "|".join(map(" '{}' ".format, cls._style_list))
2192+
+ "]")
2193+
})
2194+
21852195
def __new__(cls, stylename, **kwargs):
21862196
"""Return the instance of the subclass with the given style name."""
21872197
# The "class" should have the _style_list attribute, which is a mapping
@@ -2226,7 +2236,6 @@ def pprint_styles(cls):
22262236
*[' '.join(cell.ljust(cl) for cell, cl in zip(row, col_len))
22272237
for row in table[1:]],
22282238
table_formatstr,
2229-
'',
22302239
])
22312240
return textwrap.indent(rst_table, prefix=' ' * 4)
22322241

@@ -2247,6 +2256,7 @@ def _register_style(style_list, cls=None, *, name=None):
22472256
return cls
22482257

22492258

2259+
@_docstring.dedent_interpd
22502260
class BoxStyle(_Style):
22512261
"""
22522262
`BoxStyle` is a container class which defines several
@@ -2266,7 +2276,7 @@ class BoxStyle(_Style):
22662276
22672277
The following boxstyle classes are defined.
22682278
2269-
%(AvailableBoxstyles)s
2279+
%(BoxStyle:table)s
22702280
22712281
An instance of a boxstyle class is a callable object, with the signature ::
22722282
@@ -2624,6 +2634,7 @@ def __call__(self, x0, y0, width, height, mutation_size):
26242634
return Path(saw_vertices, codes)
26252635

26262636

2637+
@_docstring.dedent_interpd
26272638
class ConnectionStyle(_Style):
26282639
"""
26292640
`ConnectionStyle` is a container class which defines
@@ -2644,7 +2655,7 @@ class ConnectionStyle(_Style):
26442655
26452656
The following classes are defined
26462657
2647-
%(AvailableConnectorstyles)s
2658+
%(ConnectionStyle:table)s
26482659
26492660
An instance of any connection style class is an callable object,
26502661
whose call signature is::
@@ -3060,6 +3071,7 @@ def _point_along_a_line(x0, y0, x1, y1, d):
30603071
return x2, y2
30613072

30623073

3074+
@_docstring.dedent_interpd
30633075
class ArrowStyle(_Style):
30643076
"""
30653077
`ArrowStyle` is a container class which defines several
@@ -3080,7 +3092,7 @@ class ArrowStyle(_Style):
30803092
30813093
The following classes are defined
30823094
3083-
%(AvailableArrowstyles)s
3095+
%(ArrowStyle:table)s
30843096
30853097
An instance of any arrow style class is a callable object,
30863098
whose call signature is::
@@ -3810,17 +3822,6 @@ def transmute(self, path, mutation_size, linewidth):
38103822
return path, True
38113823

38123824

3813-
_docstring.interpd.update(
3814-
AvailableBoxstyles=BoxStyle.pprint_styles(),
3815-
ListBoxstyles=_simpleprint_styles(BoxStyle._style_list),
3816-
AvailableArrowstyles=ArrowStyle.pprint_styles(),
3817-
AvailableConnectorstyles=ConnectionStyle.pprint_styles(),
3818-
)
3819-
_docstring.dedent_interpd(BoxStyle)
3820-
_docstring.dedent_interpd(ArrowStyle)
3821-
_docstring.dedent_interpd(ConnectionStyle)
3822-
3823-
38243825
class FancyBboxPatch(Patch):
38253826
"""
38263827
A fancy box around a rectangle with lower left at *xy* = (*x*, *y*)
@@ -3865,7 +3866,7 @@ def __init__(self, xy, width, height,
38653866
38663867
The following box styles are available:
38673868
3868-
%(AvailableBoxstyles)s
3869+
%(BoxStyle:table)s
38693870
38703871
mutation_scale : float, default: 1
38713872
Scaling factor applied to the attributes of the box style
@@ -3911,9 +3912,8 @@ def __init__(self, xy, width, height,
39113912
@_docstring.dedent_interpd
39123913
def set_boxstyle(self, boxstyle=None, **kwargs):
39133914
"""
3914-
Set the box style.
3915+
Set the box style, possibly with further attributes.
39153916
3916-
Most box styles can be further configured using attributes.
39173917
Attributes from the previous box style are not reused.
39183918
39193919
Without argument (or with ``boxstyle=None``), the available box styles
@@ -3922,17 +3922,14 @@ def set_boxstyle(self, boxstyle=None, **kwargs):
39223922
Parameters
39233923
----------
39243924
boxstyle : str or `matplotlib.patches.BoxStyle`
3925-
The style of the fancy box. This can either be a `.BoxStyle`
3926-
instance or a string of the style name and optionally comma
3927-
separated attributes (e.g. "Round, pad=0.2"). This string is
3928-
passed to `.BoxStyle` to construct a `.BoxStyle` object. See
3929-
there for a full documentation.
3925+
The style of the box: either a `.BoxStyle` instance, or a string,
3926+
which is the style name and optionally comma separated attributes
3927+
(e.g. "Round,pad=0.2"). Such a string is used to construct a
3928+
`.BoxStyle` object, as documented in that class.
39303929
39313930
The following box styles are available:
39323931
3933-
%(AvailableBoxstyles)s
3934-
3935-
.. ACCEPTS: %(ListBoxstyles)s
3932+
%(BoxStyle:table_and_accepts)s
39363933
39373934
**kwargs
39383935
Additional attributes for the box style. See the table above for
@@ -3942,17 +3939,20 @@ def set_boxstyle(self, boxstyle=None, **kwargs):
39423939
--------
39433940
::
39443941
3945-
set_boxstyle("round,pad=0.2")
3942+
set_boxstyle("Round,pad=0.2")
39463943
set_boxstyle("round", pad=0.2)
3947-
39483944
"""
39493945
if boxstyle is None:
39503946
return BoxStyle.pprint_styles()
39513947
self._bbox_transmuter = (
3952-
BoxStyle(boxstyle, **kwargs) if isinstance(boxstyle, str)
3953-
else boxstyle)
3948+
BoxStyle(boxstyle, **kwargs)
3949+
if isinstance(boxstyle, str) else boxstyle)
39543950
self.stale = True
39553951

3952+
def get_boxstyle(self):
3953+
"""Return the boxstyle object."""
3954+
return self._bbox_transmuter
3955+
39563956
def set_mutation_scale(self, scale):
39573957
"""
39583958
Set the mutation scale.
@@ -3984,10 +3984,6 @@ def get_mutation_aspect(self):
39843984
return (self._mutation_aspect if self._mutation_aspect is not None
39853985
else 1) # backcompat.
39863986

3987-
def get_boxstyle(self):
3988-
"""Return the boxstyle object."""
3989-
return self._bbox_transmuter
3990-
39913987
def get_path(self):
39923988
"""Return the mutated path of the rectangle."""
39933989
boxstyle = self.get_boxstyle()
@@ -4163,7 +4159,7 @@ def __init__(self, posA=None, posB=None, path=None,
41634159
meant to be scaled with the *mutation_scale*. The following arrow
41644160
styles are available:
41654161
4166-
%(AvailableArrowstyles)s
4162+
%(ArrowStyle:table)s
41674163
41684164
connectionstyle : str or `.ConnectionStyle` or None, optional, \
41694165
default: 'arc3'
@@ -4172,7 +4168,7 @@ def __init__(self, posA=None, posB=None, path=None,
41724168
names, with optional comma-separated attributes. The following
41734169
connection styles are available:
41744170
4175-
%(AvailableConnectorstyles)s
4171+
%(ConnectionStyle:table)s
41764172
41774173
patchA, patchB : `.Patch`, default: None
41784174
Head and tail patches, respectively.
@@ -4269,34 +4265,44 @@ def set_patchB(self, patchB):
42694265
self.patchB = patchB
42704266
self.stale = True
42714267

4272-
def set_connectionstyle(self, connectionstyle, **kwargs):
4268+
@_docstring.dedent_interpd
4269+
def set_connectionstyle(self, connectionstyle=None, **kwargs):
42734270
"""
4274-
Set the connection style. Old attributes are forgotten.
4271+
Set the connection style, possibly with further attributes.
4272+
4273+
Attributes from the previous connection style are not reused.
4274+
4275+
Without argument (or with ``connectionstyle=None``), the available box
4276+
styles are returned as a human-readable string.
42754277
42764278
Parameters
42774279
----------
4278-
connectionstyle : str or `.ConnectionStyle` or None, optional
4279-
Can be a string with connectionstyle name with
4280-
optional comma-separated attributes, e.g.::
4280+
connectionstyle : str or `matplotlib.patches.ConnectionStyle`
4281+
The style of the connection: either a `.ConnectionStyle` instance,
4282+
or a string, which is the style name and optionally comma separated
4283+
attributes (e.g. "Arc,armA=30,rad=10"). Such a string is used to
4284+
construct a `.ConnectionStyle` object, as documented in that class.
42814285
4282-
set_connectionstyle("arc,angleA=0,armA=30,rad=10")
4286+
The following connection styles are available:
42834287
4284-
Alternatively, the attributes can be provided as keywords, e.g.::
4288+
%(ConnectionStyle:table_and_accepts)s
42854289
4286-
set_connectionstyle("arc", angleA=0,armA=30,rad=10)
4290+
**kwargs
4291+
Additional attributes for the connection style. See the table above
4292+
for supported parameters.
42874293
4288-
Without any arguments (or with ``connectionstyle=None``), return
4289-
available styles as a list of strings.
4290-
"""
4294+
Examples
4295+
--------
4296+
::
42914297
4298+
set_connectionstyle("Arc,armA=30,rad=10")
4299+
set_connectionstyle("arc", armA=30, rad=10)
4300+
"""
42924301
if connectionstyle is None:
42934302
return ConnectionStyle.pprint_styles()
4294-
4295-
if (isinstance(connectionstyle, ConnectionStyle._Base) or
4296-
callable(connectionstyle)):
4297-
self._connector = connectionstyle
4298-
else:
4299-
self._connector = ConnectionStyle(connectionstyle, **kwargs)
4303+
self._connector = (
4304+
ConnectionStyle(connectionstyle, **kwargs)
4305+
if isinstance(connectionstyle, str) else connectionstyle)
43004306
self.stale = True
43014307

43024308
def get_connectionstyle(self):
@@ -4305,31 +4311,41 @@ def get_connectionstyle(self):
43054311

43064312
def set_arrowstyle(self, arrowstyle=None, **kwargs):
43074313
"""
4308-
Set the arrow style. Old attributes are forgotten. Without arguments
4309-
(or with ``arrowstyle=None``) returns available box styles as a list of
4310-
strings.
4314+
Set the arrow style, possibly with further attributes.
4315+
4316+
Attributes from the previous arrow style are not reused.
4317+
4318+
Without argument (or with ``arrowstyle=None``), the available box
4319+
styles are returned as a human-readable string.
43114320
43124321
Parameters
43134322
----------
4314-
arrowstyle : None or ArrowStyle or str, default: None
4315-
Can be a string with arrowstyle name with optional comma-separated
4316-
attributes, e.g.::
4323+
arrowstyle : str or `matplotlib.patches.ArrowStyle`
4324+
The style of the arrow: either a `.ArrowStyle` instance, or a
4325+
string, which is the style name and optionally comma separated
4326+
attributes (e.g. "Fancy,head_length=0.2"). Such a string is used to
4327+
construct a `.ArrowStyle` object, as documented in that class.
43174328
4318-
set_arrowstyle("Fancy,head_length=0.2")
4329+
The following arrow styles are available:
43194330
4320-
Alternatively attributes can be provided as keywords, e.g.::
4331+
%(ArrowStyle:table_and_accepts)s
43214332
4322-
set_arrowstyle("fancy", head_length=0.2)
4333+
**kwargs
4334+
Additional attributes for the arrow style. See the table above for
4335+
supported parameters.
43234336
4324-
"""
4337+
Examples
4338+
--------
4339+
::
43254340
4341+
set_arrowstyle("Fancy,head_length=0.2")
4342+
set_arrowstyle("fancy", head_length=0.2)
4343+
"""
43264344
if arrowstyle is None:
43274345
return ArrowStyle.pprint_styles()
4328-
4329-
if isinstance(arrowstyle, ArrowStyle._Base):
4330-
self._arrow_transmuter = arrowstyle
4331-
else:
4332-
self._arrow_transmuter = ArrowStyle(arrowstyle, **kwargs)
4346+
self._arrow_transmuter = (
4347+
ArrowStyle(arrowstyle, **kwargs)
4348+
if isinstance(arrowstyle, str) else arrowstyle)
43334349
self.stale = True
43344350

43354351
def get_arrowstyle(self):

0 commit comments

Comments
 (0)