Skip to content

Commit

Permalink
Use last previous assignment for comprehension-escape (#2131)
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile authored and PCManticore committed May 22, 2018
1 parent 3896ed6 commit 6b4c4d2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
7 changes: 4 additions & 3 deletions pylint/checkers/python3.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,9 +638,10 @@ def visit_name(self, node):
# On Python 3 we don't find the leaked objects as
# they are already deleted, so instead look for them manually.
scope = node.scope()
assign_names = (node_ for node_ in scope.nodes_of_class(astroid.AssignName)
if node_.name == node.name and node_.lineno < node.lineno)
assigned = next(assign_names, None)
assigned = None
for node_ in scope.nodes_of_class(astroid.AssignName):
if node_.name == node.name and node_.lineno < node.lineno:
assigned = node_
if not assigned:
return

Expand Down
9 changes: 9 additions & 0 deletions pylint/test/unittest_checker_python3.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,15 @@ def test_comprehension_escape(self):
with self.assertNoMessages():
self.checker.visit_name(node)

def test_comprehension_escape_newly_introduced(self):
node = astroid.extract_node('''
[i for i in range(3)]
for i in range(3):
i
''')
with self.assertNoMessages():
self.walk(node)

def test_exception_escape(self):
bad, good = astroid.extract_node('''
try: 1/0
Expand Down

0 comments on commit 6b4c4d2

Please sign in to comment.