python/mypy

Classes with __call__ defined should be a subtype of Callable

zhangwen0411 opened this issue · 9 comments

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]

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 notifications@github.com
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
#797 (comment), 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