From 476d57134f953670b2a7c429411847c354394875 Mon Sep 17 00:00:00 2001 From: Michael Chow Date: Mon, 26 Jun 2023 18:19:34 -0400 Subject: [PATCH 1/5] fix: numpy returns handles more name, type cases --- src/griffe/docstrings/numpy.py | 4 +++- tests/test_docstrings/test_numpy.py | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/griffe/docstrings/numpy.py b/src/griffe/docstrings/numpy.py index cff0e863..63054180 100644 --- a/src/griffe/docstrings/numpy.py +++ b/src/griffe/docstrings/numpy.py @@ -217,7 +217,9 @@ def _read_block(docstring: Docstring, *, offset: int) -> tuple[str, int]: | # or (?P{_RE_NAME})\s*:\s* # just name | # or - (?P{_RE_TYPE})\s* # just type + \s*:$ # no name, no type + | + :?\s*(?P{_RE_TYPE})\s* # just type ) """, re.IGNORECASE | re.VERBOSE, diff --git a/tests/test_docstrings/test_numpy.py b/tests/test_docstrings/test_numpy.py index 496a6259..d2923d81 100644 --- a/tests/test_docstrings/test_numpy.py +++ b/tests/test_docstrings/test_numpy.py @@ -354,6 +354,12 @@ def test_returns_section(parse_numpy: ParserType) -> None: flag : bool Some kind of flag. + x : + Name only + : + No name or annotation + : int + Only annotation """ sections, _ = parse_numpy(docstring) @@ -367,6 +373,21 @@ def test_returns_section(parse_numpy: ParserType) -> None: DocstringReturn(name="", annotation="bool", description="Some kind\nof flag."), ) + assert_element_equal( + sections[0].value[2], + DocstringReturn(name="", annotation=None, description="Name only"), + ) + + assert_element_equal( + sections[0].value[3], + DocstringReturn(name="", annotation=None, description="No name or annotation"), + ) + + assert_element_equal( + sections[0].value[4], + DocstringReturn(name="", annotation="int", description="Only annotation"), + ) + def test_yields_section(parse_numpy: ParserType) -> None: """Parse yields section. From ebf40567a9f3a8918322a7c7e4d624c3a49733f1 Mon Sep 17 00:00:00 2001 From: Michael Chow Date: Tue, 27 Jun 2023 11:01:56 -0400 Subject: [PATCH 2/5] refactor: tweak numpy returns regex --- src/griffe/docstrings/numpy.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/griffe/docstrings/numpy.py b/src/griffe/docstrings/numpy.py index 63054180..2cc565ad 100644 --- a/src/griffe/docstrings/numpy.py +++ b/src/griffe/docstrings/numpy.py @@ -209,7 +209,7 @@ def _read_block(docstring: Docstring, *, offset: int) -> tuple[str, int]: _RE_OB: str = r"\{" # opening bracket _RE_CB: str = r"\}" # closing bracket _RE_NAME: str = r"\*{0,2}[_a-z][_a-z0-9]*" -_RE_TYPE: str = r".+" +_RE_TYPE: str = r"[^:\s].*" _RE_RETURNS: Pattern = re.compile( rf""" (?: @@ -217,9 +217,9 @@ def _read_block(docstring: Docstring, *, offset: int) -> tuple[str, int]: | # or (?P{_RE_NAME})\s*:\s* # just name | # or - \s*:$ # no name, no type - | - :?\s*(?P{_RE_TYPE})\s* # just type + (?::\s*)?(?P{_RE_TYPE})\s* # just type + | # or + \s*:\s* # no name, no type ) """, re.IGNORECASE | re.VERBOSE, From 719ac31f5dcc7a64d02e4e9d93ac7a392c0bfaa6 Mon Sep 17 00:00:00 2001 From: Michael Chow Date: Wed, 28 Jun 2023 11:36:01 -0400 Subject: [PATCH 3/5] tests: numpy docstring tests more element attributes --- tests/test_docstrings/helpers.py | 10 ++++++++-- tests/test_docstrings/test_numpy.py | 8 ++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/tests/test_docstrings/helpers.py b/tests/test_docstrings/helpers.py index b55593c4..84bcb042 100644 --- a/tests/test_docstrings/helpers.py +++ b/tests/test_docstrings/helpers.py @@ -98,5 +98,11 @@ def assert_element_equal(actual: DocstringElement, expected: DocstringElement) - actual: The actual element. expected: The expected element. """ - assert actual.annotation == expected.annotation # type: ignore[operator] - assert actual.description == expected.description + + assert isinstance(actual, type(expected)) + + for k in actual.as_dict(): + src = getattr(actual, k) + dst = getattr(expected, k) + + assert src == dst, f"attribute {repr(k)}, {repr(src)} != {repr(dst)}" diff --git a/tests/test_docstrings/test_numpy.py b/tests/test_docstrings/test_numpy.py index d2923d81..f9a0f427 100644 --- a/tests/test_docstrings/test_numpy.py +++ b/tests/test_docstrings/test_numpy.py @@ -370,12 +370,12 @@ def test_returns_section(parse_numpy: ParserType) -> None: ) assert_element_equal( sections[0].value[1], - DocstringReturn(name="", annotation="bool", description="Some kind\nof flag."), + DocstringReturn(name="flag", annotation="bool", description="Some kind\nof flag."), ) assert_element_equal( sections[0].value[2], - DocstringReturn(name="", annotation=None, description="Name only"), + DocstringReturn(name="x", annotation=None, description="Name only"), ) assert_element_equal( @@ -413,7 +413,7 @@ def test_yields_section(parse_numpy: ParserType) -> None: ) assert_element_equal( sections[0].value[1], - DocstringYield(name="", annotation="bool", description="Some kind\nof flag."), + DocstringYield(name="flag", annotation="bool", description="Some kind\nof flag."), ) @@ -441,7 +441,7 @@ def test_receives_section(parse_numpy: ParserType) -> None: ) assert_element_equal( sections[0].value[1], - DocstringReceive(name="", annotation="bool", description="Some kind\nof flag."), + DocstringReceive(name="flag", annotation="bool", description="Some kind\nof flag."), ) From 861d01980fcb667a13dffb402dcc990f29c28503 Mon Sep 17 00:00:00 2001 From: Michael Chow Date: Wed, 28 Jun 2023 11:45:35 -0400 Subject: [PATCH 4/5] chore: fix linter errors --- tests/test_docstrings/helpers.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_docstrings/helpers.py b/tests/test_docstrings/helpers.py index 84bcb042..3c784b97 100644 --- a/tests/test_docstrings/helpers.py +++ b/tests/test_docstrings/helpers.py @@ -98,11 +98,10 @@ def assert_element_equal(actual: DocstringElement, expected: DocstringElement) - actual: The actual element. expected: The expected element. """ - assert isinstance(actual, type(expected)) for k in actual.as_dict(): src = getattr(actual, k) dst = getattr(expected, k) - assert src == dst, f"attribute {repr(k)}, {repr(src)} != {repr(dst)}" + assert src == dst, f"attribute {k!r}, {src!r} != {dst!r}" From e2f3a9b0ebb0d40332dd28ee688a442e8e576e76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Mazzucotelli?= Date: Wed, 12 Jul 2023 14:07:36 +0200 Subject: [PATCH 5/5] fixup! fix: numpy returns handles more name, type cases --- src/griffe/docstrings/numpy.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/griffe/docstrings/numpy.py b/src/griffe/docstrings/numpy.py index 2cc565ad..9e97ab77 100644 --- a/src/griffe/docstrings/numpy.py +++ b/src/griffe/docstrings/numpy.py @@ -209,7 +209,7 @@ def _read_block(docstring: Docstring, *, offset: int) -> tuple[str, int]: _RE_OB: str = r"\{" # opening bracket _RE_CB: str = r"\}" # closing bracket _RE_NAME: str = r"\*{0,2}[_a-z][_a-z0-9]*" -_RE_TYPE: str = r"[^:\s].*" +_RE_TYPE: str = r".+" _RE_RETURNS: Pattern = re.compile( rf""" (?: @@ -217,9 +217,9 @@ def _read_block(docstring: Docstring, *, offset: int) -> tuple[str, int]: | # or (?P{_RE_NAME})\s*:\s* # just name | # or - (?::\s*)?(?P{_RE_TYPE})\s* # just type + \s*:\s*$ # no name, no type | # or - \s*:\s* # no name, no type + (?::\s*)?(?P{_RE_TYPE})\s* # just type ) """, re.IGNORECASE | re.VERBOSE,