diff options
author | cpopa <devnull@localhost> | 2014-01-14 19:45:02 +0200 |
---|---|---|
committer | cpopa <devnull@localhost> | 2014-01-14 19:45:02 +0200 |
commit | a52b89b97bd15a696e6eb00d44358ed404c352f7 (patch) | |
tree | 8713074941d18c6f6167c9cdb2d8cab71bf557fb | |
parent | 796e9d7a23c46ad6afbe0328e922ea2fe4925617 (diff) | |
download | pylint-a52b89b97bd15a696e6eb00d44358ed404c352f7.tar.gz |
Simplify the lookup for nonlocals.
-rw-r--r-- | checkers/variables.py | 32 | ||||
-rw-r--r-- | 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()
|