Description
The following should be an error, but it currently is not.
from collections.abc import Callable
from typing import Any
def f(call: Callable[..., Any]) -> None:
print(call.__name__) # why is there no error here?
IMO the last line should cause a mypy error to the effect of "Callable" has no attribute "__name__"
. The callable protocol generally does not define __name__
.
To further drive this point home, add the following piece:
class Bar:
def __call__(self) -> None:
print(f"hi mom")
f(Bar()) # this is valid
print(Bar().__name__) # this is an error
This means that
Bar
is correctly viewed as being aCallable
subtype (structurally) and- it is also correctly identified to not have the
__name__
attribute by mypy. - Yet when something is annotated as
Callable
, mypy just assumes it has the__name__
attribute. 🤨
Correct me if I am wrong, but these points cannot logically all be true.
The documentation of the Callable
ABC clearly states that it is a class that defines the __call__
method. Nowhere does it mention the need for a Callable
class to define __name__
(or anything else for that matter).
There is also no mention of any attributes/methods other than __call__
in the glossary entry for callable.
Lastly, the relevant section on Callable types of the standard type hierarchy chapter in the data model documentation again broadly defines them as
types to which the function call operation [...] can be applied
and further down even explicitly states the following:
Instances of arbitrary classes can be made callable by defining a
__call__()
method in their class.
I realize that user defined functions do always have the __name__
attribute (as well as a bunch of others listed in the aforementioned data model docs). But this should make them a subtype of Callable
.
Yet it seems as though mypy treats a Callable
as a user defined function. My example f
above can be changed to any of the attributes of user defined functions, such as __kwdefaults__
for example and it would still pass mypy checks. This seems wildly inconsistent with all the documentation I could find, as well as logically inconsistent in itself.
I can only assume that there is some historic/pragmatic explanation for this, but I could not find it.
What is the reasoning behind this?
I found a discussion in #5958 that seems to be related, but found no answer to my question.