问题描述
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 Cythonhttps://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 informationvisible 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:
- just annotate using own types,
- create type aliases,
- use
NewType
, or - use own generic types
如果您寻求的是最重要的:
If what you seek is above all else:
然后第二种方法就可以给您.如果您这样做:
then the 2nd approach gives you just that. If you do:
Int32 = int
Int64 = int
x = 0 # type: Int32
y = 0 # type: Int64
然后Int32
和Int64
在PEP484领域中是相同的,但是您可以使用社区维护的 typed-ast
模块.该模块除了分析代码外还分析类型注释,因此您可以阅读所使用的 exact 批注,从而获得有关x
和y
的一些其他类型信息.
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
和 mypy
现在(2016年)处于非常活跃的开发阶段.并非一切都按预期工作,但据我所知,它们已经足以应付许多用例,而且似乎没有其他选择.
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类型注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!