summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2021-07-19 17:57:27 (GMT)
committerGitHub <noreply@github.com>2021-07-19 17:57:27 (GMT)
commitc895f2bc4f270efce30fe3687ce85095418175f4 (patch)
treec0760cf6068fb8e0444f5d77b16d1f0ee18fed6e /Lib
parent8c43bf1a923754fa6d97772151c6ac23c48759d3 (diff)
downloadcpython-c895f2bc4f270efce30fe3687ce85095418175f4.zip
cpython-c895f2bc4f270efce30fe3687ce85095418175f4.tar.gz
cpython-c895f2bc4f270efce30fe3687ce85095418175f4.tar.bz2
bpo-44524: Add missed __name__ and __qualname__ to typing module objects (GH-27237) (#27246)
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> (cherry picked from commit bce1418541a64a793960182772f985f64afbfa1a) Co-authored-by: Yurii Karabas <1998uriyyo@gmail.com>
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_typing.py61
-rw-r--r--Lib/typing.py9
2 files changed, 70 insertions, 0 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 5602150..ad7f385 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -4491,6 +4491,67 @@ class TypeGuardTests(BaseTestCase):
issubclass(int, TypeGuard)
+class SpecialAttrsTests(BaseTestCase):
+ def test_special_attrs(self):
+ cls_to_check = (
+ # ABC classes
+ 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,
+ # 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:
+ 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__."""
diff --git a/Lib/typing.py b/Lib/typing.py
index 660ad35..cb70394 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -357,6 +357,12 @@ class _SpecialForm(_Final, _root=True):
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}")
@@ -934,6 +940,9 @@ class _BaseGenericAlias(_Final, _root=True):
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):