Skip to content

Commit

Permalink
fix: Don't crash on Google parameters sections found in non-function …
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Apr 18, 2022
1 parent e577e1b commit 4a417bc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/griffe/docstrings/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,14 @@ def _read_parameters( # noqa: WPS231
if annotation is None:
_warn(docstring, line_number, f"No type or annotation for parameter '{name}'")

if warn_unknown_params and docstring.parent is not None:
if name not in docstring.parent.parameters: # type: ignore[attr-defined]
_warn(
docstring,
line_number,
f"Parameter '{name}' does not appear in the parent signature",
)
if warn_unknown_params:
with suppress(AttributeError): # for parameters sections in non-function docstrings
if name not in docstring.parent.parameters: # type: ignore[union-attr]
_warn(
docstring,
line_number,
f"Parameter '{name}' does not appear in the parent signature",
)

parameters.append(DocstringParameter(name=name, value=default, annotation=annotation, description=description))

Expand Down
16 changes: 16 additions & 0 deletions tests/test_docstrings/test_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,22 @@ def test_never_warn_about_unknown_other_parameters(parse_google):
assert not warnings


def test_unknown_params_scan_doesnt_crash_on_non_function_docstrings(parse_google):
"""Never warn about unknown parameters in "Other parameters" sections.
Parameters:
parse_google: Fixture parser.
"""
docstring = """
Parameters:
this (str): This.
that (str): That.
"""

_, warnings = parse_google(docstring, parent=Class("c"))
assert not warnings


# TODO: possible feature
# def test_missing_parameter(parse_google):
# """Warn on missing parameter in docstring.
Expand Down

0 comments on commit 4a417bc

Please sign in to comment.