8000 bpo-44524: Don't modify MRO when inheriting from typing.Annotated (GH… · python/cpython@23384a1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 23384a1

Browse files
bpo-44524: Don't modify MRO when inheriting from typing.Annotated (GH-27841)
1 parent 551da59 commit 23384a1

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
@@ -4568,6 +4568,11 @@ def test_annotated_in_other_types(self):
45684568
X = List[Annotated[T, 5]]
45694569
self.assertEqual(X[int], List[Annotated[int, 5]])
45704570

4571+
def test_annotated_mro(self):
4572+
class X(Annotated[int, (1, 10)]): ...
4573+
self.assertEqual(X.__mro__, (X, int, object),
4574+
"Annotated should be transparent.")
4575+
45714576

45724577
class TypeAliasTests(BaseTestCase):
45734578
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
@@ -1575,7 +1575,7 @@ def __init__(self, origin, metadata):
15751575
if isinstance(origin, _AnnotatedAlias):
15761576
metadata = origin.__metadata__ + metadata
15771577
origin = origin.__origin__
1578-
super().__init__(origin, origin, name="Annotated")
1578+
super().__init__(origin, origin)
15791579
self.__metadata__ = metadata
15801580

15811581
def copy_with(self, params):
@@ -1603,6 +1603,11 @@ def __eq__(self, other):
16031603
def __hash__(self):
16041604
return hash((self.__origin__, self.__metadata__))
16051605

1606+
def __getattr__(self, attr):
1607+
if attr in {'__name__', '__qualname__'}:
1608+
return 'Annotated'
1609+
return super().__getattr__(attr)
1610+
16061611

16071612
class Annotated:
16081613
"""Add context specific metadata to a type.

0 commit comments

Comments
 (0)
0