Fix type-hint mistakes by checking actual values and return paths.
What It Does and Why It Matters
Common problems include returning None from a function declared to return str, assuming Optional values are never missing, and confusing list[T] with list[Any]. Use explicit branches to narrow unions and update inaccurate annotations instead of hiding issues with cast().
Typing catches inconsistencies in code structure; use unit tests for business behavior.
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.
def display(value: str | None) -> str:
if value is None:
return "(missing)"
return value.strip()
print(display(None))
print(display(" ready "))
How the Code Works
- The definition of display 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:
(missing)
ready
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
Write a typed lookup function that returns str | None and handle its result safely.
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
- Using cast() to conceal a real None case rather than validating or narrowing it.
- Assuming a type checker has checked values received dynamically from an untrusted source.
Key Takeaways
- Fix type-hint mistakes by checking actual values and return paths.
- Common problems include returning None from a function declared to return str, assuming Optional values are never missing, and confusing list[T] with list[Any].
- Write a typed lookup function that returns str | None and handle its result safely.
- Annotations guide static tools; by default they do not validate external input or change the return value at runtime.