Incorrect inference of type parameter for a generic container

Aperta
#3,183 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

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

Direzione di ricerca

Start by running the supplied minimal reproduction through mypy and compare the two reveal_type results. Trace generic Iterable inference for B and list's constructor checking, then add regression coverage for the expected list[Y*] result and for accepting B as Iterable[Y].

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

Descrizione

bug
from typing import *

T = TypeVar('T', bound='A')

class X: ...

class Y(X): ...

class A(Iterable[X]):
    def __iter__(self) -> Iterator[X]: ...

class B(A, Iterable[Y]):
    def __iter__(self) -> Iterator[Y]: ...

b = B()
for x in b:
    reveal_type(x)  # 'Y*'

y = list(b)
reveal_type(y)  # 'builtins.list[X*]'

The first reveal_type is correct.
The second is wrong: it should be builtins.list[Y*]. Somehow the presence of A in the bases of B causes this mistake.

In addition, even a workaround doesn't work:

z = List[Y](b)  # Argument 1 to "list" has incompatible type "B"; expected Iterable[Y]
reveal_type(z)  # 'builtins.list[Y*]'

The revealed type is now correct, but the constructor doesn't pass the type check.

Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Merge medio
1g 18h
PR unite (30g)
54

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.

Altre issue di python/mypy

Tutte le issue di python/mypy

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.