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

Subclassing Callable #617

Closed
DaniGuardiola opened this issue Feb 27, 2019 · 3 comments
Closed

Subclassing Callable #617

DaniGuardiola opened this issue Feb 27, 2019 · 3 comments

Comments

@DaniGuardiola
Copy link

I have a bunch of types defined like this:

t_wrapped_deferred_return = Dict[str, any]
t_deferred_return = Dict[str, any]
t_sync_return = Dict[str, str]
t_return = Union[t_sync_return, t_deferred_return]


class OperationCallable(Callable[[Stack, Union[bytes, None]], t_return]):
    type: str

Then I'm using a function typed with OperationCallable elsewhere, like this:

some_instance.fn(**kwargs)

This is throwing a mypy (I assume, I'm using PyCharm) error:

'OperationCallable' object is not callable

Am I missing something? Is there a different way to go about this?

Thanks!

@gvanrossum
Copy link
Member

You can't actually use Callable as a base class. If you don't get an error on that, you're probably not using mypy but just getting whatever errors PyCharm produces on its own (it has a sophisticated type inference engine, it just works different than mypy).

There is no bug here, please see our gitter channel for more help: https://gitter.im/python/typing

@ilevkivskyi
Copy link
Member

This tracker is not about mypy. It is about runtime issues in the backport of typing to older versions of Python.

Anyway, this error is correct, because just by inheriting from Callable you don't make a class callable, you must implement the __call__() method. Also mypy shows Invalid base class for this code, see python/mypy#4031.

@DaniGuardiola
Copy link
Author

Thanks @gvanrossum and @ilevkivskyi, I'll ask on Gitter and check the mypy issue.

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

No branches or pull requests

3 participants