如何指定一个属性必须是一个(比如说)整数列表,而不仅仅是一个列表? [英] How to specify that an attribute must be a list of (say) integers, not just a list?

查看:30
本文介绍了如何指定一个属性必须是一个(比如说)整数列表,而不仅仅是一个列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 attrs 库 和 Python 3.6,我认为以下内容可以让我指定xy 只能包含整数:

Using the attrs libary and Python 3.6, I thought the following would allow me to specify that x and y can only contain integers:

import attr

@attr.s
class C:
  x : List[int] = attr.ib()   # not working
  y = attr.ib(type=List[int]) # not working either

注释的两行都抛出一个 NameError: name 'List' is not defined.

Both of the commented lines throw a NameError: name 'List' is not defined.

我希望它起作用的原因是:

The reasons I expected that to work are these:

(1) attr 文档的类型部分包括以下段落:attrs 还允许您使用 attr.ib() 的类型参数或 – 从 Python 3.6 – 使用 PEP 526 注释将类型与属性相关联".然后它演示了这两种方法:

(1) The types section of the attr documentation includes the following passage: "attrs also allows you to associate a type with an attribute using either the type argument to attr.ib() or – as of Python 3.6 – using PEP 526-annotations". It then demonstrates both methods:

@attr.s
class C:
    x = attr.ib(type=int)
    y: int = attr.ib()

(2) PEP 526 声明以下类型注释的语法有效:primes: List[int] = [].

(2) PEP 526 states that the following syntax for type annotation is valid: primes: List[int] = [].

推荐答案

语法 确实有效.但是 PEP 484 添加的泛型类型注解对象不在内置命名空间中,但在 typing 模块中.

The syntax is indeed valid. But the generic type annotation objects added by PEP 484 are not in the builtins namespace, but in the typing module.

因此,您需要执行您链接的 attrs 文档中的所有示例,以及 PEP 484、PEP 483、PEP 526 和 typing 文档:

So, you need to do what all of the examples in the attrs docs you linked, and PEP 484, PEP 483, PEP 526, and the typing docs do:

from typing import List

<小时>

另外,请注意,这只是一个注释.您仍然可以编写 c = C(x=[], y=[1.0]) 并且不会得到 TypeError.正如您链接的文档所说:


Also, note that this is just an annotation. You can still write c = C(x=[], y=[1.0]) and you won't get a TypeError. As the docs you linked say:

attrs 本身还没有在类型元数据之上工作的任何功能.但是,它对于编写您自己的验证器或序列化框架很有用.

attrs itself doesn’t have any features that work on top of type metadata yet. However it’s useful for writing your own validators or serialization frameworks.

完全不清楚attrs 应该对这个元数据做什么.PEP 483/PEP 484 设计的核心部分是类型注释只不过是运行时的注释,不影响值的类型或合法的存储位置;它们仅供静态类型检查器和其他与 Python 分开运行的工具使用.

It's not at all clear what attrs should do with this metadata. It's a central part of the design of PEP 483/PEP 484 that type annotations are nothing more than annotations are runtime, and do not affect the types of values or what's legal to store where; they're only there to be used by static type checkers and other tools that runs separately from Python.

特别是,Mypy(参考标准静态类型检查器)、一些 linter 和一些 IDE 应该将此标记为错误.如果他们还不支持 attrib 注释,那么他们几乎肯定会致力于它(因为它们大致相当于 3.7/PEP 557 dataclass).

In particular, Mypy (the reference-standard static type checker), some linters, and some IDEs should flag this as an error. If they don't support attrib annotations yet, they're almost certainly working on it (since they're roughly equivalent to annotated attributes in 3.7/PEP 557 dataclass).

这篇关于如何指定一个属性必须是一个(比如说)整数列表,而不仅仅是一个列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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