Skip to content

empty string constant in f-string format_spec #110309

Closed
@15r10nk

Description

@15r10nk

Bug report

Bug description:

The f-string f'{1:{name}}' has a extra Constant(value='') in the format_spec part in 3.12, which is not part of the AST in 3.11. This has also an effect on the generated bytecode.

import ast import dis print(ast.dump(ast.parse("f'{1:{name}}'"),indent=2)) dis.dis(compile("f'{1:{name}}'","<string>","exec"))

output (Python 3.12.0):

Module( body=[ Expr( value=JoinedStr( values=[ FormattedValue( value=Constant(value=1), conversion=-1, format_spec=JoinedStr( values=[ FormattedValue( value=Name(id='name', ctx=Load()), conversion=-1), Constant(value='')]))]))], # <-- extra Constant type_ignores=[]) 0 0 RESUME 0 1 2 LOAD_CONST 0 (1) 4 LOAD_NAME 0 (name) 6 FORMAT_VALUE 0 8 LOAD_CONST 1 ('') # <-- extra LOAD_CONST 10 BUILD_STRING 2 # <-- extra BUILD_STRING 12 FORMAT_VALUE 4 (with format) 14 POP_TOP 16 RETURN_CONST 2 (None)

output (Python 3.11.3):

Module( body=[ Expr( value=JoinedStr( values=[ FormattedValue( value=Constant(value=1), conversion=-1, format_spec=JoinedStr( values=[ FormattedValue( value=Name(id='name', ctx=Load()), conversion=-1)]))]))], type_ignores=[]) 0 0 RESUME 0 1 2 LOAD_CONST 0 (1) 4 LOAD_NAME 0 (name) 6 FORMAT_VALUE 0 8 FORMAT_VALUE 4 (with format) 10 POP_TOP 12 LOAD_CONST 1 (None) 14 RETURN_VALUE

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Labels

3.12only security fixes3.13bugs and security fixestopic-parsertype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions