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

Handle syntax errors in type hints #60

Merged
merged 4 commits into from
Aug 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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Change Log

## [0.1.7] - 2023-08-15

- Fixed
- Correctly handle potentially unacceptable type hint formats
- Full diff
- https://github.com/jsh9/pydoclint/compare/0.1.6...0.1.7

## [0.1.6] - 2023-08-13

- Added
Expand Down
12 changes: 10 additions & 2 deletions pydoclint/utils/arg.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,16 @@ def _eq(cls, str1: str, str2: str) -> bool:
# >>> "def",
# >>> "ghi",
# >>> ]
str1_: str = unparseAnnotation(ast.parse(stripQuotes(str1)))
str2_: str = unparseAnnotation(ast.parse(stripQuotes(str2)))
try:
str1_: str = unparseAnnotation(ast.parse(stripQuotes(str1)))
except SyntaxError:
str1_ = str1

try:
str2_: str = unparseAnnotation(ast.parse(stripQuotes(str2)))
except SyntaxError:
str2_ = str2

return str1_ == str2_


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.1.6
version = 0.1.7
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
34 changes: 34 additions & 0 deletions tests/data/edge_cases/edge_case_02_syntax_error_in_type_hints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Related issue: https://github.com/jsh9/pydoclint/issues/59


def func1(a='a'):
"""
Title

Parameters
----------
a : str, default a
"""
pass


def func2(a='a'):
"""
Title

Parameters
----------
a : str, default=a
"""
pass


def func3(a='a'):
"""
Title

Parameters
----------
a : str, default: a
"""
pass
21 changes: 21 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,27 @@ def testNonAscii() -> None:
'filename, options, expectedViolations',
[
('edge_case_01.py', {'style': 'sphinx'}, []),
(
'edge_case_02_syntax_error_in_type_hints.py',
{'style': 'numpy'},
[
'DOC106: Function `func1`: The option `--arg-type-hints-in-signature` is '
'`True` but there are no argument type hints in the signature ',
'DOC107: Function `func1`: The option `--arg-type-hints-in-signature` is '
'`True` but not all args in the signature have type hints ',
'DOC105: Function `func1`: Argument names match, but type hints do not match ',
'DOC106: Function `func2`: The option `--arg-type-hints-in-signature` is '
'`True` but there are no argument type hints in the signature ',
'DOC107: Function `func2`: The option `--arg-type-hints-in-signature` is '
'`True` but not all args in the signature have type hints ',
'DOC105: Function `func2`: Argument names match, but type hints do not match ',
'DOC106: Function `func3`: The option `--arg-type-hints-in-signature` is '
'`True` but there are no argument type hints in the signature ',
'DOC107: Function `func3`: The option `--arg-type-hints-in-signature` is '
'`True` but not all args in the signature have type hints ',
'DOC105: Function `func3`: Argument names match, but type hints do not match ',
],
),
],
)
def testEdgeCases(
Expand Down