diff --git a/ChangeLog b/ChangeLog index 66758ffdd9..c0d6cb3a7b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -39,6 +39,10 @@ Release date: TBA Closes #3802 +* Fix false positive for ``function-redefined`` for simple type annotations + + Closes #4936 + What's New in Pylint 2.10.3? ============================ diff --git a/doc/whatsnew/2.11.rst b/doc/whatsnew/2.11.rst index ce66052739..c2333911f4 100644 --- a/doc/whatsnew/2.11.rst +++ b/doc/whatsnew/2.11.rst @@ -41,3 +41,7 @@ Other Changes * Added ``py-version`` config key (if ``[MASTER]`` section). Used for version dependant checks. Will default to whatever Python version pylint is executed with. * The ``invalid-name`` message is now more detailed when using multiple naming style regexes. + +* Fix false positive for ``function-redefined`` for simple type annotations + + Closes #4936 diff --git a/pylint/checkers/base.py b/pylint/checkers/base.py index d2599d9447..12b512fe3e 100644 --- a/pylint/checkers/base.py +++ b/pylint/checkers/base.py @@ -862,7 +862,11 @@ def _check_redefinition(self, redeftype, node): parent_frame = node.parent.frame() # Ignore function stubs created for type information - redefinitions = parent_frame.locals[node.name] + redefinitions = [ + i + for i in parent_frame.locals[node.name] + if not (isinstance(i.parent, nodes.AnnAssign) and i.parent.simple) + ] defined_self = next( (local for local in redefinitions if not utils.is_overload_stub(local)), node, diff --git a/tests/functional/f/function_redefined.py b/tests/functional/f/function_redefined.py index daac81dd69..a70ccf9c4d 100644 --- a/tests/functional/f/function_redefined.py +++ b/tests/functional/f/function_redefined.py @@ -1,6 +1,9 @@ # pylint: disable=no-self-use,missing-docstring,using-constant-test, useless-object-inheritance # pylint: disable=unused-import,wrong-import-position,reimported, unnecessary-pass from __future__ import division + +from typing import Callable + __revision__ = '' class AAAA(object): """docstring""" @@ -118,3 +121,8 @@ def callback1(): def callback2(): return 24 return callback1(), callback2() + +do_something: Callable[[], int] + +def do_something() -> int: + return 1 diff --git a/tests/functional/f/function_redefined.txt b/tests/functional/f/function_redefined.txt index 26f1c28d2a..dcf04c3b93 100644 --- a/tests/functional/f/function_redefined.txt +++ b/tests/functional/f/function_redefined.txt @@ -1,7 +1,7 @@ -function-redefined:15:4:AAAA.method2:method already defined line 12 -function-redefined:18:0:AAAA:class already defined line 5 -function-redefined:32:0:func2:function already defined line 29 -redefined-outer-name:34:4:func2:Redefining name '__revision__' from outer scope (line 4) -function-redefined:51:4:exclusive_func2:function already defined line 45 -function-redefined:86:0:ceil:function already defined line 85 -function-redefined:90:0:math:function already defined line 89 +function-redefined:18:4:AAAA.method2:method already defined line 15:HIGH +function-redefined:21:0:AAAA:class already defined line 8:HIGH +function-redefined:35:0:func2:function already defined line 32:HIGH +redefined-outer-name:37:4:func2:Redefining name '__revision__' from outer scope (line 7):HIGH +function-redefined:54:4:exclusive_func2:function already defined line 48:HIGH +function-redefined:89:0:ceil:function already defined line 88:HIGH +function-redefined:93:0:math:function already defined line 92:HIGH