Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
Closed
Description
Bug report
The handling of whitespace around the argument in literal string interpolation (f-string) is unexpected when a conversion specifier (e.g. !r) is applied.
Consider
f"{None}"# OKf"{None}"# OKf"{None!r}"# OKf"{None!r}"# SyntaxError# detailsf"{None!r}# OKf"{None !r}# OKf"{None!r}# SyntaxError
PEP-498 states:
Leading and trailing whitespace in expressions is ignored
For ease of readability, leading and trailing whitespace in expressions is ignored. This is a by-product of enclosing the expression in parentheses before evaluation.
I suppose that the text above may be technically accurate, but is misleading to common folk:
In the example above,None is enclosed in parentheses(None) but notNone!r.
There are no mentions of whitespace in the rest of the PEP.
Formal reproducer:
> python3.11 -c'print(f"{ None!r }")' File"<string>", line 1 print(f"{ None!r }") ^SyntaxError: f-string: expecting'}'
Your environment
- CPython versions tested on: 3.8, 3.9, 3.10, 3.11a2, 3.11a7
- Operating system and architecture: Linux, MacOS