Tools for static type checking in Python

2019-01-13 20:14发布

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.)

EDIT: An example:

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

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

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.

7条回答
祖国的老花朵
2楼-- · 2019-01-13 20:29

I don't know if this helps but for what it's worth, Jeremy Siek at U of Colorado did some work on gradual typing, and I found this doing a quick search. http://www.wiki.jvmlangsummit.com/pdf/28_Siek_gradual.pdf

My guess (I might be wrong) is there isn't any promising open source tools you can find at the moment since his research looks relatively new.

Your best bet might be to contact the authors and ask if they can release their code to you.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-13 20:29

There is the 'gradual' package for Python 3; see PIP or the Bitbucket Repo

Apparently this is an implementation by the group around Jeremy Siek who seems to be quite an authority in the field of gradual typing.

Some annotations are apparently necessary, here is an example:

from gradual import *

@typed
def calculate_total(a:int, b:int) -> int:
    return a + b//100

As far as annotations go, this is not so bad. I have not used the package so I cannot speak to its quality, but the syntax (and the people behind it) certainly make it look promising.

查看更多
【Aperson】
4楼-- · 2019-01-13 20:31

You may find mypy interesting. It has been proposed for inclusion in Python 3.5 by Guido.

查看更多
你好瞎i
5楼-- · 2019-01-13 20:31

I had a similar need some time ago. All the existing solutions that I've found had some problem or does not have a feature that I'd like to have, so I've made my own.

Here's how you use it:

from requiretype import require

@require(name=str, age=(int, float, long))
def greet_person(name, age):
    print "Hello {0} ({1})".format(name, age)

>>> greet_person("John", 42)
Hello John (42)

>>> greet_person("John", "Doe")
# [...traceback...]
TypeError: Doe is not a valid type.
Valid types: <type 'int'>, <type 'float'>, <type 'long'>

>>> greet_person(42, 43)
# [...traceback...]
TypeError: 42 is not a <type 'str'> type

I hope this is useful for you.

For more details look at:

P.S.: (quoting myself from github repo)

For most cases I'd recommend using tests instead of type checking since it's more natural to do that in Python. But, there are some cases where you want/need to specify a specific type to use and since python does not have type checks for parameters here's where this is useful.

查看更多
劫难
6楼-- · 2019-01-13 20:37

I like prospector, backend of landscape.io. It combines output of existing analyzers, such as pylint, pyflakes, pep8, frosted..., into one report. Neat.

查看更多
做个烂人
7楼-- · 2019-01-13 20:44

Check out this post: PySonar: a Static Analyzer for Python. PySonar is a tool that infers types using abstract interpretation (partially executing) of code. It finds all possible execution paths of your program and finds all possible types of all variables.

There are basically three versions of PySonar:

  • Open-sourced Java (Jython indexer)
  • Closed-sourced Java (Hidden in Google)
  • Open-sourced Python (mini-pysonar)

None of them (except of closed source one) is fully implemented. But the basic idea is that you can use it as a basis for your work.

查看更多
登录 后发表回答