Check the type annotations at runtime with typeguard
This commit is contained in:
parent
893b6bd8f1
commit
5f7d3108de
5 changed files with 63 additions and 0 deletions
|
|
@ -81,6 +81,9 @@ jobs:
|
|||
- name: Run the doctests
|
||||
run: uv run nox -s doctest
|
||||
|
||||
- name: Check the type annotations at runtime with `typeguard`
|
||||
run: uv run nox -s test-types
|
||||
|
||||
- name: Run the `pre-commit` hooks not covered by `nox`
|
||||
run: uv run pre-commit run --all-files --show-diff-on-failure
|
||||
env:
|
||||
|
|
|
|||
35
noxfile.py
35
noxfile.py
|
|
@ -6,6 +6,7 @@ By default, `uv run nox` runs the sessions that assure code quality:
|
|||
- "lint": Lint source files with `flake8`, `isort`, `mypy`, `pyright`, and `ruff`
|
||||
- "test": Run the test suite with `pytest`
|
||||
- "doctest": Check if code snippets in the docstrings run
|
||||
- "test-types": Run the test suite with runtime type checks by `typeguard`
|
||||
|
||||
Further sessions run on demand with `uv run nox -s <session>`:
|
||||
|
||||
|
|
@ -139,6 +140,7 @@ nox.options.sessions = (
|
|||
"lint",
|
||||
"test",
|
||||
"doctest",
|
||||
"test-types",
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -327,6 +329,39 @@ def test_docstrings(session: nox.Session) -> None:
|
|||
)
|
||||
|
||||
|
||||
@nox.session(name="test-types", python=MAIN_PYTHON)
|
||||
def test_types(session: nox.Session) -> None:
|
||||
"""Check the type annotations at runtime with `typeguard`.
|
||||
|
||||
This runs the entire `pytest` suite.
|
||||
|
||||
Unlike the "test" session, no coverage is measured
|
||||
because the instrumentation of `typeguard` distorts the branch coverage.
|
||||
|
||||
Extra arguments are forwarded to `pytest`.
|
||||
"""
|
||||
start(session)
|
||||
|
||||
install_pinned(
|
||||
session,
|
||||
".",
|
||||
"factory-boy",
|
||||
"Faker",
|
||||
"packaging",
|
||||
"pytest",
|
||||
"pytest-cov",
|
||||
"pytest-env",
|
||||
"pytest-mock",
|
||||
"typeguard",
|
||||
)
|
||||
|
||||
# Make tests/__init__.py install the import hook for `typeguard`
|
||||
session.env["TYPEGUARD"] = "true"
|
||||
|
||||
session.run("pytest", "--version")
|
||||
session.run("pytest", *(session.posargs or (TESTS_LOCATION,)))
|
||||
|
||||
|
||||
@nox.session(name="docs", python=MAIN_PYTHON)
|
||||
def build_docs(session: nox.Session) -> None:
|
||||
"""Build the documentation with `sphinx`.
|
||||
|
|
|
|||
|
|
@ -136,6 +136,7 @@ test = [
|
|||
"pytest-env (>=1.8.8,<2.0.0)",
|
||||
"pytest-mock (>=3.17.0,<4.0.0)",
|
||||
"pytest (>=9.2.0,<10.0.0)",
|
||||
"typeguard (>=4.6.1,<5.0.0)",
|
||||
"xdoctest[colors] (>=1.3.2,<2.0.0)",
|
||||
]
|
||||
|
||||
|
|
|
|||
|
|
@ -1 +1,11 @@
|
|||
"""Test the velofy package."""
|
||||
|
||||
import os
|
||||
|
||||
|
||||
# Check the type annotations of `velofy` at runtime in the `nox` session "test-types"
|
||||
# => Must happen before tests/conftest.py imports `velofy`
|
||||
if os.environ.get("TYPEGUARD"):
|
||||
import typeguard
|
||||
|
||||
typeguard.install_import_hook("velofy")
|
||||
|
|
|
|||
14
uv.lock
generated
14
uv.lock
generated
|
|
@ -3897,6 +3897,18 @@ wheels = [
|
|||
{ url = "https://files.pythonhosted.org/packages/27/b5/88caeba349f17b7fd37b61bc88f81c138b3b5149b4e2a9deec66eaad1bdb/tryceratops-2.4.1-py3-none-any.whl", hash = "sha256:271b92367be89b243918a56361618607d2a9aa4aa4ba766ecfabd5f98c00480c", size = 27378, upload-time = "2024-10-30T16:42:38.914Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "typeguard"
|
||||
version = "4.6.1"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
dependencies = [
|
||||
{ name = "typing-extensions" },
|
||||
]
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/67/2e/ff27b371f3262048aa7b2a391f655a333e75a7774a920d5fc9e257a47991/typeguard-4.6.1.tar.gz", hash = "sha256:d94fd3552c5feb6bada6e8bb1167d9916c08d7442141aeb041b847ba9bd1afb1", size = 84528, upload-time = "2026-11-20T01:16:02.285Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/04/d5/ab99dd595bd3cc307346c9c644556b627ca0bd273d3ee23041e8ac4f20dd/typeguard-4.6.1-py3-none-any.whl", hash = "sha256:aca1f365124873d5325b18aad1a3689d62f7184abe32d899413562cf51729d02", size = 37095, upload-time = "2026-11-20T01:16:01.153Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "typing-extensions"
|
||||
version = "4.16.0"
|
||||
|
|
@ -4040,6 +4052,7 @@ test = [
|
|||
{ name = "pytest-cov" },
|
||||
{ name = "pytest-env" },
|
||||
{ name = "pytest-mock" },
|
||||
{ name = "typeguard" },
|
||||
{ name = "xdoctest", extra = ["colors"] },
|
||||
]
|
||||
|
||||
|
|
@ -4122,6 +4135,7 @@ test = [
|
|||
{ name = "pytest-cov", specifier = ">=7.1.0,<8.0.0" },
|
||||
{ name = "pytest-env", specifier = ">=1.8.8,<2.0.0" },
|
||||
{ name = "pytest-mock", specifier = ">=3.17.0,<4.0.0" },
|
||||
{ name = "typeguard", specifier = ">=4.6.1,<5.0.0" },
|
||||
{ name = "xdoctest", extras = ["colors"], specifier = ">=1.3.2,<2.0.0" },
|
||||
]
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue