[issue39694] Incorrect dictionary unpacking when calling str.format
Raymond Hettinger
report at bugs.python.org
Wed Dec 8 21:41:39 EST 2021
Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:
IMO, there is no actual problem being solved here. Instead there is just a concern that something doesn't feel right. Given that there is no problem in practice, I recommend closing this rather than cluttering docs, tests, or the C code for a non-issue.
The format() method looksup keywords on demand and it can only lookup strings. Anything not looked up is ignored. We have a long history of that working out just fine:
>>> 'The answer is %(answer)s.' % \
{'answer': 'correct', 10: 'never used'}
'The answer is correct.'
>>> 'The answer is {answer}.'.format(
**{'answer': 'correct', 10: 'never used'})
'The answer is correct.'
>>> 'The answer is {answer}.'.format_map(
{'answer': 'correct', 10: 'never used'})
'The answer is correct.
One could argue that making any of the above raise an error for a non-string in a dict is backwards incompatible and would only serve to break code that is already working fine.
I'm going to close this one. If another core dev feels strongly that this is a problem in practice, go ahead and reopen it. There was a similar change to SimpleNamespace but arguably that shouldn't have been done either, but at least it had much less risk of breaking existing code that has worked fine for over a decade.
----------
resolution: -> not a bug
stage: -> resolved
status: open -> closed
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39694>
_______________________________________
More information about the Python-bugs-list
mailing list