8
8
import re
9
9
import pydoc
10
10
from warnings import warn
11
- import collections
11
+ from collections import namedtuple
12
+ try :
13
+ from collections .abc import Callable , Mapping
14
+ except ImportError :
15
+ from collections import Callable , Mapping
12
16
import copy
13
17
import sys
14
18
@@ -106,7 +110,10 @@ def __str__(self):
106
110
return message
107
111
108
112
109
- class NumpyDocString (collections .Mapping ):
113
+ Parameter = namedtuple ('Parameter' , ['name' , 'type' , 'desc' ])
114
+
115
+
116
+ class NumpyDocString (Mapping ):
110
117
"""Parses a numpydoc string to an abstract representation
111
118
112
119
Instances define a mapping from section title to structured data.
@@ -225,7 +232,7 @@ def _parse_param_list(self, content):
225
232
desc = dedent_lines (desc )
226
233
desc = strip_blank_lines (desc )
227
234
228
- params .append ((arg_name , arg_type , desc ))
235
+ params .append (Parameter (arg_name , arg_type , desc ))
229
236
230
237
return params
231
238
@@ -317,7 +324,8 @@ def _parse_summary(self):
317
324
while True :
318
325
summary = self ._doc .read_to_next_empty_line ()
319
326
summary_str = " " .join ([s .strip () for s in summary ]).strip ()
320
- if re .compile ('^([\w., ]+=)?\s*[\w\.]+\(.*\)$' ).match (summary_str ):
327
+ compiled = re .compile (r'^([\w., ]+=)?\s*[\w\.]+\(.*\)$' )
328
+ if compiled .match (summary_str ):
321
329
self ['Signature' ] = summary_str
322
330
if not self ._is_at_section ():
323
331
continue
@@ -389,7 +397,7 @@ def _str_indent(self, doc, indent=4):
389
397
390
398
def _str_signature (self ):
391
399
if self ['Signature' ]:
392
- return [self ['Signature' ].replace ('*' , '\*' )] + ['' ]
400
+ return [self ['Signature' ].replace ('*' , r '\*' )] + ['' ]
393
401
else :
394
402
return ['' ]
395
403
@@ -409,13 +417,13 @@ def _str_param_list(self, name):
409
417
out = []
410
418
if self [name ]:
411
419
out += self ._str_header (name )
412
- for param , param_type , desc in self [name ]:
413
- if param_type :
414
- out += ['%s : %s' % (param , param_type )]
420
+ for param in self [name ]:
421
+ if param . type :
422
+ out += ['%s : %s' % (param . name , param . type )]
415
423
else :
416
- out += [param ]
417
- if desc and '' .join (desc ).strip ():
418
- out += self ._str_indent (desc )
424
+ out += [param . name ]
425
+ if param . desc and '' .join (param . desc ).strip ():
426
+ out += self ._str_indent (param . desc )
419
427
out += ['' ]
420
428
return out
421
429
@@ -521,7 +529,7 @@ def __init__(self, func, role='func', doc=None, config={}):
521
529
else :
522
530
argspec = inspect .getargspec (func )
523
531
signature = inspect .formatargspec (* argspec )
524
- signature = '%s%s' % (func_name , signature .replace ('*' , '\*' ))
532
+ signature = '%s%s' % (func_name , signature .replace ('*' , r '\*' ))
525
533
except TypeError :
526
534
signature = '%s()' % func_name
527
535
self ['Signature' ] = signature
@@ -538,7 +546,7 @@ def __str__(self):
538
546
out = ''
539
547
540
548
func , func_name = self .get_func ()
541
- signature = self ['Signature' ].replace ('*' , '\*' )
549
+ signature = self ['Signature' ].replace ('*' , r '\*' )
542
550
543
551
roles = {'func' : 'function' ,
544
552
'meth' : 'method' }
@@ -591,7 +599,8 @@ def splitlines_x(s):
591
599
for name in sorted (items ):
592
600
try :
593
601
doc_item = pydoc .getdoc (getattr (self ._cls , name ))
594
- doc_list .append ((name , '' , splitlines_x (doc_item )))
602
+ doc_list .append (
603
+ Parameter (name , '' , splitlines_x (doc_item )))
595
604
except AttributeError :
596
605
pass # method doesn't exist
597
606
self [field ] = doc_list
@@ -603,7 +612,7 @@ def methods(self):
603
612
return [name for name , func in inspect .getmembers (self ._cls )
604
613
if ((not name .startswith ('_' )
605
614
or name in self .extra_public_methods )
606
- and isinstance (func , collections . Callable )
615
+ and isinstance (func , Callable )
607
616
and self ._is_show_member (name ))]
608
617
609
618
@property
0 commit comments