python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.26k stars 2.79k forks source link

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

Open jszopi opened 4 years ago

jszopi commented 4 years ago

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

JukkaL commented 4 years ago

Something weird happens with the type variables. It doesn't look specific to tuples either:

from typing import Type, TypeVar, Generic, List

T = TypeVar("T")

CT = TypeVar("CT", bound="C")
DT = TypeVar("DT", bound="D")

class C(Generic[T]):
    def __new__(cls: Type[CT], x: List[T]) -> CT:
        ...

class D(C[int]):
    def __new__(cls: Type[DT]) -> DT:
        result = C.__new__(cls, [1])  # OK
        result = super().__new__(cls, [1])  # Error
        return result

I wonder if we have some magic that can see the correspondence between CT and T in C.__new__, but it fails for super().

CarliJoy commented 2 years ago

In mypy 0.931 within Python 3.9 I can still reproduce the issue:

class MyNumbers(tuple[int, ...]):
    def __new__(cls, a: int, b: int, *c: int) -> "MyNumbers":
        iterables = a, b, *c
        return super().__new__(cls, iterables)
        # Argument 2 to "__new__" of "tuple" has incompatible type "Tuple[int, ...]"; expected "Iterable[_T_co]"
finite-state-machine commented 7 months ago

This persists in mypy 1.8.0; the reveal_type() results are changed somewhat.

[mypy-play.net]

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 [Self <: builtins.tuple[_T_co`1, ...]] (
        #       cls: Type[Self`190],
        #       typing.Iterable[_T_co`1] =
        #       ) -> Self`190
        reveal_type(tuple.__new__)
        # Revealed type is:
        #   def [_T_co, Self <: builtins.tuple[_T_co`1, ...]] (
        #       cls: Type[Self`191],
        #       typing.Iterable[_T_co`1] =
        #       ) -> Self`191

        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