python / python/mypy

Possibly-undefined: allow assertions on the lengths of sequences

Aperta
#14,830 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

The new (disabled-by-default) possibly-undefined error code is great, and I'm using it in several of my projects. There are many hits if you enable it for typechecking mypy's own code, however. An example is this function, where mypy (correctly) points out that arg may be undefined on line 526 (arg_types might be an empty list).

https://github.com/python/mypy/blob/456dcbdd6a2f9b36249e0e7d31e521f5678a8235/mypy/suggestions.py#L509-L527

It would be nice if we could use assertions based on the truthiness of a sequence to make this error go away. E.g., it would be nice if we could silence the error here with this diff:

--- a/mypy/suggestions.py
+++ b/mypy/suggestions.py
@@ -512,6 +512,7 @@ class SuggestionEngine:
         arg_names: list[list[str | None]],
         arg_types: list[list[Type]],
     ) -> str:
+        assert arg_types
         args: list[str] = []

Mypy doesn't currently understand this idiom, however. @ilinum, how hard might this kind of thing be to implement?

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 l'esempio in mypy/suggestions.py intorno alle righe 509-527 e riproduci l'avviso possibly-undefined con l'opzione abilitata. Traccia il percorso del controllo dei tipi per l'esempio assert arg_types; il lavoro è completato quando questa asserzione stabilisce correttamente che la sequenza non è vuota e rimuove l'avviso.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.