Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
zhangwen0411 opened this issue Aug 18, 2015 · 9 comments
Closed

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

zhangwen0411 opened this issue Aug 18, 2015 · 9 comments
Assignees
Labels

Comments

@zhangwen0411
Copy link
Contributor

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]

@JukkaL JukkaL added bug mypy got something wrong hack and removed hack labels Aug 18, 2015
@JukkaL JukkaL added this to the 0.5 milestone May 5, 2016
@JukkaL
Copy link
Collaborator

JukkaL commented May 5, 2016

Moved to 0.5 since this comes up pretty frequently.

@margaret
Copy link
Contributor

margaret commented Jun 3, 2016

I'm going to start looking into this one.

@margaret
Copy link
Contributor

margaret commented Jun 4, 2016

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"

@gvanrossum
Copy link
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)

@gvanrossum
Copy link
Member

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

@JukkaL
Copy link
Collaborator

JukkaL commented Jan 31, 2017

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

@gvanrossum
Copy link
Member

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

@ilevkivskyi
Copy link
Member

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

dehnert added a commit to dehnert/typeshed that referenced this issue Jul 28, 2017
According to the documentation, the auth parameter should be an AuthBase
subclass[1].  In practice, the actual requirement seems to just be a
Callable[[Request], Request], and AuthBase implements that with the __call__
method. However, mypy isn't currently smart enough to infer that __call__
implies Callable[2]. In the interim (and perhaps also to add clearer errors),
explicitly support AuthBase.

Additionally, this adds typing of AuthBase.__call__, to match the
Callable[[Request], Request] declaration used elsewhere.

[1] http://docs.python-requests.org/en/master/user/advanced/#custom-authentication
[2] python/mypy#797
dehnert added a commit to dehnert/typeshed that referenced this issue Jul 28, 2017
According to the documentation, the auth parameter should be an AuthBase
subclass[1].  In practice, the actual requirement seems to just be a
Callable[[Request], Request], and AuthBase implements that with the __call__
method. However, mypy isn't currently smart enough to infer that __call__
implies Callable[2]. In the interim (and perhaps also to add clearer errors),
explicitly support AuthBase.

Additionally, this adds typing of AuthBase.__call__, to match the
Callable[[Request], Request] declaration used elsewhere.

[1] http://docs.python-requests.org/en/master/user/advanced/#custom-authentication
[2] python/mypy#797
@ilevkivskyi
Copy link
Member

This is now fixed by #3132

JelleZijlstra pushed a commit to python/typeshed that referenced this issue Dec 15, 2017
…bject (#1504)

* requests: allow strings in Session.verify

Per the documentation[1] (and actual usage), the verify parameter can be a
string or a bool.

[1] http://docs.python-requests.org/en/master/user/advanced/#ssl-cert-verification

* requests: explicitly support AuthBase for Session.auth

According to the documentation, the auth parameter should be an AuthBase
subclass[1].  In practice, the actual requirement seems to just be a
Callable[[Request], Request], and AuthBase implements that with the __call__
method. However, mypy isn't currently smart enough to infer that __call__
implies Callable[2]. In the interim (and perhaps also to add clearer errors),
explicitly support AuthBase.

Additionally, this adds typing of AuthBase.__call__, to match the
Callable[[Request], Request] declaration used elsewhere.

[1] http://docs.python-requests.org/en/master/user/advanced/#custom-authentication
[2] python/mypy#797
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants