-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
Closed
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-parsertype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
When an f-string contains an =
specifier, the characters before the equals sign are interpreted as if they were within a normal string. In particular, a backslash is always interpreted as starting an escape sequence, even when it doesn’t, which causes unexpected output.
>>> print(f'{br'\N{OX}'=}')
br'🐂'=b'\\N{OX}'
>>> print(f'{b'\N{OX}'=}')
<unknown>:1: SyntaxWarning: invalid escape sequence '\N'
b'🐂'=b'\\N{OX}'
>>> print(f'{r'\xff'=}')
r'ÿ'='\\xff'
>>> print(f'{r'\n'=}')
r'
'='\\n'
>>> print(f'{'\''=}')
'''="'"
Those results are misleading because the expressions printed before the equals signs are either syntax errors or do not match the values printed after the equals signs. I expected these results:
>>> print(f'{br'\N{OX}'=}')
br'\N{OX}'=b'\\N{OX}'
>>> print(f'{b'\N{OX}'=}')
<unknown>:1: SyntaxWarning: invalid escape sequence '\N'
b'\N{OX}'=b'\\N{OX}'
>>> print(f'{r'\xff'=}')
r'\xff'='\\xff'
>>> print(f'{r'\n'=}')
r'\n'='\\n'
>>> print(f'{'\''=}')
'\''="'"
Even when the result is not so misleading, it would still be more helpful for debugging if the output kept the expression unmodified, instead of interpreting the escape sequences. For example, this:
>>> print(f'{'\xc5'=}')
'Å'='Å'
would be better as this:
>>> print(f'{'\xc5'=}')
'\xc5'='Å'
CPython versions tested on:
3.12, 3.13
Operating systems tested on:
macOS
Linked PRs
tusharsadhwani
Metadata
Metadata
Assignees
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-parsertype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error