From e050ac6947b3b62800f40123307ead5c749091a2 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Tue, 2 Dec 2025 15:26:16 +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. Note that the change brings in stevedore type hints, which necessitates some small fixes. Signed-off-by: Stephen Finucane Change-Id: I17d8e2d78d95c0d780dbefe084092738962016f0 --- .pre-commit-config.yaml | 26 +++++----------------- oslo_middleware/healthcheck/__init__.py | 10 +++++++-- oslo_middleware/tests/test_entry_points.py | 8 +++++-- pyproject.toml | 9 ++------ requirements.txt | 2 +- tox.ini | 17 +++++++++++++- 6 files changed, 39 insertions(+), 33 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index fa95ed5..52e0eb3 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -12,29 +12,15 @@ repos: - id: debug-statements - id: check-yaml files: .*\.(yaml|yml)$ + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.14.8 + hooks: + - id: ruff-check + args: ['--fix', '--unsafe-fixes'] + - id: ruff-format - repo: https://opendev.org/openstack/hacking rev: 8.0.0 hooks: - id: hacking additional_dependencies: [] exclude: '^(doc|releasenotes|tools)/.*$' - - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.14.5 - hooks: - - id: ruff-check - args: ['--fix', '--unsafe-fixes'] - - id: ruff-format - - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.18.2 - hooks: - - id: mypy - additional_dependencies: [ - 'types-requests', - 'types-WebOb>=1.8.0.20250822', - ] - # keep this in-sync with '[tool.mypy] exclude' in 'pyproject.toml' - exclude: | - (?x)( - doc/.* - | releasenotes/.* - ) diff --git a/oslo_middleware/healthcheck/__init__.py b/oslo_middleware/healthcheck/__init__.py index 158baff..f5c6bbc 100644 --- a/oslo_middleware/healthcheck/__init__.py +++ b/oslo_middleware/healthcheck/__init__.py @@ -38,7 +38,7 @@ import webob.response try: import greenlet except ImportError: - greenlet = None + greenlet = None # type: ignore from oslo_middleware import base from oslo_middleware.exceptions import ConfigInvalid @@ -418,6 +418,10 @@ Reason # `disable_by_file` backends are not enabled at same time. self._verify_configured_plugins() + if ty.TYPE_CHECKING: + self._backends: stevedore.NamedExtensionManager[ + pluginbase.HealthcheckBaseExtension + ] self._backends = stevedore.NamedExtensionManager( self.NAMESPACE, self._conf_get('backends'), @@ -648,7 +652,9 @@ Reason return None results = [ - ext.obj.healthcheck(req.server_port) for ext in self._backends + ext.obj.healthcheck(req.server_port) + for ext in self._backends + if ext.obj ] healthy = self._are_results_healthy(results) if req.method == "HEAD": diff --git a/oslo_middleware/tests/test_entry_points.py b/oslo_middleware/tests/test_entry_points.py index 53e59eb..0ac261a 100644 --- a/oslo_middleware/tests/test_entry_points.py +++ b/oslo_middleware/tests/test_entry_points.py @@ -10,12 +10,14 @@ # License for the specific language governing permissions and limitations # under the License. -from oslotest import base +from oslotest import base as test_base import stevedore from testtools import matchers +from oslo_middleware import base -class TestPasteDeploymentEntryPoints(base.BaseTestCase): + +class TestPasteDeploymentEntryPoints(test_base.BaseTestCase): def test_entry_points(self): factory_classes = { 'catch_errors': 'CatchErrors', @@ -27,6 +29,7 @@ class TestPasteDeploymentEntryPoints(base.BaseTestCase): 'sizelimit': 'RequestBodySizeLimiter', } + em: stevedore.ExtensionManager[base.ConfigurableMiddleware] em = stevedore.ExtensionManager('paste.filter_factory') # Ensure all the factories are defined by their names @@ -40,6 +43,7 @@ class TestPasteDeploymentEntryPoints(base.BaseTestCase): 'enable_by_files': 'EnableByFilesHealthcheck', } + em: stevedore.ExtensionManager[base.ConfigurableMiddleware] em = stevedore.ExtensionManager('oslo.middleware.healthcheck') # Ensure all the healthcheck plugins are defined by their names diff --git a/pyproject.toml b/pyproject.toml index 44a3a34..d595748 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -78,13 +78,8 @@ python_version = "3.10" show_column_numbers = true show_error_context = true strict = true -# keep this in-sync with 'mypy.exclude' in '.pre-commit-config.yaml' -exclude = ''' -(?x)( - doc - | releasenotes - ) -''' +ignore_missing_imports = true +exclude = '(?x)(doc | releasenotes)' [[tool.mypy.overrides]] module = ["oslo_middleware.tests.*"] diff --git a/requirements.txt b/requirements.txt index 7d97f02..4829346 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,6 +10,6 @@ oslo.i18n>=3.15.3 # Apache-2.0 oslo.utils>=3.33.0 # Apache-2.0 pbr>=2.0.0 # Apache-2.0 statsd>=3.2.1 # MIT -stevedore>=1.20.0 # Apache-2.0 +stevedore>=5.6.0 # Apache-2.0 typing-extensions>=4.11.0 # PSF-2.0 WebOb>=1.8.0 # MIT diff --git a/tox.ini b/tox.ini index cc098f1..ca63aa7 100644 --- a/tox.ini +++ b/tox.ini @@ -9,11 +9,26 @@ 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-requests + types-WebOb>=1.8.0.20250822 +commands = + mypy --cache-dir="{envdir}/mypy_cache" {posargs:oslo_middleware} [testenv:venv] commands = {posargs}