1
1
"""Implementation of __array_function__ overrides from NEP-18."""
2
2
import collections
3
3
import functools
4
+ import ast
4
5
import os
5
6
import textwrap
6
7
@@ -86,6 +87,23 @@ def set_array_function_like_doc(public_api):
86
87
ArgSpec = collections .namedtuple ('ArgSpec' , 'args varargs keywords defaults' )
87
88
88
89
90
+ def extract_text_signature (obj , docstring ):
91
+ head = docstring .lstrip ().split ('\n ' )[0 ]
92
+ objname = obj .__name__
93
+ if not head .startswith (objname ):
94
+ return None
95
+ return head
96
+
97
+
98
+ def add_docstring_with_textsig (implementation , doc ):
99
+ if doc is None :
100
+ return
101
+ textsig = extract_text_signature (implementation , doc )
102
+ if textsig is not None :
103
+ doc = '{}\n --\n \n {}' .format (textsig , doc )
104
+ add_docstring (implementation , doc )
105
+
106
+
89
107
def verify_matching_signatures (implementation , dispatcher ):
90
108
"""Verify that a dispatcher function has the right signature."""
91
109
implementation_spec = ArgSpec (* getargspec (implementation ))
@@ -162,7 +180,7 @@ def array_function_dispatch(dispatcher, module=None, verify=True,
162
180
if not ARRAY_FUNCTION_ENABLED :
163
181
def decorator (implementation ):
164
182
if docs_from_dispatcher :
165
- add_docstring (implementation , dispatcher .__doc__ )
183
+ add_docstring_with_textsig (implementation , dispatcher .__doc__ )
166
184
if module is not None :
167
185
implementation .__module__ = module
168
186
return implementation
@@ -173,7 +191,7 @@ def decorator(implementation):
173
191
verify_matching_signatures (implementation , dispatcher )
174
192
175
193
if docs_from_dispatcher :
176
- add_docstring (implementation , dispatcher .__doc__ )
194
+ add_docstring_with_textsig (implementation , dispatcher .__doc__ )
177
195
178
196
@functools .wraps (implementation )
179
197
def public_api (* args , ** kwargs ):
0 commit comments