This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: `ast.unparse` fails on `class C: i: int`… convert broken from `ast.AnnAssign` to `ast.Assign`
Type: Stage: resolved
Components: Versions: Python 3.11, Python 3.10, Python 3.9, Python 3.8
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Crowthebird, samuelmarks
Priority: normal Keywords:

Created on 2021-12-15 02:37 by samuelmarks, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg408580 - (view) Author: Samuel Marks (samuelmarks) * Date: 2021-12-15 02:37
astor fails with:
```
  File "python3.8/site-packages/astor/code_gen.py", line 63, in to_source
    generator.visit(node)
  File "python3.8/site-packages/astor/node_util.py", line 143, in visit
    return visitor(node)
  File "python3.8/site-packages/astor/code_gen.py", line 878, in visit_Module
    self.write(*node.body)
  File "python3.8/site-packages/astor/code_gen.py", line 178, in write
    visit(item)
  File "python3.8/site-packages/astor/node_util.py", line 143, in visit
    return visitor(node)
  File "python3.8/site-packages/astor/code_gen.py", line 364, in visit_ClassDef
    self.body(node.body)
  File "python3.8/site-packages/astor/code_gen.py", line 226, in body
    self.write(*statements)
  File "python3.8/site-packages/astor/code_gen.py", line 178, in write
    visit(item)
  File "python3.8/site-packages/astor/node_util.py", line 143, in visit
    return visitor(node)
  File "python3.8/site-packages/astor/code_gen.py", line 293, in visit_Assign
    self.visit(node.value)
  File "python3.8/site-packages/astor/node_util.py", line 143, in visit
    return visitor(node)
  File "python3.8/site-packages/astor/node_util.py", line 137, in abort_visit
    raise AttributeError(msg % node.__class__.__name__)
AttributeError: No defined handler for node of type NoneType
```

Whereas the now builtin to Python (3.9+) `ast.unparse` gives:
```
File "3.10/lib/python3.10/ast.py", line 1673, in unparse
    return unparser.visit(ast_obj)
  File "3.10/lib/python3.10/ast.py", line 807, in visit
    self.traverse(node)
  File "3.10/lib/python3.10/ast.py", line 798, in traverse
    super().visit(node)
  File "3.10/lib/python3.10/ast.py", line 410, in visit
    return visitor(node)
  File "3.10/lib/python3.10/ast.py", line 822, in visit_Module
    self._write_docstring_and_traverse_body(node)
  File "3.10/lib/python3.10/ast.py", line 815, in _write_docstring_and_traverse_body
    self.traverse(node.body)
  File "3.10/lib/python3.10/ast.py", line 796, in traverse
    self.traverse(item)
  File "3.10/lib/python3.10/ast.py", line 798, in traverse
    super().visit(node)
  File "3.10/lib/python3.10/ast.py", line 410, in visit
    return visitor(node)
  File "3.10/lib/python3.10/ast.py", line 1001, in visit_ClassDef
    self._write_docstring_and_traverse_body(node)
  File "3.10/lib/python3.10/ast.py", line 815, in _write_docstring_and_traverse_body
    self.traverse(node.body)
  File "3.10/lib/python3.10/ast.py", line 796, in traverse
    self.traverse(item)
  File "3.10/lib/python3.10/ast.py", line 798, in traverse
    super().visit(node)
  File "3.10/lib/python3.10/ast.py", line 410, in visit
    return visitor(node)
  File "3.10/lib/python3.10/ast.py", line 863, in visit_Assign
    self.traverse(node.value)
  File "3.10/lib/python3.10/ast.py", line 798, in traverse
    super().visit(node)
  File "3.10/lib/python3.10/ast.py", line 410, in visit
    return visitor(node)
  File "3.10/lib/python3.10/ast.py", line 414, in generic_visit
    for field, value in iter_fields(node):
  File "3.10/lib/python3.10/ast.py", line 252, in iter_fields
    for field in node._fields:
AttributeError: 'NoneType' object has no attribute '_fields'
```

Found it when I tried to unparse previously parsed:
```py
class C:
    i: int
```

Found in reality when trying to parse then emit:
- https://github.com/tensorflow/tensorflow/blob/ba146843/tensorflow/compiler/xla/python/xla_client.py#L402-L413


Which is creating this:
```
ast.Assign(targets=[ast.Name(id="interior_padding")],
           type_comment="int",
           value=None)
```

(one thing my library does is convert betwixt `ast.Assign` and `ast.AnnAssign`… later I'll be doing more interesting variable type tracing to generate big `typing.Union` of all possible types each variable may have… but if it finds `None` then it will infer `typing.Optional`.)

Here is my `ast.NodeTransformer` override for `visit_AnnAssign`: https://github.com/offscale/cdd-python/blob/968507e/cdd/doctrans_utils.py#L111-L131

Is there some way of rewriting an `ast.AnnAssign` to `ast.Assign` without losing type information?
msg408581 - (view) Author: Jeremiah Gabriel Pascual (Crowthebird) * Date: 2021-12-15 03:00
This seems like just a problem of your library instead of a problem with Python. Are you sure this is the right place you're in?
msg408583 - (view) Author: Samuel Marks (samuelmarks) * Date: 2021-12-15 03:37
Just fixed the issue by using this value:
```py
"```(None)```" if sys.version_info[:2] >= (3, 9) else "```None```"
```
History
Date User Action Args
2022-04-11 14:59:53adminsetgithub: 90236
2021-12-15 03:37:52samuelmarkssetstatus: open -> closed

messages: + msg408583
stage: resolved
2021-12-15 03:00:40Crowthebirdsetnosy: + Crowthebird
messages: + msg408581
2021-12-15 02:37:47samuelmarkscreate