Skip to content

Commit

Permalink
Raise ParseError when section contents can't be parsed
Browse files Browse the repository at this point in the history
  • Loading branch information
jsh9 committed Jan 10, 2025
1 parent 80489b0 commit f3b9dc0
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 11 additions & 1 deletion docstring_parser/numpydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import typing as T
from textwrap import dedent

from . import ParseError
from .common import (
Docstring,
DocstringAttr,
Expand Down Expand Up @@ -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

Expand Down
77 changes: 77 additions & 0 deletions docstring_parser/tests/test_numpydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
[
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit f3b9dc0

Please sign in to comment.