diff options
author | Claudiu Popa <pcmanticore@gmail.com> | 2020-05-29 08:37:52 +0200 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2020-05-29 19:21:29 +0200 |
commit | 627d07d249d17c12ed9e1a52044843607a5353d0 (patch) | |
tree | a9d700661bb81117db987d1a2a6de80b4ce106b0 | |
parent | 1ad2f2550ca34f12da0a1065cd1e61cdeefba90b (diff) | |
download | pylint-git-627d07d249d17c12ed9e1a52044843607a5353d0.tar.gz |
Protect against `AttributeError` when checking `cell-var-from-loop`
Close #3646
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | pylint/checkers/variables.py | 5 | ||||
-rw-r--r-- | tests/functional/c/cellvar_escaping_loop.py | 13 | ||||
-rw-r--r-- | tests/functional/c/cellvar_escaping_loop.txt | 12 |
4 files changed, 25 insertions, 9 deletions
@@ -52,6 +52,10 @@ Release date: TBA Close #3547 +* Protect against `AttributeError` when checking `cell-var-from-loop` + + Close #3646 + What's New in Pylint 2.5.2? =========================== diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 28dc2178f..a6208f146 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -1716,13 +1716,14 @@ class VariablesChecker(BaseChecker): else: assign_scope = assignment_node.scope() maybe_for = assignment_node - while not isinstance(maybe_for, astroid.For): + while maybe_for and not isinstance(maybe_for, astroid.For): if maybe_for is assign_scope: break maybe_for = maybe_for.parent else: if ( - maybe_for.parent_of(node_scope) + maybe_for + and maybe_for.parent_of(node_scope) and not _is_direct_lambda_call() and not isinstance(node_scope.statement(), astroid.Return) ): diff --git a/tests/functional/c/cellvar_escaping_loop.py b/tests/functional/c/cellvar_escaping_loop.py index 193f289ac..48d2291dd 100644 --- a/tests/functional/c/cellvar_escaping_loop.py +++ b/tests/functional/c/cellvar_escaping_loop.py @@ -1,7 +1,10 @@ -# pylint: disable=print-statement, unnecessary-comprehension +# pylint: disable=print-statement, unnecessary-comprehension,missing-docstring,too-few-public-methods """Tests for loopvar-in-closure.""" from __future__ import print_function +from enum import Enum + + def good_case(): """No problems here.""" lst = [] @@ -118,3 +121,11 @@ def bad_case6(): print(j) lst.append(lambda: i) # [cell-var-from-loop] return lst + + +class Test(Enum): + TEST = (40, 160) + + @staticmethod + def new_test(minimum=TEST[0], maximum=TEST[1]): + return minimum, maximum diff --git a/tests/functional/c/cellvar_escaping_loop.txt b/tests/functional/c/cellvar_escaping_loop.txt index 2ac0b58ef..d9fc6d025 100644 --- a/tests/functional/c/cellvar_escaping_loop.txt +++ b/tests/functional/c/cellvar_escaping_loop.txt @@ -1,6 +1,6 @@ -cell-var-from-loop:67:bad_case.<lambda>:Cell variable i defined in loop -cell-var-from-loop:72:bad_case2.<lambda>:Cell variable i defined in loop -cell-var-from-loop:80:bad_case3.<lambda>:Cell variable j defined in loop -cell-var-from-loop:90:bad_case4.nested:Cell variable i defined in loop -cell-var-from-loop:111:bad_case5.<lambda>:Cell variable i defined in loop -cell-var-from-loop:119:bad_case6.<lambda>:Cell variable i defined in loop +cell-var-from-loop:70:bad_case.<lambda>:Cell variable i defined in loop +cell-var-from-loop:75:bad_case2.<lambda>:Cell variable i defined in loop +cell-var-from-loop:83:bad_case3.<lambda>:Cell variable j defined in loop +cell-var-from-loop:93:bad_case4.nested:Cell variable i defined in loop +cell-var-from-loop:114:bad_case5.<lambda>:Cell variable i defined in loop +cell-var-from-loop:122:bad_case6.<lambda>:Cell variable i defined in loop |