Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: warn if docstring has extra parameters #63

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/griffe/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ def __len__(self):
def __iter__(self):
return iter(self._parameters_list)

def __contains__(self, item):
return item in self._parameters_dict
KevinMusgrave marked this conversation as resolved.
Show resolved Hide resolved

def add(self, parameter: Parameter) -> None:
"""Add a parameter to the container.

Expand Down
12 changes: 11 additions & 1 deletion src/griffe/docstrings/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ def _read_block(docstring: Docstring, offset: int) -> tuple[str, int]:
return "\n".join(block).rstrip("\n"), new_offset - 1


def _read_parameters(docstring: Docstring, offset: int) -> tuple[list[DocstringParameter], int]: # noqa: WPS231
def _read_parameters(
docstring: Docstring, offset: int, warn_unknown_params: bool = True
) -> tuple[list[DocstringParameter], int]: # noqa: WPS231
parameters = []
annotation: str | Name | Expression | None

Expand Down Expand Up @@ -202,6 +204,14 @@ def _read_parameters(docstring: Docstring, offset: int) -> tuple[list[DocstringP
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:
_warn(
docstring,
line_number,
f'The docstring for {docstring.parent} contains "{name}" as a parameter, but "{name}" is not an argument.',
KevinMusgrave marked this conversation as resolved.
Show resolved Hide resolved
)

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

return parameters, new_offset
Expand Down
42 changes: 27 additions & 15 deletions tests/test_docstrings/test_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,22 +702,34 @@ def test_parameter_line_without_colon_keyword_only(parse_google):
assert "Empty" in warnings[1]


# TODO: possible feature
# def test_extra_parameter(parse_google):
# """Warn on extra parameter in docstring.
#
# Parameters:
# parse_google: Fixture parser.
# """
# docstring = """
# Parameters:
# x: Integer.
# y: Integer.
# """
def test_extra_parameter(parse_google):
"""Warn on extra parameter in docstring.

# sections, warnings = parse_google(docstring)
# assert len(sections) == 1
# assert len(warnings) == 2
Parameters:
parse_google: Fixture parser.
"""
docstring = """
Parameters:
x: Integer.
y: Integer.
"""

sections, warnings = parse_google(
docstring,
parent=Function(
"func",
parameters=Parameters(
Parameter("a"),
Parameter("y"),
),
),
)
assert len(sections) == 1
assert len(warnings) == 3
assert (
'The docstring for <Function(\'func\', None, None)> contains "x" as a parameter, but "x" is not an argument.'
in warnings
)
KevinMusgrave marked this conversation as resolved.
Show resolved Hide resolved


# TODO: possible feature
Expand Down