diff options
author | Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> | 2021-08-25 18:13:59 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-25 18:13:59 (GMT) |
commit | 23384a1749359f0ae7aaae052073d44b59e715a1 (patch) | |
tree | 173d8393cd1f565c4a60fff1e6c6b83bdab8d984 | |
parent | 551da597a0996b0fb3af425f48aa5bc63ea6b963 (diff) | |
download | cpython-23384a1749359f0ae7aaae052073d44b59e715a1.zip cpython-23384a1749359f0ae7aaae052073d44b59e715a1.tar.gz cpython-23384a1749359f0ae7aaae052073d44b59e715a1.tar.bz2 |
bpo-44524: Don't modify MRO when inheriting from typing.Annotated (GH-27841)
-rw-r--r-- | Lib/test/test_typing.py | 5 | ||||
-rw-r--r-- | Lib/typing.py | 7 |
2 files changed, 11 insertions, 1 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index de4db51..847d583 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -4568,6 +4568,11 @@ class AnnotatedTests(BaseTestCase): 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 a283fe4..35c57c2 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -1575,7 +1575,7 @@ class _AnnotatedAlias(_GenericAlias, _root=True): 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 @@ class _AnnotatedAlias(_GenericAlias, _root=True): 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. |