PEP 647 TypeGuard

E884133

PEP 647 TypeGuard is a Python typing feature that allows developers to define user-defined type guard functions, enabling more precise type narrowing and improved static type checking.

All labels observed (5)

How this entity was disambiguated

Statements (47)

Predicate Object
instanceOf PEP ⓘ
Python typing feature ⓘ
affects type checkers behavior ⓘ
backportedFor Python versions earlier than 3.10 ⓘ
backportLocation typing_extensions.TypeGuard ⓘ
belongsTo Python type system ⓘ
Python typing module ⓘ
category Typing PEP ⓘ
linked to: typing
clarifies how user-defined predicates influence type narrowing ⓘ
defines typing.TypeGuard ⓘ
linked to: PEP 647 TypeGuard
definesConcept TypeGuard ⓘ
definesSyntaxElement TypeGuard[X] return annotation ⓘ
designDecision TypeGuard is covariant in its type parameter ⓘ
linked to: PEP 647 TypeGuard

TypeGuard is intended only for return types, not parameters ⓘ
linked to: PEP 647 TypeGuard
documentationLocation https://peps.python.org/pep-0647/ ⓘ
exampleUse def is_str_list(val: list[object]) -> TypeGuard[list[str]] ⓘ
hasAuthor Eric Traut ⓘ
hasName PEP 647: User-Defined Type Guards ⓘ
linked to: PEP 647 TypeGuard
introducedIn Python 3.10 ⓘ
language Python ⓘ
moduleLocation typing.TypeGuard ⓘ
linked to: TypeGuard
motivatedBy limitations of isinstance-based narrowing ⓘ
need for reusable predicate functions ⓘ
primaryGoal enable user-defined type guards ⓘ
improve static type checking precision ⓘ
support more precise type narrowing ⓘ
relatedTo PEP 484 ⓘ
PEP 544 ⓘ
PEP 589 ⓘ
replacesPattern ad-hoc isinstance checks for complex predicates ⓘ
requires TypeGuard generic parameter to be a subtype of input type ⓘ
returnTypeConstraint TypeGuard must be used as function return type ⓘ
runtimeEffect no direct runtime enforcement ⓘ
scope static type checking only ⓘ
semantics if function returns True, argument type is narrowed to TypeGuard parameter ⓘ
type narrowing applies only in True branch ⓘ
type narrowing is local to the guarded expression ⓘ
status Accepted ⓘ
targetVersion Python 3.10 ⓘ
usedBy Pylance ⓘ
Pyre ⓘ
Pyright ⓘ
mypy ⓘ
static type checkers ⓘ
usedFor flow-sensitive type analysis ⓘ
refining union types ⓘ
type narrowing ⓘ

How these facts were elicited

Referenced by (5)

Full triples — surface form annotated when it differs from this entity's canonical label.

Python 3.10 → hasTypingFeature → PEP 647 TypeGuard ⓘ
PEP 647 TypeGuard → hasName → PEP 647: User-Defined Type Guards ⓘ
linked to: PEP 647 TypeGuard
PEP 647 TypeGuard → defines → typing.TypeGuard ⓘ
linked to: PEP 647 TypeGuard
PEP 647 TypeGuard → designDecision → TypeGuard is covariant in its type parameter ⓘ
linked to: PEP 647 TypeGuard
PEP 647 TypeGuard → designDecision → TypeGuard is intended only for return types, not parameters ⓘ
linked to: PEP 647 TypeGuard