Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Update "Format String Syntax" documentation block to match actual behaviour #132736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
Loading
from

Conversation

pyctrl
Copy link

@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

python-cla-bot bot commented Apr 19, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@StanFromIreland
Copy link
Contributor

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
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
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
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
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
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
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 skip issue skip news
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

5 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.