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}" # OK f"{ None }" # OK f"{None!r}" # OK f"{ None!r }" # SyntaxError # details f"{ None!r} # OK f"{None !r} # OK f"{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 not None!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