[issue45704] string.Formatter.parse does not handle auto-numbered positional fields
New submission from Sascha Desch : 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 KeyError ``` -- messages: 405610 nosy: SDesch priority: normal severity: normal status: open title: string.Formatter.parse does not handle auto-numbered positional fields type: behavior versions: Python 3.10, Python 3.11, Python 3.6, Python 3.7, Python 3.8, Python 3.9 ___ Python tracker <https://bugs.python.org/issue45704> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue45704] string.Formatter.parse does not handle auto-numbered positional fields
Sascha Desch added the comment: 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. -- ___ Python tracker <https://bugs.python.org/issue45704> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue45704] string.Formatter.parse does not handle auto-numbered positional fields
Sascha Desch added the comment: Another thing that occurred to me is the question of what `.parse()` should do when a mix of auto-numbered and manually numbered fields is supplied e.g. `{}{1}`. As of now `.parse()` happily processes such inputs and some other piece of code deals with this and ultimately raises an exception that mixing manual with automatic numbering is not allowed. If `.parse()` supported automatic numbering it would have to be aware of this too I guess? -- ___ Python tracker <https://bugs.python.org/issue45704> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue45704] string.Formatter.parse does not handle auto-numbered positional fields
Sascha Desch added the comment: That definition of `.parse()` definitely makes sense. Do you then think this is out of scope for `Formatter` in general or just for `.parse()`?. Just for reference, this is what I currently use to get automatic numbering to work for my use case. ``` def parse_command_template(format_string): auto_numbering_error = ValueError( 'cannot switch from automatic field numbering to manual field specification') index = 0 auto_numbering = None for literal_text, field_name, spec, conversion in Formatter().parse(format_string): if field_name is not None: if field_name.isdigit(): if auto_numbering is True: raise auto_numbering_error auto_numbering = False if field_name == '': if auto_numbering is False: raise auto_numbering_error auto_numbering = True field_name = str(index) index += 1 yield literal_text, field_name, spec, conversion ``` -- ___ Python tracker <https://bugs.python.org/issue45704> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com