python / python/mypy

False positive error "incompatible type" on bound TypeVar

Aperta
#13,596 1 commento 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-inheritance topic-type-variables
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Originally posted on gitter july 29th, where it was suggested I open a bug report

Bug Report
For this code:

from typing import Generic, TypeVar, Union

ListT = TypeVar("ListT", str, float)


class TestListA(Generic[ListT]):
    def __init__(self, device: ListT) -> None:
        self._device: ListT = device


class TestListB(TestListA[ListT]):
    def __init__(self, device: ListT) -> None:
        super().__init__(device)

mypy outputs error:

test.py:13: error: Argument 1 to "__init__" of "TestListA" has incompatible type "str"; expected "ListT"  [arg-type]
test.py:13: error: Argument 1 to "__init__" of "TestListA" has incompatible type "float"; expected "ListT"  [arg-type]

which is false positive.

Please note that when using a TypeVar with bound=Union, then mypy doesn't complain.

ListT = TypeVar("ListT", bound=Union[str, float])

Your Environment

  • Mypy version used: mypy 0.971 (compiled: yes)
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: Python 3.9.12
  • Operating system and version:

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 TestVar fornito con mypy 0.971, quindi confronta il comportamento del suo TypeVar vincolato con la variante bound=Union. Individua il percorso del controllo dei tipi per TestListB.init e aggiungi una copertura di regressione per le diagnosi di falsi positivi. Il lavoro è completato quando l'esempio vincolato passa senza errori, mentre le chiamate incompatibili continuano a essere diagnosticate.

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
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.