Why is `Callable` assumed to be a user defined function?
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
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
Baris correctly viewed as being aCallablesubtype (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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the examples in the issue and compare mypy's treatment of collections.abc.Callable with the behavior of Bar and user-defined functions. Read the related discussion in issue #5958 and review the unresolved question about whether Callable should expose function attributes. Done would require an agreed behavior and corresponding documentation or tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100