88import re
99import pydoc
1010from 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
1216import copy
1317import sys
1418
@@ -106,7 +110,10 @@ def __str__(self):
106110 return message
107111
108112
109- class NumpyDocString (collections .Mapping ):
113+ Parameter = namedtuple ('Parameter' , ['name' , 'type' , 'desc' ])
114+
115+
116+ class NumpyDocString (Mapping ):
110117 """Parses a numpydoc string to an abstract representation
111118
112119 Instances define a mapping from section title to structured data.
@@ -225,7 +232,7 @@ def _parse_param_list(self, content):
225232 desc = dedent_lines (desc )
226233 desc = strip_blank_lines (desc )
227234
228- params .append ((arg_name , arg_type , desc ))
235+ params .append (Parameter (arg_name , arg_type , desc ))
229236
230237 return params
231238
@@ -317,7 +324,8 @@ def _parse_summary(self):
317324 while True :
318325 summary = self ._doc .read_to_next_empty_line ()
319326 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 ):
321329 self ['Signature' ] = summary_str
322330 if not self ._is_at_section ():
323331 continue
@@ -389,7 +397,7 @@ def _str_indent(self, doc, indent=4):
389397
390398 def _str_signature (self ):
391399 if self ['Signature' ]:
392- return [self ['Signature' ].replace ('*' , '\*' )] + ['' ]
400+ return [self ['Signature' ].replace ('*' , r '\*' )] + ['' ]
393401 else :
394402 return ['' ]
395403
@@ -409,13 +417,13 @@ def _str_param_list(self, name):
409417 out = []
410418 if self [name ]:
411419 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 )]
415423 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 )
419427 out += ['' ]
420428 return out
421429
@@ -521,7 +529,7 @@ def __init__(self, func, role='func', doc=None, config={}):
521529 else :
522530 argspec = inspect .getargspec (func )
523531 signature = inspect .formatargspec (* argspec )
524- signature = '%s%s' % (func_name , signature .replace ('*' , '\*' ))
532+ signature = '%s%s' % (func_name , signature .replace ('*' , r '\*' ))
525533 except TypeError :
526534 signature = '%s()' % func_name
527535 self ['Signature' ] = signature
@@ -538,7 +546,7 @@ def __str__(self):
538546 out = ''
539547
540548 func , func_name = self .get_func ()
541- signature = self ['Signature' ].replace ('*' , '\*' )
549+ signature = self ['Signature' ].replace ('*' , r '\*' )
542550
543551 roles = {'func' : 'function' ,
544552 'meth' : 'method' }
@@ -591,7 +599,8 @@ def splitlines_x(s):
591599 for name in sorted (items ):
592600 try :
593601 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 )))
595604 except AttributeError :
596605 pass # method doesn't exist
597606 self [field ] = doc_list
@@ -603,7 +612,7 @@ def methods(self):
603612 return [name for name , func in inspect .getmembers (self ._cls )
604613 if ((not name .startswith ('_' )
605614 or name in self .extra_public_methods )
606- and isinstance (func , collections . Callable )
615+ and isinstance (func , Callable )
607616 and self ._is_show_member (name ))]
608617
609618 @property
0 commit comments