Represent type expressions as values with typing.TypeForm.
What It Does and Why It Matters
TypeForm[T] was introduced in Python 3.15 to describe a runtime object that represents a type expression assignable to T. This is more general than type[T], which describes concrete class objects. Typical applications include validation frameworks and helpers that accept unions or parameterized type expressions. TypeForm itself is not a validation engine.
Requires Python 3.15 or newer; it describes type expressions and may be used with static analysis tools supporting PEP 747.
Version requirement: this example needs Python 3.15 or newer. TypeForm is a typing construct, not a runtime type validator.
Step-by-Step Example
Start with the complete example below. Read the inputs and predict what the final print statement will show before you run it.
from typing import TypeForm
def remember_type[T](form: TypeForm[T]) -> TypeForm[T]:
return form
print(remember_type(list[int]))
print(remember_type(int | str))
How the Code Works
- The imports make these standard-library tools available: typing.
- The definition of remember_type creates reusable behavior; its body runs only when called or instantiated as appropriate.
- The print calls display the results. Compare these results with the method’s documented return type and the input used.
- list[int] and int | str are type expressions; the helper returns the form rather than constructing or validating a list or an integer.
This example requires a newer interpreter than the local verification environment. Run it in a compatible Python installation and compare with the documented behavior.
Second Example: Compare Two Cases
Use a different input or a related operation to observe an important difference. The two examples help separate the general rule from one particular result.
from typing import TypeForm
example: TypeForm[int | str] = int | str
print(example)
Where You Would Use This
This helps maintain larger projects where functions and data shapes cross module boundaries. Check the code with a static analyzer, and still validate data received from files, users or remote APIs. Annotation constructs do not normally enforce types automatically at runtime.
Practice Exercise
Compare a generic helper accepting type[T] with one accepting TypeForm[T] for list[int].
Before running your edited example, write down the result you expect. Then test at least one valid boundary or deliberately invalid value, using appropriate exception handling where necessary.
Reference: official Python documentation for the complete API contract and version notes.
Common Beginner Mistakes
- Assuming type[int] and TypeForm[int] mean exactly the same thing, or expecting TypeForm to check values automatically.
- Assuming a type checker has checked values received dynamically from an untrusted source.
Key Takeaways
- Represent type expressions as values with typing.TypeForm.
- TypeForm[T] was introduced in Python 3.15 to describe a runtime object that represents a type expression assignable to T.
- Compare a generic helper accepting type[T] with one accepting TypeForm[T] for list[int].
- Version requirement: this example needs Python 3.15 or newer. TypeForm is a typing construct, not a runtime type validator.