21
21
contributors may be used to endorse or promote products derived
22
22
from this software without specific prior written permission. """
23
23
24
+ import textwrap , re , copy
25
+ from warnings import warn
26
+ from collections import namedtuple
27
+ from collections .abc import Mapping
28
+
24
29
__all__ = ['Parameter' , 'NumpyDocString' , 'dedent_lines' ]
25
30
26
31
Parameter = namedtuple ('Parameter' , ['name' , 'type' , 'desc' ])
@@ -89,14 +94,17 @@ def __str__(self):
89
94
90
95
class NumpyDocString (Mapping ):
91
96
"""Parses a numpydoc string to an abstract representation """
92
- sections = { 'Signature' : '' , ' Summary' : ['' ], 'Extended' : [], 'Parameters' : [], 'Returns' : [], 'Yields' : [], 'Raises ' : [] }
97
+ sections = { 'Summary' : ['' ], 'Extended' : [], 'Parameters' : [], 'Returns' : [] }
93
98
94
99
def __init__ (self , docstring , config = None ):
95
100
docstring = textwrap .dedent (docstring ).split ('\n ' )
96
101
self ._doc = Reader (docstring )
97
102
self ._parsed_data = copy .deepcopy (self .sections )
98
103
self ._parse ()
99
- if 'Parameters' in self : self ['Parameters' ] = {o .name :o for o in self ['Parameters' ]}
104
+ self ['Parameters' ] = {o .name :o for o in self ['Parameters' ]}
105
+ if self ['Returns' ]: self ['Returns' ] = self ['Returns' ][0 ]
106
+ self ['Summary' ] = dedent_lines (self ['Summary' ], split = False )
107
+ self ['Extended' ] = dedent_lines (self ['Extended' ], split = False )
100
108
101
109
def __iter__ (self ): return iter (self ._parsed_data )
102
110
def __len__ (self ): return len (self ._parsed_data )
@@ -171,7 +179,6 @@ def _parse_summary(self):
171
179
summary_str = " " .join ([s .strip () for s in summary ]).strip ()
172
180
compiled = re .compile (r'^([\w., ]+=)?\s*[\w\.]+\(.*\)$' )
173
181
if compiled .match (summary_str ):
174
- self ['Signature' ] = summary_str
175
182
if not self ._is_at_section (): continue
176
183
break
177
184
@@ -216,16 +223,12 @@ def _obj(self):
216
223
217
224
def _error_location (self , msg , error = True ):
218
225
if self ._obj is not None :
219
- # we know where the docs came from:
220
- try : filename = inspect .getsourcefile (self ._obj )
221
- except TypeError : filename = None
222
226
# Make UserWarning more descriptive via object introspection.
223
227
# Skip if introspection fails
224
228
name = getattr (self ._obj , '__name__' , None )
225
229
if name is None :
226
230
name = getattr (getattr (self ._obj , '__class__' , None ), '__name__' , None )
227
231
if name is not None : msg += f" in the docstring of { name } "
228
- msg += f" in { filename } ." if filename else ""
229
232
if error : raise ValueError (msg )
230
233
else : warn (msg )
231
234
@@ -234,10 +237,6 @@ def _error_location(self, msg, error=True):
234
237
def _str_header (self , name , symbol = '-' ): return [name , len (name )* symbol ]
235
238
def _str_indent (self , doc , indent = 4 ): return [' ' * indent + line for line in doc ]
236
239
237
- def _str_signature (self ):
238
- if self ['Signature' ]: return [self ['Signature' ].replace ('*' , r'\*' )] + ['' ]
239
- return ['' ]
240
-
241
240
def _str_summary (self ):
242
241
if self ['Summary' ]: return self ['Summary' ] + ['' ]
243
242
return []
@@ -259,18 +258,10 @@ def _str_param_list(self, name):
259
258
out += ['' ]
260
259
return out
261
260
262
- def __str__ (self , func_role = '' ):
263
- out = []
264
- out += self ._str_signature ()
265
- out += self ._str_summary ()
266
- out += self ._str_extended_summary ()
267
- for param_list in ('Parameters' , 'Returns' , 'Yields' , 'Receives' , 'Other Parameters' , 'Raises' , 'Warns' ):
268
- out += self ._str_param_list (param_list )
269
- for param_list in ('Attributes' , 'Methods' ): out += self ._str_param_list (param_list )
270
- return '\n ' .join (out )
271
-
272
261
273
- def dedent_lines (lines ):
262
+ def dedent_lines (lines , split = True ):
274
263
"""Deindent a list of lines maximally"""
275
- return textwrap .dedent ("\n " .join (lines )).split ("\n " )
264
+ res = textwrap .dedent ("\n " .join (lines ))
265
+ if split : res = res .split ("\n " )
266
+ return res
276
267
0 commit comments