tox: Integrate mypy

mypy is an experimental optional static type checker for Python that
aims to combine the benefits of dynamic (or "duck") typing and static
typing. While still in development, most features are supported and it's
already being used by real world projects like Sphinx. Let's start small
by integrating it into some of the interfaces that nova exposes. We can
further build upon this if it works out.

This change sets up the boilerplate necessary to use mypy in nova. Type
annotations are not included for any module - these will be added
separately. We're calling mypy by way of a script, as this allows us to
store a list of files that we have converted while we're in the process
of adding type annotations where necessary.

Change-Id: I75ab46a6768c4ca2050fdde2b7f8eeb90724c8c6
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
This commit is contained in:
Stephen Finucane 2017-06-02 16:45:41 +01:00 committed by Stephen Finucane
parent 575a91ff5b
commit 34ecf5ab91
7 changed files with 43 additions and 0 deletions

1
.gitignore vendored
View File

@ -9,6 +9,7 @@
*~ *~
.autogenerated .autogenerated
.coverage .coverage
.mypy_cache
.nova-venv .nova-venv
.project .project
.pydevproject .pydevproject

View File

@ -57,6 +57,7 @@ monotonic==1.4
msgpack==0.5.6 msgpack==0.5.6
msgpack-python==0.5.6 msgpack-python==0.5.6
munch==2.2.0 munch==2.2.0
mypy==0.761
netaddr==0.7.18 netaddr==0.7.18
netifaces==0.10.4 netifaces==0.10.4
networkx==1.11 networkx==1.11

1
mypy-files.txt Normal file
View File

@ -0,0 +1 @@
nova/virt/libvirt/__init__.py

View File

@ -84,3 +84,12 @@ console_scripts =
wsgi_scripts = wsgi_scripts =
nova-api-wsgi = nova.api.openstack.compute.wsgi:init_application nova-api-wsgi = nova.api.openstack.compute.wsgi:init_application
nova-metadata-wsgi = nova.api.metadata.wsgi:init_application nova-metadata-wsgi = nova.api.metadata.wsgi:init_application
[mypy]
show_column_numbers = true
show_error_context = true
ignore_missing_imports = true
follow_imports = skip
incremental = true
check_untyped_defs = true
warn_unused_ignores = true

View File

@ -3,6 +3,7 @@
# process, which may cause wedges in the gate later. # process, which may cause wedges in the gate later.
hacking>=3.0.1,<3.1.0 # Apache-2.0 hacking>=3.0.1,<3.1.0 # Apache-2.0
mypy>=0.761 # MIT
coverage!=4.4,>=4.0 # Apache-2.0 coverage!=4.4,>=4.0 # Apache-2.0
ddt>=1.0.1 # MIT ddt>=1.0.1 # MIT
fixtures>=3.0.0 # Apache-2.0/BSD fixtures>=3.0.0 # Apache-2.0/BSD

22
tools/mypywrap.sh Normal file
View File

@ -0,0 +1,22 @@
#!/bin/sh
#
# A wrapper around mypy that allows us to specify what files to run 'mypy' type
# checks on. Intended to be invoked via tox:
#
# tox -e mypy
#
# Eventually this should go away once we have either converted everything or
# converted enough and ignored [1] the rest.
#
# [1] http://mypy.readthedocs.io/en/latest/config_file.html#per-module-flags
ROOT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if [ $# -eq 0 ]; then
# if no arguments provided, use the standard converted lists
lines=$(grep -v '#' $ROOT_DIR/../mypy-files.txt)
python -m mypy ${lines[@]}
else
# else test what the user asked us to
python -m mypy $@
fi

View File

@ -38,11 +38,19 @@ commands =
env TEST_OSPROFILER=1 stestr run --combine --no-discover 'nova.tests.unit.test_profiler' env TEST_OSPROFILER=1 stestr run --combine --no-discover 'nova.tests.unit.test_profiler'
stestr slowest stestr slowest
[testenv:mypy]
description =
Run type checks.
envdir = {toxworkdir}/shared
commands =
bash tools/mypywrap.sh {posargs}
[testenv:pep8] [testenv:pep8]
description = description =
Run style checks. Run style checks.
envdir = {toxworkdir}/shared envdir = {toxworkdir}/shared
commands = commands =
{[testenv:mypy]commands}
bash tools/flake8wrap.sh {posargs} bash tools/flake8wrap.sh {posargs}
# Check that all JSON files don't have \r\n in line. # Check that all JSON files don't have \r\n in line.
bash -c "! find doc/ -type f -name *.json | xargs grep -U -n $'\r'" bash -c "! find doc/ -type f -name *.json | xargs grep -U -n $'\r'"