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.
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 commented on Nov 3, 2021
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.
list(Formatter().parse('hello {}')) # [('hello ', '', '', None)]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
Formatter().get_field("", [1, 2, 3], {}). # raises KeyErrorericvsmith commented on 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 commented on 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_field expects a string while .get_value expects an int for positional arguments.
5 remaining items
ericvsmith commented on Nov 5, 2021
That is, return field_name as an int if it's an int, otherwise as a string.
dg-pb commented on Jan 31, 2025
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.:
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 commented on Feb 3, 2025
That's reasonable! Looks like all that's left is to mention this in the parse/get_field docs.
gh-89867: string.Formatter auto numbering doc updates (GH-129617)
pythongh-89867: string.Formatter auto numbering doc updates (pythonGH…