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: FomattedValue conversion's default value should be -1
Type: behavior Stage: resolved
Components: Parser Versions: Python 3.11, Python 3.10
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: BTaskaya, Batuhan Taskaya, iyume, lys.nikolaou, miss-islington, pablogsal
Priority: normal Keywords: patch

Created on 2022-01-07 09:40 by iyume, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 30467 merged BTaskaya, 2022-01-07 20:33
PR 30469 merged miss-islington, 2022-01-07 22:07
Messages (8)
msg409955 - (view) Author: Anh71me (iyume) Date: 2022-01-07 09:40
An unexpected behavior lookup:

```python
>>> ast.FormattedValue(ast.Str('ss')).conversion
>>> ast.unparse(ast.FormattedValue(ast.Str('ss')))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.9/ast.py", line 1564, in unparse
    return unparser.visit(ast_obj)
  File "/usr/local/lib/python3.9/ast.py", line 801, in visit
    self.traverse(node)
  File "/usr/local/lib/python3.9/ast.py", line 795, in traverse
    super().visit(node)
  File "/usr/local/lib/python3.9/ast.py", line 407, in visit
    return visitor(node)
  File "/usr/local/lib/python3.9/ast.py", line 1153, in visit_FormattedValue
    self._fstring_FormattedValue(node, self.buffer_writer)
  File "/usr/local/lib/python3.9/ast.py", line 1178, in _fstring_FormattedValue
    conversion = chr(node.conversion)
TypeError: an integer is required (got type NoneType)
>>> ast.unparse(ast.FormattedValue(ast.Str('ss'), -1))
'f"{\'ss\'}"'
```

ast.FormattedValue conversion's default value is expected to be -1 but not None

See: https://docs.python.org/3/library/ast.html#ast.FormattedValue

Other:

If certainly, it's also a bug on typeshed.
msg409982 - (view) Author: Batuhan Taskaya (BTaskaya) * (Python committer) Date: 2022-01-07 16:10
ASDL technically allows it to be None though neither compiler nor ast.unparse can work with it at this moment. 

> FormattedValue(expr value, int? conversion, expr? format_spec)
https://github.com/python/cpython/blob/b127e70a8a682fe869c22ce04c379bd85a00db67/Parser/Python.asdl#L78

>>> import ast
>>> tree = ast.parse("f'{x + 1}'")
>>> tree.body[0].value.values[0].conversion = None
>>> compile(tree, "<test>", "exec")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
SystemError: Unrecognized conversion character 0

We can either:
A) change ASDL to reflect it is an integer (since it always has been treated that way)
B) Support this on both the compiler as well as in the ast.unparse

I'd say A, since this was always broken. @pablogsal @lys.nikolaou WDYT?
msg409985 - (view) Author: Lysandros Nikolaou (lys.nikolaou) * (Python committer) Date: 2022-01-07 16:47
I agree that A is probably the way to go.
msg409986 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2022-01-07 17:01
I think A is the best option
msg410034 - (view) Author: miss-islington (miss-islington) Date: 2022-01-07 21:05
New changeset d382f7ee0b98e4ab6ade9384268f25c06be462ad by Batuhan Taskaya in branch 'main':
bpo-46289: Make conversion of FormattedValue not optional on ASDL (GH-30467)
https://github.com/python/cpython/commit/d382f7ee0b98e4ab6ade9384268f25c06be462ad
msg410042 - (view) Author: Batuhan Taskaya (Batuhan Taskaya) Date: 2022-01-07 21:46
Should we backport this?

On Sat, Jan 8, 2022, 12:05 AM miss-islington <report@bugs.python.org> wrote:

>
> miss-islington <mariatta.wijaya+miss-islington@gmail.com> added the
> comment:
>
>
> New changeset d382f7ee0b98e4ab6ade9384268f25c06be462ad by Batuhan Taskaya
> in branch 'main':
> bpo-46289: Make conversion of FormattedValue not optional on ASDL
> (GH-30467)
>
> https://github.com/python/cpython/commit/d382f7ee0b98e4ab6ade9384268f25c06be462ad
>
>
> ----------
> nosy: +miss-islington
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue46289>
> _______________________________________
>
msg410044 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2022-01-07 21:56
I would say yes, for consistency. It doesn't have any effects on user code that I am aware
msg410049 - (view) Author: miss-islington (miss-islington) Date: 2022-01-07 22:30
New changeset bea3f42bb7c360921f864949ef7472a7ecb02cd3 by Miss Islington (bot) in branch '3.10':
bpo-46289: Make conversion of FormattedValue not optional on ASDL (GH-30467)
https://github.com/python/cpython/commit/bea3f42bb7c360921f864949ef7472a7ecb02cd3
History
Date User Action Args
2022-04-11 14:59:54adminsetgithub: 90447
2022-01-07 22:53:32BTaskayasetstatus: open -> closed
stage: patch review -> resolved
versions: + Python 3.10, Python 3.11, - Python 3.9
2022-01-07 22:30:25miss-islingtonsetmessages: + msg410049
2022-01-07 22:07:03miss-islingtonsetpull_requests: + pull_request28672
2022-01-07 21:56:43pablogsalsetmessages: + msg410044
2022-01-07 21:46:42Batuhan Taskayasetnosy: + Batuhan Taskaya
messages: + msg410042
2022-01-07 21:05:36miss-islingtonsetnosy: + miss-islington
messages: + msg410034
2022-01-07 20:33:16BTaskayasetkeywords: + patch
stage: patch review
pull_requests: + pull_request28670
2022-01-07 17:01:52pablogsalsetmessages: + msg409986
2022-01-07 16:47:46lys.nikolaousetmessages: + msg409985
2022-01-07 16:10:06BTaskayasetmessages: + msg409982
2022-01-07 11:22:15pablogsalsetnosy: + BTaskaya
2022-01-07 09:40:18iyumecreate