Skip to content

Commit 9ba1d64

Browse files
committed
Protect against AttributeError when checking cell-var-from-loop
Close #3646
1 parent ffb6006 commit 9ba1d64

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ Release date: TBA
3232

3333
Close #3538
3434

35+
* Protect against `AttributeError` when checking `cell-var-from-loop`
36+
37+
Close #3646
38+
3539

3640
What's New in Pylint 2.5.2?
3741
===========================

pylint/checkers/variables.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,13 +1716,14 @@ def _is_direct_lambda_call():
17161716
else:
17171717
assign_scope = assignment_node.scope()
17181718
maybe_for = assignment_node
1719-
while not isinstance(maybe_for, astroid.For):
1719+
while maybe_for and not isinstance(maybe_for, astroid.For):
17201720
if maybe_for is assign_scope:
17211721
break
17221722
maybe_for = maybe_for.parent
17231723
else:
17241724
if (
1725-
maybe_for.parent_of(node_scope)
1725+
maybe_for
1726+
and maybe_for.parent_of(node_scope)
17261727
and not _is_direct_lambda_call()
17271728
and not isinstance(node_scope.statement(), astroid.Return)
17281729
):

tests/functional/c/cellvar_escaping_loop.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
# pylint: disable=print-statement, unnecessary-comprehension
1+
# pylint: disable=print-statement, unnecessary-comprehension,missing-docstring,too-few-public-methods
22
"""Tests for loopvar-in-closure."""
33
from __future__ import print_function
44

5+
from enum import Enum
6+
7+
58
def good_case():
69
"""No problems here."""
710
lst = []
@@ -118,3 +121,11 @@ def bad_case6():
118121
print(j)
119122
lst.append(lambda: i) # [cell-var-from-loop]
120123
return lst
124+
125+
126+
class Test(Enum):
127+
TEST = (40, 160)
128+
129+
@staticmethod
130+
def new_test(minimum=TEST[0], maximum=TEST[1]):
131+
return minimum, maximum
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
cell-var-from-loop:67:bad_case.<lambda>:Cell variable i defined in loop
2-
cell-var-from-loop:72:bad_case2.<lambda>:Cell variable i defined in loop
3-
cell-var-from-loop:80:bad_case3.<lambda>:Cell variable j defined in loop
4-
cell-var-from-loop:90:bad_case4.nested:Cell variable i defined in loop
5-
cell-var-from-loop:111:bad_case5.<lambda>:Cell variable i defined in loop
6-
cell-var-from-loop:119:bad_case6.<lambda>:Cell variable i defined in loop
1+
cell-var-from-loop:70:bad_case.<lambda>:Cell variable i defined in loop
2+
cell-var-from-loop:75:bad_case2.<lambda>:Cell variable i defined in loop
3+
cell-var-from-loop:83:bad_case3.<lambda>:Cell variable j defined in loop
4+
cell-var-from-loop:93:bad_case4.nested:Cell variable i defined in loop
5+
cell-var-from-loop:114:bad_case5.<lambda>:Cell variable i defined in loop
6+
cell-var-from-loop:122:bad_case6.<lambda>:Cell variable i defined in loop

0 commit comments

Comments
 (0)