Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-40397: Fix subscription of nested generic alias without parameters. #20021

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Lib/test/test_typing.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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