Search GimmeJob

Type at least 2 characters. Enter also searches by meaning.
Python interview question
Typing & static analysisMiddleCommonTooling

What do tools like mypy and pyright actually do, and why doesn't the Python interpreter perform this checking itself?

Answer

mypy and pyright are separate static analysis tools that read a codebase's type hints and its actual code paths without executing anything, then report inconsistencies they can detect within their analyzed type model and configured strictness — passing a `str` where an `int` is expected, calling a method that does not exist on a given type, or returning the wrong type from a function. They do not catch every inconsistency: dynamic features, `Any`-typed or unannotated code, incomplete third-party stubs, and code they cannot statically analyze (some metaprogramming or reflection) can all hide real mismatches even from a strict configuration. The interpreter itself does not perform this checking because Python's typing system, per its own specification, is deliberately optional, gradual, static-analysis metadata rather than a runtime contract — a design choice about keeping typed and untyped code fully interoperable, not something reducible to a single performance justification.

Strong answer includes

  • report inconsistencies within their analyzed type model and configured strictness, not exhaustively every mismatch
  • dynamic code, Any, and incomplete typing can hide real errors even from strict checkers
  • Python's typing system is specified as optional, gradual static metadata by design, not attributable to one performance reason

Practical example

`mypy app.py` can catch

Python example
user.name.append("x")

(calling a list method on a `str` attribute) as a type error before the code ever runs, while the interpreter itself would only discover the mistake if and when that exact line executed.

#typing#tooling#mypy

Sources

typing — Support for type hintsPython Software Foundation · Generics, protocols, overloads and typing-module semanticsPEP 484 — Type HintsPython Software Foundation · Foundational type-hint syntax and static-typing intent