Skip to content

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

Open
pyctrl wants to merge 1 commit 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 1 commit into
python:mainfrom
pyctrl:fix-docs-to-match-actual-behavior

Conversation

@pyctrl
Copy link
Copy Markdown

@pyctrl pyctrl commented Apr 19, 2025

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
Copy link
Copy Markdown

python-cla-bot Bot commented Apr 19, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@StanFromIreland
Copy link
Copy Markdown
Member

StanFromIreland commented Apr 19, 2025

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
Copy link
Copy Markdown
Author

pyctrl commented Jun 19, 2025

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
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
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.

Comment thread Doc/library/string.rst
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.

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