A colleague and I just stumbled across an interesting problem using an f-string. Here is a minimal example:
>>> f"{ 42:x}"
'2a'
Writing a space after the hexadecimal type leads to a ValueError
:
>>> f"{ 42:x }"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Invalid format specifier
I understand the paragraph Leading and trailing whitespace in expressions is ignored in PEP 498 to mean that the space should actually be ignored.
Why does the space character lead to an error and what is the reasoning behind this?