Understand how type hints clarify contracts in growing codebases.
What It Does and Why It Matters
Annotations tell readers and static checkers which values a function expects and returns. They improve editor completion and catch inconsistent calls before execution, especially across module boundaries. Standard Python does not enforce most type annotations at runtime, so input from users and files still requires validation.
Type hints describe an intended interface; they do not replace tests or validation.
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 greeting(name: str, visits: int) -> str:
return f"Hello {name}; visit #{visits}"
print(greeting("Nora", 3))
How the Code Works
- The definition of greeting 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:
Hello Nora; visit #3
For the demonstrated inputs, Python displays 'Hello Nora; visit #3'. This result follows from the operation described above; change one input at a time to understand which rule matters.
Second Example: Compare Two Cases
Use a different input or a related operation to observe an important difference. The two examples help separate the general rule from one particular result.
def double(value: int) -> int: return value * 2
print(double(3))
Expected output:
6
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
Annotate a price calculator and intentionally pass a wrong type to a static 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
- Thinking that greeting(123, "x") will automatically raise a runtime type error because annotations exist.
- Overlooking the documented condition: standard Python does not enforce most type annotations at runtime, so input from users and files still requires validation.
Key Takeaways
- Understand how type hints clarify contracts in growing codebases.
- Annotations tell readers and static checkers which values a function expects and returns.
- Annotate a price calculator and intentionally pass a wrong type to a static checker.
- Annotations guide static tools; by default they do not validate external input or change the return value at runtime.