Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork34k
Closed
Description
Bug report
Bug description:
The following example shows that it is not possible to unparse a f-string with a ' in the format_spec, but such code can be generated when the f-string is double-quoted.
expected behaviour:unparse should use different quotes if quotes are part of the format_spec.
This is only a problem in 3.12 and worked in 3.11
importastcode="""f"{something:'}" """print("original code:",code)tree=ast.parse(code)print("original tree:",ast.dump(tree,indent=2))new_code=ast.unparse(tree)print("unparsed code:",new_code)ast.parse(new_code)
output (Python 3.12.0):
originalcode:f"{something:'}"originaltree:Module(body=[Expr(value=JoinedStr(values=[FormattedValue(value=Name(id='something',ctx=Load()),conversion=-1,format_spec=JoinedStr(values=[Constant(value="'")]))]))],type_ignores=[])unparsedcode:f'{something:'}'Traceback (mostrecentcalllast):File"/home/frank/projects/pysource-playground/pysource-codegen/codi.py",line13,in<module>ast.parse(new_code)File"/home/frank/.pyenv/versions/3.12.0/lib/python3.12/ast.py",line52,inparsereturncompile(source,filename,mode,flags,^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File"<unknown>",line1f'{something:'}'^SyntaxError:unterminatedstringliteral (detectedatline1)
CPython versions tested on:
3.12
Operating systems tested on:
Linux