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
{{ message }}
Repository navigation
Unpacking TypeAliasType to tuple of types using * errors at runtime, Unpack doesn't #126085
I'm writing classes using variadic generic code and as a minimal example I have the below code which raises no type errors on pyright playground and is crashing at runtime which I believe is a bug in cpython. The code:
from __future__ importannotationsfromtypingimportUnpack, assert_type, TypeAlias
type FooArgT=tuple[str, str]
# deprecated but works at runtimeFooArgAliasT: TypeAlias=tuple[str, str]
classFoo[*Arg]:
pass
type FooT=Foo[str, *FooArgT]
type FooUT=Foo[str, Unpack[FooArgT]]
classBarAU(Foo[str, *FooArgAliasT]):
passclassBarU(Foo[str, Unpack[FooArgT]]):
passdeffoo_fn(t_arg: FooT, ut_arg: FooUT):
assert_type(t_arg, FooT)
assert_type(t_arg, FooUT)
assert_type(t_arg, Foo[str, Unpack[FooArgT]])
assert_type(t_arg, Foo[str, *FooArgT])
assert_type(ut_arg, FooT)
assert_type(ut_arg, FooUT)
assert_type(ut_arg, Foo[str, Unpack[FooArgT]])
assert_type(ut_arg, Foo[str, *FooArgT])
classBar(Foo[str, *FooArgT]):
pass
When I run this locally I get
Traceback (most recent call last):
File "...", line 31, in <module>
class Bar(Foo[str, *FooArgT]):
^^^^^^^^^^^^^
TypeError: Value after * must be an iterable, not typing.TypeAliasType
By my understanding, the 3 class declarations are equivalent, pyright checks pass and from my understanding of the documentation cpython should allow unpacking of the aliased TypeVarTuple.
I'm running python3.13.0 in wsl installed using pyenv, output of python -V -V
Python 3.13.0 (main, Oct 24 2024, 00:12:39) [GCC 11.4.0]
To fix this, we can add a tp_iter slot to TypeAliasType that produces Unpack[self]. Something very similar already exists for TypeVarTuple; in fact we can probably reuse the same C function. If anyone reading this is interested, please file a PR! Otherwise I'll do it myself at some point.
I reclassified this as a feature request because it's adding a new capability to TypeAliasType, which we only do in new feature versions. However, we'll be able to backport this behavior to typing-extensions for the benefit of earlier Python versions.
Reacted by Kirill Podoprigora, alwaysmpe, Alex Waygood and Carl Meyer
I'd actually recommend to use typevartuple_iter directly; it looks like it should work unchanged. However, the function should be renamed for clarity in that case.
@tomasr8 feel free to take it on, if nobody else gets to it I'll take it on at some point but it's not a blocker for my other projects as I'm using the T: TypeAlias = tuple[...] workaround so it's low on my priorities. I also finished the project that was using lots of variadics.
Activity
JelleZijlstra commentedon Oct 28, 2024
To fix this, we can add a
tp_iterslot to TypeAliasType that producesUnpack[self]. Something very similar already exists for TypeVarTuple; in fact we can probably reuse the same C function. If anyone reading this is interested, please file a PR! Otherwise I'll do it myself at some point.I reclassified this as a feature request because it's adding a new capability to TypeAliasType, which we only do in new feature versions. However, we'll be able to backport this behavior to typing-extensions for the benefit of earlier Python versions.
alwaysmpe commentedon Oct 28, 2024
I'll have a look but make zero promises as I've never touched the cpython code base before so anyone else is welcome.
Eclips4 commentedon Oct 28, 2024
FYI, you need to modify
Objects/typevarobject.cfile (add atp_iterslot to_PyTypeAlias_Type). Seetypevartuple_iteras reference implementation.JelleZijlstra commentedon Oct 28, 2024
I'd actually recommend to use
typevartuple_iterdirectly; it looks like it should work unchanged. However, the function should be renamed for clarity in that case.tomasr8 commentedon Dec 15, 2024
@alwaysmpe Are you still planning to work on this? If not, I can take a look :)
alwaysmpe commentedon Dec 15, 2024
@tomasr8 feel free to take it on, if nobody else gets to it I'll take it on at some point but it's not a blocker for my other projects as I'm using the
T: TypeAlias = tuple[...]workaround so it's low on my priorities. I also finished the project that was using lots of variadics.gh-126085: Add `tp_iter` to TypeAliasType to allow star unpacking (#1…