python / python/mypy

Mypy should warn when comparing lists that hold a union of incomparable types

Aperta
#14,227 1 commento 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

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

  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 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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.