Review New Typing and Native ABI Contracts
Core idea
Python 3.15 adds TypeForm for values that represent type expressions, TypedDict options closed and extra_items, and disjoint_base for describing restricted inheritance relationships to type checkers. These describe static contracts; they are not general runtime validators. For native extensions, 3.15 introduces the abi3t stable ABI for free-threaded builds, distinct from the older abi3. Migrating a C extension may require changes to object layout access and export hooks (PyModExport_* and PySlot); ABI compatibility does not prove concurrency safety.
from typing import TypeForm
def remember[T](shape: TypeForm[T]) -> TypeForm[T]: return shape
form = remember(list[int])print(form == list[int])print(TypeForm(int) is int)TypeForm[T] describes type expressions such as list[int]. TypeForm(x) returns the supplied expression at runtime; no data conversion occurs.
Documented expected output (target runtime not tested locally):
TrueTrueTarget: Python 3.15 type extra dictionary fields
from typing import TypedDictclass Reading(TypedDict, extra_items=int): station: strreading: Reading = {'station': 'A', 'voltage': 12}print(reading['station'], reading['voltage'])print(isinstance(reading, dict))The extra voltage field follows the declared integer type contract. A TypedDict value remains a normal dictionary at runtime.
Documented expected output (target runtime not tested locally):
A 12TrueCommon mistake
Do not treat TypeForm as an isinstance validator for arbitrary type expressions. A checker supporting these features must test positive and negative cases; runtime output does not establish inferred types. Do not relabel an abi3 wheel as abi3t without a compatible build and tests. Confirm build tooling, headers, target tags, thread safety, and the supported Python versions independently.
Practice
On 3.15 with a compatible type checker, add an invalid string-valued extra field and compare diagnostics with a closed TypedDict. For a native extension, list the existing ABI, supported build modes and migration requirements from the official 3.15 C API documentation; build and test each claimed target separately.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Python 3.15 typing and native ABI
Check your understanding of python 3.15 typing and native abi.
On Python 3.15 (documented expectation; target runtime not tested locally), what is printed?
from typing import TypeFormprint(TypeForm(str) is str)What is printed?
from typing import TypedDictclass Entry(TypedDict): n: inte: Entry = {'n': 5}print(type(e).__name__)What does the closed TypedDict option describe?
What does successful abi3t compilation establish by itself?