Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
typing.TypeIs is a special typing form introduced in Python 3.13 (via PEP 742) used to annotate the return type of user-defined type guard functions. It instructs static type checkers to narrow the type of a function’s argument in both the positive (True) and negative (False) control flow branches based on the boolean return value.
Syntax
TypeIs is parameterized with a single type and is exclusively used as a return type annotation.
Type Narrowing Mechanics
When a function annotated with-> TypeIs[T] is evaluated in a conditional statement, the static type checker modifies the type of the passed argument within the resulting control flow graph:
- Positive Branch (
True): The type checker calculates the intersection of the argument’s original type andT. The argument is narrowed toT. - Negative Branch (
False): The type checker calculates the type complement by subtractingTfrom the argument’s original type. The argument is narrowed to the remaining types.
TypeIs vs. TypeGuard
TypeIs was introduced to resolve a specific theoretical limitation of its predecessor, typing.TypeGuard (PEP 647).
TypeGuard[T](Non-strict narrowing): Only narrows the type in the positive branch. If the function returnsFalse, the type checker leaves the original type unmodified. This is necessary for functions that check for a specific property rather than an exhaustive type (e.g.,is_non_empty_list).TypeIs[T](Strict narrowing): Narrows the type in both branches. It guarantees to the type checker that the boolean return value perfectly partitions the type space.
Structural Constraints
To be valid under static type checking rules, aTypeIs function must adhere to the following constraints:
- Runtime Return Type: The function must return a boolean value at runtime.
- Argument Binding: The narrowing effect applies implicitly to the first positional argument of the function. If
TypeIsis used as the return type of an instance method or class method, the type checker automatically skips the implicitselforclsparameter and applies the narrowing to the first positional argument afterselforcls. - Type Intersection and Disjointness: The target type
TinTypeIs[T]should intersect with the first argument’s annotated type. IfTis entirely disjoint from the argument’s type (e.g., checking if anintis astr), the type checker evaluates the positive branch asNever(unreachable), because the intersection of disjoint types is empty.
Master Python with Deep Grasping Methodology!Learn More





