typing.
What It Does and Why It Matters
typing.overload declares several static signatures for one runtime implementation. Only the non-decorated implementation executes, and it must handle every promised overload case.
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 typing import overload
@overload
def normalize(value: str) -> str: ...
@overload
def normalize(value: int) -> int: ...
def normalize(value: str | int) -> str | int:
return value.strip() if isinstance(value, str) else abs(value)
print(normalize(' ready '))
print(normalize(-4))
How the Code Works
- The imports make these standard-library tools available: typing.
- The definition of normalize, normalize, normalize 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.
- The annotations describe intended types for editors and checkers. They are not a replacement for validating input values when the function runs.
Expected output:
ready
4
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
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
Create overloads for text and numeric input to one formatting function.
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
- Defining only @overload signatures without a real implementation or assuming overload chooses runtime dispatch.
- Overlooking the documented condition: only the non-decorated implementation executes, and it must handle every promised overload case.
Key Takeaways
- typing.
- typing.overload declares several static signatures for one runtime implementation.
- Create overloads for text and numeric input to one formatting function.
- Annotations guide static tools; by default they do not validate external input or change the return value at runtime.