diff --git a/CHANGELOG.md b/CHANGELOG.md index 29a811d..4abe574 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# (Fork) 0.0.10 (2025-01-10) + +- For numpy style, raise `ParseError` when a section with non-empty contents is detected + but nothing can be parsed + # (Fork) 0.0.9 (2024-06-26) - Switched to pprint to show details of a `Docstring` object diff --git a/docstring_parser/numpydoc.py b/docstring_parser/numpydoc.py index 2a9f3c2..5cdcf72 100644 --- a/docstring_parser/numpydoc.py +++ b/docstring_parser/numpydoc.py @@ -9,6 +9,7 @@ import typing as T from textwrap import dedent +from . import ParseError from .common import ( Docstring, DocstringAttr, @@ -409,7 +410,16 @@ def parse(self, text: str) -> Docstring: # ends at the start of the next header start = match.end() end = nextmatch.start() if nextmatch is not None else None - ret.meta.extend(factory.parse(meta_chunk[start:end])) + + substring: str = meta_chunk[start:end] + parsed = list(factory.parse(substring)) + + if substring != "" and len(parsed) == 0: + raise ParseError( + f"Section '{title}' is not empty but nothing was parsed." + ) + + ret.meta.extend(parsed) return ret diff --git a/docstring_parser/tests/test_numpydoc.py b/docstring_parser/tests/test_numpydoc.py index 70fdb02..e1a90a3 100644 --- a/docstring_parser/tests/test_numpydoc.py +++ b/docstring_parser/tests/test_numpydoc.py @@ -3,9 +3,86 @@ import pytest +from docstring_parser import ParseError from docstring_parser.numpydoc import compose, parse +@pytest.mark.parametrize( + "source", + [ + "\n".join( + [ + "This is a function", + "", + "Parameters", + "----------", + " arg1 : str", + " This is arg 1" + ] + ), + "\n".join( + [ + "This is a function", + "", + "Parameters", + "----------", + " arg1 : str", + " This is arg 1" + ] + ), + "\n".join( + [ + "This is a function", + "", + "Parameters", + "----------", + " arg1 : str", + " This is arg 1" + ] + ), + "\n".join( + [ + "This is a function", + "", + "Parameters", + "----------", + " This is arg 1" + ] + ), + "\n".join( + [ + "This is a function", + "", + "Returns", + "-------", + " The return variable" + ] + ), + "\n".join( + [ + "This is a function", + "", + "Yields", + "------", + " Something yielded" + ] + ), + "\n".join( + [ + "This is a function", + "", + "Raises", + "------", + " Some error" + ] + ), + ], +) +def test_detect_formatting_error(source: str) -> None: + with pytest.raises(ParseError): + parse(source) + + @pytest.mark.parametrize( "source, expected", [ diff --git a/pyproject.toml b/pyproject.toml index a2d8ce8..bc11ae9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "docstring_parser_fork" -version = "0.0.9" +version = "0.0.10" description = "Parse Python docstrings in reST, Google and Numpydoc format" authors = ["Marcin Kurczewski "] license = "MIT"