Skip to content

gh-150626: Update "Format String Syntax" documentation block to match actual behaviour#132736

Open
pyctrl wants to merge 4 commits into
python:mainfrom
pyctrl:fix-docs-to-match-actual-behavior
Open

gh-150626: Update "Format String Syntax" documentation block to match actual behaviour#132736
pyctrl wants to merge 4 commits into
python:mainfrom
pyctrl:fix-docs-to-match-actual-behavior

Conversation

@pyctrl

@pyctrl pyctrl commented Apr 19, 2025

Copy link
Copy Markdown

Hello, let me try to help a bit with docs.

Here is an string formatting example:

In [29]: "{a-b} {-}".format(**{"-": "_-_", "a-b": "ABC"})
Out[29]: 'ABC _-_'

This behaviour differs from Format String Syntax:

arg_name: [identifier | digit+]
attribute_name: identifier

So I updated arg_name and attribute_name syntax in that section to follow the actual behaviour:

  • replaced identifier with attribute_name in arg_name
  • set syntax for attribute_name as anything that doesn’t contain "." or "]"

You can find original discussion thread here


📚 Documentation preview 📚: https://cpython-previews--132736.org.readthedocs.build/en/132736/library/string.html#format-string-syntax

@python-cla-bot

python-cla-bot Bot commented Apr 19, 2025

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

@StanFromIreland

StanFromIreland commented Apr 19, 2025

Copy link
Copy Markdown
Member

See: #127833

We could update the whole snippet now. I'd ask Petr for what his plan was but he's unavailable at the moment.

@ZeroIntensity ZeroIntensity added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes skip issue labels May 18, 2025
Comment thread Doc/library/string.rst Outdated
arg_name: [`~python-grammar:identifier` | `~python-grammar:digit`+]
attribute_name: `~python-grammar:identifier`
arg_name: [`~python-grammar:digit`+ | `attribute_name`]
attribute_name: <any source character except "]" or "."> +

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any?

Are you sure that can use both N and as attribute names?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, any. 👌

It works the same way as kwargs in functions.

In [1]: def f(**kwargs):
   ...:     print(kwargs)
   ...: 

In [2]: d = {"-": 1, "N": 2, "ℕ": 3, "փ": 4, " ": 5}

In [3]: f(**d)
{'-': 1, 'N': 2, 'ℕ': 3, 'փ': 4, ' ': 5}

In [4]: "{-} {N} {ℕ} {փ} { }".format(**d)
Out[4]: '1 2 3 4 5'

image

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can even use ]:

>> class C:
...     def __init__(self, path=()): self.path = path
...     def __getitem__(self, key): return C((*self.path, key))
...     def __getattr__(self, name): return C((*self.path, name))
...     def __str__(self): return str(self.path)
...     
>>> '{].][{].\0!s}'.format_map(C())
"(']', ']', '{', '\\x00')"
>>> '{.][{].\0!s}'.format(C())
"(']', '{', '\\x00')"
Suggested change
attribute_name: <any source character except "]" or "."> +
attribute_name: <any source character except "{", "}", "[", or "."> +

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, thank you! 👍

Updated the PR. 👌

@pyctrl pyctrl force-pushed the fix-docs-to-match-actual-behavior branch from 8ce5764 to ce915de Compare May 20, 2025 08:01
@pyctrl

pyctrl commented Jun 19, 2025

Copy link
Copy Markdown
Author

Can I help here more with something?

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 23, 2026
@serhiy-storchaka serhiy-storchaka added the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label May 30, 2026
@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label May 31, 2026
@StanFromIreland StanFromIreland changed the title Update "Format String Syntax" documentation block to match actual behaviour gh-150626: Update "Format String Syntax" documentation block to match actual behaviour Jun 8, 2026
Comment thread Doc/library/string.rst Outdated
arg_name: [`~python-grammar:identifier` | `~python-grammar:digit`+]
attribute_name: `~python-grammar:identifier`
arg_name: [`~python-grammar:digit`+ | `attribute_name`]
attribute_name: <any source character except "{", "}", "[", or "."> +

@robsdedude robsdedude Jun 8, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
attribute_name: <any source character except "{", "}", "[", or "."> +
attribute_name: <any source character except "{", "}", "[", ".", "!", or ":"> +

":" and "!" are also invalid attribute names.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed 👌

Comment thread Doc/library/string.rst Outdated
field_name: `arg_name` ("." `attribute_name` | "[" `element_index` "]")*
arg_name: [`~python-grammar:identifier` | `~python-grammar:digit`+]
attribute_name: `~python-grammar:identifier`
arg_name: [`~python-grammar:digit`+ | `attribute_name`]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cf: #151064 (comment)

TL;DR: any str.isdecimal string works for indexing, not just roman decimal digits ("0"..."9") . "١" for example is also valid.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check my wording and solution with digital_number.

@pyctrl pyctrl force-pushed the fix-docs-to-match-actual-behavior branch from ce915de to 1015809 Compare June 17, 2026 23:35
@read-the-docs-community

read-the-docs-community Bot commented Jun 17, 2026

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #33277785 | 📁 Comparing ac7f8c9 against main (a8d74c0)

  🔍 Preview build  

32 files changed · ± 32 modified

± Modified

@robsdedude robsdedude left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a side-note: I (and a fair few others I've interacted with on GH) don't like force pushes on PRs once a review has started. It makes it harder for reviewers to follow what has changed. For a ±4 lines PR this isn't such a big deal, but still.

Other than that and the nit pick, I'd say this is looking good 👌

Comment thread Doc/library/string.rst Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review docs Documentation in the Doc dir needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes skip issue skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

7 participants