我想做类似的事情
from typing import Callable
def a(foo: Callable[[int], None]):
foo(b=5)
此代码有效,但会发出警告 Unexpected argument
.定义为
def a(foo: Callable[[int], None]):
foo(5)
按预期工作,没有警告。如何将预期参数作为
kwarg
传递进入一个类型检查器不生我气的函数?
最佳答案
Callable
docs说
There is no syntax to indicate optional or keyword arguments; such function types are rarely used as callback types.
不过他们也说
Callable[..., ReturnType]
(literal ellipsis) can be used to type hint a callable taking any number of arguments and returningReturnType
在这里申请,那就是
def a(foo: Callable[..., None]):
您将丢失 int
注释,但要么接受警告,要么明确超越警告。
关于python - 为需要 kwargs 的 Callable 输入提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68472236/