[New-bugs-announce] [issue37341] str.format and f-string divergence
Tim Hatch
report at bugs.python.org
Wed Jun 19 13:36:46 EDT 2019
New submission from Tim Hatch <tim at timhatch.com>:
TL;DR
f"{x+2}" and f"{x=}" do something sensible.
"{x+2}".format(x=1) and "{x=}".format(x=1) raise KeyError.
f"{0.1}" and "{0.1}".format(...) are different.
Having had a feature request to be able to codemod f-strings[1] and just learning about issue36817[2] last night, I went digging into how these are parsed in the standard library to be able to reuse that.
str.format, string.Formatter, and the _string module can only parse literal keys, not expressions, despite appearing to take the same syntax as f-strings. I'm happy to contribute code to change this, but unsure if it's considered a bug or feature (now that we're past feature freeze for 3.8). I would love to see these converge to prevent confusion and let us document in just one place.
The {} and {1} style are still exceptional.
We already parse expressions and the "=" suffix acceptably,
>>> list(_string.formatter_parser("{1+1}"))
[('', '1+1', '', None)]
>>> list(_string.formatter_parser("{(x)}"))
[('', '(x)', '', None)]
>>> list(_string.formatter_parser("{x=!r:1}"))
[('', 'x=', '1', 'r')]
>>> list(_string.formatter_parser("{ x = }"))
[('', ' x = ', '', None)]
But the consumers would need to check for /=\s*$/ and call eval on the items in formatter_field_name_split. (I lack a good heuristic, maybe we eval every time unless it's strictly numeric?)
It would also break unusual uses like these in the name of unification
>>> "{1+1}".format(**{"1+1": "zzz"})
'zzz'
and
>>> class T:
... pass
...
>>> setattr(T, "0", "zero")
>>> f"{T.0}"
File "<fstring>", line 1
(T.0)
^
SyntaxError: invalid syntax
>>> "{0.0}".format(T)
'zero'
[1] https://github.com/facebookincubator/Bowler/issues/87
[2] incorrectly listed in Misc/NEWS.d/3.8.0b1.rst as issue36774 btw
----------
components: Library (Lib)
messages: 346065
nosy: barry, eric.smith, larry, lisroach, lukasz.langa, serhiy.storchaka, thatch
priority: normal
severity: normal
status: open
title: str.format and f-string divergence
type: enhancement
versions: Python 3.8, Python 3.9
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37341>
_______________________________________
More information about the New-bugs-announce
mailing list