Skip to content

Conversation

@achhina
Copy link
Contributor

@achhina achhina commented Apr 24, 2023

Fixes GH-95088.

@JelleZijlstra JelleZijlstra added the needs backport to 3.11 only security fixes label Apr 29, 2023
@JelleZijlstra
Copy link
Member

Hold on, this isn't actually true:

>>> "{[0a]}".format([1])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers or slices, not str
>>> "{[0a]}".format({"0a": "x"})
'x'

This is implemented in

static Py_ssize_t
. The actual implementation is equivalent to: if key.isdecimal(), treat it as an int, else treat it as a string.

That's different from what the PEP said, but it's probably a better idea, so I'd prefer to document this behavior.

@achhina
Copy link
Contributor Author

achhina commented May 6, 2023

The actual implementation is equivalent to: if key.isdecimal(), treat it as an int, else treat it as a string.

Ah, interesting! Okay updated it to be:

"A key is treated as a number if str(key).isdecimal() would evaluate to true."

@achhina achhina requested a review from JelleZijlstra May 28, 2023 23:16
@JelleZijlstra JelleZijlstra added the needs backport to 3.12 only security fixes label Jun 7, 2023
achhina and others added 2 commits June 6, 2023 20:41
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
@JelleZijlstra JelleZijlstra merged commit 3e7316d into python:main Jun 7, 2023
@miss-islington
Copy link
Contributor

Thanks @achhina for the PR, and @JelleZijlstra for merging it 🌮🎉.. I'm working now to backport this PR to: 3.11, 3.12.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

Sorry @achhina and @JelleZijlstra, I had trouble checking out the 3.12 backport branch.
Please retry by removing and re-adding the "needs backport to 3.12" label.
Alternatively, you can backport using cherry_picker on the command line.
cherry_picker 3e7316d7e8969febb56fbc7416d483b073bd1702 3.12

@bedevere-bot
Copy link

GH-105418 is a backport of this pull request to the 3.11 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.11 only security fixes label Jun 7, 2023
@JelleZijlstra JelleZijlstra removed the needs backport to 3.12 only security fixes label Jun 7, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 7, 2023
…ngs (pythonGH-103779)

(cherry picked from commit 3e7316d)

Co-authored-by: achhina <amanschhina@gmail.com>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
@JelleZijlstra JelleZijlstra added the needs backport to 3.12 only security fixes label Jun 7, 2023
@miss-islington
Copy link
Contributor

Thanks @achhina for the PR, and @JelleZijlstra for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 7, 2023
…ngs (pythonGH-103779)

(cherry picked from commit 3e7316d)

Co-authored-by: achhina <amanschhina@gmail.com>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
@bedevere-bot
Copy link

GH-105419 is a backport of this pull request to the 3.12 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.12 only security fixes label Jun 7, 2023
JelleZijlstra added a commit that referenced this pull request Jun 7, 2023
…ings (GH-103779) (#105418)

GH-95088: Clarify rules for parsing an item key for format strings (GH-103779)
(cherry picked from commit 3e7316d)

Co-authored-by: achhina <amanschhina@gmail.com>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
JelleZijlstra added a commit that referenced this pull request Jun 7, 2023
…ings (GH-103779) (#105419)

GH-95088: Clarify rules for parsing an item key for format strings (GH-103779)
(cherry picked from commit 3e7316d)

Co-authored-by: achhina <amanschhina@gmail.com>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation in the Doc dir skip news

Projects

None yet

Development

Successfully merging this pull request may close these issues.

String formatting clarification

5 participants