You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
importtypingTs=typing.TypeVarTuple("Ts")
classAccepts(typing.Generic[*Ts]):
# error: Unpack is only valid in a variadic position [valid-type]deffoo(self, x: typing.Union[*Ts]): ...
classA: ...
classB: ...
classC: ...
classTest(Accepts[A, B]): ...
Test().foo(A()) # okTest().foo(B()) # okTest().foo(C()) # should raise [arg-type] error
Mypy 1.7.1, Python 3.11
Reacted by Kazantcev Andrey, Javier Leguina and stan
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.
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.
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.
Activity
erictraut commentedon Dec 22, 2023
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 commentedon Feb 1, 2024
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 commentedon Feb 1, 2024
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 ofUnion[*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.