Repository: PyCQA/pyflakes

Issue:
Internal error on invalid print (py2) statement
Hi, I'd like to report a bug that occurred during using flake8. Please don't ask me about the code, I'm just analyzing data submitted by other people. Thanks!

## Packages
```
$ pip freeze
flake8==6.0.0
flake8-json==21.7.0
mccabe==0.7.0
pycodestyle==2.10.0
pyflakes==3.0.0
```

## code
```py
print *= -1
```

## Traceback
```
flake8.checker            MainProcess     85 INFO     Making checkers
flake8.checker            ForkPoolWorker-4  44861 CRITICAL Plugin pyflakes[F] raised an unexpected exception
Traceback (most recent call last):
  File "/venv/lib/python3.10/site-packages/flake8/checker.py", line 341, in run_check
    return plugin.obj(**arguments, **params)
  File "/venv/lib/python3.10/site-packages/flake8/plugins/pyflakes.py", line 96, in __init__
    super().__init__(tree, filename=filename, withDoctest=with_doctest)
  File "/venv/lib/python3.10/site-packages/pyflakes/checker.py", line 787, in __init__
    self.handleChildren(tree)
  File "/venv/lib/python3.10/site-packages/pyflakes/checker.py", line 1238, in handleChildren
    self.handleNode(node, tree)
  File "/venv/lib/python3.10/site-packages/pyflakes/checker.py", line 1283, in handleNode
    handler(node)
  File "/venv/lib/python3.10/site-packages/pyflakes/checker.py", line 2052, in AUGASSIGN
    self.handleNodeLoad(node.target)
  File "/venv/lib/python3.10/site-packages/pyflakes/checker.py", line 1096, in handleNodeLoad
    parent = self.getParent(node)
  File "/venv/lib/python3.10/site-packages/pyflakes/checker.py", line 941, in getParent
    node = node._pyflakes_parent
AttributeError: 'Name' object has no attribute '_pyflakes_parent'
flake8.main.application   MainProcess  44924 INFO     Finished running
flake8.main.application   MainProcess  44924 INFO     Reporting errors
flake8.main.application   MainProcess  44924 INFO     Found a total of 0 violations and reported 0
Flake8 failed.
offending_file.py: "pyflakes[F]" failed during execution due to AttributeError("'Name' object has no attribute '_pyflakes_parent'")
```


Discussion:

Patch:
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)
 


Test patch:
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.