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

Skip to content

Commit 8dbb2cc

Browse files
committed
ENH: Append docs in @unpack_labeled_data
1 parent 1f36892 commit 8dbb2cc

File tree

2 files changed

+78
-3
lines changed

2 files changed

+78
-3
lines changed

lib/matplotlib/__init__.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
import functools
120120
# cbook must import matplotlib only within function
121121
# definitions, so it is safe to import from it here.
122-
from matplotlib.cbook import is_string_like, mplDeprecation
122+
from matplotlib.cbook import is_string_like, mplDeprecation, dedent
123123
from matplotlib.compat import subprocess
124124
from matplotlib.rcsetup import (defaultParams,
125125
validate_backend,
@@ -1534,6 +1534,19 @@ def _replacer(data, key):
15341534
return key
15351535

15361536

1537+
_DATA_DOC_APPENDIX = """
1538+
1539+
Notes
1540+
-----
1541+
1542+
In addition to the above described arguments, this function can take a
1543+
**data** keyword argument. If such a **data** argument is given, the
1544+
following arguments are replaced by **data[<arg>]**:
1545+
1546+
{replaced}
1547+
"""
1548+
1549+
15371550
def unpack_labeled_data(replace_names=None, replace_all_args=False,
15381551
label_namer=None, positional_parameter_names=None):
15391552
"""
@@ -1708,10 +1721,25 @@ def inner(ax, *args, **kwargs):
17081721
RuntimeWarning, stacklevel=2)
17091722
# raise Exception()
17101723
return func(ax, *args, **kwargs)
1724+
pre_doc = inner.__doc__
1725+
if pre_doc is None:
1726+
pre_doc = ''
1727+
else:
1728+
pre_doc = dedent(pre_doc)
1729+
_repl = ""
1730+
if replace_names is None:
1731+
_repl = "* All positional and all keyword arguments."
1732+
else:
1733+
if len(replace_names) != 0:
1734+
_repl = "* All arguments with the following names: '{names}'."
1735+
if replace_all_args:
1736+
_repl += "\n* All positional arguments."
1737+
_repl = _repl.format(names="', '".join(replace_names))
1738+
inner.__doc__ = (pre_doc +
1739+
_DATA_DOC_APPENDIX.format(replaced=_repl))
17111740
return inner
17121741
return param
17131742

1714-
17151743
verbose.report('matplotlib version %s' % __version__)
17161744
verbose.report('verbose.level %s' % verbose.level)
17171745
verbose.report('interactive is %s' % is_interactive())

lib/matplotlib/tests/test_labled_data_unpacking.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from __future__ import (absolute_import, division, print_function,
22
unicode_literals)
33

4-
from nose.tools import assert_raises, assert_equal
4+
from nose.tools import (assert_raises, assert_equal, assert_regexp_matches,
5+
assert_not_regexp_matches)
56
from nose.plugins.skip import SkipTest
67

78
from .. import unpack_labeled_data
@@ -327,3 +328,49 @@ def funcy(ax, *args, **kwargs):
327328
"x: [1, 2], y: [8, 9], ls: x, w: xyz, label: ")
328329
assert_equal(func2(None, "a", "b", w="x", label="text", data=data),
329330
"x: [1, 2], y: [8, 9], ls: x, w: xyz, label: text")
331+
332+
333+
def test_docstring_addition():
334+
@unpack_labeled_data()
335+
def funcy(ax, *args, **kwargs):
336+
"""Funcy does nothing"""
337+
pass
338+
339+
assert_regexp_matches(funcy.__doc__,
340+
r".*All positional and all keyword arguments\.")
341+
assert_not_regexp_matches(funcy.__doc__, r".*All positional arguments\.")
342+
assert_not_regexp_matches(funcy.__doc__,
343+
r".*All arguments with the following names: .*")
344+
345+
@unpack_labeled_data(replace_all_args=True, replace_names=[])
346+
def funcy(ax, x, y, z, bar=None):
347+
"""Funcy does nothing"""
348+
pass
349+
350+
assert_regexp_matches(funcy.__doc__, r".*All positional arguments\.")
351+
assert_not_regexp_matches(funcy.__doc__,
352+
r".*All positional and all keyword arguments\.")
353+
assert_not_regexp_matches(funcy.__doc__,
354+
r".*All arguments with the following names: .*")
355+
356+
@unpack_labeled_data(replace_all_args=True, replace_names=["bar"])
357+
def funcy(ax, x, y, z, bar=None):
358+
"""Funcy does nothing"""
359+
pass
360+
361+
assert_regexp_matches(funcy.__doc__, r".*All positional arguments\.")
362+
assert_regexp_matches(funcy.__doc__,
363+
r".*All arguments with the following names: 'bar'\.")
364+
assert_not_regexp_matches(funcy.__doc__,
365+
r".*All positional and all keyword arguments\.")
366+
367+
@unpack_labeled_data(replace_names=["x", "bar"])
368+
def funcy(ax, x, y, z, bar=None):
369+
"""Funcy does nothing"""
370+
pass
371+
372+
assert_regexp_matches(funcy.__doc__,
373+
r".*All arguments with the following names: 'x', 'bar'\.")
374+
assert_not_regexp_matches(funcy.__doc__,
375+
r".*All positional and all keyword arguments\.")
376+
assert_not_regexp_matches(funcy.__doc__, r".*All positional arguments\.")

0 commit comments

Comments
 (0)