bpo-44524: Add missed __name__ and __qualname__ to typing module obje… · python/cpython@bce1418

3 files changed

lines changed

Original file line numberDiff line numberDiff line change

@@ -4498,6 +4498,67 @@ def test_no_isinstance(self):

44984498

issubclass(int, TypeGuard)

44994499
45004500
4501+

class SpecialAttrsTests(BaseTestCase):

4502+

def test_special_attrs(self):

4503+

cls_to_check = (

4504+

# ABC classes

4505+

typing.AbstractSet,

4506+

typing.AsyncContextManager,

4507+

typing.AsyncGenerator,

4508+

typing.AsyncIterable,

4509+

typing.AsyncIterator,

4510+

typing.Awaitable,

4511+

typing.ByteString,

4512+

typing.Callable,

4513+

typing.ChainMap,

4514+

typing.Collection,

4515+

typing.Container,

4516+

typing.ContextManager,

4517+

typing.Coroutine,

4518+

typing.Counter,

4519+

typing.DefaultDict,

4520+

typing.Deque,

4521+

typing.Dict,

4522+

typing.FrozenSet,

4523+

typing.Generator,

4524+

typing.Hashable,

4525+

typing.ItemsView,

4526+

typing.Iterable,

4527+

typing.Iterator,

4528+

typing.KeysView,

4529+

typing.List,

4530+

typing.Mapping,

4531+

typing.MappingView,

4532+

typing.MutableMapping,

4533+

typing.MutableSequence,

4534+

typing.MutableSet,

4535+

typing.OrderedDict,

4536+

typing.Reversible,

4537+

typing.Sequence,

4538+

typing.Set,

4539+

typing.Sized,

4540+

typing.Tuple,

4541+

typing.Type,

4542+

typing.ValuesView,

4543+

# Special Forms

4544+

typing.Any,

4545+

typing.NoReturn,

4546+

typing.ClassVar,

4547+

typing.Final,

4548+

typing.Union,

4549+

typing.Optional,

4550+

typing.Literal,

4551+

typing.TypeAlias,

4552+

typing.Concatenate,

4553+

typing.TypeGuard,

4554+

)

4555+
4556+

for cls in cls_to_check:

4557+

with self.subTest(cls=cls):

4558+

self.assertEqual(cls.__name__, cls._name)

4559+

self.assertEqual(cls.__qualname__, cls._name)

4560+

self.assertEqual(cls.__module__, 'typing')

4561+
45014562

class AllTests(BaseTestCase):

45024563

"""Tests for __all__."""

45034564
Original file line numberDiff line numberDiff line change

@@ -358,6 +358,12 @@ def __init__(self, getitem):

358358

self._name = getitem.__name__

359359

self.__doc__ = getitem.__doc__

360360
361+

def __getattr__(self, item):

362+

if item in {'__name__', '__qualname__'}:

363+

return self._name

364+
365+

raise AttributeError(item)

366+
361367

def __mro_entries__(self, bases):

362368

raise TypeError(f"Cannot subclass {self!r}")

363369

@@ -935,6 +941,9 @@ def __mro_entries__(self, bases):

935941

return tuple(res)

936942
937943

def __getattr__(self, attr):

944+

if attr in {'__name__', '__qualname__'}:

945+

return self._name

946+
938947

# We are careful for copy and pickle.

939948

# Also for simplicity we just don't relay all dunder names

940949

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.