10000 bpo-44524: Don't modify MRO when inheriting from typing.Annotated by Fidget-Spinner · Pull Request #27841 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-44524: Don't modify MRO when inheriting from typing.Annotated #27841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
7 changes: 6 additions & 1 deletion Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1575 8F08 ,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):
Expand Down Expand Up @@ -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.
Expand Down
0