Closed
Description
I'm trying to annotate a method that only accepts a union of specific types, with the types specified via variadic generic. Although mypy supports variadic unpacking like Tuple[*Ts]
, unpacking Union[*Ts]
doesn't seem to work yet:
import typing
Ts = typing.TypeVarTuple("Ts")
class Accepts(typing.Generic[*Ts]):
# error: Unpack is only valid in a variadic position [valid-type]
def foo(self, x: typing.Union[*Ts]): ...
class A: ...
class B: ...
class C: ...
class Test(Accepts[A, B]): ...
Test().foo(A()) # ok
Test().foo(B()) # ok
Test().foo(C()) # should raise [arg-type] error
Mypy 1.7.1, Python 3.11