python / python/cpython

Keyword typo suggestions differ between file and string tokenizers

Abierto
#156,047 4 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

3.14 3.15 3.16 interpreter-core topic-parser type-bug
Lenguaje dominante
Python
Estrellas
77.2k
Forks
35.9k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

Bug report

Bug description:

Keyword-typo SyntaxError suggestions depend on whether CPython processes the same source using the file tokenizer or the string tokenizer.

Create t.py containing:

a=b=c=d=e=f=g=h=i=0
def fn():
    retrun True

Running the file directly:

$ python t.py

uses the file-tokenizer path and produces the helpful diagnostic:

  File ".../t.py", line 3
    retrun True
    ^^^^^^
SyntaxError: invalid syntax. Did you mean 'return'?

Running the same file as a module:

$ python -m t

uses the string-tokenizer path and produces:

  File ".../t.py", line 3
    retrun True
           ^^^^
SyntaxError: invalid syntax

The same discrepancy is visible in the default PyREPL-based REPL.

Pasting the complete block as one input:

a=b=c=d=e=f=g=h=i=0
def fn():
    retrun True

produces the generic error:

  File "<python-input-0>", line 3
    retrun True
           ^^^^
SyntaxError: invalid syntax

Entering and submitting the assignment separately, followed by the function:

a=b=c=d=e=f=g=h=i=0
def fn():
    retrun True

produces:

  File "<python-input-2>", line 2
    retrun True
    ^^^^^^
SyntaxError: invalid syntax. Did you mean 'return'?

I would expect the same source code to produce the same keyword suggestion regardless of whether it is:

  • executed as a file with python t.py;
  • loaded as a module with python -m t;
  • pasted into PyREPL as one block; or
  • entered into PyREPL statement by statement.

This appears related to the keyword-typo suggestions added in #132449.

traceback.TracebackException._find_keyword_typos() treats the two source paths differently:

  • When the source is loaded from the filename, tokens outside the reported error line are skipped before consuming the token-search budget.
  • When the source is included directly in the SyntaxError metadata, all non-keyword NAME tokens in the extracted source fragment consume that budget.

The search is limited to ten such tokens. In this example, a through i account for nine tokens and fn is the tenth, so the string-source path stops before examining retrun. The file-source path skips those earlier lines and finds the typo.

One possible approach would be to prioritize tokens on the reported error line for both source paths, and only then search the surrounding source fragment. That would preserve the existing work limit while avoiding this tokenizer-dependent result.

CPython versions tested on:

CPython main branch, 3.15, 3.14

Operating systems tested on:

macOS

Linked PRs
  • gh-156087

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza en traceback.TracebackException._find_keyword_typos(), comparando sus rutas de filename y metadatos de SyntaxError, así como el presupuesto de diez tokens descrito aquí. Reproduce los cuatro modos de ejecución anteriores; se considera terminado cuando el mismo código fuente produce la misma sugerencia de palabra clave al ejecutar un archivo, cargar un módulo y usar ambas formas de entrada de PyREPL.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
compilers
Tipo de issue
Error
Dificultad
3/5
Tiempo estimado
1-2 días
Estado de actividad
Estancado
Claridad
Bien especificado
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.