Skip to content

Classes with __call__ defined should be a subtype of Callable #797

Closed

Description

@zhangwen0411
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]

Activity

  1. added
    bugmypy got something wrong
    and removed on Aug 18, 2015
  2. added this to the milestone on May 5, 2016
  3. JukkaL commented on May 5, 2016

    @JukkaL
    Collaborator

    Moved to 0.5 since this comes up pretty frequently.

  4. margaret commented on Jun 3, 2016

    @margaret
    Contributor

    I'm going to start looking into this one.

  5. margaret commented on Jun 4, 2016

    @margaret
    Contributor

    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"

  6. gvanrossum commented on Jun 4, 2016

    @gvanrossum
    Member

    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
    #797 (comment), or mute
    the thread
    https://github.com/notifications/unsubscribe/ACwrMlU02GpgXeGUJobz55IoQXJZ9oczks5qIOP8gaJpZM4FtydI
    .

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

  7. modified the milestones: , on Nov 28, 2016
  8. gvanrossum commented on Nov 28, 2016

    @gvanrossum
    Member

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

  9. JukkaL commented on Jan 31, 2017

    @JukkaL
    Collaborator

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

  10. removed this from the milestone on Mar 29, 2017
  11. self-assigned this
    on Mar 31, 2017
  12. gvanrossum commented on May 5, 2017

    @gvanrossum
    Member

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

  13. ilevkivskyi commented on May 5, 2017

    @ilevkivskyi
    Member

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

  14. ilevkivskyi commented on Aug 19, 2017

    @ilevkivskyi
    Member

    This is now fixed by #3132

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions