[issue44825] node.annotation is not a str in `ast`'s `class _Unparser(NodeVisitor)`

2021-08-04 Thread Batuhan Taskaya


Batuhan Taskaya  added the comment:

> Which for some reason has made the `annotation` a `str` rather than `Name`. 
> So I think it's my bug then?

Yes, from what I can see it is a bug in your code.

> body=[AnnAssign(annotation='int',

The 'annotation' field in 'AnnAssign' nodes are designated as expressions. So 
you need to either wrap that into an ast.Name() node, or an ast.Constant() node 
(if you want to stringify it with quotes in the generated code). For more 
information about fields you can read this;

https://github.com/python/cpython/blob/ac811f9b5a68ce8756911ef2c8be83b46696018f/Parser/Python.asdl#L29

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44825] node.annotation is not a str in `ast`'s `class _Unparser(NodeVisitor)`

2021-08-04 Thread Samuel Marks


Samuel Marks  added the comment:

Fixed with https://github.com/offscale/cdd-python/commit/079dc28

--
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44825] node.annotation is not a str in `ast`'s `class _Unparser(NodeVisitor)`

2021-08-04 Thread Samuel Marks


Samuel Marks  added the comment:

Hmm, debugging my test and I was able to replicate it with this smaller one:

```
from ast import FunctionDef, arguments, Load, Name, AnnAssign, Store, BinOp, 
Add, unparse
unparse(FunctionDef(args=arguments(args=[],
   defaults=[],
   kw_defaults=[],
   kwarg=None,
   kwonlyargs=[],
   posonlyargs=[],
   vararg=None),
body=[AnnAssign(annotation='int',
simple=1,
target=Name(ctx=Store(),
id='res'),
value=BinOp(left=Name(ctx=Load(),
  id='a'),
op=Add(),
right=Name(ctx=Load(),
   id='b')))],
decorator_list=[],
name='sum',
returns=None,
lineno=None,
type_comment=None))
```

Which for some reason has made the `annotation` a `str` rather than `Name`. So 
I think it's my bug then?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44825] node.annotation is not a str in `ast`'s `class _Unparser(NodeVisitor)`

2021-08-04 Thread Batuhan Taskaya


Batuhan Taskaya  added the comment:

@samuelmarks can you share the full reproducer / AST and what you expect it to 
be unparsed? (you can use gist or other pasting services if it is too big)

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44825] node.annotation is not a str in `ast`'s `class _Unparser(NodeVisitor)`

2021-08-04 Thread Batuhan Taskaya


Change by Batuhan Taskaya :


--
nosy: +BTaskaya

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44825] node.annotation is not a str in `ast`'s `class _Unparser(NodeVisitor)`

2021-08-04 Thread Samuel Marks


New submission from Samuel Marks :

I tried making `node.annotation` an `ast.Name("str", ast.Load())`, which worked 
but when the AST was unparsed to a string it shows as `# type: `.
https://github.com/offscale/cdd-python/runs/3213864077

Replicate with:
```
unparse(Assign(annotation=None, simple=1, targets=[Name("foo", Store())], 
value=Constant(value=5, kind=None), expr=None, expr_targe
   ...: t=None, expr_annotation=None, type_comment=Name('str', Load()), 
lineno=None))
```

Checking what it expects, it does expect a str. E.g.,:
```
$ python3.9 -c 'import ast; tc=ast.parse("foo = 5 # type: int", 
type_comments=True).body[0].type_comment; print("type_comment is a", 
type(tc).__name__, "with value", tc)'
type_comment is a str with value int
```

But when I do make it a str and unparse it, I get:
```
File "/opt/python3.10/lib/python3.10/ast.py", line 1674, in unparse
return unparser.visit(ast_obj)
  File "/opt/python3.10/lib/python3.10/ast.py", line 808, in visit
self.traverse(node)
  File "/opt/python3.10/lib/python3.10/ast.py", line 799, in traverse
super().visit(node)
  File "/opt/python3.10/lib/python3.10/ast.py", line 410, in visit
return visitor(node)
  File "/opt/python3.10/lib/python3.10/ast.py", line 1005, in visit_FunctionDef
self._function_helper(node, "def")
  File "/opt/python3.10/lib/python3.10/ast.py", line 1023, in _function_helper
self._write_docstring_and_traverse_body(node)
  File "/opt/python3.10/lib/python3.10/ast.py", line 816, in 
_write_docstring_and_traverse_body
self.traverse(node.body)
  File "/opt/python3.10/lib/python3.10/ast.py", line 797, in traverse
self.traverse(item)
  File "/opt/python3.10/lib/python3.10/ast.py", line 799, in traverse
super().visit(node)
  File "/opt/python3.10/lib/python3.10/ast.py", line 410, in visit
return visitor(node)
  File "/opt/python3.10/lib/python3.10/ast.py", line 879, in visit_AnnAssign
self.traverse(node.annotation)
  File "/opt/python3.10/lib/python3.10/ast.py", line 799, in traverse
super().visit(node)
  File "/opt/python3.10/lib/python3.10/ast.py", line 410, in visit
return visitor(node)
  File "/opt/python3.10/lib/python3.10/ast.py", line 414, in generic_visit
for field, value in iter_fields(node):
  File "/opt/python3.10/lib/python3.10/ast.py", line 252, in iter_fields
for field in node._fields:
AttributeError: 'str' object has no attribute '_fields'
```

--
messages: 398878
nosy: samuelmarks
priority: normal
severity: normal
status: open
title: node.annotation is not a str in `ast`'s `class _Unparser(NodeVisitor)`
versions: Python 3.10, Python 3.11, Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com