-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: update docstring parser and add tests
- Loading branch information
1 parent
98b0996
commit 3d9d681
Showing
2 changed files
with
109 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import inspect | ||
|
||
import pytest | ||
|
||
from arger.parser.docstring import parse_docstring, ParamDocTp | ||
|
||
|
||
def func_numpy(): | ||
"""Summary line. | ||
Extended description of function. | ||
Parameters | ||
---------- | ||
arg1 : int | ||
Description of arg1 | ||
arg2 : str | ||
Description of arg2 | ||
a line that extends to next line | ||
arg3 : | ||
arg3 without any type | ||
Returns | ||
------- | ||
bool | ||
Description of return value | ||
""" | ||
|
||
|
||
def func_google(): | ||
"""Summary line. | ||
Extended description of function. | ||
Args: | ||
arg1 (int): Description of arg1 | ||
arg2 (str): Description of arg2 | ||
a line that extends to next line | ||
arg3 : arg3 without any type | ||
Returns: | ||
bool: Description of return value | ||
""" | ||
|
||
|
||
# https://sphinx-rtd-tutorial.readthedocs.io/en/latest/docstrings.html | ||
def func_rst(): | ||
"""Summary line. | ||
Extended description of function. | ||
:param int arg1: Description of arg1 | ||
:param str arg2: Description of arg2 | ||
a line that extends to next line | ||
:param arg3 : arg3 without any type | ||
:return: Description of return value | ||
:rtype: bool | ||
""" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'fn', | ||
[ | ||
func_numpy, | ||
func_google, | ||
func_rst, | ||
], | ||
) | ||
def test_docstring_parser(fn): | ||
result = parse_docstring(inspect.getdoc(fn)) | ||
assert result.description == "Summary line.\n\nExtended description of function." | ||
assert list(result.params) == ['arg1', 'arg2', 'arg3'] | ||
assert list(result.params.values()) == [ | ||
ParamDocTp( | ||
type_hint='int', | ||
flags=[], | ||
doc='Description of arg1', | ||
), | ||
ParamDocTp( | ||
type_hint='str', | ||
flags=[], | ||
doc='Description of arg2 a line that extends to next line', | ||
), | ||
ParamDocTp( | ||
type_hint=None, | ||
flags=[], | ||
doc='arg3 without any type', | ||
), | ||
] | ||
assert 'Description of return value' in result.epilog |