Know which mistakes static type checkers can find and which they cannot.
What It Does and Why It Matters
Type checkers reason about declared types and supported control flow. They can flag unsupported operations, missing attributes and incompatible return values, but they cannot prove arbitrary business rules such as password strength, valid file contents or correct tax policy. Runtime behavior is unaffected by whether a checker has been run.
Use type checking alongside tests, validation, code review and error handling.
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 area(width: float, height: float) -> float:
return width * height
print(area(4, 5))
print(area(-4, 5)) # Type-correct but logically invalid for many uses
How the Code Works
- The definition of area 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:
20
-20
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
Add a runtime check for negative dimensions, then identify what the type checker contributes.
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 a program that passes a type checker must produce correct results for all valid-looking inputs.
- Overlooking the documented condition: they can flag unsupported operations, missing attributes and incompatible return values, but they cannot prove arbitrary business rules such as password strength, valid file contents or correct tax policy.
Key Takeaways
- Know which mistakes static type checkers can find and which they cannot.
- Type checkers reason about declared types and supported control flow.
- Add a runtime check for negative dimensions, then identify what the type checker contributes.
- Annotations guide static tools; by default they do not validate external input or change the return value at runtime.