PEP 849 Draft Proposes More Expressive Type Expressions in Python 3.16
PEP 849 is a draft proposal to enhance Python's type annotation system, allowing any Python expression to be used in type annotations by returning the annotation's Abstract Syntax Tree (AST) and namespaces for runtime evaluation. This aims to overcome current limitations where many valid expressions cannot be introspected at runtime, restricting type expressiveness. If accepted, the PEP would pave the way for more intuitive type syntax like `1 | 2 | 3` for literal unions and conditional types, affecting Python developers who rely on advanced type checking and runtime introspection. It is currently in draft status for Python 3.16.
- →New Annotation Format for Expanded Type Expressiveness
- →Addressing Limitations of Current Type Annotation Introspection
- →Runtime Introspection via ASTs and `get_type_annotations`
- →Implementation Details and Utility Functionality
- →Performance Impact Considerations
Features (1) ›
- New Annotation Format for Expanded Type Expressiveness
PEP 849 proposes a new annotation format that allows arbitrary Python expressions within type annotations. This format instructs annotation functions to return the AST and namespaces of the annotation, enabling runtime consumers to evaluate them into typing objects. This fundamentally expands the kinds of expressions that can be assigned meaning within the typing specification.
Enhancements (1) ›
- Runtime Introspection via ASTs and `get_type_annotations`
The core mechanism involves modifying `__annotate__` functions to return `AnnotationAST` objects when called with a new `Format.AST` option, containing the AST and namespace. A new `typing.get_type_annotations` function would internally handle this, evaluating the ASTs to construct familiar type objects, thus decoupling type expression evaluation from standard Python semantics for improved runtime introspection.
Notes (3) ›
- Addressing Limitations of Current Type Annotation Introspection
Currently, Python's type annotations are restricted to a small subset of expressions due to runtime introspection limitations. Examples include the inability to use `1 | 2 | 3` for literal unions, or ternary expressions for conditional types. The PEP provides groundwork to support such intuitive type expressions, which are currently not feasible because their runtime evaluation semantics are incompatible with reflection needs.
- Implementation Details and Utility Functionality
The proposal suggests `__annotate__` could populate namespaces and load compact binary AST data, then use an intrinsic to build `AnnotationAST` objects. Additionally, a `create_annotate_function` in the `annotationlib` module is proposed to help users synthesize annotate functions for this more complex format, building on existing functionality in the `dataclasses` module.
- Performance Impact Considerations
The PEP acknowledges the need for careful consideration of performance impact, especially since type annotations are optional. It aims to analyze the effects on three user groups: those who don't use type annotations, those who use them for type checkers/linters without runtime introspection, and those who utilize runtime introspection.
https://peps.python.org/pep-0849/
Related releases
- Django Project Approves New Technical Governance Model Django Weblog ·
- Pydantic-AI v2.47.0 Improves Type Safety and Error Handling for Model Outputs Pydantic AI Releases ·
- PEP 824 Proposes None-Coalescing Operators `??` and `??=` for Python 3.16 Python PEPs ·
- PEP 823 Proposes None-Aware Access Operators for Python 3.16 Python PEPs ·
- LangGraph 1.2.12 improves subgraph detection and adds interrupt response schema LangGraph Releases ·
- LangGraph SDK 0.4.5 Adds `response_schema` to `interrupt()` Method LangGraph Releases ·