From e0d7a6be89591f913beb740af348df8b7723e7a4 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Thu, 24 Nov 2022 11:51:46 -0500 Subject: fix crash on augmented-assign to print builtin (#745) --- pyflakes/checker.py | 7 +++---- pyflakes/test/test_other.py | 4 ++++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/pyflakes/checker.py b/pyflakes/checker.py index b87bc88..15b4c2b 100644 --- a/pyflakes/checker.py +++ b/pyflakes/checker.py @@ -1068,7 +1068,7 @@ class Checker: ) return handler - def handleNodeLoad(self, node): + def handleNodeLoad(self, node, parent): name = getNodeName(node) if not name: return @@ -1093,7 +1093,6 @@ class Checker: 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) @@ -1880,7 +1879,7 @@ class Checker: """ # 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 @@ -2049,7 +2048,7 @@ class Checker: 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) diff --git a/pyflakes/test/test_other.py b/pyflakes/test/test_other.py index b138cf6..ce742a5 100644 --- a/pyflakes/test/test_other.py +++ b/pyflakes/test/test_other.py @@ -2052,6 +2052,10 @@ class TestIncompatiblePrintOperator(TestCase): 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. -- cgit v1.2.1