mirror of https://github.com/python/cpython
bpo-44524: Don't modify MRO when inheriting from typing.Annotated (GH-27841)
This commit is contained in:
parent
551da597a0
commit
23384a1749
|
@ -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):
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue