This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author tekknolagi
Recipients tekknolagi
Date 2020-03-17.01:42:19
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1584409340.15.0.380295025491.issue39985@roundup.psfhosted.org>
In-reply-to
Content
As I understand it, str.format and string.Formatter are supposed to behave the
same, with string.Formatter being a pluggable variant. While poking at
string.Formatter, I noticed that they do not behave the same when formatting a
nameless subscript:

```
import string
str.format("{[0]}", "hello")  # => "h"
string.Formatter().format("{[0]}", "hello")  # => KeyError("")
```

They seem to work the same in the case where the arg is either indexed by
number or by name:

```
import string
str.format("{0[0]}", "hello")  # => "h"
string.Formatter().format("{0[0]}", "hello")  # => "h"
str.format("{a[0]}", a="hello")  # => "h"
string.Formatter().format("{a[0]}", a="hello")  # => "h"
```

After some digging, I have come up with a couple ideas:

* Change _string.formatter_field_name_split to treat an empty string field name
  as 0, so that string.Formatter.get_value looks up the arg in args, instead of
  kwargs
* Change string.Formatter.get_value to treat empty string key as 0, and look up
  the arg in args, instead of kwargs

I'm happy to submit a PR if people find one of these two solutions palatable or
have some solutions of their own.

(Note: this may appear in other versions, but I don't have them on my machine
to test.)
History
Date User Action Args
2020-03-17 01:42:20tekknolagisetrecipients: + tekknolagi
2020-03-17 01:42:20tekknolagisetmessageid: <1584409340.15.0.380295025491.issue39985@roundup.psfhosted.org>
2020-03-17 01:42:20tekknolagilinkissue39985 messages
2020-03-17 01:42:19tekknolagicreate