python / python/mypy

With v1.4 module-global variables become fixed in type.

Aperta
#15,533 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

This seems to be a regression with v1.4 of mypy.

It looks to me like globals are considered constant if they’re not explicitly typed. For example (see issue https://github.com/Bogdanp/dramatiq/issues/555) a module-global variable like

global_broker = None

seems to be typed now as None. Consequently reading that variable and then asserting its value is always false and therefore raises an exception

broker = some.module.global_broker
assert broker is not None  # Previously this worked; now it's considered None and therefore raises.

and that means that the next statement

broker.enqueue(message, delay=delay)

becomes unreachable.

I therefore changed the module-global variable declaration to

global_broker: Optional["Broker"] = None

which solved the problem. However, that’s in an external module and therefore my own code is now broken after updating to v1.4 of mypy.

Would it make sense to consider the type for such a module-global variable as Any | None (or just Any) instead of just None? (Assuming that a module-global None variable will change.)

To Reproduce

See above.

Expected Behavior

As with v1.3 I would have expected mypy to pass the type check.

Actual Behavior

With v1.4 mypy now flags the statement following the assert as unreachable.

Your Environment

  • Mypy version used: mypy 1.4.1
  • Mypy configuration options from mypy.ini (and other config files):
     [tool.mypy]
     show_error_codes = true
     show_column_numbers = true
     check_untyped_defs = true
     incremental = false
     strict_equality = true
     warn_return_any = true
     warn_redundant_casts = true
     warn_unreachable = true
     warn_unused_configs = true
     warn_unused_ignores = true
     disallow_untyped_calls = true
     disallow_untyped_defs = true
     disallow_incomplete_defs = true
     disallow_untyped_decorators = true
     disable_error_code = []
     plugins = "sqlalchemy.ext.mypy.plugin"
    
  • Python version used: 3.10.12

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 reproducer minimo a livello di modulo presente nell’issue e confronta mypy 1.3 con 1.4.1 usando la configurazione elencata, in particolare warn_unreachable e strict_equality. Il lavoro è completo quando il comportamento di una variabile globale non annotata inizializzata a None è determinato e coperto da un test di regressione, incluso l’assert e la successiva chiamata al metodo.

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

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 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.