@@ -44,14 +44,23 @@ class _axis_method_wrapper:
44
44
45
45
The docstring of ``get_foo`` is built by replacing "this Axis" by "the
46
46
{attr_name}" (i.e., "the xaxis", "the yaxis") in the wrapped method's
47
- docstring; additional replacements can by given in *doc_sub*. The
48
- docstring is also dedented to simplify further manipulations.
47
+ dedented docstring; additional replacements can by given in *doc_sub*.
49
48
"""
50
49
51
50
def __init__ (self , attr_name , method_name , * , doc_sub = None ):
52
51
self .attr_name = attr_name
53
52
self .method_name = method_name
54
- self .doc_sub = doc_sub
53
+ # Immediately put the docstring in ``self.__doc__`` so that docstring
54
+ # manipulations within the class body work as expected.
55
+ doc = inspect .getdoc (getattr (maxis .Axis , method_name ))
56
+ self ._missing_subs = []
57
+ if doc :
58
+ doc_sub = {"this Axis" : f"the { self .attr_name } " , ** (doc_sub or {})}
59
+ for k , v in doc_sub .items ():
60
+ if k not in doc : # Delay raising error until we know qualname.
61
+ self ._missing_subs .append (k )
62
+ doc = doc .replace (k , v )
63
+ self .__doc__ = doc
55
64
56
65
def __set_name__ (self , owner , name ):
57
66
# This is called at the end of the class body as
@@ -65,22 +74,19 @@ def wrapper(self, *args, **kwargs):
65
74
wrapper .__module__ = owner .__module__
66
75
wrapper .__name__ = name
67
76
wrapper .__qualname__ = f"{ owner .__qualname__ } .{ name } "
77
+ wrapper .__doc__ = self .__doc__
68
78
# Manually copy the signature instead of using functools.wraps because
69
79
# displaying the Axis method source when asking for the Axes method
70
80
# source would be confusing.
71
- wrapped_method = getattr (maxis .Axis , self .method_name )
72
- wrapper .__signature__ = inspect .signature (wrapped_method )
73
- doc = wrapped_method .__doc__
74
- if doc :
75
- doc_sub = {"this Axis" : f"the { self .attr_name } " ,
76
- ** (self .doc_sub or {})}
77
- for k , v in doc_sub .items ():
78
- assert k in doc , \
79
- (f"The definition of { wrapper .__qualname__ } expected that "
80
- f"the docstring of Axis.{ self .method_name } contains "
81
- f"{ k !r} as a substring." )
82
- doc = doc .replace (k , v )
83
- wrapper .__doc__ = inspect .cleandoc (doc )
81
+ wrapper .__signature__ = inspect .signature (
82
+ getattr (maxis .Axis , self .method_name ))
83
+
84
+ if self ._missing_subs :
85
+ raise ValueError (
86
+ "The definition of {} expected that the docstring of Axis.{} "
87
+ "contains {!r} as substrings" .format (
88
+ wrapper .__qualname__ , self .method_name ,
89
+ ", " .join (map (repr , self ._missing_subs ))))
84
90
85
91
setattr (owner , name , wrapper )
86
92
0 commit comments