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 <stephenfin@redhat.com>
Change-Id: I17d8e2d78d95c0d780dbefe084092738962016f0
This commit is contained in:
Stephen Finucane
2025-12-02 15:26:16 +00:00
parent c83131ed3e
commit e050ac6947
6 changed files with 39 additions and 33 deletions

View File

@@ -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/.*
)

View File

@@ -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":

View File

@@ -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

View File

@@ -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.*"]

View File

@@ -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

17
tox.ini
View File

@@ -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}