Skip to content

Commit

Permalink
fix: update numpy docstring parser
Browse files Browse the repository at this point in the history
  • Loading branch information
jnoortheen committed Nov 11, 2020
1 parent 883e43f commit b479611
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
8 changes: 3 additions & 5 deletions arger/docstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ class NumpyDocParser(DocstringParser):
def __init__(self):
self.pattern = re.compile(r'(Parameters\n[-]+)')
self.section_ptrn = re.compile(r'\n\s*(?P<section>\w+)\n\s*[-]+\n+')
self.param_ptrn = re.compile(
r'^(?P<param>\w+)[ \t]*:[ \t]*(?P<type>\w+)?'
) # matches parameter_name e.g. param1: or param2 (int):
self.param_ptrn = re.compile(r'^(?P<param>\w+)[ \t]*:?[ \t]*(?P<type>\w+)?')

def get_rest_of_section(self, params: str) -> tp.Tuple[str, str]:
other_sect = self.section_ptrn.search(params)
Expand Down Expand Up @@ -138,15 +136,15 @@ def parse(self, doc: str) -> DocstringTp:


@functools.lru_cache(None)
def get_parsers():
def _docstring_parsers():
"""Cache costly init phase per session."""
return [NumpyDocParser(), GoogleDocParser(), RstDocParser()]


def parse_docstring(func: tp.Optional[tp.Callable]) -> DocstringTp:
doc = (inspect.getdoc(func) or '') if func else ''
if doc:
for parser in get_parsers():
for parser in _docstring_parsers():
if parser.matches(doc):
return parser.parse(doc)
return DocstringTp(description=doc, epilog='', params={})
10 changes: 9 additions & 1 deletion tests/test_docstrings_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def func_numpy():
a line that extends to next line
arg3 :
arg3 without any type
arg4
arg4 help content
Returns
-------
Expand All @@ -35,6 +37,7 @@ def func_google():
arg2 (str): Description of arg2
a line that extends to next line
arg3 : arg3 without any type
arg4: arg4 help content
Returns:
bool: Description of return value
Expand All @@ -51,6 +54,7 @@ def func_rst():
:param str arg2: Description of arg2
a line that extends to next line
:param arg3 : arg3 without any type
:param arg4: arg4 help content
:return: Description of return value
:rtype: bool
"""
Expand All @@ -67,7 +71,7 @@ def func_rst():
def test_docstring_parser(fn):
result = parse_docstring(fn)
assert result.description == "Summary line.\n\nExtended description of function."
assert list(result.params) == ['arg1', 'arg2', 'arg3']
assert list(result.params) == ['arg1', 'arg2', 'arg3', 'arg4']
assert list(result.params.values()) == [
ParamDocTp.init(
type_hint='int',
Expand All @@ -81,5 +85,9 @@ def test_docstring_parser(fn):
type_hint=None,
doc='arg3 without any type',
),
ParamDocTp.init(
type_hint=None,
doc='arg4 help content',
),
]
assert 'Description of return value' in result.epilog

0 comments on commit b479611

Please sign in to comment.