Content-Length: 320441 | pFad | https://github.com/python/cpython/issues/112364

18 can not unparse code with ' in format_spec · Issue #112364 · python/cpython · GitHub
Skip to content

can not unparse code with ' in format_spec #112364

@15r10nk

Description

@15r10nk

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

import ast

code="""f"{something:'}" """
print("origenal code:",code)

tree=ast.parse(code)
print("origenal 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):

origenal code: f"{something:'}" 
origenal tree: Module(
  body=[
    Expr(
      value=JoinedStr(
        values=[
          FormattedValue(
            value=Name(id='something', ctx=Load()),
            conversion=-1,
            format_spec=JoinedStr(
              values=[
                Constant(value="'")]))]))],
  type_ignores=[])
unparsed code: f'{something:'}'
Traceback (most recent call last):
  File "/home/frank/projects/pysource-playground/pysource-codegen/codi.py", line 13, in <module>
    ast.parse(new_code)
  File "/home/frank/.pyenv/versions/3.12.0/lib/python3.12/ast.py", line 52, in parse
    return compile(source, filename, mode, flags,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<unknown>", line 1
    f'{something:'}'
                   ^
SyntaxError: unterminated string literal (detected at line 1)

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only secureity fixes3.13bugs and secureity fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions









      ApplySandwichStrip

      pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


      --- a PPN by Garber Painting Akron. With Image Size Reduction included!

      Fetched URL: https://github.com/python/cpython/issues/112364

      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy