Skip to content

Support for Union[*TypeVarTuple] #1523

Closed
@pwuertz

Description

@pwuertz

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: featureDiscussions about new features for Python's type annotations

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions