parser: cpython parser bug in template strings t'{0!=0:}'
Ouverte
@lysnikolaou y travaille déjà.
Depuis le 29/3/2026.
3.14
3.15
interpreter-core
topic-parser
type-bug
- Langage dominant
- Python
- Étoiles
- 77.2k
- Forks
- 35.9k
- Métriques de merge des PR
- Métriques de PR en attente
Description
Bug report
Bug description:
There is an bug in the parser for template strings when the expression contains a ! like in !=.
The value is parsed correct but the str attribute contains only the part before the !.
bug
>>> import ast
>>> print(ast.dump(ast.parse("t'{0!=0:}'"),indent=2))
Module(
body=[
Expr(
value=TemplateStr(
values=[
Interpolation(
value=Compare(
left=Constant(value=0),
ops=[
NotEq()],
comparators=[
Constant(value=0)]),
str='0', # <- this should be 0!=0
conversion=-1,
format_spec=JoinedStr())]))])
>>> print(ast.dump(ast.parse("t'{0!=0:s}'"),indent=2))
Module(
body=[
Expr(
value=TemplateStr(
values=[
Interpolation(
value=Compare(
left=Constant(value=0),
ops=[
NotEq()],
comparators=[
Constant(value=0)]),
str='0', # <- same here
conversion=-1,
format_spec=JoinedStr(
values=[
Constant(value='s')]))]))])
correct
>>> import ast
>>> print(ast.dump(ast.parse("t'{0!=0}'"),indent=2))
Module(
body=[
Expr(
value=TemplateStr(
values=[
Interpolation(
value=Compare(
left=Constant(value=0),
ops=[
NotEq()],
comparators=[
Constant(value=0)]),
str='0!=0',
conversion=-1)]))])
>>> print(ast.dump(ast.parse("t'{0==0:}'"),indent=2))
Module(
body=[
Expr(
value=TemplateStr(
values=[
Interpolation(
value=Compare(
left=Constant(value=0),
ops=[
Eq()],
comparators=[
Constant(value=0)]),
str='0==0',
conversion=-1,
format_spec=JoinedStr())]))])
I must also share the credit with Claude, who is currently in charge of developing pysource-codegen.
CPython versions tested on:
3.14, 3.15
Operating systems tested on:
No response
Linked PRs
- gh-146614
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Évaluation
Cette issue n'a pas encore été évaluée.