PEP 647
E916620
PEP 647 is the Python Enhancement Proposal that introduces "user-defined type guards," enabling more precise static type narrowing in Python code.
Statements (48)
| Predicate | Object |
|---|---|
| instanceOf | Python Enhancement Proposal ⓘ |
| aimsTo | enable more precise static type narrowing ⓘ |
| allows |
functions to signal refined types to type checkers
ⓘ
user code to define custom type-narrowing functions ⓘ |
| author |
Jelle Zijlstra
NERFINISHED
ⓘ
Michael Lee NERFINISHED ⓘ |
| belongsTo | CPython PEP index NERFINISHED ⓘ |
| category | Typing ⓘ |
| clarifies |
difference between TypeGuard and bool return types
ⓘ
interaction of user-defined type guards with union types ⓘ |
| createdForPythonVersion | Python 3.10 NERFINISHED ⓘ |
| definesConstraint |
TypeGuard may only appear in the return type position of a function
ⓘ
TypeGuard return type must be a subtype of bool at runtime ⓘ |
| definesSyntaxElement |
TypeGuard return type
ⓘ
typing.TypeGuard NERFINISHED ⓘ |
| definesTerm | user-defined type guard ⓘ |
| discussesModule | typing NERFINISHED ⓘ |
| doesNotAffect | runtime semantics of Python programs ⓘ |
| ensures | type guards are only used in positive test positions ⓘ |
| firstIncludedIn | Python 3.10 GENERATED ⓘ |
| governsBehaviorOf | TypeGuard in typing module NERFINISHED ⓘ |
| hasTitle | User-Defined Type Guards NERFINISHED ⓘ |
| improves | precision of conditional type inference ⓘ |
| introducesConcept | user-defined type guards ⓘ |
| language | English ⓘ |
| modifiesTypingBehavior | type checkers ⓘ |
| motivatedBy |
limitations of isinstance-based narrowing
ⓘ
limitations of literal truthiness checks for narrowing ⓘ |
| notes | TypeGuard is intended for use by static type checkers only NERFINISHED ⓘ |
| partOf | Python typing ecosystem ⓘ |
| PEPNumber | 647 ⓘ |
| relatedTo |
PEP 484
NERFINISHED
ⓘ
PEP 544 NERFINISHED ⓘ PEP 589 NERFINISHED ⓘ |
| relatesToLanguage | Python GENERATED ⓘ |
| relatesToTopic |
static typing
ⓘ
type checking ⓘ type narrowing ⓘ |
| repository | https://peps.python.org/pep-0647/ ⓘ |
| specifies |
rules for how type checkers interpret TypeGuard
ⓘ
semantics of TypeGuard in conditional statements ⓘ |
| status | Accepted ⓘ |
| targetsAudience |
Python developers using static typing
ⓘ
library authors ⓘ type checker implementers ⓘ |
| useCase |
custom validation functions that narrow types
ⓘ
filtering heterogeneous collections ⓘ refining Optional types after checks ⓘ |
Referenced by (1)
Full triples — surface form annotated when it differs from this entity's canonical label.