python / python/mypy

Declaration-order-dependent possibly-undefined false positive with nested ifs/matches, decorated any functions and NoReturn

Aperta
#17,895 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

Some obtuse code results in a false positive for the possibly-undefined error code.

To Reproduce

Run mypy --enable-error-code possibly-undefined file.py on this file:

decorator = lambda f: f


def unreachable() -> None:
    raise NotImplementedError()


def do_it(x: bool, y: None) -> None:
    if x:
        decorated()
        var = 0
    else:
        match y:
            case None:
                var = 1
            case _:
                unreachable()
    print(var)


@decorator
def decorated() -> None:
    pass

This is as small as I've gotten the reproducer. All these parts seem to be significant:

  • the outer if and an inner match
  • the decorated call being separate to the match
  • the match being a match (replacing the match with the equivalent if y is None: ... else: ... construct works fine)
  • the match having exhaustive arms with the unreachable call being unreachable (as far as mypy can tell) (e.g. deleting the case None: entirely works fine)
  • the unreachable call existing (inlining case _: raise NotImplementedError() works fine)
  • decorator being a lambda (or an class with __call__ method) (using def decorator(f): return f works fine)
  • decorated being defined after do_it in the file (changing the order works fine) (NB. it's just decorated, decorator and unreachable can be defined before or after do_it and still reproduce the behaviour)

Expected Behavior

This to pass type checking.

In particular, have same behaviour as any of the very similar ablations mentioned above, e.g. both of these work:

  • changing decorator = lambda f: f to def decorator(f): return f
  • moving decorated to be before do_it

Actual Behavior

file.py:19: error: Name "var" may be undefined  [possibly-undefined]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.11.2
  • Mypy command-line flags: --enable-error-code possibly-undefined
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.10

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 il riproduttore minimo file.py ed esegui mypy --enable-error-code possibly-undefined per confermare la diagnostica segnalata. Confronta le ablazioni elencate, inclusi la definizione del decoratore, l’ordine delle dichiarazioni e i rami match, per isolare l’interazione. Il lavoro è completato quando il riproduttore supera il controllo dei tipi senza il falso positivo.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.