diff options
author | Claudiu Popa <pcmanticore@gmail.com> | 2019-01-13 14:29:29 +0100 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2019-01-13 14:29:29 +0100 |
commit | 2e7266e53059c48ea1ae96195c9e751bd2cf11e5 (patch) | |
tree | 10d044103c2b9582ef03ac40973b0a0328910894 | |
parent | 1ef849fcf9b14e309ba6998a018d565e4ed1826b (diff) | |
download | pylint-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-- | ChangeLog | 4 | ||||
-rw-r--r-- | pylint/checkers/variables.py | 4 | ||||
-rw-r--r-- | pylint/test/functional/unused_variable.py | 13 |
3 files changed, 20 insertions, 1 deletions
@@ -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() |