簡體   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