Visitar URL original
bpo-39985: Make string.Formatter with empty field name default to 0 by tekknolagi · Pull Request #19065 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20260310160619/https://github.com/python/cpython/pull/19065
Skip to content
/ cpython Public

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

Open
tekknolagi wants to merge 2 commits intopython:mainfrom
tekknolagi:mb-fix-string-formatter
Open

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

Conversation

@tekknolagi
Copy link
Contributor

@tekknolagi tekknolagi commented Mar 19, 2020 •

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`.
Copy link
Member

@ericvsmith ericvsmith left a comment

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.

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
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
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
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
Contributor Author

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

@csabella
Copy link
Contributor

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

@python-cla-bot
Copy link

python-cla-bot bot commented Apr 6, 2025

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

CLA signed

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.

6 participants