python / python/cpython

tokenize: `e.offset` on `IndentationError` is not set correctly

Aperta
#153,837 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

3.14 3.15 3.16 interpreter-core topic-parser type-bug
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug report

Bug description:
import io, tokenize

input_s = """
def foo():
    x = 1
   x = "some_very_long_expression"
"""
try:
    list(tokenize.generate_tokens(io.StringIO(input_s).readline))
except IndentationError as e:
    print(f"{e.offset = }")

The line x = "some_very_long_expression" is indented with 3 spaces instead of 4, so we get an error.
We would expect the offset to be around 3, since that is where the issue is.
On python 3.12+, the offset is instead 35, equal to the length of the line + 1.

This appears to be caused by the refactor of tokenize done in 3.12 for PEP 701
Potentially related to https://github.com/python/cpython/issues/84515
Funnily enough, the exact same thing seems to have happened around 3.8 https://github.com/python/cpython/issues/75676

Tested versions:

Version e.offset
3.11.15 3
3.12.13 35
3.13.13 35
3.14.4 35
3.15.0b1 35

Testing command:
nix-shell -p python311 --run "python --version && python tmp.py"
(and replace 311 by 312, 313, etc)

CPython versions tested on:

3.15

Operating systems tested on:

Linux

Linked PRs
  • gh-154281

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con tokenize.generate_tokens e riproduci l’esempio di IndentationError fornito nelle versioni di Python elencate. Traccia il modo in cui il tokenizer costruisce IndentationError e confronta l’offset con il comportamento di Python 3.11. Il lavoro è completato quando la riga con indentazione errata segnala un offset vicino a 3, con copertura di regressione per l’esempio.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.