Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 7 additions & 6 deletions src/docstub/_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,19 +816,20 @@ def _section_annotations(self, name):
for param in params:
param = self._handle_missing_whitespace(param) # noqa: PLW2901

if param.type.strip() == "":
# Missing doctype in docstring, might have an inlined annotation
# so skip
continue

if param.name in annotated_params:
# TODO make error
self.reporter.warn(
"Duplicate parameter / attribute name in docstring",
details=self.reporter.underline(param.name),
)
continue

if param.type:
ds_line = self._find_docstring_line(param.name, param.type)
annotation = self._doctype_to_annotation(param.type, ds_line=ds_line)
else:
annotation = FallbackAnnotation
ds_line = self._find_docstring_line(param.name, param.type)
annotation = self._doctype_to_annotation(param.type, ds_line=ds_line)
annotated_params[param.name.strip()] = annotation

return annotated_params
Expand Down
13 changes: 5 additions & 8 deletions tests/test_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,9 @@ def test_parameters(self, doctype, expected):
)
transformer = DoctypeTransformer()
annotations = DocstringAnnotations(docstring, transformer=transformer)
assert len(annotations.parameters) == 2
assert len(annotations.parameters) == 1
assert annotations.parameters["a"].value == expected
assert annotations.parameters["b"].value == "Incomplete"
assert annotations.parameters["b"].imports == {PyImport.typeshed_Incomplete()}
assert "b" not in annotations.parameters

@pytest.mark.parametrize(
("doctypes", "expected"),
Expand Down Expand Up @@ -539,12 +538,10 @@ def test_combined_numpydoc_params(self):
)
transformer = DoctypeTransformer()
annotations = DocstringAnnotations(docstring, transformer=transformer)
assert len(annotations.parameters) == 5
assert len(annotations.parameters) == 3
assert annotations.parameters["a"].value == "bool"
assert annotations.parameters["b"].value == "bool"
assert annotations.parameters["c"].value == "bool"

assert annotations.parameters["d"].value == "Incomplete"
assert annotations.parameters["e"].value == "Incomplete"
assert annotations.parameters["d"].imports == {PyImport.typeshed_Incomplete()}
assert annotations.parameters["e"].imports == {PyImport.typeshed_Incomplete()}
assert "d" not in annotations.parameters
assert "e" not in annotations.parameters
12 changes: 8 additions & 4 deletions tests/test_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,14 +481,16 @@ def foo(a: int) -> None: ...
assert "ignoring docstring: Sized" in caplog.records[0].details
assert caplog.records[0].levelno == logging.WARNING

def test_missing_param(self, caplog):
@pytest.mark.parametrize("missing", ["", "b", "b :"])
def test_missing_param(self, missing, caplog):
source = dedent(
'''
f'''
def foo(a, b) -> None:
"""
Parameters
----------
a : int
{missing}
"""
'''
)
Expand Down Expand Up @@ -523,14 +525,16 @@ def foo(a: int, b: Incomplete) -> None: ...
assert caplog.messages == ["Missing annotation for parameter 'b'"]
assert caplog.records[0].levelno == logging.WARNING

def test_missing_attr(self, caplog):
@pytest.mark.parametrize("missing", ["", "b", "b :"])
def test_missing_attr(self, missing, caplog):
source = dedent(
'''
f'''
class Foo:
"""
Attributes
----------
a : ClassVar[int]
{missing}
"""
a = 3
b = True
Expand Down
Loading