Type parameter bounds restrict a generic parameter to a subtype of a base type, while constraints restrict it to one of listed options.
What It Does and Why It Matters
Type parameter bounds restrict a generic parameter to a subtype of a base type, while constraints restrict it to one of listed options. These limits guide static checking rather than inspecting every value automatically at runtime.
Annotations guide static tools; by default they do not validate external input or change the return value 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 collections.abc import Sized
def size_of[ItemType: Sized](value: ItemType) -> int:
return len(value)
def combine[ItemType: (str, bytes)](first: ItemType, second: ItemType) -> ItemType:
return first + second
print(size_of(['a', 'b']))
print(combine('North', 'wind'))
How the Code Works
- The imports make these standard-library tools available: collections.abc.
- The definition of size_of, combine 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.
Expected output:
2
Northwind
Each displayed line comes from the corresponding calculation or check. The output helps verify the return values before you adapt the example to your own input.
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
Use the technique when it makes a real task easier to understand or safer to maintain. Check input and output behavior with small tests before combining it with larger application code.
Practice Exercise
Define one bounded and one constrained generic helper and compare type checker feedback.
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
- Confusing a bound with a list of allowable individual values or mixing constrained types arbitrarily.
- Checking only the easiest input and missing a relevant error or boundary case.
Key Takeaways
- Type parameter bounds restrict a generic parameter to a subtype of a base type, while constraints restrict it to one of listed options.
- Define one bounded and one constrained generic helper and compare type checker feedback.
- Annotations guide static tools; by default they do not validate external input or change the return value at runtime.