我正在尝试定义一个返回另一个函数的函数。它返回的函数已重载。
例如:
from typing import overload, Union, Callable
@overload
def foo(a: str) -> str:
pass
@overload
def foo(a: int) -> int:
pass
def foo(a: Union[str, int]) -> Union[str, int]:
if isinstance(a, int):
return 1
else:
# str
return "one"
def bar() -> Callable[[Union[str, int]], Union[str, int]]:
return foo # Incompatible return value type (got overloaded function, expected "Callable[[Union[str, int]], Union[str, int]]")
但是,我使用 Mypy 输入函数 bar
时出现错误。
如何正确输入 bar
?我做错了什么?
最佳答案
这里的问题部分是因为 Callable
类型有点太有限,无法准确表达 foo
的类型,还有部分是 mypy 目前在分析重载与 Callables 的兼容性。 (一般情况下很难做到)。
目前最好的方法可能是使用 Callback protocol 定义更精确的返回类型。并返回:
例如:
from typing import overload, Union, Callable
# Or if you're using Python 3.8+, just 'from typing import Protocol'
from typing_extensions import Protocol
# A callback protocol encoding the exact signature you want to return
class FooLike(Protocol):
@overload
def __call__(self, a: str) -> str: ...
@overload
def __call__(self, a: int) -> int: ...
def __call__(self, a: Union[str, int]) -> Union[str, int]: ...
@overload
def foo(a: str) -> str:
pass
@overload
def foo(a: int) -> int:
pass
def foo(a: Union[str, int]) -> Union[str, int]:
if isinstance(a, int):
return 1
else:
# str
return "one"
def bar() -> FooLike:
return foo # now type-checks
注意:自 Python 3.8 起,Protocol
已添加到 typing
模块中。如果您希望在早期版本的 Python 中使用它,请安装 typing_extensions 模块 (
pip install Typing_extensions`) 并从那里导入它。
不得不像这样复制签名两次确实有点笨拙。人们似乎普遍认为这是一个问题(typing 和 mypy 问题跟踪器中有关于此问题的各种问题),但我认为对于如何最好地解决这个问题还没有达成共识。
关于python - 如何处理 "Incompatible return value type (got overloaded function)"问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56624854/