Skip to content

Commit fb0e229

Browse files
authored
Merge pull request matplotlib#15776 from anntzer/datakwargdoc
Improve doc for data kwarg.
2 parents 18aa912 + 67ef911 commit fb0e229

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
@@ -1402,14 +1402,12 @@ def _label_from_arg(y, default_name):
14021402
_DATA_DOC_APPENDIX = """
14031403
14041404
.. note::
1405-
In addition to the above described arguments, this function can take a
1406-
**data** keyword argument. If such a **data** argument is given, the
1407-
following arguments are replaced by **data[<arg>]**:
1405+
In addition to the above described arguments, this function can take
1406+
a *data* keyword argument. If such a *data* argument is given,
1407+
{replaced}
14081408
1409-
{replaced}
1410-
1411-
Objects passed as **data** must support item access (``data[<arg>]``) and
1412-
membership test (``<arg> in data``).
1409+
Objects passed as **data** must support item access (``data[s]``) and
1410+
membership test (``s in data``).
14131411
"""
14141412

14151413

@@ -1429,13 +1427,17 @@ def _add_data_doc(docstring, replace_names):
14291427
-------
14301428
The augmented docstring.
14311429
"""
1432-
docstring = inspect.cleandoc(docstring) if docstring is not None else ""
1433-
repl = ("* All positional and all keyword arguments."
1434-
if replace_names is None else
1435-
""
1436-
if len(replace_names) == 0 else
1437-
"* All arguments with the following names: {}.".format(
1438-
", ".join(map(repr, sorted(replace_names)))))
1430+
if (docstring is None
1431+
or replace_names is not None and len(replace_names) == 0):
1432+
return docstring
1433+
docstring = inspect.cleandoc(docstring)
1434+
repl = (
1435+
(" every other argument can also be string ``s``, which is\n"
1436+
" interpreted as ``data[s]`` (unless this raises an exception).")
1437+
if replace_names is None else
1438+
(" the following arguments can also be string ``s``, which is\n"
1439+
" interpreted as ``data[s]`` (unless this raises an exception):\n"
1440+
" " + ", ".join(map("*{}*".format, replace_names))) + ".")
14391441
addendum = _DATA_DOC_APPENDIX.format(replaced=repl)
14401442
if _DATA_DOC_TITLE not in docstring:
14411443
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"every 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"every 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"every 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"every other argument", funcy.__doc__)
216+
assert not re.search(r"the following arguments .*: \*x\*, \*t\*\.",
238217
funcy.__doc__)

0 commit comments

Comments
 (0)