具有自己类型的PEP-484类型注释 [英] PEP-484 Type Annotations with own types

查看:104
本文介绍了具有自己类型的PEP-484类型注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PEP-484提供类型注释的语义. 这些非常适合a)文档和b)IDE的帮助.他们不太适合代码优化.

PEP-484 provides semantics for type annotations. These are geared very much towards a) documentation and b) help for IDEs. They are less geared towards code optimization.

例如,很遗憾,无法与Cython一起使用PEP 484注释 https://groups.google.com/d/msg/cython -users/DHcbk78rDec/6-b5XtCRGBEJ

For example, it is unfortunately not possible to use PEP 484 annotations either with Cython https://groups.google.com/d/msg/cython-users/DHcbk78rDec/6-b5XtCRGBEJ

或与Numba一起使用,后者使用诸如"float64(int32,int32)"之类的字符串形式的注释格式 http://numba.pydata.org/numba-doc/0.24 .0/reference/types.html

or with Numba, the latter using its own annotation format in the form of strings like "float64(int32, int32)" http://numba.pydata.org/numba-doc/0.24.0/reference/types.html

如何在具有自己类型的PEP 484框架内工作? 我明确地不想破坏PEP-484的语义,而是使用其他信息来扩展现有类型. 对我自己的类型检查器可见,但对任何符合PEP-484的类型检查器或IDE不可见.

How do I work within the framework of PEP 484 with my own types? I explicitly do not want to break PEP-484 semantics, but augment the existing types with additional information visible to my own type checker, but invisible to any PEP-484 conforming type checker or IDE.

以下内容是否会在PEP-484语义中解释为List [int]?

Will the following be interpreted within the PEP-484 semantics as List[int]?

class Int32(int): pass
x = [1]   # type: List[Int32]

像这样的更精美的类型怎么样?

def combine(typeA, typeB):
    class X(typeA, typeB): pass
    return X

class Metre(): pass

# is y an 'int' to PEP-484 typecheckers?
y = 1 # type: combine(Int32, Metre)

对于库进行类型提示和类型检查的任何建议吗?

Any recommendations for libraries to work with type-hinting, both for type parsing and type checking?

推荐答案

从Python 3.5开始,我们不仅拥有 PEP 483 PEP 484 ,还要实现它的 typing模块.

Since Python 3.5, we not only have the PEP 483, PEP 484, but also typing module that implements it.

为全面理解,您可能需要通读这三个文档.但是对于您的特定情况,简短的答案是,在PEP484领域中,您可以通过4种方式使用自己的类型:

For complete understanding, you might want to read through those 3 documents. But for your specific case, the short answer is that in PEP484 realm you can work with own types in 4 ways:

  1. 仅使用自己的类型进行注释,
  2. 创建类型别名
  3. 使用 NewType
  4. 使用自己的通用类型
  1. just annotate using own types,
  2. create type aliases,
  3. use NewType, or
  4. use own generic types


如果您寻求的是最重要的:


If what you seek is above all else:

我自己的类型检查器可以看到的其他信息,但任何符合PEP-484的类型检查器都看不到

additional information visible to my own type checker, but invisible to any PEP-484 conforming type checker

然后第二种方法就可以给您.如果您这样做:

then the 2nd approach gives you just that. If you do:

Int32 = int
Int64 = int

x = 0 # type: Int32
y = 0 # type: Int64

然后Int32Int64在PEP484领域中是相同的,但是您可以使用社区维护的

Then Int32 and Int64 would be the same in PEP484 realm, but you could add some additional checks by looking into the AST (Abstract Syntax Tree) of your code using community-maintained typed-ast module. That module parses type comments in addition to code, so you can read the exact annotation used, and thus get some additional type information for x and y.

而且,如果隐形不是第一要务,那么:

And, if being invisible is not the number one priority, then:

  • 而不是class Int32(int): pass我宁愿做typing.NewType('Int32', int)

而不是combine(Int32, Metre)我会使用typing.Union[Int32, Metre].

Int32 = typing.NewType('Int32', int)

class Metre:
    pass

x = [Int32(1)]  # type: List[Int32]
y = Int32(1) # type: typing.Union[Int32, Metre]

print(x[0] + 1) # ok, since Int32 is still int
y = Metre() # ok, since y can be Int32 or Metre

在上述代码上,您可以运行社区维护的静态类型检查器mypy .

On the above code, you can run community-maintained static type-checker mypy.

typed-ast

Both typed-ast and mypy are now (year 2016) under very active development. Not everything works as expected, but as far as I can see they are good enough for many use cases already, and also there seem to be no alternatives.

这篇关于具有自己类型的PEP-484类型注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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