Integrate mypy type checker

Use mypy for type checking Octavia code and add basic typing
annotations.

- Add octavia-mypy job
- Add new mypy tox testenv
- Fix mypy errors and add basic typing annotations

The report that the new mypy generates shows which areas of the code
contain typing information and which don't. This should serve as a
starting point and over time we should gradually add more typing
information as we integrate new changes into our code base.

Apart from the benefit of finding possible bugs using mypy,
type annotations also have other benefits like for instance:

- IDEs like PyCharm and VS Code can use annotations to provide better
  code completion, highlight errors and to make coding more efficient
- It serves as documentation of the code and helps to understand code
  better. The annotation syntax is very easy to read.
- Sphinx generated module documentation can use this information
  (without the need for additional type info in comments/docstrings)

The syntax used here should be supported by Python 3.6, so it should
not create issues when backporting patches in the future.

Test code is excluded from checks.

Closes-Bug: #2017974
Change-Id: Id8b1c2109abd199b13374094ea64a4cacb34085b
This commit is contained in:
Tom Weininger 2024-01-10 09:37:03 +01:00
parent c38c458413
commit 753184cca7
6 changed files with 50 additions and 3 deletions

View File

@ -0,0 +1,8 @@
- hosts: all
name: Copy mypy report
tasks:
- synchronize:
dest: "{{ zuul.executor.work_root }}/logs/"
mode: pull
src: "/home/zuul/src/opendev.org/openstack/octavia/mypy-report"
verify_host: true

View File

@ -116,5 +116,22 @@ redis =
redis>=3.4.0
# Required in case of AmphoraV2 zookeeper jobboard is used
zookeeper =
kazoo>=2.6.0 # Apache-2.0
zake>=0.1.6 # Apache-2.0
kazoo>=2.6.0 # Apache-2.0
zake>=0.1.6 # Apache-2.0
[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
show_error_codes = true
pretty = true
html_report = mypy-report
no_implicit_optional = true
packages = octavia
[mypy-octavia.tests.*]
ignore_errors = True

View File

@ -20,3 +20,9 @@ tempest>=23.0.0 # Apache-2.0
sphinx>=2.0.0,!=2.1.0 # BSD
bashate>=0.5.1 # Apache-2.0
WebTest>=2.0.26 # MIT
mypy>=1.1.1 # MIT
lxml>=4.5.2 # BSD
types-python-dateutil>=2.8.19.20240106 # Apache-2.0
types-requests>=2.31.0.6 # Apache-2.0
types-setuptools>=69.0.0.20240125 # Apache-2.0
types-simplejson>=3.19.0.2 # Apache-2.0

View File

@ -241,4 +241,10 @@ deps = pip-check-reqs<=2.2.2
pip<=21.1.3
-r{toxinidir}/requirements.txt
commands =
pip-extra-reqs --ignore-file=octavia/tests/* octavia
pip-extra-reqs --ignore-file=octavia/tests/* octavia
[testenv:mypy]
description =
Run type checks.
commands =
mypy {posargs}

View File

@ -268,3 +268,11 @@
OCTAVIA_AMP_DISTRIBUTION_RELEASE_ID: 9-stream
OCTAVIA_AMP_IMAGE_SIZE: 3
OCTAVIA_AMP_ENABLE_FIPS: True
- job:
name: octavia-mypy
parent: openstack-tox
vars:
tox_envlist: mypy
tox_inline_comments: false
post-run: playbooks/mypy-report.yaml

View File

@ -53,6 +53,8 @@
irrelevant-files: *irrelevant-files
- octavia-v2-dsvm-tls-barbican:
irrelevant-files: *irrelevant-files
- octavia-mypy:
irrelevant-files: *irrelevant-files
- octavia-grenade:
irrelevant-files: &grenade-irrelevant-files
- ^.*\.rst$