Skip to content

Commit c895f2b

Browse files
bpo-44524: Add missed __name__ and __qualname__ to typing module objects (GH-27237) (#27246)
Co-authored-by: Ken Jin <[email protected]> (cherry picked from commit bce1418) Co-authored-by: Yurii Karabas <[email protected]>
1 parent 8c43bf1 commit c895f2b

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

Lib/test/test_typing.py

+61
Original file line numberDiff line numberDiff line change
@@ -4491,6 +4491,67 @@ def test_no_isinstance(self):
44914491
issubclass(int, TypeGuard)
44924492

44934493

4494+
class SpecialAttrsTests(BaseTestCase):
4495+
def test_special_attrs(self):
4496+
cls_to_check = (
4497+
# ABC classes
4498+
typing.AbstractSet,
4499+
typing.AsyncContextManager,
4500+
typing.AsyncGenerator,
4501+
typing.AsyncIterable,
4502+
typing.AsyncIterator,
4503+
typing.Awaitable,
4504+
typing.ByteString,
4505+
typing.Callable,
4506+
typing.ChainMap,
4507+
typing.Collection,
4508+
typing.Container,
4509+
typing.ContextManager,
4510+
typing.Coroutine,
4511+
typing.Counter,
4512+
typing.DefaultDict,
4513+
typing.Deque,
4514+
typing.Dict,
4515+
typing.FrozenSet,
4516+
typing.Generator,
4517+
typing.Hashable,
4518+
typing.ItemsView,
4519+
typing.Iterable,
4520+
typing.Iterator,
4521+
typing.KeysView,
4522+
typing.List,
4523+
typing.Mapping,
4524+
typing.MappingView,
4525+
typing.MutableMapping,
4526+
typing.MutableSequence,
4527+
typing.MutableSet,
4528+
typing.OrderedDict,
4529+
typing.Reversible,
4530+
typing.Sequence,
4531+
typing.Set,
4532+
typing.Sized,
4533+
typing.Tuple,
4534+
typing.Type,
4535+
typing.ValuesView,
4536+
# Special Forms
4537+
typing.Any,
4538+
typing.NoReturn,
4539+
typing.ClassVar,
4540+
typing.Final,
4541+
typing.Union,
4542+
typing.Optional,
4543+
typing.Literal,
4544+
typing.TypeAlias,
4545+
typing.Concatenate,
4546+
typing.TypeGuard,
4547+
)
4548+
4549+
for cls in cls_to_check:
4550+
with self.subTest(cls=cls):
4551+
self.assertEqual(cls.__name__, cls._name)
4552+
self.assertEqual(cls.__qualname__, cls._name)
4553+
self.assertEqual(cls.__module__, 'typing')
4554+
44944555
class AllTests(BaseTestCase):
44954556
"""Tests for __all__."""
44964557

Lib/typing.py

+9
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,12 @@ def __init__(self, getitem):
357357
self._name = getitem.__name__
358358
self.__doc__ = getitem.__doc__
359359

360+
def __getattr__(self, item):
361+
if item in {'__name__', '__qualname__'}:
362+
return self._name
363+
364+
raise AttributeError(item)
365+
360366
def __mro_entries__(self, bases):
361367
raise TypeError(f"Cannot subclass {self!r}")
362368

@@ -934,6 +940,9 @@ def __mro_entries__(self, bases):
934940
return tuple(res)
935941

936942
def __getattr__(self, attr):
943+
if attr in {'__name__', '__qualname__'}:
944+
return self._name
945+
937946
# We are careful for copy and pickle.
938947
# Also for simplicity we just don't relay all dunder names
939948
if '__origin__' in self.__dict__ and not _is_dunder(attr):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add missing ``__name__`` and ``__qualname__`` attributes to ``typing`` module
2+
classes. Patch provided by Yurii Karabas.

0 commit comments

Comments
 (0)