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..ba66394e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -84,9 +84,45 @@ 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" + + # 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,ros] + pip install open3d 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 +136,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. 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,