Skip to content

The floor guard walks annotations but not bare assignments, so a module-level Handler = str | None alias passes and still kills every 3.9 leg #239

Closed

Description

@fdaviddpt

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/Subscript that is not a known int/flags constant, or is None. Catches the common alias; misses Flags = A | B where A/B are ints, in the safe direction.
  • Casing/convention — aliases here are CapWords. Cheap, and wrong the first time someone writes MASK = A | B.
  • typing proximity — flag when the module imports from typing and 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

  1. added a commit that references this issue on Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions