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

Comments

Projects
None yet
3 participants
@DaniGuardiola
Copy link

DaniGuardiola commented Feb 27, 2019

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

This comment has been minimized.

Copy link
Member

gvanrossum commented Feb 27, 2019

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

@gvanrossum gvanrossum closed this Feb 27, 2019

@ilevkivskyi

This comment has been minimized.

Copy link
Collaborator

ilevkivskyi commented Feb 27, 2019

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

This comment has been minimized.

Copy link
Author

DaniGuardiola commented Feb 27, 2019

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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.