Python中的静态类型检查工具 [英] Tools for static type checking in Python

查看:216
本文介绍了Python中的静态类型检查工具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用现有的大型Python代码库,并希望开始添加类型注释,以便获得某种程度的静态检查.我在想像 Erlang

I'm working with a large existing Python codebase and would like to start adding in type annotations so I can get some level of static checking. I'm imagining something like Erlang, Strongtalk, or Typed Scheme/Racket.

我见过一些肮脏的装饰器,它们会根据函数参数插入动态检查并返回类型注释,但是我正在寻找一种更健壮的东西,并在编译时执行检查.

I've seen quick-and-dirty decorators that insert dynamic checks based on function parameter and return type annotations, but I'm looking for something that is more robust and that performs checks at compile-time.

现在可以使用哪些工具来进行此类操作?我熟悉编译器和类型检查,并且如果有良好的基础,我肯定愿意改进不完整的工具.

What tools are available right now for this kind of thing? I'm familiar with compilers and type checking and am definitely willing to improve an incomplete tool if it has a good foundation.

(注意:我对静态类型的优点/缺点的讨论不感兴趣.)

(Note: I'm not interested in a discussion of the pros/cons of static typing.)

编辑:示例:

def put(d, k, v):
   d[k] = v

我希望能够将put函数注释为具有put<K,V>(dict<K,V>, K, V) -> None类型.

I'd like to be able to annotate the put function as having type put<K,V>(dict<K,V>, K, V) -> None.

更新:新的 PEP 484 (2014年9月)定义了Python 3.5+中的静态类型和类型注释的标准.有一个与PEP 484兼容的名为 mypy 的类型检查工具.

UPDATE: The new PEP 484 (Sep 2014) defines a standard for static typing and type annotations in Python 3.5+. There's a type-checking tool called mypy that is compatible with PEP 484.

推荐答案

编辑2016-11-11:只需使用 PEP 484 类型提示.类型仍然可以在Python 2中使用特殊注释来表示. Guido喜欢它.

Edit 2016-11-11: Just use mypy. Type hints can be added gradually. In Python 3 source code, it verifies standard PEP 484 type hints. Types can still be expressed in Python 2 using special comments. Guido likes it.

这篇文章最初是很久以前写的,当时还没有mypy成为事实.即使帖子不太准确,我也将其保留在下面.

This post was originally written a long time ago before mypy was a thing. I've preserved the post's original content below, even though it isn't quite accurate.

原始帖子:

您可能想查看本中提到的一些项目有关Python静态分析的相关StackOverflow帖子.

总结:

  • pylint
  • PyFlakes
  • PyChecker

由于Python广泛使用鸭子输入,因此在其他情况下,这些可能被称为类型错误"语言可能最终成为Python中的对象 X 不支持方法 Y ".

Since Python uses duck typing extensively, things that might be called "type errors" in other languages might end up being "object X doesn't support method Y" in Python.

编辑2011-05-17:

我同意delnan的观点,即Python [显然错误] 无法进行静态类型输入.但是由于我们的怀疑似乎并没有阻止您,所以我只能为您提供有关该主题的更多信息.我提出:

I agree with delnan that static typing is not possible for Python [apparently wrong]. But since our skepticism doesn't seem to deter you, I can only give you more information on the subject. I present:

  • A discussion of type inference for Python. (Other links are from here.)
  • Guido van van Rossum's articles on adding optional static typing: part 1 and part 2.
  • RPython, a subset of Python that might stand a chance of being statically analyzed enough to do some form of type checking.

这篇关于Python中的静态类型检查工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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