-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
string.Formatter.parse does not handle auto-numbered positional fields #89867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
It appears when adding auto-numbered positional fields in python 3.1
This does not align with When supplying an empty string to
|
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: |
Yes it should return a string containing the index of the positional argument i.e. |
.parse is just concerned with parsing, so it works on and returns strings. .get_field takes strings because it is the thing that's trying to determine whether or not a field name looks like an integer or not. At least that's how I remember it. |
Another thing that occurred to me is the question of what |
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. Just as it's not .parse's job to give you an error if you switch back and forth between numbered and un-numbered fields. It's literally just telling you what's in the string as it breaks it apart, not assigning any further meaning to the parts. I guess I should have called it .lex, not .parse. |
That definition of
|
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. |
That is, return field_name as an int if it's an int, otherwise as a string. |
I agree with this. However, the main issue is that this is not possible for things with nesting to work. If parsing only one level, then suggested list(string.Formatter().parse('{}{}'))
[('', '0', '', None), ('', '1', '', None)] However, it would not return things correctly for: list(string.Formatter().parse('{:.{}f}{}'))
[('', '0', '.{}f', None), ('', '1', '', None)] While its equivalent of manually numbered fields would look like: fmt = '{0:.{1}f}{2}'
# And correct parse output should be:
list(string.Formatter().parse('{:.{}f}{}'))
[('', '0', '.{}f', None), ('', '2', '', None)] Auto-numbering is very easy to do outside it. Especially for a flat case. I suggest closing this as "Not planned". |
That's reasonable! Looks like all that's left is to mention this in the |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
The text was updated successfully, but these errors were encountered: