From 67bfcbdd0b8714cae883ebda59bd9f0a02f852fc Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Mon, 1 Dec 2025 13:46:27 +0000 Subject: [PATCH] Run mypy from tox This avoids the need to duplicate our dependency list in multiple places and allows us to take advantage of tox's dependency management infrastructure, to ensure we always get the latest and greatest version of a package allowed by upper-constraints. While here, we also bump the other pre-commit hooks and update hints to accommodate typed fixtures. Change-Id: I4020374b45c8ead4b3b65651389c1e903a1de7bd Signed-off-by: Stephen Finucane --- .pre-commit-config.yaml | 32 +++---------------- oslo_utils/encodeutils.py | 2 +- oslo_utils/eventletutils.py | 2 +- oslo_utils/imageutils/qemu.py | 2 +- oslo_utils/secretutils.py | 2 +- oslo_utils/tests/base.py | 2 +- .../tests/imageutils/test_format_inspector.py | 4 +-- oslo_utils/tests/imageutils/test_qemu.py | 2 +- oslo_utils/tests/test_eventletutils.py | 2 +- oslo_utils/tests/test_secretutils.py | 2 +- oslo_utils/tests/test_strutils.py | 4 +-- oslo_utils/tests/test_timeutils.py | 2 +- oslo_utils/version.py | 3 +- pyproject.toml | 13 ++------ tox.ini | 18 ++++++++++- 15 files changed, 38 insertions(+), 54 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b7f20a15..0868aef8 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,49 +1,25 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v5.0.0 + rev: v6.0.0 hooks: - id: trailing-whitespace - # Replaces or checks mixed line ending - id: mixed-line-ending args: ['--fix', 'lf'] exclude: '.*\.(svg)$' - # Forbid files which have a UTF-8 byte-order marker - - id: check-byte-order-marker - # Checks that non-binary executables have a proper shebang + - id: fix-byte-order-marker - id: check-executables-have-shebangs - # Check for files that contain merge conflict strings. - id: check-merge-conflict - # Check for debugger imports and py37+ breakpoint() - # calls in python source - id: debug-statements - id: check-yaml files: .*\.(yaml|yml)$ - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.12.7 + rev: v0.14.8 hooks: - id: ruff-check args: ['--fix', '--unsafe-fixes'] - id: ruff-format - repo: https://opendev.org/openstack/hacking - rev: 7.0.0 + rev: 8.0.0 hooks: - id: hacking additional_dependencies: [] - - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.17.1 - hooks: - - id: mypy - additional_dependencies: - - fixtures - - iso8601 - - packaging - - pyparsing - - types-PyYAML - - types-greenlet - - types-netaddr - - types-psutil - exclude: | - (?x)( - doc/.* - | releasenotes/.* - ) diff --git a/oslo_utils/encodeutils.py b/oslo_utils/encodeutils.py index 4372b352..b0cc0302 100644 --- a/oslo_utils/encodeutils.py +++ b/oslo_utils/encodeutils.py @@ -16,7 +16,7 @@ import sys from typing import Any -import debtcollector.removals +import debtcollector.removals # type: ignore def safe_decode( diff --git a/oslo_utils/eventletutils.py b/oslo_utils/eventletutils.py index 7a6e284c..635265ad 100644 --- a/oslo_utils/eventletutils.py +++ b/oslo_utils/eventletutils.py @@ -25,7 +25,7 @@ import threading from typing import TYPE_CHECKING import warnings -import debtcollector +import debtcollector # type: ignore from oslo_utils import importutils from oslo_utils import timeutils diff --git a/oslo_utils/imageutils/qemu.py b/oslo_utils/imageutils/qemu.py index af8c40a2..e8cb9e43 100644 --- a/oslo_utils/imageutils/qemu.py +++ b/oslo_utils/imageutils/qemu.py @@ -29,7 +29,7 @@ import json import re from typing import Any -import debtcollector +import debtcollector # type: ignore from oslo_utils._i18n import _ from oslo_utils import strutils diff --git a/oslo_utils/secretutils.py b/oslo_utils/secretutils.py index 51e486ee..55cfdccf 100644 --- a/oslo_utils/secretutils.py +++ b/oslo_utils/secretutils.py @@ -25,7 +25,7 @@ import secrets import string as _string from typing import Any, cast -import debtcollector.removals +import debtcollector.removals # type: ignore @debtcollector.removals.remove( diff --git a/oslo_utils/tests/base.py b/oslo_utils/tests/base.py index a23298da..036af9d3 100644 --- a/oslo_utils/tests/base.py +++ b/oslo_utils/tests/base.py @@ -13,7 +13,7 @@ # License for the specific language governing permissions and limitations # under the License. -from oslotest import base +from oslotest import base # type: ignore class BaseTestCase(base.BaseTestCase): ... diff --git a/oslo_utils/tests/imageutils/test_format_inspector.py b/oslo_utils/tests/imageutils/test_format_inspector.py index e2a809a4..aa60e837 100644 --- a/oslo_utils/tests/imageutils/test_format_inspector.py +++ b/oslo_utils/tests/imageutils/test_format_inspector.py @@ -20,7 +20,7 @@ import subprocess import tempfile from unittest import mock -import ddt +import ddt # type: ignore from oslo_utils.imageutils import format_inspector from oslo_utils.imageutils import QemuImgInfo @@ -1161,7 +1161,7 @@ class TestFormatInspectorInfra(test_base.BaseTestCase): # Test with an expected format, but not the one we're going to # intentionally fail to make sure that we do not log failures # for non-expected formats. - self.test_wrapper_iter_like_eats_error(expected='vhd') # type: ignore + self.test_wrapper_iter_like_eats_error(expected='vhd') def test_wrapper_aborts_early(self): # Run the InspectWrapper with non-qcow2 data, expecting qcow2, first diff --git a/oslo_utils/tests/imageutils/test_qemu.py b/oslo_utils/tests/imageutils/test_qemu.py index 41e3daae..4d437004 100644 --- a/oslo_utils/tests/imageutils/test_qemu.py +++ b/oslo_utils/tests/imageutils/test_qemu.py @@ -16,7 +16,7 @@ from unittest import mock import warnings -import testscenarios +import testscenarios # type: ignore from oslo_utils import imageutils from oslo_utils.tests import base as test_base diff --git a/oslo_utils/tests/test_eventletutils.py b/oslo_utils/tests/test_eventletutils.py index 6b8006ee..e2e495cc 100644 --- a/oslo_utils/tests/test_eventletutils.py +++ b/oslo_utils/tests/test_eventletutils.py @@ -16,7 +16,7 @@ import threading from unittest import mock import warnings -import eventlet +import eventlet # type: ignore from eventlet import greenthread from oslo_utils import eventletutils diff --git a/oslo_utils/tests/test_secretutils.py b/oslo_utils/tests/test_secretutils.py index 6d8b6cd3..a4ed77fd 100644 --- a/oslo_utils/tests/test_secretutils.py +++ b/oslo_utils/tests/test_secretutils.py @@ -15,7 +15,7 @@ import hashlib import hmac -import testscenarios +import testscenarios # type: ignore from oslo_utils import secretutils from oslo_utils.tests import base as test_base diff --git a/oslo_utils/tests/test_strutils.py b/oslo_utils/tests/test_strutils.py index 5e8969e4..2fb1735e 100644 --- a/oslo_utils/tests/test_strutils.py +++ b/oslo_utils/tests/test_strutils.py @@ -19,8 +19,8 @@ import math from typing import Any from unittest import mock -import ddt -import testscenarios +import ddt # type: ignore +import testscenarios # type: ignore from oslo_utils import strutils from oslo_utils.tests import base as test_base diff --git a/oslo_utils/tests/test_timeutils.py b/oslo_utils/tests/test_timeutils.py index dc2ea73f..e886c20c 100644 --- a/oslo_utils/tests/test_timeutils.py +++ b/oslo_utils/tests/test_timeutils.py @@ -19,7 +19,7 @@ import time from unittest import mock import iso8601 -from testtools import matchers +from testtools import matchers # type: ignore from oslo_utils.tests import base as test_base from oslo_utils import timeutils diff --git a/oslo_utils/version.py b/oslo_utils/version.py index ee5e03b1..330c80b6 100644 --- a/oslo_utils/version.py +++ b/oslo_utils/version.py @@ -12,7 +12,6 @@ # License for the specific language governing permissions and limitations # under the License. - -import pbr.version +import pbr.version # type: ignore version_info = pbr.version.VersionInfo('oslo.utils') diff --git a/pyproject.toml b/pyproject.toml index 76eef8c6..6881e7ee 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -42,21 +42,14 @@ show_error_context = true strict = true # debtcollector is untyped (for now) disallow_untyped_decorators = false -exclude = ''' -(?x)( - doc - | releasenotes - ) -''' +exclude = '(?x)(doc | releasenotes)' [[tool.mypy.overrides]] -module = [ - "oslo_utils.fixture", - "oslo_utils.tests.*", -] +module = ["oslo_utils.tests.*"] disallow_untyped_calls = false disallow_untyped_defs = false disallow_subclassing_any = false +ignore_missing_imports = true [tool.ruff] line-length = 79 diff --git a/tox.ini b/tox.ini index ea87e0a8..75313e0e 100644 --- a/tox.ini +++ b/tox.ini @@ -10,11 +10,27 @@ deps = commands = stestr run --slowest {posargs} [testenv:pep8] -skip_install = true +description = + Run style checks. deps = pre-commit + {[testenv:mypy]deps} commands = pre-commit run -a + {[testenv:mypy]commands} + +[testenv:mypy] +description = + Run type checks. +deps = + {[testenv]deps} + mypy + types-greenlet + types-netaddr + types-psutil + types-PyYAML +commands = + mypy --cache-dir="{envdir}/mypy_cache" {posargs:oslo_utils} [testenv:venv] commands = {posargs}