[issue39694] Incorrect dictionary unpacking when calling str.format
Akos Kiss
report at bugs.python.org
Fri Feb 21 07:03:28 EST 2020
Akos Kiss <akosthekiss at gmail.com> added the comment:
I've come up with some extra examples (use cases?):
```py
import collections
class str2(str):
def format(self, *args, **kwargs):
return super().format(*args, **kwargs)
def unpacknonekey(s):
print('input:', type(s), s)
try:
print('str key:', s.format(**{'bar': 'qux'}))
print('none key:', s.format(**{'bar': 'qux', None: ''}))
except TypeError as e:
print('error:', e)
template = 'foo {bar} baz'
unpacknonekey(template)
unpacknonekey(str2(template))
unpacknonekey(collections.UserString(template))
```
The above script gives the following output:
```
input: <class 'str'> foo {bar} baz
str key: foo qux baz
none key: foo qux baz
input: <class '__main__.str2'> foo {bar} baz
str key: foo qux baz
error: format() keywords must be strings
input: <class 'collections.UserString'> foo {bar} baz
str key: foo qux baz
error: format() keywords must be strings
```
This shows inconsistency between `format` of `str` and subclasses of `str` or the standard library's `UserString`.
----------
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39694>
_______________________________________
More information about the Python-bugs-list
mailing list