Callable is invalid base class?

落花浮王杯 提交于 2021-02-18 06:44:06

问题


Can someone explain why inheriting from unparameterized and parameterized Callable:

from typing import Callable
from typing import NoReturn
from typing import TypeVar


T = TypeVar('T', str, int)
C = Callable[[T], NoReturn]


class Foo(Callable):

    def __call__(self, t: T):
        pass


class Bar(C):

    def __call__(self, t: T):
        pass

when passed to mypy raises errors for both Foo and Bar:

tmp.py:13: error: Invalid base class
tmp.py:19: error: Invalid base class

回答1:


This is in part because classes at runtime can't really inherit from a function or a callable to begin with, and in part because you don't need to explicitly inherit from Callable to indicate that a class is callable.

For example, the following program typechecks as expected using mypy 0.630:

from typing import Callable, Union, NoReturn, List

class Foo:
    def __call__(self, t: Union[str, int]) -> NoReturn:
        pass


class FooChild(Foo): pass


class Bad:
    def __call__(self, t: List[str]) -> NoReturn:
        pass


def expects_callable(x: Callable[[Union[str, int]], NoReturn]) -> None: 
    pass


expects_callable(Foo())         # No error
expects_callable(FooChild())    # No error
expects_callable(Bad())         # Error here: Bad.__call__ has an incompatible signature

Basically, if a class has a __call__ method, it's implicitly assumed that class is also a callable.



来源:https://stackoverflow.com/questions/52652595/callable-is-invalid-base-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!