Check the type annotations at runtime with typeguard #15
11 changed files with 100 additions and 29 deletions
|
|
@ -78,10 +78,13 @@ jobs:
|
|||
- name: Run the test suite
|
||||
run: uv run nox -s test
|
||||
|
||||
- name: Run the doctests
|
||||
run: uv run nox -s doctest
|
||||
- name: Test the code snippets in the docstrings with `xdoctest`
|
||||
run: uv run nox -s test-docstrings
|
||||
|
||||
- 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:
|
||||
SKIP: local-format,local-lint,local-test,local-doctest,no-commit-to-branch
|
||||
SKIP: local-format,local-lint,local-test,local-test-docstrings,local-test-types,no-commit-to-branch
|
||||
|
|
|
|||
|
|
@ -47,9 +47,18 @@ repos:
|
|||
always_run: true
|
||||
require_serial: true
|
||||
|
||||
- id: local-doctest
|
||||
- id: local-test-docstrings
|
||||
name: Run the doctests
|
||||
entry: uv run nox -s doctest
|
||||
entry: uv run nox -s test-docstrings
|
||||
language: unsupported
|
||||
stages: [pre-merge-commit]
|
||||
pass_filenames: false # => No `--` at the end of the "entry"
|
||||
always_run: true
|
||||
require_serial: true
|
||||
|
||||
- id: local-test-types
|
||||
name: Check the type annotations at runtime
|
||||
entry: uv run nox -s test-types
|
||||
language: unsupported
|
||||
stages: [pre-merge-commit]
|
||||
pass_filenames: false # => No `--` at the end of the "entry"
|
||||
|
|
|
|||
57
noxfile.py
57
noxfile.py
|
|
@ -5,7 +5,8 @@ By default, `uv run nox` runs the sessions that assure code quality:
|
|||
- "format": Format source files with `autoflake`, `black`, `flynt`, `isort`, and `ruff`
|
||||
- "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-docstrings": 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>`:
|
||||
|
||||
|
|
@ -93,6 +94,19 @@ SCRIPTS_LOCATION = "scripts/"
|
|||
|
||||
TESTS_LOCATION = "tests/"
|
||||
|
||||
# Packages the "test" and "test-types" sessions need to run the test suite
|
||||
TEST_DEPENDENCIES = (
|
||||
# The tests import `velofy`, so it and its dependencies must be installed
|
||||
".", # => A non-editable copy as it would really be installed
|
||||
"factory-boy",
|
||||
"Faker",
|
||||
"packaging",
|
||||
"pytest",
|
||||
"pytest-cov",
|
||||
"pytest-env",
|
||||
"pytest-mock",
|
||||
)
|
||||
|
||||
# Locations that `mypy` and `pyright` always check as a whole
|
||||
TYPECHECK_LOCATIONS = (
|
||||
NOXFILE_LOCATION,
|
||||
|
|
@ -109,7 +123,6 @@ SRC_LOCATIONS = (
|
|||
TESTS_LOCATION,
|
||||
)
|
||||
|
||||
|
||||
SUPPORTED_PYTHONS = load_supported_python_versions()
|
||||
|
||||
# Must be listed in the "classifiers", which is checked at the bottom
|
||||
|
|
@ -138,7 +151,8 @@ nox.options.sessions = (
|
|||
"format",
|
||||
"lint",
|
||||
"test",
|
||||
"doctest",
|
||||
"test-docstrings",
|
||||
"test-types",
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -274,18 +288,7 @@ def test_code(session: nox.Session) -> None:
|
|||
"""
|
||||
start(session)
|
||||
|
||||
install_pinned(
|
||||
session,
|
||||
# The tests import `velofy`, so it and its dependencies must be installed
|
||||
".", # => A non-editable copy as it would really be installed
|
||||
"factory-boy",
|
||||
"Faker",
|
||||
"packaging",
|
||||
"pytest",
|
||||
"pytest-cov",
|
||||
"pytest-env",
|
||||
"pytest-mock",
|
||||
)
|
||||
install_pinned(session, *TEST_DEPENDENCIES)
|
||||
|
||||
session.run("pytest", "--version")
|
||||
|
||||
|
|
@ -302,7 +305,7 @@ def test_code(session: nox.Session) -> None:
|
|||
session.run("pytest", *(session.posargs or pytest_default_args))
|
||||
|
||||
|
||||
@nox.session(name="doctest", python=MAIN_PYTHON)
|
||||
@nox.session(name="test-docstrings", python=MAIN_PYTHON)
|
||||
def test_docstrings(session: nox.Session) -> None:
|
||||
"""Test the code snippets in the docstrings with `xdoctest`.
|
||||
|
||||
|
|
@ -327,6 +330,28 @@ 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, *TEST_DEPENDENCIES, "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)",
|
||||
]
|
||||
|
||||
|
|
|
|||
|
|
@ -22,7 +22,6 @@ class Location:
|
|||
|
||||
def __init__(self, latitude: float, longitude: float) -> None:
|
||||
"""Create a location from a WGS84-conforming `latitude`-`longitude` pair."""
|
||||
# The SQLAlchemy columns come as `Decimal`s due to the `DOUBLE_PRECISION`.
|
||||
self._latitude = float(latitude)
|
||||
self._longitude = float(longitude)
|
||||
|
||||
|
|
|
|||
|
|
@ -255,7 +255,7 @@ class OrderHistory:
|
|||
pixel_id: int,
|
||||
predict_at: dt.datetime,
|
||||
train_horizon: int,
|
||||
) -> tuple["pd.Series[int]", int, "pd.Series[int]"]:
|
||||
) -> tuple[pd.Series, int, pd.Series]:
|
||||
"""Slice a horizontal time series out of the `.totals`.
|
||||
|
||||
Create a time series covering `train_horizon` weeks that can be used
|
||||
|
|
@ -331,7 +331,7 @@ class OrderHistory:
|
|||
pixel_id: int,
|
||||
predict_day: dt.date,
|
||||
train_horizon: int,
|
||||
) -> tuple["pd.Series[int]", int, "pd.Series[int]"]:
|
||||
) -> tuple[pd.Series, int, pd.Series]:
|
||||
"""Slice a vertical time series out of the `.totals`.
|
||||
|
||||
Create a time series covering `train_horizon` weeks that can be used
|
||||
|
|
@ -424,7 +424,7 @@ class OrderHistory:
|
|||
pixel_id: int,
|
||||
predict_at: dt.datetime,
|
||||
train_horizon: int,
|
||||
) -> tuple["pd.Series[int]", int, "pd.Series[int]"]:
|
||||
) -> tuple[pd.Series, int, pd.Series]:
|
||||
"""Slice a vertical real-time time series out of the `.totals`.
|
||||
|
||||
Create a time series covering `train_horizon` weeks that can be used
|
||||
|
|
@ -557,7 +557,7 @@ class OrderHistory:
|
|||
# `+1` as both `first_day` and `last_day` are included.
|
||||
n_days: int = (last_day - first_day).days + 1
|
||||
|
||||
return round(training_ts.sum() / n_days, 1)
|
||||
return round(float(training_ts.sum()) / n_days, 1)
|
||||
|
||||
def choose_tactical_model(
|
||||
self,
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
|
|||
|
|
@ -5,11 +5,13 @@ in the CLI layer need access to the database.
|
|||
"""
|
||||
|
||||
import os
|
||||
import random
|
||||
|
||||
import pytest
|
||||
import sqlalchemy as sa
|
||||
from alembic import command as alembic_cmd
|
||||
from alembic import config as alembic_config
|
||||
from factory import random as factory_random
|
||||
from sqlalchemy import orm
|
||||
|
||||
from velofy import config as pkg_config
|
||||
|
|
@ -111,3 +113,10 @@ def db_session(db_connection):
|
|||
session.close()
|
||||
|
||||
transaction.rollback()
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _seed_random() -> None:
|
||||
"""Make the fake data reproducible across test runs."""
|
||||
random.seed(42)
|
||||
factory_random.reseed_random(42)
|
||||
|
|
|
|||
|
|
@ -32,8 +32,8 @@ class AddressFactory(alchemy.SQLAlchemyModelFactory):
|
|||
)
|
||||
|
||||
# Place the addresses somewhere in downtown Paris.
|
||||
latitude = factory.Faker("coordinate", center=48.855, radius=0.01)
|
||||
longitude = factory.Faker("coordinate", center=2.34, radius=0.03)
|
||||
latitude = factory.Faker("pyfloat", min_value=48.845, max_value=48.865)
|
||||
longitude = factory.Faker("pyfloat", min_value=2.31, max_value=2.37)
|
||||
# city -> set by the `make_address` fixture as there is only one `city`
|
||||
city_name = "Paris"
|
||||
zip_code = factory.LazyFunction(lambda: random.randint(75001, 75020))
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
import datetime as dt
|
||||
|
||||
import pandas as pd
|
||||
import pytest
|
||||
|
||||
from tests import config as test_config
|
||||
|
|
@ -51,7 +52,7 @@ class TestProperties:
|
|||
that is cached into `OrderHistory._data`, which must be unset first.
|
||||
"""
|
||||
monkeypatch.setattr(order_history, "_data", None)
|
||||
sentinel = object()
|
||||
sentinel = pd.DataFrame()
|
||||
monkeypatch.setattr(order_history, "aggregate_orders", lambda: sentinel)
|
||||
|
||||
result1 = order_history.totals
|
||||
|
|
|
|||
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