python / python/cpython

parser: cpython parser bug in template strings t'{0!=0:}'

オープン
#146,448 コメント 0 件 リアクション 0 件 担当者 1 名 GitHub で見る

@lysnikolaou がすでに取り組んでいます。

2026年3月29日 から。

3.14 3.15 interpreter-core topic-parser type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

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.

Image
CPython versions tested on:

3.14, 3.15

Operating systems tested on:

No response

Linked PRs
  • gh-146614

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。