From 8cb9133ed4918aae9f02210ef4dc2f9c22278674 Mon Sep 17 00:00:00 2001 From: Peter Corke Date: Fri, 20 Nov 2026 12:52:00 +1000 Subject: [PATCH 1/2] ci: run mypy and check the wheel carries py.typed A typecheck job, as in bdsim, part of the required All tests passed check. The package passes mypy with no errors, so any new error fails the build. CONTRIBUTING.md said pyright, which nothing runs and which has no configuration; AGENTS.md said mypy was not in CI. Fixes #40 Co-Authored-By: Claude Sonnet 5.5 --- .github/CONTRIBUTING.md | 2 +- .github/workflows/ci.yml | 40 +++++++++++++++++++++++++++++++++++++++- AGENTS.md | 5 +++-- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 6654c28b..0df0a196 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -18,7 +18,7 @@ Issues labelled [`good first issue`](https://github.com/petercorke/machinevision - Keep pull requests scoped to a single feature or fix. If you have several unrelated changes, open separate PRs so each can be reviewed and merged independently. - For API changes, propose the change in [Discussions](https://github.com/petercorke/machinevision-toolbox-python/discussions) before opening a PR. - PR titles follow [Conventional Commits](https://www.conventionalcommits.org/) (`type: description`) โ€” checked automatically on the PR. -- Code is linted and import-sorted with [ruff](https://docs.astral.sh/ruff/); configuration is in `pyproject.toml` under `[tool.ruff]`. Type checking is done with [pyright](https://microsoft.github.io/pyright/) (`[tool.pyright]`). +- Code is linted and import-sorted with [ruff](https://docs.astral.sh/ruff/); configuration is in `pyproject.toml` under `[tool.ruff]`. Type checking is done with [mypy](https://mypy.readthedocs.io/) (`[tool.mypy]`); run `mypy` from the repo root. The package passes with no errors, and CI fails if that changes. - New or changed code should be type-hinted using modern syntax (`X | Y`, `X | None`, `list[X]`, `dict[K, V]` โ€” not `Union`, `Optional`, `List`, `Dict`). - Docstrings use reST style (`:param:`, `:returns:`). Type hints in the function signature already cover types, so `:type:`/`:rtype:` are rarely needed. - Any code change should be covered by tests and must not break existing ones. Tests live in `tests/`. Install the dev dependencies with `pip install -e '.[dev]'` (add `,docs` too if you're touching documentation), then run: diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f8cd6d99..68cf7998 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -84,9 +84,43 @@ jobs: MPLBACKEND: Agg run: python -m pytest tests/test_bundleadjust.py --timeout=50 --timeout-method=thread + typecheck: + name: typecheck and build wheel + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7 + + - uses: actions/setup-python@v7 + with: + python-version: "3.12" + + - name: Install package and development dependencies + run: | + python -m pip install --upgrade pip + pip install -e .[dev] + pip install build + + # configuration is in pyproject.toml, [tool.mypy]; the package must stay at zero errors + - name: Run mypy + run: mypy + + - name: Build wheel and verify typed marker + run: | + python -m build --wheel + python - <<'PY' + from pathlib import Path + from zipfile import ZipFile + + wheel = next(Path("dist").glob("machinevision_toolbox_python-*.whl")) + with ZipFile(wheel) as archive: + assert "machinevisiontoolbox/py.typed" in archive.namelist(), ( + f"{wheel.name} is missing machinevisiontoolbox/py.typed" + ) + PY + all-tests-passed: name: All tests passed - needs: [test, pgraph-compat] + needs: [test, pgraph-compat, typecheck] runs-on: ubuntu-latest if: always() steps: @@ -100,6 +134,10 @@ jobs: echo "pgraph compat did not succeed: ${{ needs.pgraph-compat.result }}" exit 1 fi + if [ "${{ needs.typecheck.result }}" != "success" ]; then + echo "typecheck did not succeed: ${{ needs.typecheck.result }}" + exit 1 + fi codecov: needs: test diff --git a/AGENTS.md b/AGENTS.md index cb9a9cd4..1752b4c3 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -44,8 +44,9 @@ description and ask first. exists, tracked via `tech-debt`-labelled issues (including a "mypy not in CI" entry) โ€” see ecosystem `AGENTS.md` ยง5 for how that target is framed. - Type checking: `mypy` (config in `pyproject.toml` under `[tool.mypy]`; run it from the repo - root with no arguments). The package passes with zero errors and must stay that way. It is - not yet run in CI (tracked as Issue 40), so run it yourself before opening a PR. Write + root with no arguments). The package passes with zero errors and must stay that way. CI runs + it (the `typecheck` job, part of the required `All tests passed` check), so run it yourself + before opening a PR. Write ignores as `# type: ignore[code]` with the specific error code; unused ignores are reported. Import from the defining submodule (for example `machinevisiontoolbox.base.graphics`), not through the package's star imports, which mypy cannot follow. From c1adc7c12280de540a5546b992235c2c83e8f739 Mon Sep 17 00:00:00 2001 From: Peter Corke Date: Fri, 20 Nov 2026 13:03:08 +1000 Subject: [PATCH 2/2] ci: typecheck with open3d and the ros extra, and call kmeans untyped The first run of the typecheck job failed on 14 errors that I did not see locally: the job had no open3d or rosbags, so the type: ignore comments for them were unused, and newer OpenCV stubs changed what kmeans reports. Install them in the job, and call cv2.kmeans through Any, since its stubs differ between OpenCV releases. Checked in a clean virtual environment with the job's packages: mypy reports no errors. Refs #40 Co-Authored-By: Claude Sonnet 5.5 --- .github/workflows/ci.yml | 6 ++++-- src/machinevisiontoolbox/BagOfWords.py | 4 +++- src/machinevisiontoolbox/ImageColor.py | 6 ++++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 68cf7998..ba66394e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -94,11 +94,13 @@ jobs: with: python-version: "3.12" + # open3d and the ros extra are installed so that mypy sees their types: a + # "type: ignore" for one of them is unused, and so an error, where it is missing - name: Install package and development dependencies run: | python -m pip install --upgrade pip - pip install -e .[dev] - pip install build + pip install -e .[dev,ros] + pip install open3d build # configuration is in pyproject.toml, [tool.mypy]; the package must stay at zero errors - name: Run mypy diff --git a/src/machinevisiontoolbox/BagOfWords.py b/src/machinevisiontoolbox/BagOfWords.py index 7c0a3795..67ec5b54 100644 --- a/src/machinevisiontoolbox/BagOfWords.py +++ b/src/machinevisiontoolbox/BagOfWords.py @@ -117,7 +117,9 @@ def __init__( if seed is not None: cv2.setRNGSeed(seed) - ret, labels, centroids = cv2.kmeans( # type: ignore[call-overload] + # OpenCV's stubs for kmeans differ between releases, so call it untyped + kmeans: Any = cv2.kmeans + ret, labels, centroids = kmeans( data=features._descriptor, K=k, bestLabels=None, diff --git a/src/machinevisiontoolbox/ImageColor.py b/src/machinevisiontoolbox/ImageColor.py index fc849b8d..c0dbcf08 100644 --- a/src/machinevisiontoolbox/ImageColor.py +++ b/src/machinevisiontoolbox/ImageColor.py @@ -5,7 +5,7 @@ from __future__ import annotations import sys -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Any import cv2 import numpy as np @@ -267,7 +267,9 @@ def kmeans_color( if k is not None: # perform clustering - ret, label, centres = cv2.kmeans( # type: ignore[call-overload] + # OpenCV's stubs for kmeans differ between releases, so call it untyped + kmeans: Any = cv2.kmeans + ret, label, centres = kmeans( data=data, K=k, bestLabels=None,