[issue45704] string.Formatter.parse does not handle auto-numbered positional fields

2021-11-05 Thread Eric V. Smith


Eric V. Smith  added the comment:

That is, return field_name as an int if it's an int, otherwise as a string.

--

___
Python tracker 

___
___
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

2021-11-05 Thread Eric V. Smith


Eric V. Smith  added the comment:

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.

--

___
Python tracker 

___
___
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

2021-11-05 Thread Sascha Desch


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 

___
___
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

2021-11-04 Thread Eric V. Smith


Eric V. Smith  added the comment:

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.

--

___
Python tracker 

___
___
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

2021-11-03 Thread Sascha Desch


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 

___
___
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

2021-11-03 Thread Eric V. Smith


Eric V. Smith  added the comment:

> Side note: It's a somewhat weird that `.get_field` expects a string while 
> `.get_value` expects an int for positional arguments.

.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.

--

___
Python tracker 

___
___
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

2021-11-03 Thread Sascha Desch


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 

___
___
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

2021-11-03 Thread Eric V. Smith


Eric V. Smith  added the comment:

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)]

--
nosy: +eric.smith

___
Python tracker 

___
___
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

2021-11-03 Thread Sascha Desch


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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com