bpo-44524: Add missed name and qualname to typing module obje… · python/cpython@c895f2b (original) (raw)

3 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -4491,6 +4491,67 @@ def test_no_isinstance(self):
4491 4491 issubclass(int, TypeGuard)
4492 4492
4493 4493
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 +
4494 4555 class AllTests(BaseTestCase):
4495 4556 """Tests for __all__."""
4496 4557
Original file line number Diff line number Diff line change
@@ -357,6 +357,12 @@ def __init__(self, getitem):
357 357 self._name = getitem.__name__
358 358 self.__doc__ = getitem.__doc__
359 359
360 +def __getattr__(self, item):
361 +if item in {'__name__', '__qualname__'}:
362 +return self._name
363 +
364 +raise AttributeError(item)
365 +
360 366 def __mro_entries__(self, bases):
361 367 raise TypeError(f"Cannot subclass {self!r}")
362 368
@@ -934,6 +940,9 @@ def __mro_entries__(self, bases):
934 940 return tuple(res)
935 941
936 942 def __getattr__(self, attr):
943 +if attr in {'__name__', '__qualname__'}:
944 +return self._name
945 +
937 946 # We are careful for copy and pickle.
938 947 # Also for simplicity we just don't relay all dunder names
939 948 if '__origin__' in self.__dict__ and not _is_dunder(attr):
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1 +Add missing ``__name__`` and ``__qualname__`` attributes to ``typing`` module
2 +classes. Patch provided by Yurii Karabas.