summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2019-01-13 14:29:29 +0100
committerClaudiu Popa <pcmanticore@gmail.com>2019-01-13 14:29:29 +0100
commit2e7266e53059c48ea1ae96195c9e751bd2cf11e5 (patch)
tree10d044103c2b9582ef03ac40973b0a0328910894
parent1ef849fcf9b14e309ba6998a018d565e4ed1826b (diff)
downloadpylint-git-2e7266e53059c48ea1ae96195c9e751bd2cf11e5.tar.gz
Fixed a false positive for ``unused-variable`` and ``nonlocal`` assignments
When looking for nonlocal names and assignments in upper scope, also look for AnnAssign nodes. Close #2671
-rw-r--r--ChangeLog4
-rw-r--r--pylint/checkers/variables.py4
-rw-r--r--pylint/test/functional/unused_variable.py13
3 files changed, 20 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index 09d82e474..a7c2794b4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -7,6 +7,10 @@ What's New in Pylint 2.3.0?
Release date: TBA
+* Fixed a false positive for ``unused-variable`` and ``nonlocal`` assignments
+
+ Close #2671
+
* Added ``load_configuration()`` hook for plugins
New optional hook for plugins is added: ``load_configuration()``.
diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py
index 5a39f036d..3e487d309 100644
--- a/pylint/checkers/variables.py
+++ b/pylint/checkers/variables.py
@@ -957,7 +957,9 @@ class VariablesChecker(BaseChecker):
"unused-argument", args=name, node=stmt, confidence=confidence
)
else:
- if stmt.parent and isinstance(stmt.parent, astroid.Assign):
+ if stmt.parent and isinstance(
+ stmt.parent, (astroid.Assign, astroid.AnnAssign)
+ ):
if name in nonlocal_names:
return
diff --git a/pylint/test/functional/unused_variable.py b/pylint/test/functional/unused_variable.py
index 4e6d58fe4..cbf18fb8e 100644
--- a/pylint/test/functional/unused_variable.py
+++ b/pylint/test/functional/unused_variable.py
@@ -53,3 +53,16 @@ def locals_does_not_account_for_subscopes():
def unused_import_from():
from functools import wraps as abc # [unused-import]
from collections import namedtuple # [unused-import]
+
+
+def function():
+ ann: int = 0
+ assign = 0
+
+ def inner():
+ nonlocal ann, assign
+ ann += 1
+ assign += 1
+ return ann + assign
+
+ return inner()