Skip to content

Commit 06e9a35

Browse files
bpo-44524: Don't modify MRO when inheriting from typing.Annotated (GH-27841)
(cherry picked from commit 23384a1) Co-authored-by: Ken Jin <[email protected]>
1 parent ec5a031 commit 06e9a35

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

Lib/test/test_typing.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4512,6 +4512,11 @@ def test_annotated_in_other_types(self):
45124512
X = List[Annotated[T, 5]]
45134513
self.assertEqual(X[int], List[Annotated[int, 5]])
45144514

4515+
def test_annotated_mro(self):
4516+
class X(Annotated[int, (1, 10)]): ...
4517+
self.assertEqual(X.__mro__, (X, int, object),
4518+
"Annotated should be transparent.")
4519+
45154520

45164521
class TypeAliasTests(BaseTestCase):
45174522
def test_canonical_usage_with_variable_annotation(self):

Lib/typing.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1573,7 +1573,7 @@ def __init__(self, origin, metadata):
15731573
if isinstance(origin, _AnnotatedAlias):
15741574
metadata = origin.__metadata__ + metadata
15751575
origin = origin.__origin__
1576-
super().__init__(origin, origin, name="Annotated")
1576+
super().__init__(origin, origin)
15771577
self.__metadata__ = metadata
15781578

15791579
def copy_with(self, params):
@@ -1601,6 +1601,11 @@ def __eq__(self, other):
16011601
def __hash__(self):
16021602
return hash((self.__origin__, self.__metadata__))
16031603

1604+
def __getattr__(self, attr):
1605+
if attr in {'__name__', '__qualname__'}:
1606+
return 'Annotated'
1607+
return super().__getattr__(attr)
1608+
16041609

16051610
class Annotated:
16061611
"""Add context specific metadata to a type.

0 commit comments

Comments
 (0)