mypy icon indicating copy to clipboard operation
mypy copied to clipboard

(🐞) `Protocol` compatibility of `Unpack`ed `TypeVarTuple` is broken when signature is not a trivial `*args: Any`

Open KotlinIsland opened this issue 2 years ago • 0 comments

# mypy: disable-error-code=empty-body
from typing import Any, Protocol, Unpack, TypeVarTuple

Ts = TypeVarTuple("Ts")


class A1(Protocol[Unpack[Ts]]):
    def f(self, *args: Unpack[Ts]) -> None: ...

class B1:
    def f(self, x: str) -> None: ...

def f1(x: A1[Unpack[Ts]]) -> None: ...

f1(B1())

class A2(Protocol[Unpack[Ts]]):
    def f(self, z: str, *args: Unpack[Ts]) -> None: ...

class B2(A2[str]):
    def f(self, z: str, x: str) -> None: ...

class C2:
    def f(self, z: str, x: str) -> None: ...


def f2(x: A2[Unpack[Ts]]) -> None: ...

f2(B2())
f2(C2())
# error: Argument 1 to "f2" has incompatible type "C2"; expected "A2[*tuple[Never, ...]]"  [arg-type]
# note: Following member(s) of "C2" have conflicts:
# note:     Expected:
# note:         def f(self, z: str, *args: Never) -> None
# note:     Got:
# note:         def f(self, z: str, x: str) -> None

I believe this is caused by: https://github.com/python/mypy/blob/1200d1d956e589a0a33c86ef8a7cb3f5a9b64f1f/mypy/subtypes.py#L1597-L1607 @ilevkivskyi

KotlinIsland avatar Nov 26 '23 09:11 KotlinIsland