python / python/cpython

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

Aberta
#146,448 0 comentários 0 reações 1 responsável Ver no GitHub

@lysnikolaou já está trabalhando nisso.

Desde 29/3/2026.

3.14 3.15 interpreter-core topic-parser type-bug
Linguagem predominante
Python
Estrelas
77.2k
Forks
35.9k
Métricas de merge de PRs
Métricas de PR pendentes

Descrição

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

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Avaliação

Esta issue ainda não foi avaliada.

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.