Skip to content

bpo-39985: Make string.Formatter with empty field name default to 0#19065

Closed
tekknolagi wants to merge 2 commits into
python:mainfrom
tekknolagi:mb-fix-string-formatter
Closed

bpo-39985: Make string.Formatter with empty field name default to 0#19065
tekknolagi wants to merge 2 commits into
python:mainfrom
tekknolagi:mb-fix-string-formatter

Conversation

@tekknolagi

@tekknolagi tekknolagi commented Mar 19, 2020

Copy link
Copy Markdown
Contributor

This causes string.Formatter to behave like str.format in cases like
"[0]".format((1, 2, 3)), where [0] implicitly refers to the first
argument to str.format.

https://bugs.python.org/issue39985

This causes string.Formatter to behave like str.format in cases like
`"[0]".format((1, 2, 3))`, where `[0]` implicitly refers to the first
argument to `str.format`.

@ericvsmith ericvsmith left a comment

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.

I don't recall if auto-field numbering was implemented, or implemented correctly, for string.Formatter.

Comment thread Lib/test/test_string.py
fmt = string.Formatter()
lookup = ["eggs", "and", "spam"]
self.assertEqual(fmt.format("{0[2]}{0[0]}", lookup), 'spameggs')
self.assertEqual(fmt.format("{[2]}{1[0]}", lookup, (1, 2, 3)), 'spam1')

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.

This is an error with str.format:

>>> "{[2]}{1[0]}".format(["eggs", "and", "spam"], (1, 2, 3))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: cannot switch from automatic field numbering to manual field specification

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hm, okay, so I can change the test to only have 1 argument.

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.

That doesn't solve the problem. If we're going to fix this, we need to make sting.Formatter work the same as str.format. The real problem here is a lack of auto-numbering support in string.Formatter (I think).

@tekknolagi

Copy link
Copy Markdown
Contributor Author

Will update later today to take patch from https://bugs.python.org/issue27307

@csabella

Copy link
Copy Markdown
Contributor

@tekknolagi, were you still looking into updating your PR with the patch? Thanks!

@python-cla-bot

python-cla-bot Bot commented Apr 6, 2025

Copy link
Copy Markdown

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

CLA signed

@github-actions

github-actions Bot commented Apr 7, 2026

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 7, 2026
@tekknolagi

Copy link
Copy Markdown
Contributor Author

Sorry @csabella, I really don't have time these days. Please feel free to take over or close as appropriate.

@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label May 10, 2026
@serhiy-storchaka serhiy-storchaka self-requested a review June 4, 2026 13:52
@serhiy-storchaka

Copy link
Copy Markdown
Member

It seems this issue was fixed by #21767.

@tekknolagi

Copy link
Copy Markdown
Contributor Author

Neat, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants