Skip to content

Commit de6a2c6

Browse files
committed
Merge remote-tracking branch 'upstream/main' into concatenate/ellipsis
2 parents 0991b42 + 2c84de1 commit de6a2c6

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
by PEP 649. Patches by Jelle Zijlstra and Alex Waygood.
88
- Copy the coroutine status of functions and methods wrapped
99
with `@typing_extensions.deprecated`. Patch by Sebastian Rittau.
10+
- Fix bug where `TypeAliasType` instances could be subscripted even
11+
where they were not generic. Patch by [Daraan](https://github.com/Daraan).
1012

1113
# Release 4.12.2 (June 7, 2024)
1214

src/test_typing_extensions.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7319,6 +7319,20 @@ def test_getitem(self):
73197319
self.assertEqual(get_args(fully_subscripted), (Iterable[float],))
73207320
self.assertIs(get_origin(fully_subscripted), ListOrSetT)
73217321

7322+
def test_subscription_without_type_params(self):
7323+
Simple = TypeAliasType("Simple", int)
7324+
with self.assertRaises(TypeError, msg="Only generic type aliases are subscriptable"):
7325+
Simple[int]
7326+
7327+
# A TypeVar in the value does not allow subscription
7328+
T = TypeVar('T')
7329+
MissingTypeParamsErr = TypeAliasType("MissingTypeParamsErr", List[T])
7330+
self.assertEqual(MissingTypeParamsErr.__type_params__, ())
7331+
self.assertEqual(MissingTypeParamsErr.__parameters__, ())
7332+
with self.assertRaises(TypeError, msg="Only generic type aliases are subscriptable"):
7333+
MissingTypeParamsErr[int]
7334+
7335+
73227336
def test_pickle(self):
73237337
global Alias
73247338
Alias = TypeAliasType("Alias", int)

src/typing_extensions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3559,6 +3559,8 @@ def __repr__(self) -> str:
35593559
return self.__name__
35603560

35613561
def __getitem__(self, parameters):
3562+
if not self.__type_params__:
3563+
raise TypeError("Only generic type aliases are subscriptable")
35623564
if not isinstance(parameters, tuple):
35633565
parameters = (parameters,)
35643566
parameters = [

0 commit comments

Comments
 (0)