parser: cpython parser bug in template strings t'{0!=0:}'
Open
@lysnikolaou is already working on this.
Since Mar 29, 2026.
3.14
3.15
interpreter-core
topic-parser
type-bug
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.