gpt4 book ai didi

python - 为需要 kwargs 的 Callable 输入提示

转载 作者:行者123 更新时间:2023-12-04 07:21:26 24 4
gpt4 key购买 nike

我想做类似的事情

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 returning ReturnType


在这里申请,那就是
def a(foo: Callable[..., None]):
您将丢失 int注释,但要么接受警告,要么明确超越警告。

关于python - 为需要 kwargs 的 Callable 输入提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68472236/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com