summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Walls <jacobtylerwalls@gmail.com>2022-05-01 11:48:26 -0400
committerGitHub <noreply@github.com>2022-05-01 17:48:26 +0200
commit9d300422fe346b80af05db23404cbefd2b174530 (patch)
treed8029376a319ddb42cfb9bf193df7c0732bb6f8c
parentdede647aba2b2c64cd91fdc31fcd682ebfda26cf (diff)
downloadpylint-git-9d300422fe346b80af05db23404cbefd2b174530.tar.gz
Fix false positive for `undefined-loop-variable` for lambda in first of two loops (#6479)
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
-rw-r--r--ChangeLog5
-rw-r--r--doc/whatsnew/2.13.rst5
-rw-r--r--pylint/checkers/variables.py5
-rw-r--r--tests/functional/u/undefined/undefined_loop_variable.py20
4 files changed, 34 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index b7e0f0519..5252535a9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -242,6 +242,11 @@ What's New in Pylint 2.13.8?
============================
Release date: TBA
+* Fix a false positive for ``undefined-loop-variable`` for a variable used in a lambda
+ inside the first of multiple loops.
+
+ Closes #6419
+
* Fix a crash when linting a file that passes an integer ``mode=`` to
``open``
diff --git a/doc/whatsnew/2.13.rst b/doc/whatsnew/2.13.rst
index a9ec569c6..0663fbe7d 100644
--- a/doc/whatsnew/2.13.rst
+++ b/doc/whatsnew/2.13.rst
@@ -625,6 +625,11 @@ Other Changes
Closes #5930
+* Fix a false positive for ``undefined-loop-variable`` for a variable used in a lambda
+ inside the first of multiple loops.
+
+ Closes #6419
+
* Fix a crash when linting a file that passes an integer ``mode=`` to
``open``
diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py
index a9c29155a..0ad9b0c99 100644
--- a/pylint/checkers/variables.py
+++ b/pylint/checkers/variables.py
@@ -2198,7 +2198,10 @@ class VariablesChecker(BaseChecker):
# the usage is safe because the function will not be defined either if
# the variable is not defined.
scope = node.scope()
- if isinstance(scope, nodes.FunctionDef) and any(
+ # FunctionDef subclasses Lambda due to a curious ontology. Check both.
+ # See https://github.com/PyCQA/astroid/issues/291
+ # TODO: Revisit when astroid 3.0 includes the change
+ if isinstance(scope, nodes.Lambda) and any(
asmt.scope().parent_of(scope) for asmt in astmts
):
return
diff --git a/tests/functional/u/undefined/undefined_loop_variable.py b/tests/functional/u/undefined/undefined_loop_variable.py
index 94b27d2dd..6bcf6103d 100644
--- a/tests/functional/u/undefined/undefined_loop_variable.py
+++ b/tests/functional/u/undefined/undefined_loop_variable.py
@@ -119,3 +119,23 @@ bigger = [
]
for item in lst
]
+
+
+def lambda_in_first_of_two_loops():
+ """https://github.com/PyCQA/pylint/issues/6419"""
+ my_list = []
+ for thing in my_list:
+ print_it = lambda: print(thing) # pylint: disable=cell-var-from-loop
+ print_it()
+
+ for thing in my_list:
+ print(thing)
+
+
+def variable_name_assigned_in_body_of_second_loop():
+ for alias in tuple(bigger):
+ continue
+ for _ in range(3):
+ alias = True
+ if alias:
+ print(alias)