如何使用函数注解来验证函数调用类型

abc*_*ccd 3 python type-hinting python-3.x python-decorators

我最近刚刚发现有一种叫做函数注释的东西,但我不太确定如何使用它。这是我到目前为止:

def check_type(f):
    def decorated(*args, **kwargs):
        counter=0
        for arg, type in zip(args, f.__annotations__.items()):
            if not isinstance(arg, type[1]):
                msg = 'Not the valid type'
                raise ValueError(msg)
            counter+=1

        return f(*args, **kwargs)
    return decorated

@check_type
def foo(a: int, b: list, c: str): #a must be int, b must be list, c must be str
    print(a,b,c)

foo(12, [1,2], '12') #This works

foo(12, 12, 12) #This raises a value error just as I wanted to

foo(a=12, b=12, c=12) #But this works too:(
Run Code Online (Sandbox Code Playgroud)

如您所见,我正在尝试检查 的类型abc使用注释和装饰器,ValueError如果类型不正确,则会引发。当我在调用时不使用关键字参数时,它工作正常。但是如果我使用关键字参数,则不会检查类型。我正在努力让它发挥作用,但我没有运气。

我的代码不支持关键字参数。因为我没有任何东西可以检查。我也不知道如何检查它。这就是我需要帮助的地方。

我也是这样做的:

def check_type(f):
    def decorated(*args, **kwargs):
        for name, type in f.__annotations__.items():
            if not isinstance(kwargs[name], type):
                msg = 'Not the valid type'
                raise ValueError(msg)

        return f(*args, **kwargs)
    return decorated

#But now they have to be assigned using keyword args
#so only foo(a=3,b=[],c='a') works foo(3,[],'a') results in a keyerror
#How can I combine them?
Run Code Online (Sandbox Code Playgroud)

Jim*_*ard 5

正如保罗建议的,最好是使用bind的方法Signature的对象(位于inspect)以结合*args**kwargs将被提供给f再检查如果类型匹配:

from inspect import signature
from typing import get_type_hints

def check_range(f):
    def decorated(*args, **kwargs):
        counter=0
        # use get_type_hints instead of __annotations__
        annotations = get_type_hints(f)
        # bind signature to arguments and get an 
        # ordered dictionary of the arguments
        b = signature(f).bind(*args, **kwargs).arguments            
        for name, value in b.items():
            if not isinstance(value, annotations[name]):
                msg = 'Not the valid type'
                raise ValueError(msg)
            counter+=1

        return f(*args, **kwargs)
    return decorated
Run Code Online (Sandbox Code Playgroud)

你的第一个案例实际上是随机成功的。dicts 在 Python< 3.6中有一个随机顺序,当您再次启动 Python 解释器时,它很可能会改变,这意味着zip您执行ping 不是确定性的。

不是迭代通过f.__annotations__,而是通过获取它get_type_hints,然后通过获取名称和值b.items()(这是一个 OrderedDict 并保证顺序)用 索引它name