Skip to content

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

@cyrus-

Description

@cyrus-

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"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions