You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
string.Formatter.parse does not handle auto-numbered positional fields #89867
It appears when adding auto-numbered positional fields in python 3.1 Formatter.parse was not updated to handle them and currently returns an empty string as the field name.
This does not align with Formatter.get_field which according to the docs: "Given field_name as returned by parse() (see above), convert it to an object to be formatted."
When supplying an empty string to .get_field() you get a KeyError
I guess in your example it should return:
[('hello ', '0', '', None)]
SDesch commented on Nov 3, 2021
SDeschmannequin
MannequinAuthor
More actions
Yes it should return a string containing the index of the positional argument i.e. "0" so that it is compatible with .get_field(). Side note: It's a somewhat weird that .get_field expects a string while .get_value expects an int for positional arguments.
I think your code is rational. But since string.Formatter gets such little use, I'm not sure it's worth adding this to the stdlib. On the other hand, it could be used internal to string.Formatter.
We'd need to pick a better name, though. And maybe it should return the field_name as an int.
The more I think about this, the more I think it's not .parse's job to fill in the field numbers, it's the job of whoever is calling it.
I agree with this. .parse correctly returns empty string for empty field name. This is factorised well providing one-to-one relationship between input and output.
However, the main issue is that this is not possible for things with nesting to work.
If parsing only one level, then suggested parse change would return things correctly. E.g.:
Activity
SDesch commentedon Nov 3, 2021
It appears when adding auto-numbered positional fields in python 3.1
Formatter.parsewas not updated to handle them and currently returns an empty string as the field name.This does not align with
Formatter.get_fieldwhich according to the docs: "Given field_name as returned by parse() (see above), convert it to an object to be formatted."When supplying an empty string to
.get_field()you get a KeyErrorericvsmith commentedon Nov 3, 2021
For reference, the documentation is at https://docs.python.org/3/library/string.html#custom-string-formatting
I guess in your example it should return:
[('hello ', '0', '', None)]
SDesch commentedon Nov 3, 2021
Yes it should return a string containing the index of the positional argument i.e.
"0"so that it is compatible with.get_field(). Side note: It's a somewhat weird that.get_fieldexpects a string while.get_valueexpects an int for positional arguments.4 remaining items
ericvsmith commentedon Nov 5, 2021
I think your code is rational. But since string.Formatter gets such little use, I'm not sure it's worth adding this to the stdlib. On the other hand, it could be used internal to string.Formatter.
We'd need to pick a better name, though. And maybe it should return the field_name as an int.
ericvsmith commentedon Nov 5, 2021
That is, return field_name as an int if it's an int, otherwise as a string.
dg-pb commentedon Jan 31, 2025
I agree with this.
.parsecorrectly returns empty string for empty field name. This is factorised well providing one-to-one relationship between input and output.However, the main issue is that this is not possible for things with nesting to work.
If parsing only one level, then suggested
parsechange would return things correctly. E.g.:However, it would not return things correctly for:
While its equivalent of manually numbered fields would look like:
Auto-numbering is very easy to do outside it. Especially for a flat case. I suggest closing this as "Not planned".
encukou commentedon Feb 3, 2025
That's reasonable! Looks like all that's left is to mention this in the
parse/get_fielddocs.gh-89867: string.Formatter auto numbering doc updates (GH-129617)
pythongh-89867: string.Formatter auto numbering doc updates (pythonGH…