Repository navigation
test(#239): flag bare type aliases at the floor, and report what cannot be decided - #241
Merged
Merged
Conversation
…ot be decided `Handler = str | None` is an ast.Assign, not an ast.AnnAssign. It is evaluated at import exactly like the annotations the guard already walked, and raises the same TypeError on 3.9 — a collection error that takes out the whole leg. Same for TypeVar(bound=), NewType() and cast(). Zero live instances today; this is about what the guard catches tomorrow. The discriminator is the language, not a heuristic: a `|` chain is a union when some operand cannot be bitwise-or'd on any Python (None, a builtin type name, a name imported from typing, or a subscript of one). So it cannot false-positive on `MASK = READ | WRITE`. It errs toward silence instead — `Ids = A | B` is not flagged — because a guard people learn to ignore is worse than no guard. What it cannot classify comes back as GuardReport.undecided and is counted in the report's reason, rather than passing quietly as clean. 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.
The #237 guard walked
AnnAssignannotations andisinstance()/issubclass()arguments. It did not walkHandler = str | None— anast.Assign, evaluated at import in exactly the same way, raising the sameTypeErroron 3.9 and taking out the whole leg at collection. Same forTypeVar(..., bound=X | None),NewType("Id", X | None)andcast(X | None, v).I re-ran the scan rather than trusting the issue's number: an independent AST walk over all 81 files in the guard's scope, checking every module- and class-level
Assignwhose value contains aBitOr(including insideCallnodes) and everycast/TypeVar/NewTypecall anywhere. Zero hits, confirming the issue. Nothing is broken today; this is about what the guard catches tomorrow.The discriminator
The issue listed four candidates. I rejected all four and used a fifth.
Rejected:
CapWords= alias) — wrong the first time someone writesMASK = A | B, and wrong again onhandler = str | None. It guesses.typingproximity alone — needs name resolution it cannot actually do, and missesHandler = str | Nonein a module that imports nothing fromtyping.Shipped: a language-guaranteed type operand. A
|chain is a union when any operand is something that cannot be bitwise-or'd on any Python:None—x | Noneis aTypeErroron every version, so it has no legitimate bitwise reading;int,str,dict, …) — classes have no__or__before 3.10, soint | stris always a union attempt;typing/typing_extensions, or a module attribute of one (typing.List);Dict[str, int] | None).A name the module rebinds itself (
str = SomeFlag) stops counting as a type, and name collection deliberately over-collects — over-collecting only ever moves a verdict toward undecided, which is the safe direction.The point is that this is not a judgement call. The language decides.
MASK = READ | WRITE,FLAGS = re.I | re.M,MASK = 1 | 2andtotal = a | bare all untouched, not because a heuristic tolerated them but because none of them contains an operand that is definitely a type.What it misses, and which direction it errs
It misses an alias whose operands it cannot resolve:
Ids = A | BwhereAandBare locally-defined or third-party names. That is a real violation and it ships unflagged.I chose to err toward silence on a real violation rather than toward noise on legitimate bitwise code, and the asymmetry is the reason. A missed violation costs one red CI leg on the day someone writes it — visible, attributable, fixed in minutes. A false positive on
MASK = READ | WRITEcosts the guard its credibility permanently, and a guard nobody reads catches nothing at all. That isclaude-supertool#577's lesson and the same reasoning by which #238 correctly declined to flag function-local annotations.Also deliberately out of scope, and documented: function-local assignments (evaluated at call, not import — they fail one test, not a whole leg) and the body of
if TYPE_CHECKING:(never executed at runtime).The three-state contract
The miss is not silence. A
|in an import-time assignment that resolves to neither a type nor arithmetic comes back asGuardReport.undecided, and the count appears in the report'sreasonon bothokandviolations. The repo-wide test asserts that set is currently empty, so the first ambiguous construct anyone adds gets a human's eye once instead of disappearing.An absence the checker produced, read as an absence in the world, is this repo's recurring defect. Building an instance of it into the guard written to catch one would have been a poor trade.
Two false positives fixed in passing
Both were already reachable by the old walk, and my addition would have made them worse:
if TYPE_CHECKING:bodies are no longer scanned (theelse:branch still is);X: TypeAlias = str | Nonenow has its value checked — the future import rescues the annotation but not the assigned expression.TDD
Tests first, run red: 19 failed, 28 passed — each new behaviour failing on its own assertion, with the 28 pre-existing (including every must-not-flag case) still green. Then implemented, green: 49 passed in the guard file, 1081 passed / 42 skipped for the full suite at 94% coverage.
"Would this test still pass if the discriminator did nothing?" is answerable no in both directions: flag nothing and the nine must-flag tests fail; flag everything and the seven must-not-flag tests fail; record nothing as undecided and the three-state tests fail.
Teeth check, both directions in one run — planted in
pipeline/types.py, then removed:Docs:
CHANGELOG.md, and the README's "The Python floor guard" section now states the limitation plainly — a user who believes bare aliases are fully covered when they are not is worse off than one who reads the caveat.Closes #239