python / python/mypy

`super().__new__` inherited from Tuple doesn't match element type

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

Nessuno ha ancora preso questa issue.

needs discussion priority-1-normal
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Hi, when I inherit from Tuple[float, float, float], and pass a tuple of floats as an argument to super().__new__, I get an error saying that an Iterable[_T_co] was expected instead. As per the typeshed stub, _T_co is supposed to be the type of the tuple elements, so I guess it's not getting deduced correctly. Further, this works with tuple.__new__, so the involvement of super() must be to blame and the type signatures indeed aren't identical.

from typing import Tuple, Type, TypeVar

CoordsT = TypeVar('CoordsT', bound='Coords')
class Coords(Tuple[float, float, float]):
    def __new__(cls: Type[CoordsT]) -> CoordsT:

        reveal_type(super().__new__)
        # Revealed type is 'def [_T] (cls: Type[_T`-1], iterable: typing.Iterable[_T_co`1] =) -> _T`-1'
        reveal_type(tuple.__new__)
        # Revealed type is 'def [_T_co, _T] (cls: Type[_T`-1], iterable: typing.Iterable[_T_co`1] =) -> _T`-1'

        value = (0.0, 0.0, 0.0)
        result = tuple.__new__(cls, value)
        # fine
        super().__new__(cls, value)
        # error: Argument 2 to "__new__" of "tuple" has incompatible type "Tuple[float, float, float]"; expected "Iterable[_T_co]"

        return result

I'm not sure if it's mypy or typeshed that is responsible for the function signature of super's __new__, but I didn't find an overload for it in typeshed, so I suspect this is mypy's remit

Edit: mypy 0.770, Python 3.7.3

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 con il riproduttore e confronta i tipi rivelati per super().__new__ e tuple.__new__. Esamina la firma builtins.pyi di typeshed a cui si fa riferimento, quindi segui il modo in cui mypy gestisce la chiamata ereditata a __new__. Il lavoro è completato quando la chiamata mostrata super().__new__(cls, value) accetta la tupla di numeri in virgola mobile senza l’errore di tipo segnalato.

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

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
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.