为需要 kwargs 的 Callable 键入提示

Gul*_*zar 13 python types type-hinting

我想做类似的事情

from typing import Callable


def a(foo: Callable[[int], None]):
    foo(b=5)
Run Code Online (Sandbox Code Playgroud)

该代码可以工作,但会发出警告Unexpected argument

定义为

def a(foo: Callable[[int], None]):
    foo(5)
Run Code Online (Sandbox Code Playgroud)

按预期工作,没有任何警告。


如何将预期的参数作为 a 传递kwarg到函数中,而类型检查器不会生我的气?

jua*_*aga 15

您可以在这里使用“可调用协议”,因此:

import typing

class MyCallableType(typing.Protocol):
    def __call__(self, bar:int) -> None:
        ...

def a(foo: MyCallableType):
    foo(32)
    foo(bar=32)
Run Code Online (Sandbox Code Playgroud)

现在,使用以下命令测试上述内容mypy

jarrivillaga$ mypy --version
mypy 0.910
jarrivillaga$ mypy test.py
Success: no issues found in 1 source file
Run Code Online (Sandbox Code Playgroud)

注意,这允许 mypy 捕获各种错误,例如参数名称错误的函数,或者如果我们想要b成为一个指定仅关键字 bar 参数的函数:

import typing

class MyCallableType(typing.Protocol):
    def __call__(self, b:int) -> None:
        ...

def a(foo: MyCallableType):
    foo(32)
    foo(b=32)

def bar(b: int) -> None:
    pass

def baz(*, b: int) -> None:
    pass

def bing(x: int) -> None:
    pass

a(bar)
a(baz)
a(bing)
Run Code Online (Sandbox Code Playgroud)

mypy 会抱怨以下内容:

jarrivillaga$ mypy test.py
test.py:21: error: Argument 1 to "a" has incompatible type "Callable[[NamedArg(int, 'b')], None]"; expected "MyCallableType"
test.py:22: error: Argument 1 to "a" has incompatible type "Callable[[int], None]"; expected "MyCallableType"
Found 2 errors in 1 file (checked 1 source file)
Run Code Online (Sandbox Code Playgroud)


Dee*_*ace 10

文档说Callable

没有语法来指示可选参数或关键字参数;此类函数类型很少用作回调类型。

然而他们也说

Callable[..., ReturnType](文字省略号)可用于键入提示可调用函数,该可调用函数接受任意数量的参数并返回ReturnType

在这里申请,那就是

def a(foo: Callable[..., None]):
Run Code Online (Sandbox Code Playgroud)

您将丢失int注释,但要么是接受警告,要么明确超越它。