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

Fix incorrect tuple annotation detection #45

Merged
merged 3 commits into from
Jul 15, 2023
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
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Change Log

## [0.0.16] - 2023-07-14

- Fixed
- Fixed a bug (https://github.com/jsh9/pydoclint/issues/44) where tuple type
annotation is incorrectly detected
- Full diff
- https://github.com/jsh9/pydoclint/compare/0.0.15...0.0.16

## [0.0.15] - 2023-07-10

- Fixed
Expand All @@ -8,7 +16,7 @@
- Fixed false positives when `return` and `raise` statements are in a block
within a nested function
- Full diff
- https://github.com/jsh9/pydoclint/compare/0.0.15...0.0.15
- https://github.com/jsh9/pydoclint/compare/0.0.14...0.0.15

## [0.0.14] - 2023-07-05

Expand Down
9 changes: 5 additions & 4 deletions pydoclint/utils/return_anno.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ def decompose(self) -> List[str]:
return self.putAnnotationInList()

def _isTuple(self) -> bool:
return (
self.annotation is not None
and self.annotation.lower().startswith('tuple[')
)
try:
annoHead = ast.parse(self.annotation).body[0].value.value.id
return annoHead in {'tuple', 'Tuple'}
except Exception:
return False

def putAnnotationInList(self) -> List[str]:
"""Put annotation string in a list"""
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = pydoclint
version = 0.0.15
version = 0.0.16
description = A Python docstring linter that checks arguments, returns, yields, and raises sections
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down
35 changes: 35 additions & 0 deletions tests/utils/test_return_anno.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import pytest

from pydoclint.utils.return_anno import ReturnAnnotation


@pytest.mark.parametrize(
'annotation, expected',
[
(None, False),
('', False),
(' ', False),
('Tuple[int]', True),
('Tuple[int, str]', True),
('Tuple[int, str, bool]', True),
('Tuple[int, str, None, Dict[str, Any]]', True),
('Tuple[int, ...]', True),
('tuple[int, str]', True),
('Optional[Tuple[int, str]]', False),
('Union[Tuple[int, str], Tuple[int, str]]', False),
('tuple[int, str] | tuple[int, bool]', False),
('tuple[str, str, bool, bool] | None', False),
('tuple[int | str, str | bool, bool | float]', True),
('int', False),
('(int, str, bool)', False),
('tuple(int, str, bool)', False),
('Tuple(int, str, bool)', False),
('tuple(1, 2, 3)', False),
("'tuple[int, int, str]'", True),
('"tuple[int, int, str]"', True),
("'tuple'[int, int, str]", True),
],
)
def testIsTuple(annotation: str, expected: bool) -> None:
retAnno = ReturnAnnotation(annotation=annotation)
assert retAnno._isTuple() == expected