Typing and Generics
typing.TypeIs
Annotates a boolean predicate that narrows both true and false branches. T must be consistent with the input type; the predicate must accept every matching value and reject nonmatching values. Added in 3.13.
Formal Syntax & Structure
python syntaxpython
typing.TypeIs[T]Return Value
type expression
Typing construct or runtime result as described; static typing rules are not runtime validation.
Working Example
Example Snippetpython
from typing import TypeIs
def is_text(value: str | int) -> TypeIs[str]: return isinstance(value,str)print((is_text('a'), is_text(7)))