[Python-checkins] bpo-44524: Don't modify MRO when inheriting from typing.Annotated (GH-27841)
serhiy-storchaka
webhook-mailer at python.org
Wed Aug 25 14:14:08 EDT 2021
https://github.com/python/cpython/commit/23384a1749359f0ae7aaae052073d44b59e715a1
commit: 23384a1749359f0ae7aaae052073d44b59e715a1
branch: main
author: Ken Jin <28750310+Fidget-Spinner at users.noreply.github.com>
committer: serhiy-storchaka <storchaka at gmail.com>
date: 2021-08-25T21:13:59+03:00
summary:
bpo-44524: Don't modify MRO when inheriting from typing.Annotated (GH-27841)
files:
M Lib/test/test_typing.py
M Lib/typing.py
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index de4db51bdc707..847d583cdafb9 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -4568,6 +4568,11 @@ def test_annotated_in_other_types(self):
X = List[Annotated[T, 5]]
self.assertEqual(X[int], List[Annotated[int, 5]])
+ def test_annotated_mro(self):
+ class X(Annotated[int, (1, 10)]): ...
+ self.assertEqual(X.__mro__, (X, int, object),
+ "Annotated should be transparent.")
+
class TypeAliasTests(BaseTestCase):
def test_canonical_usage_with_variable_annotation(self):
diff --git a/Lib/typing.py b/Lib/typing.py
index a283fe40d2d3f..35c57c21b37c2 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -1575,7 +1575,7 @@ def __init__(self, origin, metadata):
if isinstance(origin, _AnnotatedAlias):
metadata = origin.__metadata__ + metadata
origin = origin.__origin__
- super().__init__(origin, origin, name="Annotated")
+ super().__init__(origin, origin)
self.__metadata__ = metadata
def copy_with(self, params):
@@ -1603,6 +1603,11 @@ def __eq__(self, other):
def __hash__(self):
return hash((self.__origin__, self.__metadata__))
+ def __getattr__(self, attr):
+ if attr in {'__name__', '__qualname__'}:
+ return 'Annotated'
+ return super().__getattr__(attr)
+
class Annotated:
"""Add context specific metadata to a type.
More information about the Python-checkins
mailing list