Skip to content

Commit

Permalink
bpo-40397: Fix subscription of nested generic alias without parameter…
Browse files Browse the repository at this point in the history
…s. (GH-20021)
  • Loading branch information
serhiy-storchaka committed May 10, 2020
1 parent 86a93fd commit 0122d48
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
13 changes: 12 additions & 1 deletion Lib/test/test_typing.py
Expand Up @@ -10,7 +10,7 @@
from typing import TypeVar, AnyStr
from typing import T, KT, VT # Not in __all__.
from typing import Union, Optional, Literal
from typing import Tuple, List, MutableMapping
from typing import Tuple, List, Dict, MutableMapping
from typing import Callable
from typing import Generic, ClassVar, Final, final, Protocol
from typing import cast, runtime_checkable
Expand Down Expand Up @@ -3173,6 +3173,17 @@ def test_frozenset(self):
def test_dict(self):
self.assertIsSubclass(dict, typing.Dict)

def test_dict_subscribe(self):
K = TypeVar('K')
V = TypeVar('V')
self.assertEqual(Dict[K, V][str, int], Dict[str, int])
self.assertEqual(Dict[K, int][str], Dict[str, int])
self.assertEqual(Dict[str, V][int], Dict[str, int])
self.assertEqual(Dict[K, List[V]][str, int], Dict[str, List[int]])
self.assertEqual(Dict[K, List[int]][str], Dict[str, List[int]])
self.assertEqual(Dict[K, list[V]][str, int], Dict[str, list[int]])
self.assertEqual(Dict[K, list[int]][str], Dict[str, list[int]])

def test_no_list_instantiation(self):
with self.assertRaises(TypeError):
typing.List()
Expand Down
6 changes: 4 additions & 2 deletions Lib/typing.py
Expand Up @@ -702,8 +702,10 @@ def __getitem__(self, params):
if isinstance(arg, TypeVar):
arg = subst[arg]
elif isinstance(arg, (_GenericAlias, GenericAlias)):
subargs = tuple(subst[x] for x in arg.__parameters__)
arg = arg[subargs]
subparams = arg.__parameters__
if subparams:
subargs = tuple(subst[x] for x in subparams)
arg = arg[subargs]
new_args.append(arg)
return self.copy_with(tuple(new_args))

Expand Down

0 comments on commit 0122d48

Please sign in to comment.