FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.10] bpo-44524: Don't modify MRO when inheriting from typing.Annotated (GH-27841) by miss-islington · Pull Request #27950 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
5 changes: 5 additions & 0 deletions Lib/test/test_typing.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -4512,6 +4512,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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1573,7 +1573,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 @@ -1601,6 +1601,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

Back | FazBrowse Home | New Git URL