Repository navigation
Description
The #237 guard (tests/pep604_floor.py, PR #238) flags PEP 604 unions in every position it walks: parameter, return and module/class-level AnnAssign annotations, plus isinstance()/issubclass() arguments. That covers the defect #236 actually shipped.
It does not cover a bare assignment:
Handler = str | None # ast.Assign, not ast.AnnAssign
Ids = Union[int, str] | None
T = TypeVar("T", bound=int | None)All three are evaluated at import on any interpreter, so all three raise TypeError on 3.9 and take out the whole leg at collection — the exact failure #237 exists to prevent. from __future__ import annotations does not rescue them; they are ordinary runtime expressions, the same category as the isinstance case the guard does handle.
Currently unreachable, deliberately filed anyway
I walked every file in the guard's own scope for module-level Assign whose value contains a BitOr, including inside Call nodes: zero hits. So this is not a live violation and #238 is not blocked by it.
It is filed because the guard's value is entirely in what it catches later, and a module-level type alias is a completely ordinary thing to add. A guard that reports ok on a file containing Handler = str | None is this repo's recurring defect wearing the guard's own uniform — an absence the checker produced, read as an absence in the world.
The reason it was left out is good, and is the actual design problem
x = 1 | 2 and flags = READ | WRITE are legitimate bitwise operations, and nothing in the AST distinguishes them from a type alias without type information. Flagging every module-level BitOr would produce false positives, and a guard people learn to ignore is worse than no guard — which is claude-supertool #577's whole lesson, and is why #238 correctly declined to flag function-local annotations too.
So the work here is deciding what discriminates them, not writing the walk. Some candidates, none obviously right:
- Operand shape — flag only when every leaf is a
Name/Attribute/Subscriptthat is not a known int/flags constant, or isNone. Catches the common alias; missesFlags = A | Bwhere A/B are ints, in the safe direction. - Casing/convention — aliases here are
CapWords. Cheap, and wrong the first time someone writesMASK = A | B. typingproximity — flag when the module imports fromtypingand the operands resolve to imported type names. Narrower, more code.- Accept and document — say in the docstring that bare aliases are out of scope and why. Legitimate if written down; an undocumented known-limitation is indistinguishable from an oversight.
Whichever wins, the three-state contract applies: a discriminator that cannot decide should say so rather than pass quietly.
Also worth checking in the same pass
TypeVar(..., bound=X | None) and cast(X | None, v) are Call arguments rather than assignments, and are evaluated the same way. The isinstance/issubclass handling already has the right shape for them — it may just be a matter of which callables are in the set, though cast's first argument is genuinely a type position while TypeVar's is a keyword. Worth deciding rather than assuming.
Found while reviewing #238, which is otherwise sound and merging on green.
Activity
test(#239): flag bare type aliases at the floor, and report what cann…