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

标签 python types type-hinting

我想做类似的事情

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/

相关文章:

clojure - defrecord 构造函数中未强制执行类型提示

Python Django 网址无法正常工作?

python - 将两个 boolean 列转换为 Pandas 中的类 ID

javascript - 来自多个 "active"客户端的 AppEngine 应用程序的可扩展轮询?

c++ - size_t、ptrdiff_t 和 uintmax_t 等类型是否有通用术语?

types - OCaml 中的参数化类型

python - Into 和 From 接口(interface) : struggling with typing. 类型的协方差

python - main() 接受 0 个位置参数,但给出了 2 个

types - Hadoop 流式传输示例失败 映射中的键类型不匹配

python - 键入具有特定列的提示 pandas 数据框