Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

dispatch() raises UnboundLocalError instead of NumberOfModesInvalidError for an out-of-domain mode count

Aperta Adatta ai principianti
#192 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
76/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Tranquilla
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

Inizia da tools/generate-dispatch.py e dalla sua funzione _core_dispatch_source; poi esamina il modulo dispatch generato e i test pytest che coprono dispatch(). Verifica l’errore con None e con un valore non intero e aggiungi test che mostrino che entrambi sollevano NumberOfModesInvalidError invece di UnboundLocalError. L’issue segnala una possibile sovrapposizione con la semplificazione separata del modulo dispatch, quindi controlla quel lavoro prima di iniziare.

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

Descrizione

bug

🤖 AI text below 🤖

What

The dispatch() function emitted by tools/generate-dispatch.py (_core_dispatch_source) has no
terminal case, so an argument matching none of its arms falls through to a read of an unbound local:

def dispatch(num_modes: int) -> type[_SimulatorAdapter]:
    match num_modes:
        case n if n <= 0:
            raise NumberOfModesInvalidError(errmsg)
        case 1:
            cls = MonomialPropagator001
        ...
        case 250:
            cls = MonomialPropagator250
        case n if n > 250:
            raise NumberOfModesInvalidError(errmsg)

    return cls          # <-- UnboundLocalError if nothing matched

Why this is a problem

The arms cover n <= 0, the integer literals 1..MAX, and n > MAX. Anything else — a non-integral
value, or None — reaches return cls with cls never assigned, and the user sees

UnboundLocalError: cannot access local variable 'cls' where it is not associated with a value

instead of the NumberOfModesInvalidError the module defines for exactly this purpose.

None is reachable in practice: MajoranaOperator._from_terms(..., num_modes=None)
(src/monoprop/majorana.py) leaves num_modes unset, and _init_simulator passes
majorana_operator.num_modes straight into dispatch()
(src/monoprop/monomial_propagator.py:105,121).

Suggested fix

Add a terminal arm that raises NumberOfModesInvalidError with the received value and its type.

Note this overlaps with the dispatch-module simplification (see the separate issue on collapsing the
generated _dispatch.py): that change replaces the match with an explicit guard, at which point
this becomes a plain if. If both land in the same pass, fix it there rather than twice.

Verification

pytest case asserting NumberOfModesInvalidError for dispatch(None) and for a non-integral value.


Found by a code-reading review of the repository at 29a8050. No build tree was available, so the
analysis is from source inspection and should be confirmed against a build.

Lingua principale
C++
Stelle
52
Fork
2
Merge medio
22h 23m
PR unite (30g)
36

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

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.

Altre issue di Algorithmiq/monoprop

Tutte le issue di Algorithmiq/monoprop

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.