python / python/mypy

Add "is invariant" warning to Set as well as List for situations where a Collection should be used

Aperta
#16,631 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Feature

mypy has a lovely warning about List being invariant in situations like this:

ls: List[int] = [1, 2, 3]

def process(stuff: List[Union[int, str]]):
    ...

process(ls)  # type error here, w/ nice explanation of invariance and suggestion to use Sequence

However, it does not have the same warning when Set is used in the same way:

s: Set[int] = {1, 2, 3}

def process(stuff: Set[Union[int, str]]):
    ...

process(s)  # merely states that the type is incompatible, no mention of invariance

Would be nice if the same warning mechanism could trigger here, suggesting Collection.

Pitch

Hopefully this is a simple widening of a rule that's special-cased for lists to get sets too? Not sure if other types like Dict/Tuple might be included too; I haven't tested them. If it's a lot of effort probably not worth it.

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 individuando l’avviso esistente sull’invarianza di List e i relativi test; l’issue non indica file o punti di ingresso specifici. Estendi lo stesso comportamento dell’avviso a Set e verifica che la diagnostica spieghi l’invarianza e suggerisca Collection, come mostrato nell’esempio.

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

Valutazione

Stack tecnologico
python
Ambito
tooling
Tipo di issue
Funzionalità
Difficoltà
3/5
Tempo stimato
1-2 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.