python / python/cpython

Incorrect code collapse while prompting where the error is.

Aperta
#128,327 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

stdlib 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:

Read the following code:

# incorrect_collapse.py
def fib(number: int) -> int:
    assert number > 0
    if number == 1:
        return 1

    return fib(number - 1) + fib(number - 2)  # Notice here


print(fib(2))

There is a mistake in the code above, so if you try to run it, you will get the following traceback:

PC-Killer@ArchLinuxPC ~/p/bugs> python incorrect_collapse.py
Traceback (most recent call last):
  File "/home/PC-Killer/python_work/bugs/incorrect_collapse.py", line 9, in <module>
    print(fib(2))
          ~~~^^^
  File "/home/PC-Killer/python_work/bugs/incorrect_collapse.py", line 6, in fib
    return fib(number - 1) + fib(number - 2)  # Notice here
                             ~~~^^^^^^^^^^^^
  File "/home/PC-Killer/python_work/bugs/incorrect_collapse.py", line 2, in fib
    assert number > 0
           ^^^^^^^^^^
AssertionError

Notice that the error happened while running the code fib(number - 2).
However, if the number is 5, the duplicate code will be automatically collapsed:

# incorrect_collapse.py
def fib(number: int) -> int:
    assert number > 0
    if number == 1:
        return 1

    return fib(number - 1) + fib(number - 2)  # Notice here


print(fib(5))
PC-Killer@ArchLinuxPC ~/p/bugs [1]> python incorrect_collapse.py
Traceback (most recent call last):
  File "/home/PC-Killer/python_work/bugs/incorrect_collapse.py", line 9, in <module>
    print(fib(5))
          ~~~^^^
  File "/home/PC-Killer/python_work/bugs/incorrect_collapse.py", line 6, in fib
    return fib(number - 1) + fib(number - 2)  # Notice here
           ~~~^^^^^^^^^^^^
  File "/home/PC-Killer/python_work/bugs/incorrect_collapse.py", line 6, in fib
    return fib(number - 1) + fib(number - 2)  # Notice here
           ~~~^^^^^^^^^^^^
  File "/home/PC-Killer/python_work/bugs/incorrect_collapse.py", line 6, in fib
    return fib(number - 1) + fib(number - 2)  # Notice here
           ~~~^^^^^^^^^^^^
  [Previous line repeated 1 more time]
  File "/home/PC-Killer/python_work/bugs/incorrect_collapse.py", line 2, in fib
    assert number > 0
           ^^^^^^^^^^
AssertionError

The error happened in fib(number - 2). However, in the traceback above, I can only suppose the error happened while running fib(number - 1), which increases the time to find where the real bug is.

CPython versions tested on:

3.13, 3.14

Operating systems tested on:

Linux

Linked PRs
  • gh-129973

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 riproducendo il comportamento di collasso del traceback con i due esempi di Fibonacci dell’issue su CPython 3.13 o 3.14, quindi esamina i punti di ingresso della formattazione del traceback e del collasso dei frame. Confronta l’output collassato con il traceback non ritagliato e il PR collegato gh-129973; il lavoro è completato quando l’espressione che fallisce rimane distinguibile dopo il collasso dei frame ripetuti.

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

Valutazione

Stack tecnologico
python
Ambito
developer-experience
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
20/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.