python / python/mypy

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

Offen
#8,541 3 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

needs discussion priority-1-normal
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem Reproducer und vergleiche die angezeigten Typen für super().__new__ und tuple.__new__. Untersuche die Signatur in der referenzierten typeshed-Datei builtins.pyi und verfolge anschließend, wie mypy den geerbten Aufruf von __new__ behandelt. Fertig ist die Aufgabe, wenn der gezeigte Aufruf super().__new__(cls, value) das Tupel von Gleitkommazahlen ohne den gemeldeten Typfehler akzeptiert.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.