Mypy: Classes with __call__ defined should be a subtype of Callable

Created on 18 Aug 2015  Â·  9Comments  Â·  Source: python/mypy

def apply(f: Callable[[int], int], x: int) -> int:
  return f(x)

class Add5(object):
  def __call__(self, x: int) -> int:
    return x + 5

apply(Add5(), 5)

This code snippet doesn't type check because Mypy doesn't treat Add5() as a Callable[[int], int]

bug priority-0-high topic-protocols

Most helpful comment

This is now fixed by #3132

All 9 comments

Moved to 0.5 since this comes up pretty frequently.

I'm going to start looking into this one.

WIP at https://github.com/margaret/mypy/tree/user_defined_callable (thanks @rwbarton for helping)
Currently debugging broken tests, specifically

class A(type):
    def __init__(self) -> None: pass
def f(x: type): pass
f(A())

Does not work and returns
error: Argument 1 to "f" has incompatible type Callable[[Any, Any], Any]; expected "type"

Have you tried putting a pdb.set_trace() call in errors.Errors.report()
and tracing back to the cause of this message?

I believe type is special in some ways, maybe that's biting you?

On Fri, Jun 3, 2016 at 7:33 PM, Margaret Sy [email protected]
wrote:

WIP at https://github.com/margaret/mypy/tree/user_defined_callable
Currently debugging broken tests, specifically

class A(type):
def init(self) -> None: pass
def f(x: type): pass
f(A())

Does not work and returns
error: Argument 1 to "f" has incompatible type Callable[[Any, Any], Any];
expected "type"

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/python/mypy/issues/797#issuecomment-223731348, or mute
the thread
https://github.com/notifications/unsubscribe/ACwrMlU02GpgXeGUJobz55IoQXJZ9oczks5qIOP8gaJpZM4FtydI
.

--Guido van Rossum (python.org/~guido)

Moving to an earlier milestone since the partial issue is kind of important.

A related issue is the join of Callable and an instance type that defines __call__ (see #2779).

@ilevkivskyi Are you still working on this? If not it's better to un-assign.

Protocols #3132 will fix this, no additional work here will be needed when they are merged.

This is now fixed by #3132

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jstasiak picture jstasiak  Â·  3Comments

PeterJCLaw picture PeterJCLaw  Â·  3Comments

Stiivi picture Stiivi  Â·  3Comments

mplanchard picture mplanchard  Â·  3Comments

arquolo picture arquolo  Â·  3Comments