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

Skip to content

Commit 0a79bda

Browse files
committed
Simplify docstring.py.
Unless the docstring is going to be interpolated, we do not need to bother dedenting it. Deprecated unused functionality in docstring.py.
1 parent 3423f85 commit 0a79bda

File tree

4 files changed

+59
-55
lines changed

4 files changed

+59
-55
lines changed

lib/matplotlib/docstring.py

Lines changed: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,53 @@
44
import six
55

66
from matplotlib import cbook
7-
import sys
8-
import types
97

108

119
class Substitution(object):
12-
"""
13-
A decorator to take a function's docstring and perform string
14-
substitution on it.
10+
"""A decorator that performs %-substitution on an object's docstring.
1511
16-
This decorator should be robust even if func.__doc__ is None
17-
(for example, if -OO was passed to the interpreter)
12+
This decorator should be robust even if obj.__doc__ is None (for example,
13+
if -OO was passed to the interpreter)
1814
19-
Usage: construct a docstring.Substitution with a sequence or
20-
dictionary suitable for performing substitution; then
21-
decorate a suitable function with the constructed object. e.g.
15+
Usage: construct a docstring.Substitution with a sequence or dictionary
16+
suitable for performing substitution; then decorate a suitable function
17+
with the constructed object, e.g.::
2218
23-
sub_author_name = Substitution(author='Jason')
19+
sub_author_name = Substitution(author='Jason')
2420
25-
@sub_author_name
26-
def some_function(x):
27-
"%(author)s wrote this function"
21+
@sub_author_name
22+
def some_function(x):
23+
"%(author)s wrote this function"
2824
29-
# note that some_function.__doc__ is now "Jason wrote this function"
25+
# note that some_function.__doc__ is now "Jason wrote this function"
3026
31-
One can also use positional arguments.
27+
One can also use positional arguments::
3228
33-
sub_first_last_names = Substitution('Edgar Allen', 'Poe')
29+
sub_first_last_names = Substitution('Edgar Allen', 'Poe')
3430
35-
@sub_first_last_names
36-
def some_function(x):
37-
"%s %s wrote the Raven"
31+
@sub_first_last_names
32+
def some_function(x):
33+
"%s %s wrote the Raven"
3834
"""
3935
def __init__(self, *args, **kwargs):
40-
assert not (len(args) and len(kwargs)), \
41-
"Only positional or keyword args are allowed"
36+
if args and kwargs:
37+
raise ValueError("Only positional or keyword args are allowed")
4238
self.params = args or kwargs
4339

4440
def __call__(self, func):
45-
func.__doc__ = func.__doc__ and func.__doc__ % self.params
41+
if func.__doc__:
42+
if six.PY2:
43+
getattr(func, "im_func", func).__doc__ %= self.params
44+
else:
45+
func.__doc__ %= self.params
4646
return func
4747

4848
def update(self, *args, **kwargs):
49-
"Assume self.params is a dict and update it with supplied args"
49+
"""Assume self.params is a dict and update it with supplied args."""
5050
self.params.update(*args, **kwargs)
5151

5252
@classmethod
53+
@cbook.deprecated("2.2")
5354
def from_params(cls, params):
5455
"""
5556
In the case where the params is a mutable sequence (list or
@@ -62,6 +63,7 @@ def from_params(cls, params):
6263
return result
6364

6465

66+
@cbook.deprecated("2.2")
6567
class Appender(object):
6668
"""
6769
A function decorator that will append an addendum to the docstring
@@ -86,43 +88,52 @@ def __init__(self, addendum, join=''):
8688
self.join = join
8789

8890
def __call__(self, func):
89-
docitems = [func.__doc__, self.addendum]
90-
func.__doc__ = func.__doc__ and self.join.join(docitems)
91+
if func.__doc__:
92+
func.__doc__ = self.join.join([func.__doc__, self.addendum])
9193
return func
9294

9395

96+
@cbook.deprecated("2.2")
9497
def dedent(func):
95-
"Dedent a docstring (if present)"
96-
func.__doc__ = func.__doc__ and cbook.dedent(func.__doc__)
98+
"""Dedent a docstring (if present)."""
99+
if func.__doc__:
100+
if six.PY2:
101+
getattr(func, "im_func", func).__doc__ = cbook.dedent(func.__doc__)
102+
else:
103+
func.__doc__ = cbook.dedent(func.__doc__)
97104
return func
98105

99106

107+
@cbook.deprecated("2.2")
100108
def copy(source):
101-
"Copy a docstring from another source function (if present)"
102-
def do_copy(target):
109+
"""A decorator that copies the docstring from the source (if present)."""
110+
def decorator(target):
103111
if source.__doc__:
104112
target.__doc__ = source.__doc__
105113
return target
106-
return do_copy
114+
return decorator
107115

108-
# create a decorator that will house the various documentation that
109-
# is reused throughout matplotlib
116+
# Create a decorator that will house the various documentation that is reused
117+
# throughout Matplotlib.
110118
interpd = Substitution()
111119

112120

113121
def dedent_interpd(func):
114-
"""A special case of the interpd that first performs a dedent on
115-
the incoming docstring"""
116-
if isinstance(func, types.MethodType) and not six.PY3:
117-
func = func.im_func
118-
return interpd(dedent(func))
122+
"""Decorator that dedents and interpolates an object's docstring.
123+
"""
124+
if func.__doc__:
125+
if six.PY2:
126+
getattr(func, "im_func", func).__doc__ = cbook.dedent(func.__doc__)
127+
else:
128+
func.__doc__ = cbook.dedent(func.__doc__)
129+
return interpd(func)
119130

120131

132+
@cbook.deprecated("2.2")
121133
def copy_dedent(source):
122-
"""A decorator that will copy the docstring from the source and
123-
then dedent it"""
124-
# note the following is ugly because "Python is not a functional
125-
# language" - GVR. Perhaps one day, functools.compose will exist.
126-
# or perhaps not.
127-
# http://mail.python.org/pipermail/patches/2007-February/021687.html
128-
return lambda target: dedent(copy(source)(target))
134+
"""A decorator that copies the dedented docstring from the source."""
135+
def decorator(func):
136+
if source.__doc__:
137+
dedent(source)
138+
return func
139+
return decorator

lib/matplotlib/lines.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,7 +1498,4 @@ def onpick(self, event):
14981498
fillStyles = MarkerStyle.fillstyles
14991499

15001500
docstring.interpd.update(Line2D=artist.kwdoc(Line2D))
1501-
1502-
# You can not set the docstring of an instancemethod,
1503-
# but you can on the underlying function. Go figure.
15041501
docstring.dedent_interpd(Line2D.__init__)

lib/matplotlib/pyplot.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import six
2020

21+
import functools
2122
import inspect
2223
import re
2324
import sys
@@ -200,7 +201,7 @@ def uninstall_repl_displayhook():
200201
draw_all = _pylab_helpers.Gcf.draw_all
201202

202203

203-
@docstring.copy_dedent(Artist.findobj)
204+
@functools.wraps(Artist.findobj, ["__doc__"])
204205
def findobj(o=None, match=None, include_self=True):
205206
if o is None:
206207
o = gcf()
@@ -296,7 +297,7 @@ def pause(interval):
296297
time.sleep(interval)
297298

298299

299-
@docstring.copy_dedent(matplotlib.rcdefaults)
300+
@functools.wraps(matplotlib.rcdefaults, ["__doc__"])
300301
def rcdefaults():
301302
matplotlib.rcdefaults()
302303
if matplotlib.is_interactive():

lib/mpl_toolkits/axes_grid1/anchored_artists.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
unicode_literals)
33
import six
44

5-
from matplotlib import docstring
65
from matplotlib.offsetbox import (AnchoredOffsetbox, AuxTransformBox,
76
DrawingArea, TextArea, VPacker)
87
from matplotlib.patches import Rectangle, Ellipse
@@ -13,7 +12,6 @@
1312

1413

1514
class AnchoredDrawingArea(AnchoredOffsetbox):
16-
@docstring.dedent
1715
def __init__(self, width, height, xdescent, ydescent,
1816
loc, pad=0.4, borderpad=0.5, prop=None, frameon=True,
1917
**kwargs):
@@ -89,7 +87,6 @@ def __init__(self, width, height, xdescent, ydescent,
8987

9088

9189
class AnchoredAuxTransformBox(AnchoredOffsetbox):
92-
@docstring.dedent
9390
def __init__(self, transform, loc,
9491
pad=0.4, borderpad=0.5, prop=None, frameon=True, **kwargs):
9592
"""
@@ -162,7 +159,6 @@ def __init__(self, transform, loc,
162159

163160

164161
class AnchoredEllipse(AnchoredOffsetbox):
165-
@docstring.dedent
166162
def __init__(self, transform, width, height, angle, loc,
167163
pad=0.1, borderpad=0.1, prop=None, frameon=True, **kwargs):
168164
"""
@@ -228,7 +224,6 @@ def __init__(self, transform, width, height, angle, loc,
228224

229225

230226
class AnchoredSizeBar(AnchoredOffsetbox):
231-
@docstring.dedent
232227
def __init__(self, transform, size, label, loc,
233228
pad=0.1, borderpad=0.1, sep=2,
234229
frameon=True, size_vertical=0, color='black',

0 commit comments

Comments
 (0)