Skip to content

Commit

Permalink
Handle syntax errors in type hints (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsh9 authored Aug 15, 2023
1 parent 12abb68 commit 9a183ed
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
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

0 comments on commit 9a183ed

Please # to comment.