Describe extra dictionary keys with a known value type using TypedDict.
What It Does and Why It Matters
Python 3.15 adds the extra_items keyword for TypedDict definitions. Declared keys keep their own type rules, while additional keys must have values of the specified type according to static checkers. This is useful for records with a fixed core and typed extension fields. As with all TypedDict annotations, runtime dictionaries are not validated automatically.
Requires Python 3.15 or newer for the extra_items runtime definition; it cannot be combined with closed=True.
Version requirement: this example needs Python 3.15 or newer. The extra_items declaration is understood by compatible static type checkers, but does not validate a dict at runtime.
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 TypedDict
class Measurements(TypedDict, extra_items=float):
name: str
temperature: float
sample: Measurements = {"name": "Lab", "temperature": 21.5, "humidity": 0.42}
print(sample["humidity"])
print(sample["name"])
How the Code Works
- The imports make these standard-library tools available: typing.
- The definition of Measurements creates reusable behavior; its body runs only when called or instantiated as appropriate.
- The name sample receives the value of {'name': 'Lab', 'temperature': 21.5, 'humidity': 0.42}. Changing this input lets you explore a different case.
- The print calls display the results. Compare these results with the method’s documented return type and the input used.
- The declared keys name and temperature have their own specified types; humidity is permitted as an extra key because its value is a float. A static checker enforces this rule, not the dictionary constructor.
This example requires a newer interpreter than the local verification environment. Run it in a compatible Python installation and compare with the documented behavior.
What to Notice
Pay attention to the accepted input types, the returned value and the boundary behavior. Predict what changes if you replace the example input with an empty, zero, negative or missing value, where that value is meaningful for this operation.
If the task uses a library function, inspect its documented exceptions and return type before reusing it inside a larger function.
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
Add an extra pressure reading as a float, then try a string extra key value with a 3.15-aware type checker.
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
- Expecting extra_items=float to reject a bad value at runtime; a static checker must analyze the assignment.
- Overlooking the documented condition: declared keys keep their own type rules, while additional keys must have values of the specified type according to static checkers.
Key Takeaways
- Describe extra dictionary keys with a known value type using TypedDict.
- Python 3.15 adds the extra_items keyword for TypedDict definitions.
- Add an extra pressure reading as a float, then try a string extra key value with a 3.15-aware type checker.
- Version requirement: this example needs Python 3.15 or newer. The extra_items declaration is understood by compatible static type checkers, but does not validate a dict at runtime.