defining self.__call__ makes the class callable when it isn't
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 42/100
Research direction
Start by running the example with mypy and Python 2, confirming that instance-level call is accepted statically but rejected at runtime. Trace mypy's handling of callable checks and instance attribute assignments; done means the checker reports this mismatch or otherwise matches Python's behavior.
Written by the indexing model from the issue text.
Description
I thought I had a found a great workaround for a problem I'm having. I'll define __call__ in the __init__
class A(object):
def __init__(self):
# type: () -> None
self.__call__ = self.stuff
def stuff(self, a):
# type: (int) -> int
return a + 2
a = A()
print a(7)
Mypy has no issues with this code at all. However python2 disagrees:
Traceback (most recent call last):
File "example.py", line 12, in <module>
print a(7)
TypeError: 'A' object is not callable
Looks like __call__ has to be defined on the class not the instance.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- Avg merge
- 1d 18h
- Merged PRs (30d)
- 54
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.
More from python/mypy
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
bug topic-configuration topic-error-reporting
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
bancolombia/sentinel#23 ·
-
test md OpenCI
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100