Skip to content

Commit 0122d48

Browse files
bpo-40397: Fix subscription of nested generic alias without parameters. (GH-20021)
1 parent 86a93fd commit 0122d48

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

Lib/test/test_typing.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from typing import TypeVar, AnyStr
1111
from typing import T, KT, VT # Not in __all__.
1212
from typing import Union, Optional, Literal
13-
from typing import Tuple, List, MutableMapping
13+
from typing import Tuple, List, Dict, MutableMapping
1414
from typing import Callable
1515
from typing import Generic, ClassVar, Final, final, Protocol
1616
from typing import cast, runtime_checkable
@@ -3173,6 +3173,17 @@ def test_frozenset(self):
31733173
def test_dict(self):
31743174
self.assertIsSubclass(dict, typing.Dict)
31753175

3176+
def test_dict_subscribe(self):
3177+
K = TypeVar('K')
3178+
V = TypeVar('V')
3179+
self.assertEqual(Dict[K, V][str, int], Dict[str, int])
3180+
self.assertEqual(Dict[K, int][str], Dict[str, int])
3181+
self.assertEqual(Dict[str, V][int], Dict[str, int])
3182+
self.assertEqual(Dict[K, List[V]][str, int], Dict[str, List[int]])
3183+
self.assertEqual(Dict[K, List[int]][str], Dict[str, List[int]])
3184+
self.assertEqual(Dict[K, list[V]][str, int], Dict[str, list[int]])
3185+
self.assertEqual(Dict[K, list[int]][str], Dict[str, list[int]])
3186+
31763187
def test_no_list_instantiation(self):
31773188
with self.assertRaises(TypeError):
31783189
typing.List()

Lib/typing.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,8 +702,10 @@ def __getitem__(self, params):
702702
if isinstance(arg, TypeVar):
703703
arg = subst[arg]
704704
elif isinstance(arg, (_GenericAlias, GenericAlias)):
705-
subargs = tuple(subst[x] for x in arg.__parameters__)
706-
arg = arg[subargs]
705+
subparams = arg.__parameters__
706+
if subparams:
707+
subargs = tuple(subst[x] for x in subparams)
708+
arg = arg[subargs]
707709
new_args.append(arg)
708710
return self.copy_with(tuple(new_args))
709711

0 commit comments

Comments
 (0)