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

Allow subclassing Callable #4031

Closed
ilevkivskyi opened this issue Sep 29, 2017 · 2 comments
Closed

Allow subclassing Callable #4031

ilevkivskyi opened this issue Sep 29, 2017 · 2 comments

Comments

@ilevkivskyi
Copy link
Member

This is not a priority, but probably we could allow subclassing Callable, some people might find this "nicer", than defining a __call__ method (e.g. if this is an ABC or a protocol). For example this already works at runtime:

class C(Callable[[int], int]):
    attr: str

This would be implemented by returning CallableType for C with a fallback to an instance of the original TypeInfo (like for tulpes, see also #3831).

@msullivan
Copy link
Collaborator

The protocol thing works fine

@bfontaine
Copy link

Why was this closed? Protocols are not sufficient because they don’t inherit callable properties like __name__ or __doc__:

from typing import Protocol

class MyFunction(Protocol):
    def __call__(self, x: int) -> int: ...

def print_name(fn: MyFunction):
    print(fn.__name__)

This fails with mypy 1.8.0:

test.py:9: error: "MyFunction" has no attribute "__name__"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

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

No branches or pull requests

3 participants