繁体   English   中英

python 带有 kwargs 的 function 的打字签名(typing.Callable)

[英]python typing signature (typing.Callable) for function with kwargs

我大量使用 python 来自 python 3 的打字支持。

最近我试图将 function 作为参数传递,但我没有找到在typing.Callable签名中使用kwargs的任何帮助。

请检查下面的代码和评论。

import typing

# some function with singnature typing
def fn1_as_arg_with_kwargs(a: int, b: float) -> float:
    return a + b

# some function with singnature typing
def fn2_as_arg_with_kwargs(a: int, b: float) -> float:
    return a * b

# function that get callables as arg
# this works with typing
def function_executor(
        a: int, 
        b: float, 
        fn: typing.Callable[[int, float], float]):
    return fn(a, b)

# But what if I want to name my kwargs 
# (something like below which does not work)
# ... this will help me more complex scenarios 
# ... or am I expecting a lot from python3 ;)
def function_executor(
        a: int, 
        b: float, 
        fn: typing.Callable[["a": int, "b": float], float]):
    return fn(a=a, b=b)

您可能正在寻找回调协议

简而言之,当您想用复杂的签名表示可调用对象时,您需要做的是创建一个自定义协议,该协议定义了具有您想要的精确签名的__call__方法。

例如,在您的情况下:

from typing import Protocol

# Or, if you want to support Python 3.7 and below, install the typing_extensions
# module via pip and do the below:
from typing_extensions import Protocol

class MyCallable(Protocol):
    def __call__(self, a: int, b: float) -> float: ...

def good(a: int, b: float) -> float: ...

def bad(x: int, y: float) -> float: ...


def function_executor(a: int, b: float, fn: MyCallable) -> float:
    return fn(a=a, b=b)

function_executor(1, 2.3, good)  # Ok!
function_executor(1, 2.3, bad)   # Errors

如果您尝试使用 mypy 对该程序进行类型检查,您将在最后一行收到以下(不可否认的)错误:

Argument 3 to "function_executor" has incompatible type "Callable[[int, float], float]"; expected "MyCallable"

(回调协议有点新,所以希望错误消息的质量会随着时间的推移而提高。)

我发现带有输入回调的示例有点复杂。 对于正在寻找使用 kwargs 键入 function 的简单示例的任何人:

from typing import Protocol

class MyCallable(Protocol):
    # Define types here, as if __call__ were a function (ignore self).
    def __call__(self, a: int, b: int) -> int:
        ...

# Generic function- types correspond to MyCallable.__call__ args.
def func_add(a: int, b: int) -> int:
    return a + b

# Assign the function to a variable called my_function, and add the type.
my_function: MyCallable = func_add

my_function(a=1, b=2)   # This is OK.
my_function(a=1, b="x") # This is NOK.

我大量使用python 3中的python类型支持。

最近,我试图将函数作为参数传递,但在typing.Callable 。的可typing.Callable签名中找不到使用kwargs任何帮助。

请检查下面的代码和注释。

import typing

# some function with singnature typing
def fn1_as_arg_with_kwargs(a: int, b: float) -> float:
    return a + b

# some function with singnature typing
def fn2_as_arg_with_kwargs(a: int, b: float) -> float:
    return a * b

# function that get callables as arg
# this works with typing
def function_executor(
        a: int, 
        b: float, 
        fn: typing.Callable[[int, float], float]):
    return fn(a, b)

# But what if I want to name my kwargs 
# (something like below which does not work)
# ... this will help me more complex scenarios 
# ... or am I expecting a lot from python3 ;)
def function_executor(
        a: int, 
        b: float, 
        fn: typing.Callable[["a": int, "b": float], float]):
    return fn(a=a, b=b)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM