Repository navigation
test(#237): guard the 3.9 floor against PEP 604 unions, statically - #238
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this is
A static guard for syntax the floor interpreter cannot evaluate. #236 shipped
project: str | Nonein a test helper; PEP 604 unions aretypes.UnionTypeconstruction, which does not exist before 3.10, so on 3.9 that line raisesTypeErrorwhile 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.pywalks the repo's.pyfiles as an AST;tests/test_pep604_floor_guard.pyruns 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, plusisinstance()/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'srequires-python. It does not.pyproject.tomlhere is three lines —[tool.pytest.ini_options]only, no[project]table, norequires-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 notisinstance, 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 importspipeline/transitively, and any.pyadded underscripts/orhooks.d/tomorrow is a file some leg will import. Scoping totests/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.gitis 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 walkedbuild/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.ymlis 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 hardcoded3.9would keep failing a repo that had dropped 3.9; arequires-pythonread 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()returnsok/violations/skipped, following the precedent of the launching-repo mutation guard (#219,VERDICT_UNATTRIBUTABLE). Three conditions produceskipped, 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.pyfiles. 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 = Nonecannot 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
tests.pep604_floor. Run → red:ModuleNotFoundError: No module named 'tests.pep604_floor', an ImportError during collection — fittingly, the same shape as the defect.def _plant(x: str | None) -> Noneinpipeline/, re-ran, and the repo-wide test failed withpipeline/_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, failstest_the_repo_walk_is_not_empty_and_covers_the_code_that_shipsand the three-state tests.Fixtures are staged in
tmp_pathrather 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 withfrom __future__ import annotationsandisinstance(x, int | str)(still flagged, line asserted).Docs
CHANGELOG.md—### Addedunder 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