diff --git a/ChangeLog b/ChangeLog index b64a725cd46..08e8bbe0515 100644 --- a/ChangeLog +++ b/ChangeLog @@ -386,10 +386,15 @@ must rename the associated identification. * Allow the choice of f-strings as a valid way of formatting logging strings. -Closes #2395 + Closes #2395 * Added ``--list-msgs-enabled`` command to list all enabled and disabled messages given the current RC file and command line arguments. +* Fixed ``missing-yield-type-doc`` getting incorrectly raised when + a generator does not document a yield type but has a type annotation. + + Closes #3185 + What's New in Pylint 2.3.0? =========================== diff --git a/pylint/extensions/docparams.py b/pylint/extensions/docparams.py index ca5fed23ce3..d5a15a4f3e2 100644 --- a/pylint/extensions/docparams.py +++ b/pylint/extensions/docparams.py @@ -316,7 +316,7 @@ def visit_yield(self, node): if not doc_has_yields: self.add_message("missing-yield-doc", node=func_node) - if not doc_has_yields_type: + if not (doc_has_yields_type or func_node.returns): self.add_message("missing-yield-type-doc", node=func_node) def visit_yieldfrom(self, node): diff --git a/tests/extensions/test_check_docs.py b/tests/extensions/test_check_docs.py index 90ccf633452..d2e98ee250e 100644 --- a/tests/extensions/test_check_docs.py +++ b/tests/extensions/test_check_docs.py @@ -16,6 +16,8 @@ """Unit tests for the pylint checkers in :mod:`pylint.extensions.check_docs`, in particular the parameter documentation checker `DocstringChecker` """ +import sys + import astroid import pytest diff --git a/tests/extensions/test_check_return_docs.py b/tests/extensions/test_check_return_docs.py index c8758b43c09..caaf1b2f132 100644 --- a/tests/extensions/test_check_return_docs.py +++ b/tests/extensions/test_check_return_docs.py @@ -12,7 +12,10 @@ """Unit tests for the return documentation checking in the `DocstringChecker` in :mod:`pylint.extensions.check_docs` """ +import sys + import astroid +import pytest from pylint.extensions.docparams import DocstringParameterChecker from pylint.testutils import CheckerTestCase, Message, set_config @@ -76,21 +79,6 @@ def my_func(self): ): self.checker.visit_return(return_node) - def test_sphinx_returns_annotations(self): - node = astroid.extract_node( - ''' - def my_func(self) -> bool: - """This is a docstring. - - :returns: Always False - """ - return False - ''' - ) - return_node = node.body[0] - with self.assertNoMessages(): - self.checker.visit_return(return_node) - def test_sphinx_missing_return_type_with_annotations(self): node = astroid.extract_node( ''' diff --git a/tests/extensions/test_check_yields_docs.py b/tests/extensions/test_check_yields_docs.py index 4987e75c648..93d73a1b510 100644 --- a/tests/extensions/test_check_yields_docs.py +++ b/tests/extensions/test_check_yields_docs.py @@ -9,7 +9,10 @@ """Unit tests for the yield documentation checking in the `DocstringChecker` in :mod:`pylint.extensions.check_docs` """ +import sys + import astroid +import pytest from pylint.extensions.docparams import DocstringParameterChecker from pylint.testutils import CheckerTestCase, Message, set_config @@ -445,3 +448,38 @@ def my_func(self): ) with self.assertAddsMessages(Message(msg_id="redundant-yields-doc", node=node)): self.checker.visit_functiondef(node) + + def test_sphinx_missing_yield_type_with_annotations(self): + node = astroid.extract_node( + ''' + import typing + + def generator() -> typing.Iterator[int]: + """A simple function for checking type hints. + + :returns: The number 0 + """ + yield 0 + ''' + ) + yield_node = node.body[0] + with self.assertNoMessages(): + self.checker.visit_yield(yield_node) + + def test_google_missing_yield_type_with_annotations(self): + node = astroid.extract_node( + ''' + import typing + + def generator() -> typing.Iterator[int]: + """A simple function for checking type hints. + + Yields: + The number 0 + """ + yield 0 + ''' + ) + yield_node = node.body[0] + with self.assertNoMessages(): + self.checker.visit_yield(yield_node)