python / python/mypy

Support file-level assertions as inline configuration

Aperta
#19,013 4 commenti 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature topic-configuration
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Feature

One would be able to do:

# mypy: assert=platform == "win32" and python_version >= "3.10"

and the file would only be checked if sys.platform is win32 and the tuple sys.version_info is greater than or equal to (3, 10). This is similar to the environment markers used in dependency definitions but with only support for the two static platform variables that are already used by Mypy.

Implementation-wise, there would be a pre-processing step that transforms the Python version string (if present) into a tuple. This way you can pass those two variables directly to eval.

Pitch

This would significantly help in situations such as https://github.com/python/mypy/issues/19009. Brief recap:

When you have the following provider module:

if sys.platform == "win32":
    from my_pkg._pty.windows import PtySession
else:
    from my_pkg._pty.unix import PtySession

and attempt importing this symbol from somewhere else, Mypy does not respect the platform condition.

For example, if you call pty.openpty() inside my_pkg._pty.unix and run Mypy on Windows it will show:

Module has no attribute "openpty"  [attr-defined]

The best solution available is adding a top-level assertion as introduced here. For example, on top of the module my_pkg._pty.unix you would put assert sys.platform != "win32".

This is suboptimal because it requires modifying runtime behavior. The sys import is required, perhaps only for the assertion, and if there are imports that only exist conditionally the assertion has to be in the middle of the standard import block on top.

Adding a file-level comment is the most pragmatic option at hand, in lieu of adding an option that automatically propagates conditional static assertions to enclosed imports.

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 tracciando il modo in cui Mypy gestisce le asserzioni esistenti a livello superiore relative alla piattaforma e alla versione di Python, quindi esamina il percorso di configurazione o preprocessing in cui potrebbe essere riconosciuta una direttiva inline del file. Confronta la valutazione proposta a due variabili con il comportamento attuale della configurazione della piattaforma. Il lavoro è completato quando un’asserzione a livello di file può abilitare condizionalmente il controllo senza modificare il comportamento a runtime, con copertura per le espressioni di piattaforma e versione supportate.

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

Valutazione

Stack tecnologico
python
Ambito
compilers, devtools
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
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.