From a52b89b97bd15a696e6eb00d44358ed404c352f7 Mon Sep 17 00:00:00 2001 From: cpopa Date: Tue, 14 Jan 2014 19:45:02 +0200 Subject: Simplify the lookup for nonlocals. --- checkers/variables.py | 32 +++++++------------------- test/input/func_used_before_assignment_py30.py | 8 ------- 2 files changed, 8 insertions(+), 32 deletions(-) diff --git a/checkers/variables.py b/checkers/variables.py index 4ad82c4..6ab6927 100644 --- a/checkers/variables.py +++ b/checkers/variables.py @@ -508,30 +508,14 @@ builtins. Remember that you should avoid to define new builtins when possible.' if defframe.root().lookup(name)[1]: maybee0601 = False else: - # check if we have a nonlocal - frame = node.frame() - frame_parent = frame.parent - if (isinstance(frame, astroid.Function) and - isinstance(frame_parent, astroid.Function)): - # detect that the name exists - # in the upper level - defined_higher = False - for assign in frame_parent.get_children(): - if not isinstance(assign, astroid.Assign): - continue - for target in assign.targets: - if (isinstance(target, astroid.AssName) and - target.name == name): - defined_higher = True - break - if defined_higher: - break - for child in frame.get_children(): - if not isinstance(child, astroid.Nonlocal): - continue - if name in child.names and defined_higher: - maybee0601 = False - break + # check if we have a nonlocal + if name in defframe.locals: + for child in defframe.get_children(): + if not isinstance(child, astroid.Nonlocal): + continue + if name in child.names: + maybee0601 = False + break if (maybee0601 and stmt.fromlineno <= defstmt.fromlineno and not is_defined_before(node) diff --git a/test/input/func_used_before_assignment_py30.py b/test/input/func_used_before_assignment_py30.py index 04987f3..3765d6b 100644 --- a/test/input/func_used_before_assignment_py30.py +++ b/test/input/func_used_before_assignment_py30.py @@ -17,11 +17,3 @@ def test_fail(): def wrap(): cnt = cnt + 1 wrap() - -def test2_fail(): - """ uses nonlocal, but without an - outer label defined. """ - def wrap(): - nonlocal cnt - cnt = cnt + 1 - wrap() -- cgit v1.2.1