defining self.__call__ makes the class callable when it isn't

Open
#2,113 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
42/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
devtools

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

bug priority-1-normal topic-runtime-semantics

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python/mypy

All issues in python/mypy

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.