[issue44524] __name__ attribute in typing module

Ken Jin report at bugs.python.org
Thu Aug 5 13:37:10 EDT 2021


Ken Jin <kenjin4096 at gmail.com> added the comment:

@Serhiy a summary from what I understand. I hope this helps:

> I am confused. Why do these objects should have __name__ and __qualname__ attributes? What document specifies this?

I don't think any doc specifies this. The docs for __name__ and __qualname__ say they apply only to "name of the class, function, method, descriptor, or generator instance". The typing "types" are objects, not classes, so normally they wouldn't apply, but maybe there is an exception (see below).

> How are these attributes used?

According to OP, for logging/printing purposes. Initially, I was slightly against adding this. But I realized that it make sense for ABCs and concrete classes. PEP 585 says that the builtin types.GenericAlias is a "thin proxy type that forwards all method calls and attribute accesses to the bare origin type"[1] with some exceptions. typing._GenericAlias is supposed to behave similarly to builtin version.

>>> list[int].__name__
'list'
>>> collections.abc.Callable[[int], str].__name__
'Callable'

Both typing.List[int].__name__ and typing.Callable[int].__name__ raised error before Yurii's PR. So I think it's good that we aligned typing's behavior and the builtin version.

[1] https://www.python.org/dev/peps/pep-0585/#parameters-to-generics-are-available-at-runtime

I just realized Łukasz wrote PEP 585, so maybe he can shed some insight too.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44524>
_______________________________________


More information about the Python-bugs-list mailing list