From ce1923882982cb3296dc2a6cb54235e6355d1337 Mon Sep 17 00:00:00 2001 From: Fidget-Spinner <28750310+Fidget-Spinner@users.noreply.github.com> Date: Fri, 20 Aug 2021 01:47:12 +0800 Subject: [PATCH 1/4] exclude Annotated from _name temporarily --- Lib/test/test_typing.py | 7 ++++++- Lib/typing.py | 2 +- .../next/Library/2021-08-20-01-46-29.bpo-44524.TbOZD8.rst | 2 ++ 3 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2021-08-20-01-46-29.bpo-44524.TbOZD8.rst diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index d090c5f8818da1..a3eb5f8cafc144 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -4568,6 +4568,10 @@ 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.assertNotIn(Generic, X.__mro__, "Annotated should be transparent.") + class TypeAliasTests(BaseTestCase): def test_canonical_usage_with_variable_annotation(self): @@ -4908,7 +4912,8 @@ def test_special_attrs(self): typing.TypeVar: 'TypeVar', typing.Union: 'Union', # Subscribed special forms - typing.Annotated[Any, "Annotation"]: 'Annotated', + # Fixme: make Annotated work without messing up its MRO + # typing.Annotated[Any, "Annotation"]: 'Annotated', typing.ClassVar[Any]: 'ClassVar', typing.Concatenate[Any, SpecialAttrsP]: 'Concatenate', typing.Final[Any]: 'Final', diff --git a/Lib/typing.py b/Lib/typing.py index eeeb295b6c2c21..ade070e09e8275 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -1578,7 +1578,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): diff --git a/Misc/NEWS.d/next/Library/2021-08-20-01-46-29.bpo-44524.TbOZD8.rst b/Misc/NEWS.d/next/Library/2021-08-20-01-46-29.bpo-44524.TbOZD8.rst new file mode 100644 index 00000000000000..8bef3da625e094 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-08-20-01-46-29.bpo-44524.TbOZD8.rst @@ -0,0 +1,2 @@ +Temporarily remove ``__name__`` and ``__qualname__`` from subscripted +:data:`typing.Annotated` to maintain MRO. From a943fce0546669afbc04ebf8cee9a109b45cd631 Mon Sep 17 00:00:00 2001 From: Fidget-Spinner <28750310+Fidget-Spinner@users.noreply.github.com> Date: Fri, 20 Aug 2021 01:49:05 +0800 Subject: [PATCH 2/4] stricter tests --- Lib/test/test_typing.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index a3eb5f8cafc144..5af3848ccb2916 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -4570,7 +4570,8 @@ def test_annotated_in_other_types(self): def test_annotated_mro(self): class X(Annotated[int, (1, 10)]): ... - self.assertNotIn(Generic, X.__mro__, "Annotated should be transparent.") + self.assertEqual(X.__mro__, (X, int, object), + "Annotated should be transparent.") class TypeAliasTests(BaseTestCase): From 1cebc041251cf9dbd085d9cde5689152c5e022e1 Mon Sep 17 00:00:00 2001 From: Fidget-Spinner <28750310+Fidget-Spinner@users.noreply.github.com> Date: Sat, 21 Aug 2021 16:03:37 +0800 Subject: [PATCH 3/4] fix tests, remove news --- Lib/test/test_typing.py | 4 ++-- .../next/Library/2021-08-20-01-46-29.bpo-44524.TbOZD8.rst | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) delete mode 100644 Misc/NEWS.d/next/Library/2021-08-20-01-46-29.bpo-44524.TbOZD8.rst diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 857a6762a9d06f..e45c49d1d323b2 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -4913,8 +4913,8 @@ def test_special_attrs(self): typing.TypeVar: 'TypeVar', typing.Union: 'Union', # Subscribed special forms - # Fixme: make Annotated work without messing up its MRO - # typing.Annotated[Any, "Annotation"]: 'Annotated', + # Annotated is special - it returns the contained type + typing.Annotated[Any, "Annotation"]: 'Any', typing.ClassVar[Any]: 'ClassVar', typing.Concatenate[Any, SpecialAttrsP]: 'Concatenate', typing.Final[Any]: 'Final', diff --git a/Misc/NEWS.d/next/Library/2021-08-20-01-46-29.bpo-44524.TbOZD8.rst b/Misc/NEWS.d/next/Library/2021-08-20-01-46-29.bpo-44524.TbOZD8.rst deleted file mode 100644 index 8bef3da625e094..00000000000000 --- a/Misc/NEWS.d/next/Library/2021-08-20-01-46-29.bpo-44524.TbOZD8.rst +++ /dev/null @@ -1,2 +0,0 @@ -Temporarily remove ``__name__`` and ``__qualname__`` from subscripted -:data:`typing.Annotated` to maintain MRO. From ba279d2a909476618b1551355051f44dc3f7ad82 Mon Sep 17 00:00:00 2001 From: Fidget-Spinner <28750310+Fidget-Spinner@users.noreply.github.com> Date: Sat, 21 Aug 2021 16:06:56 +0800 Subject: [PATCH 4/4] __name__ for Annotated is 'Annotated' --- Lib/test/test_typing.py | 3 +-- Lib/typing.py | 5 +++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index e45c49d1d323b2..df77e82a3c67e1 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -4913,8 +4913,7 @@ def test_special_attrs(self): typing.TypeVar: 'TypeVar', typing.Union: 'Union', # Subscribed special forms - # Annotated is special - it returns the contained type - typing.Annotated[Any, "Annotation"]: 'Any', + typing.Annotated[Any, "Annotation"]: 'Annotated', typing.ClassVar[Any]: 'ClassVar', typing.Concatenate[Any, SpecialAttrsP]: 'Concatenate', typing.Final[Any]: 'Final', diff --git a/Lib/typing.py b/Lib/typing.py index b581e0448a66e2..35c57c21b37c21 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -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.