From 1b91e829b2198530d1f022edc0e56586d8ff3bc8 Mon Sep 17 00:00:00 2001 From: Yurii Karabas <1998uriyyo@gmail.com> Date: Mon, 19 Jul 2021 14:05:32 +0300 Subject: [PATCH 1/6] Add missed __name__ and __qualname__ to typing module objects --- Lib/test/test_typing.py | 48 +++++++++++++++++++ Lib/typing.py | 3 ++ .../2021-07-19-14-04-42.bpo-44524.Nbf2JC.rst | 2 + 3 files changed, 53 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2021-07-19-14-04-42.bpo-44524.Nbf2JC.rst diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index da6775eb486341..eaebe196e04e68 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -4498,6 +4498,54 @@ def test_no_isinstance(self): issubclass(int, TypeGuard) +class SpecialAttrsTests(BaseTestCase): + def test_special_attrs(self): + cls_to_check = ( + typing.AbstractSet, + typing.AsyncContextManager, + typing.AsyncGenerator, + typing.AsyncIterable, + typing.AsyncIterator, + typing.Awaitable, + typing.ByteString, + typing.Callable, + typing.ChainMap, + typing.Collection, + typing.Container, + typing.ContextManager, + typing.Coroutine, + typing.Counter, + typing.DefaultDict, + typing.Deque, + typing.Dict, + typing.FrozenSet, + typing.Generator, + typing.Hashable, + typing.ItemsView, + typing.Iterable, + typing.Iterator, + typing.KeysView, + typing.List, + typing.Mapping, + typing.MappingView, + typing.MutableMapping, + typing.MutableSequence, + typing.MutableSet, + typing.OrderedDict, + typing.Reversible, + typing.Sequence, + typing.Set, + typing.Sized, + typing.Tuple, + typing.Type, + typing.ValuesView, + ) + + for cls in cls_to_check: + self.assertEqual(cls.__name__, cls._name) + self.assertEqual(cls.__qualname__, cls._name) + self.assertEqual(cls.__module__, 'typing') + class AllTests(BaseTestCase): """Tests for __all__.""" diff --git a/Lib/typing.py b/Lib/typing.py index 59f3ca39c1edd0..1d93d2ab672f4d 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -935,6 +935,9 @@ def __mro_entries__(self, bases): return tuple(res) def __getattr__(self, attr): + if attr in {'__name__', '__qualname__'}: + return self._name + # We are careful for copy and pickle. # Also for simplicity we just don't relay all dunder names if '__origin__' in self.__dict__ and not _is_dunder(attr): diff --git a/Misc/NEWS.d/next/Library/2021-07-19-14-04-42.bpo-44524.Nbf2JC.rst b/Misc/NEWS.d/next/Library/2021-07-19-14-04-42.bpo-44524.Nbf2JC.rst new file mode 100644 index 00000000000000..32e429e56e05fc --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-07-19-14-04-42.bpo-44524.Nbf2JC.rst @@ -0,0 +1,2 @@ +Add missed ``__name__`` and ``__qualname__`` attribute to ``typing`` module +objects. Patch provided by Yurii Karabas. From d683ee3a0bb32ea704d5882fcecc805b93b1b6ec Mon Sep 17 00:00:00 2001 From: Yurii Karabas <1998uriyyo@gmail.com> Date: Mon, 19 Jul 2021 15:58:39 +0300 Subject: [PATCH 2/6] Include typing prefix for __qualname__ attr --- Lib/test/test_typing.py | 2 +- Lib/typing.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index eaebe196e04e68..f781464f4d9615 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -4543,7 +4543,7 @@ def test_special_attrs(self): for cls in cls_to_check: self.assertEqual(cls.__name__, cls._name) - self.assertEqual(cls.__qualname__, cls._name) + self.assertEqual(cls.__qualname__, f'typing.{cls._name}') self.assertEqual(cls.__module__, 'typing') class AllTests(BaseTestCase): diff --git a/Lib/typing.py b/Lib/typing.py index 1d93d2ab672f4d..88eddf1690ead5 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -935,8 +935,10 @@ def __mro_entries__(self, bases): return tuple(res) def __getattr__(self, attr): - if attr in {'__name__', '__qualname__'}: + if attr == '__name__': return self._name + if attr == '__qualname__': + return f'typing.{self._name}' # We are careful for copy and pickle. # Also for simplicity we just don't relay all dunder names From 5440355569a1df5af4cb38bac12a062e8c4b47c4 Mon Sep 17 00:00:00 2001 From: Yurii Karabas <1998uriyyo@gmail.com> Date: Mon, 19 Jul 2021 16:16:04 +0300 Subject: [PATCH 3/6] Revert "Include typing prefix for __qualname__ attr" This reverts commit d683ee3a0bb32ea704d5882fcecc805b93b1b6ec. --- Lib/test/test_typing.py | 2 +- Lib/typing.py | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index f781464f4d9615..eaebe196e04e68 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -4543,7 +4543,7 @@ def test_special_attrs(self): for cls in cls_to_check: self.assertEqual(cls.__name__, cls._name) - self.assertEqual(cls.__qualname__, f'typing.{cls._name}') + self.assertEqual(cls.__qualname__, cls._name) self.assertEqual(cls.__module__, 'typing') class AllTests(BaseTestCase): diff --git a/Lib/typing.py b/Lib/typing.py index 88eddf1690ead5..1d93d2ab672f4d 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -935,10 +935,8 @@ def __mro_entries__(self, bases): return tuple(res) def __getattr__(self, attr): - if attr == '__name__': + if attr in {'__name__', '__qualname__'}: return self._name - if attr == '__qualname__': - return f'typing.{self._name}' # We are careful for copy and pickle. # Also for simplicity we just don't relay all dunder names From 6872f961950236a3681479fdd8ce0fb583b3291a Mon Sep 17 00:00:00 2001 From: Yurii Karabas <1998uriyyo@gmail.com> Date: Mon, 19 Jul 2021 19:17:11 +0300 Subject: [PATCH 4/6] Add __name__ and __qualname__ to _SpecialForm --- Lib/test/test_typing.py | 12 ++++++++++++ Lib/typing.py | 6 ++++++ 2 files changed, 18 insertions(+) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index eaebe196e04e68..379b67ebe5abcc 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -4501,6 +4501,7 @@ def test_no_isinstance(self): class SpecialAttrsTests(BaseTestCase): def test_special_attrs(self): cls_to_check = ( + # ABC classes typing.AbstractSet, typing.AsyncContextManager, typing.AsyncGenerator, @@ -4539,6 +4540,17 @@ def test_special_attrs(self): typing.Tuple, typing.Type, typing.ValuesView, + # Special Forms + typing.Any, + typing.NoReturn, + typing.ClassVar, + typing.Final, + typing.Union, + typing.Optional, + typing.Literal, + typing.TypeAlias, + typing.Concatenate, + typing.TypeGuard, ) for cls in cls_to_check: diff --git a/Lib/typing.py b/Lib/typing.py index 1d93d2ab672f4d..4a6efee802042b 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -358,6 +358,12 @@ def __init__(self, getitem): self._name = getitem.__name__ self.__doc__ = getitem.__doc__ + def __getattr__(self, item): + if item in {'__name__', '__qualname__'}: + return self._name + + raise AttributeError(item) + def __mro_entries__(self, bases): raise TypeError(f"Cannot subclass {self!r}") From 583acb0494ce2f759208090377fbde3ca59f3abe Mon Sep 17 00:00:00 2001 From: Yurii Karabas <1998uriyyo@gmail.com> Date: Mon, 19 Jul 2021 19:38:26 +0300 Subject: [PATCH 5/6] Update Misc/NEWS.d/next/Library/2021-07-19-14-04-42.bpo-44524.Nbf2JC.rst Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> --- .../next/Library/2021-07-19-14-04-42.bpo-44524.Nbf2JC.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2021-07-19-14-04-42.bpo-44524.Nbf2JC.rst b/Misc/NEWS.d/next/Library/2021-07-19-14-04-42.bpo-44524.Nbf2JC.rst index 32e429e56e05fc..0acdc7dff029f0 100644 --- a/Misc/NEWS.d/next/Library/2021-07-19-14-04-42.bpo-44524.Nbf2JC.rst +++ b/Misc/NEWS.d/next/Library/2021-07-19-14-04-42.bpo-44524.Nbf2JC.rst @@ -1,2 +1,2 @@ -Add missed ``__name__`` and ``__qualname__`` attribute to ``typing`` module -objects. Patch provided by Yurii Karabas. +Add missing ``__name__`` and ``__qualname__`` attributes to ``typing`` module +classes. Patch provided by Yurii Karabas. From eb9f4ef4d9856a17e0cb8533ab8162f8294ca29b Mon Sep 17 00:00:00 2001 From: Yurii Karabas <1998uriyyo@gmail.com> Date: Mon, 19 Jul 2021 19:40:43 +0300 Subject: [PATCH 6/6] Use subTest for test_special_attrs --- Lib/test/test_typing.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 379b67ebe5abcc..b696447d0982b7 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -4554,9 +4554,10 @@ def test_special_attrs(self): ) for cls in cls_to_check: - self.assertEqual(cls.__name__, cls._name) - self.assertEqual(cls.__qualname__, cls._name) - self.assertEqual(cls.__module__, 'typing') + with self.subTest(cls=cls): + self.assertEqual(cls.__name__, cls._name) + self.assertEqual(cls.__qualname__, cls._name) + self.assertEqual(cls.__module__, 'typing') class AllTests(BaseTestCase): """Tests for __all__."""