python / python/cpython

Clarify that format strings accept arbitrary tokens as field names

Open
#150,626 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

docs
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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
  • gh-151064
  • gh-153143

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the shown format-string example and reading the linked format string syntax and identifier grammar documentation. Compare the documented field-name rules with the observed behavior, and consider the linked PRs as the existing work; done means the behavior and documentation agree on whether arbitrary tokens are accepted.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.