3.8. Type Annotation Static Analysis¶
3.8.1. MyPy¶
Type Checking
$ pip install mypy
$ mypy FILE
setup.cfg
[mypy]
strict_optional = True
3.8.2. PyType¶
Type Checking
$ pip install pytype
$ pytype -V 3.9 myfile.py
3.8.3. Pyre-check¶
Type Checking
3.8.4. PyAnnotate¶
Annotating existing code
http://mypy-lang.blogspot.com/2017/11/dropbox-releases-pyannotate-auto.html
The -w flag means "go ahead, update the file":
$ pip install pyannotate
$ pyannotate -w myfile.py
3.8.5. Monkeytype¶
Annotating existing code
$ pip install monkeytype
$ monkeytype run runtests.py
$ monkeytype stub some.module
$ monkeytype apply some.module
3.8.6. Cython¶
>>>
... import cython
...
...
... def primes(nb_primes: cython.int):
... i: cython.int
... p: cython.int[1000]
...
... if nb_primes > 1000:
... nb_primes = 1000
...
... if not cython.compiled: # Only if regular Python is running
... p = [0] * 1000 # Make p work almost like a C array
...
... len_p: cython.int = 0 # The current number of elements in p.
... n: cython.int = 2
... while len_p < nb_primes:
... # Is n prime?
... for i in p[:len_p]:
... if n % i == 0:
... break
...
... # If no break occurred in the loop, we have a prime.
... else:
... p[len_p] = n
... len_p += 1
... n += 1
...
... # Let's copy the result into a Python list:
... result_as_list = [prime for prime in p[:len_p]]
... return result_as_list
3.8.7. Mypyc¶
Mypyc compiles Python modules to C extensions.
It uses standard Python type hints to generate fast code.