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

How to use function annotations to verify function call types

我最近才发现有一个东西叫做函数注释,但我不太清楚如何使用它。这是我目前所拥有的:

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:(

如您所见,我正在尝试使用注释和装饰器检查 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?

按照Paul的建议,最好使用Signature对象(位于inspect)的bind方法来绑定*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

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

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