Mypy should warn when comparing lists that hold a union of incomparable types
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Feature
As of mypy 0.991 on Python 3.11.0, mypy --strict doesn't report any problems on this code:
from typing import List, Union
a: List[Union[int, str]] = ['a']
b: List[Union[int, str]] = [1]
result = a < b
Despite mypy passing, actually running this results in TypeError: '<' not supported between instances of 'str' and 'int'. I think ideally mypy would catch this.
Pitch
While the problem was much less obvious, this affected a real project at https://github.com/certbot/certbot/issues/9481 so I think it'd be useful if mypy could help stop bugs like this from slipping through.
My knowledge of mypy and especially its internals is pretty limited, but is there a sane way to do this? Would it be possible to do something like write the annotation of list.__lt__ in the typeshed so it only accepts another list whose elements are of a type that can be compared with its own?
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia riproducendo l’esempio segnalato in Python 3.11 con mypy --strict e conferma il TypeError a runtime. Analizza come è tipizzato list.lt e se typeshed può esprimere tipi di elementi confrontabili. Il lavoro è completo quando mypy segnala il confronto non sicuro senza rifiutare confronti validi tra liste confrontabili.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100