type narrowing predicates¶
basedpython spells PEP 742 TypeIs[T] as name is T in the return
annotation, naming the parameter being narrowed:
transpiles to:
semantics¶
the runtime semantics are exactly PEP 742 — the function asserts that its
argument has type T when it returns True, and ty narrows accordingly at
call sites. the parameter name is lost in lowering (TypeIs doesn't carry it)
but is preserved in the source for readers
scope¶
the rewrite fires only when the return annotation is a single name is T
comparison where name is a bare identifier. this disambiguates from
identity checks elsewhere in the function:
- in the return annotation:
x is str→TypeIs[str] - anywhere else:
x is yfollows the identity-swap rules and lowers toisinstance(x, y)
chained comparisons (a is int is str) and non-identifier left operands are
ignored