Skip to content

Clarify that format strings accept arbitrary tokens as field names #150626

Description

@lgalfaso

Bug report

Bug description:

Should the code below produce an error?

'{(}'.format(**{'(': 2})

This produces '2' in Python 3.14.5 on MacOS.

Reading
https://docs.python.org/3/library/string.html#format-string-syntax

replacement_field: "{" [field_name] ["!" conversion] [":" format_spec] "}"
field_name:        arg_name ("." attribute_name | "[" element_index "]")*
arg_name:          [identifier | digit+]

this ends up pointing to identifier at https://docs.python.org/3/reference/lexical_analysis.html#grammar-token-python-grammar-identifier

NAME:          name_start name_continue*
name_start:    "a"..."z" | "A"..."Z" | "_" | <non-ASCII character>
name_continue: name_start | "0"..."9"
identifier:    <NAME, except keywords>

Given that '(' is an Ascii character, then ( is not a valid identifier and it should not be allowed.

CPython versions tested on:

3.14

Operating systems tested on:

macOS

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    Status
    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions