Linting and type checking
Understand which quality tool answers which question.
On this page
The concept
A formatter chooses a consistent layout. A linter flags suspicious code patterns and selected mistakes. A static type checker compares annotated interfaces without executing the program. They complement behavioral tests; none proves that a business rule is correct.
The root pyproject.toml configures Ruff and strict mypy for the final application. Run python -m ruff check ., python -m ruff format --check ., and python -m mypy 09_projects/72_taskflow_capstone/app/taskflow from the course root after installing the development tools. The lesson's small typed function is deliberately separate from these terminal commands.
Worked example
def typed_total(prices: list[int]) -> int:
return sum(prices)
print(typed_total([10, 20, 30]))Trace the result
- The annotation says typed_total accepts a list of integers.
- sum computes an integer result for that contract.
- The demo supplies three integers and displays 60. An incompatible call would need a static check or fail according to its runtime behavior.
60Line-by-line walkthrough
Lines 1–2: def typed_total(prices: list[int]) -> int:
Define the function typed_total with input names prices. Definition stores the operation rather than executing its body. An ordinary call executes a normal function; a coroutine runs when awaited/scheduled, and a generator advances when consumed. Decorators can provide a context-manager or wrapper interface. Indented statements below belong to this operation.
Line 2: return sum(prices)
Evaluate sum(prices) and return that value to the caller. This ends the current function call; subsequent statements on this execution path do not run.
Line 5: print(typed_total([10, 20, 30]))
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Common mistake
A clean linter report does not validate application rules. Do not silence all diagnostics to obtain a green result.