simonpercivall / astunparse

An AST unparser for Python
Other
223 stars 53 forks source link

Format strings with complex forrmat specifiers are not unparsed correctly #16

Closed cyrus- closed 6 years ago

cyrus- commented 7 years ago

In the description of PEP 498, there is an example of a format specifier that itself contains interpolated values:

https://www.python.org/dev/peps/pep-0498/#format-specifiers

>>> width = 10
>>> precision = 4
>>> value = decimal.Decimal('12.34567')
>>> f'result: {value:{width}.{precision}}'

The unparse method doesn't seem to handle this well:

>>> astunparse.unparse(ast.parse("f'result: {value:{width}.{precision}}'"))
"\nf'''result: {value:f'''{width}.{precision}'''}'''\n"

Notice that the part after value: is not bare but rather inside a format string, which isn't valid. The correct behavior should be:

>>> astunparse.unparse(ast.parse("f'result: {value:{width}.{precision}}'"))
"\nf'''result: {value:{width}.{precision}}'''\n"
cyrus- commented 7 years ago

If this is a problem with the upstream code that astunparse is based on, I'd be happy to file the bug elsewhere -- just let me know where it should go.

nvbn commented 7 years ago

Seems like it should be fixed in #19

cyrus- commented 7 years ago

Just checked it out, doesn't seem to fix the problem in this issue...

simonpercivall commented 6 years ago

This is (should now be) fixed in v1.6.0. (Please reopen if I missed something.)