Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 10.3 — Running a Type Checker: mypy and pyright
Python · Example 1 of 1

What stays the same at run time

reveal_type from typing (3.11+) prints the real type to the error stream. cast and TYPE_CHECKING show that checker-only tools leave no mark when running.

A type checker reads your code without running it and reports every place where the hints contradict each other. mypy and pyright are the two most used; you install one, point it at your files, read its messages, and run it in CI so mistakes cannot be merged.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Using mypy on your computer, write a function def half(n: int) -> float: return n / 2 and call half("4"). Say which error code mypy gives.

02

Make a short program that prints the real type of 3 + 4.5 using typing.reveal_type.

03

Write the three lines of a pyproject.toml section that turn on mypy strict mode for Python 3.13.

Explain it without notes

01

What does a type checker do and what does it not do?

02

What is reveal_type for? How is the typing.reveal_type function different from the checker's one?

03

When is # type: ignore acceptable, and how should you write it?

04

Why run a checker in CI as well as in the editor?

05

How do cast, # type: ignore and an isinstance check differ when you need to convince the checker of a type?

What stays the same at run time
Sign in to run this example in your browser.

Expected output

TYPE_CHECKING at run time: False
Runtime type is 'int'
value = 42
cast returned: 'not an int' str