python / python/mypy

Seemingly erroneous incompatible type "list[int]"; expected "Iterable[Never]"

Aperta
#16,788 1 commento 12 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report
mypy seems to not like an argument of type Union[T, Iterable[T]] where T = TypeVar("T"), instead considering it to be Iterable[Never].

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.12&gist=1471ed0391c1e251edc0ecd85a28d684

from typing import List, Iterable, TypeVar, Union

T = TypeVar("T")

def coerce_list(x: Union[T, Iterable[T]]) -> List[T]:
    reveal_type(x)
    raise NotImplementedError()

def test() -> None:
    coerce_list(1)
    coerce_list([1, 2])

Expected Behavior
I expected no errors. I expected [1, 2] to match Iterable[T] with T = int.

Actual Behavior

example.py:6: note: Revealed type is "Union[T`-1, typing.Iterable[T`-1]]"
example.py:11: error: Argument 1 to "coerce_list" has incompatible type "list[int]"; expected "Iterable[Never]"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

I'm confused why the revealed type of x looks to be correct (although I don't understand the -1 part), but the expected type of the parameter is Iterable[Never].

Your Environment

  • Mypy version used: mypy 1.8.0 (compiled: yes)
  • Mypy command-line flags: (none)
  • Mypy configuration options from mypy.ini (and other config files): (none)
  • Python version used: 3.11.2 (Debian 12)

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 eseguendo la riproduzione mypy-play collegata, oppure l'example.py mostrato, con mypy 1.8.0 e la versione corrente di mypy per confrontare il comportamento. Traccia il modo in cui vengono dedotti gli argomenti generici per Union[T, Iterable[T]], quindi verifica che sia coerce_list(1) sia coerce_list([1, 2]) passino senza errori.

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

Valutazione

Stack tecnologico
python
Ambito
compilers, devtools
Tipo di issue
Bug
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.