Check that you can type an existing program and explain remaining runtime checks.
What It Does and Why It Matters
For the checkpoint, identify public function inputs and outputs, add precise annotations, and run a static checker. Then test invalid runtime values separately. A passing checker is not the end: APIs with dictionaries from external sources should still validate their required fields and constraints.
This checkpoint combines type checking, explicit validation and a boundary-case test.
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 average(values: list[float]) -> float:
if not values:
raise ValueError("Provide at least one value")
return sum(values) / len(values)
print(average([10.0, 20.0, 30.0]))
How the Code Works
- The definition of average 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.0
For the demonstrated inputs, Python displays '20.0'. This result follows from the operation described above; change one input at a time to understand which rule matters.
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
Annotate and test a grade report that accepts a list of numbers and rejects empty input.
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 list[float] automatically rejects an empty list or validates incoming JSON data.
- Overlooking the documented condition: a passing checker is not the end: APIs with dictionaries from external sources should still validate their required fields and constraints.
Key Takeaways
- Check that you can type an existing program and explain remaining runtime checks.
- For the checkpoint, identify public function inputs and outputs, add precise annotations, and run a static checker.
- Annotate and test a grade report that accepts a list of numbers and rejects empty input.
- Annotations guide static tools; by default they do not validate external input or change the return value at runtime.