summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2021-08-31 11:34:58 +0200
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2021-08-31 12:09:07 +0200
commit8b778cd5d5a6220b6fc1d6a80e30066ebcabd43e (patch)
tree43c5e75a3178ad3cf73b514043f34da2d6a74590
parented8e6bbf572ed167d3778d2345405201cbe837c1 (diff)
downloadpylint-git-8b778cd5d5a6220b6fc1d6a80e30066ebcabd43e.tar.gz
Fix false positive for `function-redefined` for simple type annotations
Closes #4936
-rw-r--r--ChangeLog4
-rw-r--r--doc/whatsnew/2.11.rst4
-rw-r--r--pylint/checkers/base.py6
-rw-r--r--tests/functional/f/function_redefined.py8
-rw-r--r--tests/functional/f/function_redefined.txt14
5 files changed, 28 insertions, 8 deletions
diff --git a/ChangeLog b/ChangeLog
index 66758ffdd..c0d6cb3a7 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 ce6605273..c2333911f 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 d2599d944..12b512fe3 100644
--- a/pylint/checkers/base.py
+++ b/pylint/checkers/base.py
@@ -862,7 +862,11 @@ class BasicErrorChecker(_BasicChecker):
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 daac81dd6..a70ccf9c4 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 func(callback1=None, callback2=None):
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 26f1c28d2..dcf04c3b9 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