Skip to content

Commit

Permalink
Fix false positive D203 when returning None (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsh9 authored Jun 27, 2023
1 parent 3e044e0 commit 6aac3a6
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 1 deletion.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Change Log

## [0.0.12] - 2023-06-26

- Fixed
- False positive of DOC203 when
`--require-return-section-when-returning-None` is `False`, the docstring
has no return section, and the return annotation is `None`
- Full diff
- https://github.com/jsh9/pydoclint/compare/0.0.11...0.0.12

## [0.0.11] - 2023-06-26

- Added
Expand Down
7 changes: 7 additions & 0 deletions pydoclint/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,13 @@ def checkReturns( # noqa: C901
else:
returnSec = []

if (
returnSec == [] # no return section in docstring
and returnAnno.annotation == 'None' # `-> None` in signature
and not self.requireReturnSectionWhenReturningNone
):
return violations # no need to check return type hints at all

if self.style == 'numpy':
# If the return annotation is a tuple (such as Tuple[int, str]),
# we consider both in the docstring to be a valid style:
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.11
version = 0.0.12
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
8 changes: 8 additions & 0 deletions tests/data/google/returning_none/cases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def func(arg1: int) -> None:
"""
Do something
Args:
arg1 (int): Arg 1
"""
print(1)
10 changes: 10 additions & 0 deletions tests/data/numpy/returning_none/cases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def func(arg1: int) -> None:
"""
Do something
Parameters
----------
arg1 : int
Arg 1
"""
print(1)
29 changes: 29 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,35 @@ def testReturns(style: str, filename: str) -> None:
assert list(map(str, violations)) == expectedViolationsCopy


@pytest.mark.parametrize(
'style, require',
list(
itertools.product(
['numpy', 'google'],
[True, False],
),
),
)
def testReturns_returningNone(style: str, require: bool) -> None:
violations = _checkFile(
filename=DATA_DIR / f'{style}/returning_none/cases.py',
skipCheckingShortDocstrings=True,
requireReturnSectionWhenReturningNone=require,
style=style,
)
expectedViolationsCopy = (
[
'DOC201: Function `func` does not have a return section in docstring ',
'DOC203: Function `func` return type(s) in docstring not consistent with the '
'return annotation. Return annotation has 1 type(s); docstring return section '
'has 0 type(s).',
]
if require
else []
)
assert list(map(str, violations)) == expectedViolationsCopy


def _tweakViolationMsgForFunctions(expectedViolationsCopy: List[str]) -> None:
for i in range(len(expectedViolationsCopy)):
expectedViolationsCopy[i] = expectedViolationsCopy[i].replace(
Expand Down

0 comments on commit 6aac3a6

Please sign in to comment.