Skip to content

Commit

Permalink
_collect_type_vars should not collect Unpack objects itself (#472)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Waygood <[email protected]>
  • Loading branch information
Daraan and AlexWaygood authored Sep 26, 2024
1 parent 832253d commit 7632716
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
- Fix bug where a subscripted `TypeAliasType` instance did not have all
attributes of the original `TypeAliasType` instance on older Python versions.
Patch by [Daraan](https://github.com/Daraan) and Alex Waygood.
- Fix bug where subscripted `TypeAliasType` instances (and some other
subscripted objects) had wrong parameters if they were directly
subscripted with an `Unpack` object.
Patch by [Daraan](https://github.com/Daraan).

# Release 4.12.2 (June 7, 2024)

Expand Down
15 changes: 15 additions & 0 deletions src/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7247,6 +7247,21 @@ def test_getitem(self):
self.assertEqual(get_args(fully_subscripted), (Iterable[float],))
self.assertIs(get_origin(fully_subscripted), ListOrSetT)

def test_unpack_parameter_collection(self):
Ts = TypeVarTuple("Ts")

class Foo(Generic[Unpack[Ts]]):
bar: Tuple[Unpack[Ts]]

FooAlias = TypeAliasType("FooAlias", Foo[Unpack[Ts]], type_params=(Ts,))
self.assertEqual(FooAlias[Unpack[Tuple[str]]].__parameters__, ())
self.assertEqual(FooAlias[Unpack[Tuple[T]]].__parameters__, (T,))

P = ParamSpec("P")
CallableP = TypeAliasType("CallableP", Callable[P, Any], type_params=(P,))
call_int_T = CallableP[Unpack[Tuple[int, T]]]
self.assertEqual(call_int_T.__parameters__, (T,))

def test_alias_attributes(self):
T = TypeVar('T')
T2 = TypeVar('T2')
Expand Down
5 changes: 4 additions & 1 deletion src/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3068,7 +3068,10 @@ def _collect_type_vars(types, typevar_types=None):
for t in types:
if _is_unpacked_typevartuple(t):
type_var_tuple_encountered = True
elif isinstance(t, typevar_types) and t not in tvars:
elif (
isinstance(t, typevar_types) and not isinstance(t, _UnpackAlias)
and t not in tvars
):
if enforce_default_ordering:
has_default = getattr(t, '__default__', NoDefault) is not NoDefault
if has_default:
Expand Down

0 comments on commit 7632716

Please sign in to comment.