Skip to content

Support for Union[*TypeVarTuple] #1523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pwuertz opened this issue Dec 4, 2023 · 3 comments
Closed

Support for Union[*TypeVarTuple] #1523

pwuertz opened this issue Dec 4, 2023 · 3 comments
Labels
topic: feature Discussions about new features for Python's type annotations

Comments

@pwuertz
Copy link

pwuertz commented Dec 4, 2023

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

@pwuertz pwuertz added the topic: feature Discussions about new features for Python's type annotations label Dec 4, 2023
@erictraut
Copy link
Collaborator

This is simply a missing piece of functionality in mypy. I recommend closing this issue and opening a corresponding one in the mypy issue tracker to it gets the attention of the mypy maintainers.

FWIW, the above code type checks as expected in pyright.

@matt-gardner
Copy link

Apologies if I'm missing something, but did pyright's behavior change since the above comment was written? When I click on the link to the pyright playground, I see a failure on line 7.

@erictraut
Copy link
Collaborator

Yes, pyright's behavior changed since the above comment. Unbeknownst to me, the PEP 646 authors removed the Union[*Ts] capability prior to the PEP's acceptance. I implemented pyright's PEP 646 support prior to this removal. Since the final version of the PEP removed the notion of Union[*Ts], I likewise removed support for it in pyright.

If you would like to see Union[*Ts] become part of the type spec, that would require a new PEP, or at least a public discussion in the Typing forum, a proposed modification to the spec, and approval by the Typing Council.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: feature Discussions about new features for Python's type annotations
Projects
None yet
Development

No branches or pull requests

3 participants