@@ -44,14 +44,23 @@ class _axis_method_wrapper:
4444
4545 The docstring of ``get_foo`` is built by replacing "this Axis" by "the
4646 {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*.
4948 """
5049
5150 def __init__ (self , attr_name , method_name , * , doc_sub = None ):
5251 self .attr_name = attr_name
5352 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
5564
5665 def __set_name__ (self , owner , name ):
5766 # This is called at the end of the class body as
@@ -65,22 +74,19 @@ def wrapper(self, *args, **kwargs):
6574 wrapper .__module__ = owner .__module__
6675 wrapper .__name__ = name
6776 wrapper .__qualname__ = f"{ owner .__qualname__ } .{ name } "
77+ wrapper .__doc__ = self .__doc__
6878 # Manually copy the signature instead of using functools.wraps because
6979 # displaying the Axis method source when asking for the Axes method
7080 # 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 ))))
8490
8591 setattr (owner , name , wrapper )
8692
0 commit comments