Skip to content

Commit

Permalink
fix crash on augmented-assign to print builtin (#745)
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile committed Nov 24, 2022
1 parent 2c64ae4 commit e0d7a6b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
7 changes: 3 additions & 4 deletions pyflakes/checker.py
Expand Up @@ -1068,7 +1068,7 @@ def getNodeHandler(self, node_class):
)
return handler

def handleNodeLoad(self, node):
def handleNodeLoad(self, node, parent):
name = getNodeName(node)
if not name:
return
Expand All @@ -1093,7 +1093,6 @@ def handleNodeLoad(self, node):
continue

if name == 'print' and isinstance(binding, Builtin):
parent = self.getParent(node)
if (isinstance(parent, ast.BinOp) and
isinstance(parent.op, ast.RShift)):
self.report(messages.InvalidPrintSyntax, node)
Expand Down Expand Up @@ -1880,7 +1879,7 @@ def NAME(self, node):
"""
# Locate the name in locals / function / globals scopes.
if isinstance(node.ctx, ast.Load):
self.handleNodeLoad(node)
self.handleNodeLoad(node, self.getParent(node))
if (node.id == 'locals' and isinstance(self.scope, FunctionScope) and
isinstance(node._pyflakes_parent, ast.Call)):
# we are doing locals() call in current scope
Expand Down Expand Up @@ -2049,7 +2048,7 @@ def CLASSDEF(self, node):
self.addBinding(node, ClassDefinition(node.name, node))

def AUGASSIGN(self, node):
self.handleNodeLoad(node.target)
self.handleNodeLoad(node.target, node)
self.handleNode(node.value, node)
self.handleNode(node.target, node)

Expand Down
4 changes: 4 additions & 0 deletions pyflakes/test/test_other.py
Expand Up @@ -2052,6 +2052,10 @@ def test_invalid_print_when_imported_from_future(self):
self.assertEqual(exc.lineno, 4)
self.assertEqual(exc.col, 0)

def test_print_augmented_assign(self):
# nonsense, but shouldn't crash pyflakes
self.flakes('print += 1')

def test_print_function_assignment(self):
"""
A valid assignment, tested for catching false positives.
Expand Down

0 comments on commit e0d7a6b

Please sign in to comment.