← 返回首页
bpo-40397: Fix subscription of nested generic alias without parameters. by serhiy-storchaka · Pull Request #20021 · python/cpython · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected Viewed files
Conversations
Failed to load comments. Retry
Loading
Jump to
Jump to file
Failed to load files. Retry
Loading
Diff view
Unified
Split
Hide whitespace
Apply and reload
Show whitespace
Diff view
Unified
Split
Hide whitespace
Apply and reload
13 changes: 12 additions & 1 deletion Lib/test/test_typing.py
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
Toggle all file notes Toggle all file annotations

Footer

© 2026 GitHub, Inc.