From 8abc93987e062986c1d1a59eef55e3ec939984d3 Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Tue, 1 Dec 2026 15:27:53 +0100 Subject: [PATCH 1/8] Generate the fake coordinates as `float`s Also, remove the misleading comment in `Location` --- src/velofy/db/utils/locations.py | 1 - tests/db/fake_data/factories/addresses.py | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/velofy/db/utils/locations.py b/src/velofy/db/utils/locations.py index 8b95cba..2859f94 100644 --- a/src/velofy/db/utils/locations.py +++ b/src/velofy/db/utils/locations.py @@ -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) diff --git a/tests/db/fake_data/factories/addresses.py b/tests/db/fake_data/factories/addresses.py index b519e46..fb52c42 100644 --- a/tests/db/fake_data/factories/addresses.py +++ b/tests/db/fake_data/factories/addresses.py @@ -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.47.3 From f92fd35ce359416aa693a3a8cd7c55053820c6b9 Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Tue, 1 Dec 2026 15:31:58 +0100 Subject: [PATCH 2/8] Revert to bare `pd.Series` in `OrderHistory` `typeguard` evaluates the annotations at runtime where `pd.Series[int]` raises a `TypeError` --- src/velofy/forecasts/timify.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/velofy/forecasts/timify.py b/src/velofy/forecasts/timify.py index cc49669..bb9ab81 100644 --- a/src/velofy/forecasts/timify.py +++ b/src/velofy/forecasts/timify.py @@ -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, -- 2.47.3 From e4711875cad0de93c2e42d7502ebaf115df8c687 Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Tue, 1 Dec 2026 15:46:54 +0100 Subject: [PATCH 3/8] Use a `pd.DataFrame` as the sentinel in `test_totals_is_cached` --- tests/forecasts/timify/test_order_history.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/forecasts/timify/test_order_history.py b/tests/forecasts/timify/test_order_history.py index c122628..fa5b40c 100644 --- a/tests/forecasts/timify/test_order_history.py +++ b/tests/forecasts/timify/test_order_history.py @@ -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 -- 2.47.3 From 893b6bd8f1692bef672b8f736288579e432a722d Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Tue, 1 Dec 2026 15:51:24 +0100 Subject: [PATCH 4/8] Seed the random fake data in the tests --- tests/conftest.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/conftest.py b/tests/conftest.py index 7264128..754f515 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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) -- 2.47.3 From 5f7d3108de7e3239a55c8f470fbc9bfd576cbc58 Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Tue, 1 Dec 2026 17:07:00 +0100 Subject: [PATCH 5/8] Check the type annotations at runtime with `typeguard` --- .forgejo/workflows/ci.yaml | 3 +++ noxfile.py | 35 +++++++++++++++++++++++++++++++++++ pyproject.toml | 1 + tests/__init__.py | 10 ++++++++++ uv.lock | 14 ++++++++++++++ 5 files changed, 63 insertions(+) diff --git a/.forgejo/workflows/ci.yaml b/.forgejo/workflows/ci.yaml index f973814..befe23e 100644 --- a/.forgejo/workflows/ci.yaml +++ b/.forgejo/workflows/ci.yaml @@ -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: diff --git a/noxfile.py b/noxfile.py index f87028f..4e47c08 100644 --- a/noxfile.py +++ b/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 `: @@ -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`. diff --git a/pyproject.toml b/pyproject.toml index 8afe366..09349c4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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)", ] diff --git a/tests/__init__.py b/tests/__init__.py index 231e3b6..6172a6a 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -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") diff --git a/uv.lock b/uv.lock index 4338e67..b2c5c49 100644 --- a/uv.lock +++ b/uv.lock @@ -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" }, ] -- 2.47.3 From 2f313e7e3ab33f92ea6b99ba0554e026dbb88191 Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Tue, 1 Dec 2026 17:07:45 +0100 Subject: [PATCH 6/8] Pull out the `TEST_DEPENDENCIES` --- noxfile.py | 40 +++++++++++++++------------------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/noxfile.py b/noxfile.py index 4e47c08..94548e3 100644 --- a/noxfile.py +++ b/noxfile.py @@ -94,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, @@ -110,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 @@ -276,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") @@ -342,18 +343,7 @@ def test_types(session: nox.Session) -> None: """ start(session) - install_pinned( - session, - ".", - "factory-boy", - "Faker", - "packaging", - "pytest", - "pytest-cov", - "pytest-env", - "pytest-mock", - "typeguard", - ) + install_pinned(session, *TEST_DEPENDENCIES, "typeguard") # Make tests/__init__.py install the import hook for `typeguard` session.env["TYPEGUARD"] = "true" -- 2.47.3 From f47c87f51f91db8280787994084f0e6c42b8583a Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Tue, 1 Dec 2026 17:14:47 +0100 Subject: [PATCH 7/8] Add the "test-types" session as a `pre-commit` hook --- .forgejo/workflows/ci.yaml | 2 +- .pre-commit-config.yaml | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/.forgejo/workflows/ci.yaml b/.forgejo/workflows/ci.yaml index befe23e..3d1ebb5 100644 --- a/.forgejo/workflows/ci.yaml +++ b/.forgejo/workflows/ci.yaml @@ -87,4 +87,4 @@ jobs: - 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-doctest,local-test-types,no-commit-to-branch diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 12a17a2..d639534 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -56,6 +56,15 @@ repos: 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" + always_run: true + require_serial: true + # Keep requirements.txt in sync with uv.lock - repo: https://github.com/astral-sh/uv-pre-commit rev: 0.13.10 -- 2.47.3 From cfd8d3304925bf0182258461dd5b96029c7b04ef Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Tue, 1 Dec 2026 17:14:48 +0100 Subject: [PATCH 8/8] Rename the "doctest" session into "test-docstrings" --- .forgejo/workflows/ci.yaml | 6 +++--- .pre-commit-config.yaml | 4 ++-- noxfile.py | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/.forgejo/workflows/ci.yaml b/.forgejo/workflows/ci.yaml index 3d1ebb5..3393ed8 100644 --- a/.forgejo/workflows/ci.yaml +++ b/.forgejo/workflows/ci.yaml @@ -78,8 +78,8 @@ 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 @@ -87,4 +87,4 @@ jobs: - 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,local-test-types,no-commit-to-branch + SKIP: local-format,local-lint,local-test,local-test-docstrings,local-test-types,no-commit-to-branch diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d639534..312084d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -47,9 +47,9 @@ 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" diff --git a/noxfile.py b/noxfile.py index 94548e3..f4b38a4 100644 --- a/noxfile.py +++ b/noxfile.py @@ -5,7 +5,7 @@ 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 `: @@ -151,7 +151,7 @@ nox.options.sessions = ( "format", "lint", "test", - "doctest", + "test-docstrings", "test-types", ) @@ -305,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`. -- 2.47.3