From b479611ca0d3ae62cb359b28a4cf40af04debe52 Mon Sep 17 00:00:00 2001 From: Noortheen Raja Date: Wed, 11 Nov 2020 17:14:07 +0530 Subject: [PATCH] fix: update numpy docstring parser --- arger/docstring.py | 8 +++----- tests/test_docstrings_parser.py | 10 +++++++++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/arger/docstring.py b/arger/docstring.py index eb4670a..8f9ff33 100644 --- a/arger/docstring.py +++ b/arger/docstring.py @@ -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
\w+)\n\s*[-]+\n+') - self.param_ptrn = re.compile( - r'^(?P\w+)[ \t]*:[ \t]*(?P\w+)?' - ) # matches parameter_name e.g. param1: or param2 (int): + self.param_ptrn = re.compile(r'^(?P\w+)[ \t]*:?[ \t]*(?P\w+)?') def get_rest_of_section(self, params: str) -> tp.Tuple[str, str]: other_sect = self.section_ptrn.search(params) @@ -138,7 +136,7 @@ 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()] @@ -146,7 +144,7 @@ def get_parsers(): 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={}) diff --git a/tests/test_docstrings_parser.py b/tests/test_docstrings_parser.py index bbd0b02..232cc89 100644 --- a/tests/test_docstrings_parser.py +++ b/tests/test_docstrings_parser.py @@ -17,6 +17,8 @@ def func_numpy(): a line that extends to next line arg3 : arg3 without any type + arg4 + arg4 help content Returns ------- @@ -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 @@ -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 """ @@ -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', @@ -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