summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhaasea <44787650+haasea@users.noreply.github.com>2021-04-18 10:51:42 -0400
committerGitHub <noreply@github.com>2021-04-18 16:51:42 +0200
commitb2f0d495bb75f8c7edde474c096f60c3533ec650 (patch)
tree3fe700ab6d882c40b1d8bd1632ec3b6e0a65f940
parentbafb1497b0b46d6e7d9db5c3716c8638e3686944 (diff)
downloadpylint-git-b2f0d495bb75f8c7edde474c096f60c3533ec650.tar.gz
resolved false positive on unused variables in decorator functions (#4373)
* resolved false positive on unused variables in decorator functions and added corresponding test cases Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
-rw-r--r--CONTRIBUTORS.txt2
-rw-r--r--ChangeLog4
-rw-r--r--pylint/checkers/variables.py3
-rw-r--r--tests/functional/d/decorator_unused.py13
-rw-r--r--tests/functional/d/decorator_unused.txt0
5 files changed, 22 insertions, 0 deletions
diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt
index 0252731b5..62bcc2004 100644
--- a/CONTRIBUTORS.txt
+++ b/CONTRIBUTORS.txt
@@ -469,6 +469,8 @@ contributors:
* Andreas Finkler: contributor
+* Aidan Haase, Elizabeth Bott: contributor
+
* Sebastian Müller: contributor
* Ramiro Leal-Cavazos (ramiro050): Fixed bug preventing pylint from working with emacs tramp
diff --git a/ChangeLog b/ChangeLog
index a1dd0606a..ffbaec574 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -9,6 +9,10 @@ Release date: Undefined
..
Put new features here and also in 'doc/whatsnew/2.8.rst'
+* Resolve false positives on unused variables in decorator functions
+
+ Closes #4252
+
* Add new extension ``ConfusingConsecutiveElifChecker``. This optional checker emits a refactoring message (R5601 ``confusing-consecutive-elif``)
if if/elif statements with different indentation levels follow directly one after the other.
diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py
index 2a1587eeb..c611ae2f0 100644
--- a/pylint/checkers/variables.py
+++ b/pylint/checkers/variables.py
@@ -1695,6 +1695,9 @@ class VariablesChecker(BaseChecker):
return
message_name = "unused-variable"
+ if isinstance(stmt, astroid.FunctionDef) and stmt.decorators:
+ return
+
# Don't check function stubs created only for type information
if utils.is_overload_stub(node):
return
diff --git a/tests/functional/d/decorator_unused.py b/tests/functional/d/decorator_unused.py
new file mode 100644
index 000000000..add5b3b40
--- /dev/null
+++ b/tests/functional/d/decorator_unused.py
@@ -0,0 +1,13 @@
+# pylint: disable=C
+def foo():
+ _dispatch = {}
+
+ def save(fn):
+ _dispatch[fn.__name__] = fn
+ return fn
+
+ @save
+ def bar():
+ ...
+ # do whatever
+foo()
diff --git a/tests/functional/d/decorator_unused.txt b/tests/functional/d/decorator_unused.txt
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/tests/functional/d/decorator_unused.txt