微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python 3.9的typing.Annotation如何使用MaxLen?

如何解决python 3.9的typing.Annotation如何使用MaxLen?

我知道有一种新的输入格式 Annotation,您可以在其中为函数的入口变量指定一些元数据。 From the docs,您可以指定传入列表的最大长度,例如:

  • Annotated 可以与嵌套和通用别名一起使用:
T = TypeVar('T')
Vec = Annotated[list[tuple[T,T]],MaxLen(10)]
V = Vec[int]

V == Annotated[list[tuple[int,int]],MaxLen(10)]

但我无法理解 MaxLen 是什么。你应该从其他地方导入一个类吗?我试过导入 typing.MaxLen 但似乎不存在(我使用的是 python 3.9.6,which I think it should exist here...?)。

我想象它应该起作用的示例代码

from typing import List,Annotated,MaxLen

def function(foo: Annotated[List[int],MaxLen(10)]):
    ...
    return True

在哪里可以找到 MaxLen?

编辑:

似乎 MaxLen 是您必须创建的某种类。问题是我看不出你应该怎么做。有公开的例子吗?有人如何实现此功能

解决方法

如 AntiNeutronicPlasma 所述,Maxlen 只是一个示例,因此您需要自己创建。

以下示例说明如何创建和解析自定义注释(例如 MaxLen)以帮助您入门。

首先,我们定义注解类本身。这是一个非常简单的类,我们只需要存储相关的元数据,本例中为最大值:

class MaxLen:
    def __init__(self,value):
        self.value = value

现在,我们可以定义一个使用这个注解的函数,例如:

def sum_nums(nums: Annotated[List[int],MaxLen(10)]):
    return sum(nums)

但是如果没有人检查它,它将没有多大用处。因此,一种选择可能是实现一个装饰器,在运行时检查您的自定义注释。 get_type_hints 模块中的函数 get_originget_argstyping 将成为您最好的朋友。以下是此类装饰器的示例,它解析并强制对 MaxLen 类型进行 list 注释:

def check_annotations(func):
    @wraps(func)
    def wrapped(**kwargs):
        # perform runtime annotation checking
        # first,get type hints from function
        type_hints = get_type_hints(func,include_extras=True)
        for param,hint in type_hints.items():
            # only process annotated types
            if get_origin(hint) is not Annotated:
                continue
            # get base type and additional arguments
            hint_type,*hint_args = get_args(hint)
            # if a list type is detected,process the args
            if hint_type is list or get_origin(hint_type) is list:
                for arg in hint_args:
                    # if MaxLen arg is detected,process it
                    if isinstance(arg,MaxLen):
                        max_len = arg.value
                        actual_len = len(kwargs[param])
                        if actual_len > max_len:
                            raise ValueError(f"Parameter '{param}' cannot have a length "
                                             f"larger than {max_len} (got length {actual_len}).")
        # execute function once all checks passed
        return func(**kwargs)

    return wrapped

(请注意,此特定示例仅适用于关键字参数,但您可能会找到一种方法使其也适用于普通参数)。

现在,您可以将此装饰器应用于任何函数,并且您的自定义注释将被解析:

@check_annotations
def sum_nums_strict(nums: Annotated[List[int],MaxLen(10)]):
    return sum(nums)

以下是运行中的代码示例:

>>> sum_nums(nums=list(range(5)))
10
>>> sum_nums(nums=list(range(15)))
105
>>> sum_nums_strict(nums=list(range(5)))
10
>>> sum_nums_strict(nums=list(range(15)))
Traceback (most recent call last):
  File "<stdin>",line 1,in <module>
  File "annotated_test.py",line 29,in wrapped
    raise ValueError(f"Parameter '{param}' cannot have a length "
ValueError: Parameter 'nums' cannot have a length larger than 10 (got length 15).
,

Maxlen 只是他们使用的示例函数,而不是内置方法。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。