Skip to content

Commit 6c5d080

Browse files
committed
Improve doc for data kwarg.
1 parent 1cd9137 commit 6c5d080

File tree

2 files changed

+26
-45
lines changed

2 files changed

+26
-45
lines changed

lib/matplotlib/__init__.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,14 +1416,12 @@ def _label_from_arg(y, default_name):
14161416
_DATA_DOC_APPENDIX = """
14171417
14181418
.. note::
1419-
In addition to the above described arguments, this function can take a
1420-
**data** keyword argument. If such a **data** argument is given, the
1421-
following arguments are replaced by **data[<arg>]**:
1419+
In addition to the above described arguments, this function can take
1420+
a *data* keyword argument. If such a *data* argument is given,
1421+
{replaced}
14221422
1423-
{replaced}
1424-
1425-
Objects passed as **data** must support item access (``data[<arg>]``) and
1426-
membership test (``<arg> in data``).
1423+
Objects passed as **data** must support item access (``data[s]``) and
1424+
membership test (``s in data``).
14271425
"""
14281426

14291427

@@ -1443,13 +1441,17 @@ def _add_data_doc(docstring, replace_names):
14431441
-------
14441442
The augmented docstring.
14451443
"""
1446-
docstring = inspect.cleandoc(docstring) if docstring is not None else ""
1447-
repl = ("* All positional and all keyword arguments."
1448-
if replace_names is None else
1449-
""
1450-
if len(replace_names) == 0 else
1451-
"* All arguments with the following names: {}.".format(
1452-
", ".join(map(repr, sorted(replace_names)))))
1444+
if (docstring is None
1445+
or replace_names is not None and len(replace_names) == 0):
1446+
return docstring
1447+
docstring = inspect.cleandoc(docstring)
1448+
repl = (
1449+
(" every other argument can also be string ``s``, which is\n"
1450+
" interpreted as ``data[s]`` (unless this raises an exception).")
1451+
if replace_names is None else
1452+
(" the following arguments can also be string ``s``, which is\n"
1453+
" interpreted as ``data[s]`` (unless this raises an exception):\n"
1454+
" " + ", ".join(map("*{}*".format, replace_names))) + ".")
14531455
addendum = _DATA_DOC_APPENDIX.format(replaced=repl)
14541456
if _DATA_DOC_TITLE not in docstring:
14551457
addendum = _DATA_DOC_TITLE + addendum

lib/matplotlib/tests/test_preprocess_data.py

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -189,50 +189,29 @@ def test_docstring_addition():
189189
@_preprocess_data()
190190
def funcy(ax, *args, **kwargs):
191191
"""Funcy does nothing"""
192-
pass
193192

194-
assert re.search(r".*All positional and all keyword arguments\.",
195-
funcy.__doc__)
196-
assert not re.search(r".*All positional arguments\.",
197-
funcy.__doc__)
198-
assert not re.search(r".*All arguments with the following names: .*",
199-
funcy.__doc__)
193+
assert re.search(r"each other argument", funcy.__doc__)
194+
assert not re.search(r"the following arguments", funcy.__doc__)
200195

201196
@_preprocess_data(replace_names=[])
202197
def funcy(ax, x, y, z, bar=None):
203198
"""Funcy does nothing"""
204-
pass
205199

206-
assert not re.search(r".*All positional arguments\.",
207-
funcy.__doc__)
208-
assert not re.search(r".*All positional and all keyword arguments\.",
209-
funcy.__doc__)
210-
assert not re.search(r".*All arguments with the following names: .*",
211-
funcy.__doc__)
200+
assert not re.search(r"each other argument", funcy.__doc__)
201+
assert not re.search(r"the following arguments", funcy.__doc__)
212202

213203
@_preprocess_data(replace_names=["bar"])
214204
def funcy(ax, x, y, z, bar=None):
215205
"""Funcy does nothing"""
216-
pass
217206

218-
assert not re.search(r".*All positional arguments\.",
219-
funcy.__doc__)
220-
assert re.search(r".*All arguments with the following names: 'bar'\.",
221-
funcy.__doc__)
222-
assert not re.search(r".*All positional and all keyword arguments\.",
207+
assert not re.search(r"each other argument", funcy.__doc__)
208+
assert not re.search(r"the following arguments .*: \*bar\*\.",
223209
funcy.__doc__)
224210

225-
@_preprocess_data(replace_names=["x", "bar"])
226-
def funcy(ax, x, y, z, bar=None):
211+
@_preprocess_data(replace_names=["x", "t"])
212+
def funcy(ax, x, y, z, t=None):
227213
"""Funcy does nothing"""
228-
pass
229214

230-
# lists can print in any order, so test for both x, bar and bar, x.
231-
assert re.search(r".*All arguments with the following names: '.*', '.*'\.",
232-
funcy.__doc__)
233-
assert re.search(r".*'x'.*", funcy.__doc__)
234-
assert re.search(r".*'bar'.*", funcy.__doc__)
235-
assert not re.search(r".*All positional and all keyword arguments\.",
236-
funcy.__doc__)
237-
assert not re.search(r".*All positional arguments\.",
215+
assert not re.search(r"each other argument", funcy.__doc__)
216+
assert not re.search(r"the following arguments .*: \*x\*, \*t\*\.",
238217
funcy.__doc__)

0 commit comments

Comments
 (0)