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

Skip to content

Commit 19c2e78

Browse files
authored
Merge pull request #10284 from timhoffm/accepts-inline-comment
Allow ACCEPTS as ReST comment in docstrings
2 parents 6044a57 + 3cf8bdf commit 19c2e78

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

doc/devel/documenting_mpl.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,22 @@ acceptable values in the docs; it can also be displayed using, e.g.,
485485
``plt.setp(Line2D)`` (all properties) or ``plt.setp(Line2D, 'linestyle')``
486486
(just one property).
487487

488+
There are cases in which the ACCEPTS string is not useful in the
489+
generated Sphinx documentation, e.g. if the valid parameters are already
490+
defined in the numpydoc parameter list. You can hide the ACCEPTS string from
491+
Sphinx by making it a ReST comment (i.e. use ``.. ACCEPTS:``):
492+
493+
.. code-block:: python
494+
495+
def set_linestyle(self, linestyle):
496+
"""
497+
An ACCEPTS string invisible to Sphinx.
498+
499+
.. ACCEPTS: [ '-' | '--' | '-.' | ':' | 'steps' | 'None' | ' ' | '' ]
500+
"""
501+
502+
503+
488504
Keyword arguments
489505
-----------------
490506

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)