python 3.9 中的类型注释约束(`ValueRange`/`MinLen` 等)在哪里? [英] Where are the Type annotation constraints (`ValueRange`/`MinLen` etc) in python 3.9?

查看:39
本文介绍了python 3.9 中的类型注释约束(`ValueRange`/`MinLen` 等)在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 python 3.9 中看到(很棒的)新的 Annotated 类型注释(可变类型约束!)后,我急忙升级,以便我可以检查它们.(https://docs.python.org/3/library/typing.html?highlight=valuerange)

After seeing the (awesome) new Annotated type annotation in python 3.9 (varaidic type constraints!), I rushed to upgrade so I could check them out. (https://docs.python.org/3/library/typing.html?highlight=valuerange)

但是当我尝试使用 ValueRange[min,max]MaxLen[n] 时 - 我似乎无法在任何地方找到它们..PyCharm 没有为我提供任何帮助,而且它们似乎不在我期望的 typing 模块中.

But when I tried using ValueRange[min,max] or MaxLen[n] - I couldn't seem to find them anywhere.. PyCharm didn't offer me any help, and they don't seem to be in the typing module where I might expect them.

文档中介绍了它们,但在谷歌上搜索我在网上找不到任何关于如何实际导入它们的参考.

The docs feature them but googling I can't find any reference online as to how to actually import them.

他们还没有语言吗?还是只是在一些我不知道的新模块中?

Are they not in the language yet? or just in some new module I'm not aware of?

推荐答案

正如其他人所说,这些类只是可以注释的示例.注释只是抓取某个变量并添加一些提示";通过您创建的类(MaxLen、ValueRange 等)发送给他们.

As others have said, these classes are just an example of what can be annotated. Annotation just grabs a certain variable and adds some "hints" to them via the class you created (MaxLen, ValueRange, etc).

然后您可以获得哪些提示"?与使用 get_type_hints 的每个参数相关,并逐个抓取参数及其命中并执行您想要的检查(不是 Annotation 的意图,尽管我也发现它很有趣).您有一个 完整示例另一个帖子.

You can then obtain which "hints" are related to every parameter using get_type_hints, and crawl one per one the parameters with its hits and do the checks you want (not the idea Annotation was intended, though I also found it interesting). You have a full example in this other post.

这篇关于python 3.9 中的类型注释约束(`ValueRange`/`MinLen` 等)在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆