Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3cf8bdf

Browse files
committed
Allow ACCEPTS as ReST comment in docstrings
1 parent 407a460 commit 3cf8bdf

3 files changed

Lines changed: 43 additions & 2 deletions

File tree

doc/devel/documenting_mpl.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,22 @@ acceptable values in the docs; it can also be displayed using, e.g.,
478478
``plt.setp(Line2D)`` (all properties) or ``plt.setp(Line2D, 'linestyle')``
479479
(just one property).
480480
481+
There are cases in which the ACCEPTS string is not useful in the
482+
generated Sphinx documentation, e.g. if the valid parameters are already
483+
defined in the numpydoc parameter list. You can hide the ACCEPTS string from
484+
Sphinx by making it a ReST comment (i.e. use ``.. ACCEPTS:``):
485+
486+
.. code-block:: python
487+
488+
def set_linestyle(self, linestyle):
489+
"""
490+
An ACCEPTS string invisible to Sphinx.
491+
492+
.. ACCEPTS: [ '-' | '--' | '-.' | ':' | 'steps' | 'None' | ' ' | '' ]
493+
"""
494+
495+
496+
481497
Keyword arguments
482498
-----------------
483499

lib/matplotlib/artist.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,15 +1133,15 @@ def get_aliases(self):
11331133
return aliases
11341134

11351135
_get_valid_values_regex = re.compile(
1136-
r"\n\s*ACCEPTS:\s*((?:.|\n)*?)(?:$|(?:\n\n))"
1136+
r"\n\s*(?:\.\.\s+)?ACCEPTS:\s*((?:.|\n)*?)(?:$|(?:\n\n))"
11371137
)
11381138

11391139
def get_valid_values(self, attr):
11401140
"""
11411141
Get the legal arguments for the setter associated with *attr*.
11421142
11431143
This is done by querying the docstring of the function *set_attr*
1144-
for a line that begins with ACCEPTS:
1144+
for a line that begins with "ACCEPTS" or ".. ACCEPTS":
11451145
11461146
e.g., for a line linestyle, return
11471147
"[ ``'-'`` | ``'--'`` | ``'-.'`` | ``':'`` | ``'steps'`` | ``'None'``

lib/matplotlib/tests/test_artist.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import numpy as np
88

9+
import pytest
10+
911
import matplotlib.pyplot as plt
1012
import matplotlib.patches as mpatches
1113
import matplotlib.lines as mlines
@@ -255,3 +257,26 @@ def test_None_zorder():
255257
assert ln.get_zorder() == 123456
256258
ln.set_zorder(None)
257259
assert ln.get_zorder() == mlines.Line2D.zorder
260+
261+
262+
@pytest.mark.parametrize('accept_clause, expected', [
263+
('', 'unknown'),
264+
("ACCEPTS: [ '-' | '--' | '-.' ]", "[ '-' | '--' | '-.' ] "),
265+
('ACCEPTS: Some description.', 'Some description. '),
266+
('.. ACCEPTS: Some description.', 'Some description. '),
267+
])
268+
def test_artist_inspector_get_valid_values(accept_clause, expected):
269+
class TestArtist(martist.Artist):
270+
def set_f(self):
271+
pass
272+
273+
func = TestArtist.set_f
274+
if hasattr(func, '__func__'):
275+
func = func.__func__ # python 2 must write via __func__.__doc__
276+
func.__doc__ = """
277+
Some text.
278+
279+
%s
280+
""" % accept_clause
281+
valid_values = martist.ArtistInspector(TestArtist).get_valid_values('f')
282+
assert valid_values == expected

0 commit comments

Comments
 (0)