diff --git a/CHANGELOG.md b/CHANGELOG.md index 464e86a..e3b33a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/pydoclint/visitor.py b/pydoclint/visitor.py index 51fb079..d59c7c7 100644 --- a/pydoclint/visitor.py +++ b/pydoclint/visitor.py @@ -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: diff --git a/setup.cfg b/setup.cfg index f8bf42e..2067811 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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 diff --git a/tests/data/google/returning_none/cases.py b/tests/data/google/returning_none/cases.py new file mode 100644 index 0000000..d209581 --- /dev/null +++ b/tests/data/google/returning_none/cases.py @@ -0,0 +1,8 @@ +def func(arg1: int) -> None: + """ + Do something + + Args: + arg1 (int): Arg 1 + """ + print(1) diff --git a/tests/data/numpy/returning_none/cases.py b/tests/data/numpy/returning_none/cases.py new file mode 100644 index 0000000..54fb5c1 --- /dev/null +++ b/tests/data/numpy/returning_none/cases.py @@ -0,0 +1,10 @@ +def func(arg1: int) -> None: + """ + Do something + + Parameters + ---------- + arg1 : int + Arg 1 + """ + print(1) diff --git a/tests/test_main.py b/tests/test_main.py index 05c7a36..cfc8e7d 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -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(