Skip to content

test(#237): guard the 3.9 floor against PEP 604 unions, statically - #238

Merged
fdaviddpt merged 1 commit into
mainfrom
feat/237-pep604-floor-guard
Jul 30, 2026
Merged

fdaviddpt merged 1 commit into
mainfrom
feat/237-pep604-floor-guard

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

What this is

A static guard for syntax the floor interpreter cannot evaluate. #236 shipped project: str | None in a test helper; PEP 604 unions are types.UnionType construction, which does not exist before 3.10, so on 3.9 that line raises TypeError while pytest imports the module. Not a failing test — a collection error, which takes out the whole leg before anything runs. Three 3.9 legs red, nine green, nothing on the red legs executed. Invisible locally: this machine is on 3.14 and has no floor interpreter installed.

tests/pep604_floor.py walks the repo's .py files as an AST; tests/test_pep604_floor_guard.py runs it in the normal suite. ~1s, any interpreter, no 3.9 needed.

It flags PEP 604 unions in every position Python evaluates: parameter, return, and module- or class-level variable annotations in files without from __future__ import annotations, plus isinstance() / issubclass() arguments — which the future import does not rescue, because those are ordinary runtime expressions rather than annotations. That exception is the case a grep or a hand-rolled check misses, and it is pinned by a test.

Correcting a premise from the brief

The brief assumed the floor lives in pyproject.toml's requires-python. It does not. pyproject.toml here is three lines — [tool.pytest.ini_options] only, no [project] table, no requires-python. The floor is declared in exactly two places: the README badge (python-3.9+) and .github/workflows/tests.yml's matrix. The rest of the premise checked out: PEP 604 is 3.10+, the future import rescues annotations but not isinstance, and #236's failure was a collection error on all three 3.9 legs.

The three judgment calls

1. Scope — repository-wide, minus machine state. The defect is a collection error, and CI collects tests/ — but it imports pipeline/ transitively, and any .py added under scripts/ or hooks.d/ tomorrow is a file some leg will import. Scoping to tests/ would guard the file that happened to break last time. What the walk must not touch is machine state that happens to sit in the working tree: names first and unconditionally (dot-prefixed anything, venv, node_modules, build, dist, __pycache__, *.egg-info), then git's ignored set when the root is a git repository. Names before git because .git is never in git's ignored set and a tarball checkout has no git at all; git after names so a directory nobody anticipated is exempt without being named.

claude-supertool #577 is what the other error looks like — a floor scanner that walked build/ and .venv/ and reported violations in code nobody there wrote. Its fix (PR #579) is the pattern followed here: names unconditionally, then the ignored set, never the tracked set. Tracked-files would exempt the file being written right now, which is precisely when the guard earns its keep. Two tests pin that: a staged repo with .venv/, venv/, node_modules/, build/, __pycache__/, .git/ and an egg-info dir all carrying violations must walk exactly one file, and a brand-new untracked source file beside them must still be walked.

2. Floor source — the CI matrix. Not hardcoded, and not requires-python, which does not exist here. .github/workflows/tests.yml is the only declaration of the floor and the thing that actually produces the legs a violation destroys. When the floor moves, the matrix is what moves, and the guard follows it in the same commit. A hardcoded 3.9 would keep failing a repo that had dropped 3.9; a requires-python read would depend on a key nobody here writes. When the matrix reaches 3.10 the guard reports skipped with that as its reason rather than passing on syntax it no longer has an opinion about — tested against a staged repo whose matrix is ["3.10", "3.12"] and which contains a real violation.

3. Three-state contract — zero files walked is skipped, never a pass. run_guard() returns ok / violations / skipped, following the precedent of the launching-repo mutation guard (#219, VERDICT_UNATTRIBUTABLE). Three conditions produce skipped, each with a reason naming what it could not do: the floor could not be read from the workflow; the floor is 3.10+; the walk found no .py files. An absence the checker produced, read as an absence in the world, is the defect class this repo keeps fixing — adding another instance of it while guarding against one would be a poor trade.

A fourth, smaller call worth naming: function-local annotations are deliberately not flagged. Python never evaluates them (PEP 526), so def f(): x: int | None = None cannot break a 3.9 leg. Flagging it would be #577's over-scan in miniature — the first person to hit a false positive concludes the check is noisy and stops reading it. Same reasoning excludes string annotations (x: "int | None"). Both are pinned as must-not-flag tests, alongside ordinary bitwise | (re.I | re.M, a | b), which is what a grep-based version gets wrong.

TDD, in order

  1. Test file written first, importing tests.pep604_floor. Run → red: ModuleNotFoundError: No module named 'tests.pep604_floor', an ImportError during collection — fittingly, the same shape as the defect.
  2. Implementation. Run → green: 25 passed in 1.03s.
  3. Teeth check, separately: planted def _plant(x: str | None) -> None in pipeline/, re-ran, and the repo-wide test failed with pipeline/_teeth_check.py:1 parameter annotation: str | None. Then removed it. The bar was "would this still pass if the guard did nothing" — a walk that found zero files, or one that never flagged anything, fails test_the_repo_walk_is_not_empty_and_covers_the_code_that_ships and the three-state tests.
  4. Full suite before push: 1057 passed, 42 skipped, 570s, coverage 94.01%.

Fixtures are staged in tmp_path rather than committed — a committed file containing a real violation would be found by the repository walk and fail the guard it exists to exercise. Every fixture named in the brief is there: a file with a violation and no future import (flagged), and a file with from __future__ import annotations and isinstance(x, int | str) (still flagged, line asserted).

Docs

  • CHANGELOG.md — ### Added under Unreleased, with the reasoning above.
  • README.md — a The Python floor guard subsection under Running tests, naming the file, what it flags, and the fix when it fires (Optional[str], or add the future import). Discoverable by someone who did not build it and meets it as a red test.

Closes #237

A collection error takes out a whole matrix leg before a test runs and is
invisible on any interpreter above the floor. #236 shipped `str | None` in a
test helper and lost all three 3.9 legs.

tests/pep604_floor.py walks the repo's .py files as an AST and flags PEP 604
unions everywhere Python evaluates them — parameter/return/module- and
class-level annotations without `from __future__ import annotations`, plus
isinstance()/issubclass() arguments, which the future import does not rescue.

Floor read from the CI matrix (pyproject.toml declares no requires-python).
Walk is repo-wide minus dot-names/venv/node_modules/build/dist/__pycache__/
egg-info and git's ignored set. Zero files walked reports `skipped`, not a
pass. Function-local annotations deliberately out of scope — Python never
evaluates them.

Closes #237

Co-Authored-By: Max <noreply>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A PEP 604 annotation in a test file takes out whole matrix legs before anything runs, and is invisible on any interpreter above the floor

2 participants