Interpret Static Checker Diagnostics
Core idea
A checker analyzes source without relying on ordinary execution. For example, mypy documents arg-type for an incompatible argument type and return-value for an incompatible returned value. Save a snippet to check.py and run python -m mypy --show-error-codes check.py in an environment with mypy installed. The diagnostics below are expected categories based on that documentation, not a locally captured checker run.
def show(count: int) -> str: return f'count={count}'# Expected mypy category: arg-typeprint(show('4'))Python prints text. A checker should identify the incompatible argument even though this body executes successfully. In the linked mypy error-code documentation, a tuple passed to a list[int] parameter is categorized as arg-type. The mismatch is in the call, not the function return. This is a documented example, not our own checker log.
Repair the boundary
def show(count: int) -> str: return f'count={count}'raw = '4'count = int(raw)print(show(count))The explicit conversion supplies an integer. In a real input boundary handle ValueError for invalid text; checker success does not prove conversion always works.
Common mistake
Do not widen everything to Any or add blanket ignores just to pass a checker. Compare the declared type, inferred expression type and expected operation; fix the contract or implementation deliberately. Tool versions and options influence diagnostics.
Practice
Run mypy on each example separately, record its version and exit status, then create a function annotated -> int that returns a string. Compare its return-value diagnostic with runtime behavior.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Static checker diagnostics
Check your understanding of static checker diagnostics.
What is printed?
def f(n: int) -> str: return str(n)print(f('8'))What is printed?
def f() -> int: return '7'print(type(f()).__name__)Which documented mypy category fits passing str to an int parameter?
What should a checker report be accompanied by?