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.TypeGuard is a special typing construct introduced in PEP 647 (Python 3.10) that enables user-defined type narrowing. It instructs static type checkers (such as mypy or pyright) that if a boolean-returning function evaluates to True, a specific argument passed to that function is guaranteed to be of the type specified within the TypeGuard.
At runtime, TypeGuard[T] behaves exactly like a standard bool return type annotation. Its sole purpose is to influence static flow analysis in the calling scope.
Syntax
A function utilizing a type guard must return a boolean value and annotate its return type asTypeGuard[TargetType].
Type Checker Mechanics
When a static type checker encounters a function returningTypeGuard[T], it applies specific narrowing rules to the calling scope based on the control flow:
- The
TrueBranch (Positive Narrowing): If the type guard function returnsTrue, the type checker narrows the type of the passed argument toTwithin the correspondingifblock. - The
FalseBranch (No Negative Narrowing): If the type guard function returnsFalse, the type checker does not perform negative narrowing. The argument retains its original, un-narrowed type in theelseblock. The type checker does not subtract the guarded type from the original type because aTypeGuardfunction can contain arbitrary runtime logic and value checks (e.g.,val.isalnum()orlen(val) > 0). AFalsereturn only implies that the specific validation failed, which does not necessarily mean the argument is strictly not of typeT.
Technical Constraints
- Argument Binding: A
TypeGuardstrictly and exclusively narrows the argument bound to the first parameter defined in the function signature, regardless of whether the caller passes it as a positional or a keyword argument. When applied to an instance method or a class method,TypeGuardnarrows the argument bound to the second parameter (the first explicit parameter immediately followingselforcls). It cannot be configured to target any other parameter. - Implicit Trust: Static type checkers do not verify the internal logic of the function returning the
TypeGuard. The type checker implicitly trusts that the developer’s runtime implementation accurately guarantees the typeT. If the internal logic is flawed, it will introduce unsoundness into the type system. - Type Overriding: According to PEP 647,
TypeGuarddoes not require the target typeTto be a strict subtype of the original argument’s type. It allows narrowing to an entirely unrelated type hierarchy. In such cases,TypeGuardeffectively acts as an unsafe cast in the positive branch, completely overriding the original type rather than intersecting it.
TypeGuard allows unsafe casting to unrelated types and lacks negative narrowing in the False branch, Python 3.13 introduced typing.TypeIs via PEP 742. TypeIs enforces stricter type intersection, ensuring the narrowed type is a valid subtype, and allows for both positive and negative type narrowing.)
Master Python with Deep Grasping Methodology!Learn More





