Skip to content

If TYPE_CHECKING used

Detects usage of if TYPE_CHECKING: blocks from the typing module.

TYPE_CHECKING is False at runtime but True during static type checking. This creates a disconnect between type checking and runtime behavior that can cause errors.

The most dangerous pattern is using if TYPE_CHECKING with an else clause where signatures don't match:

What gets flagged

Python
1
2
3
4
5
6
7
8
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    def get_value() -> int:
        ...
else:
    def get_value() -> str:
        return "hello"
Unsoundness Checker Output
Text Only
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
warning[if-type-checking-used]: Using `if TYPE_CHECKING:` blocks can lead to runtime errors if imports or definitions are incorrectly referenced at runtime.
 --> main.py:3:4
  |
1 | from typing import TYPE_CHECKING
2 |
3 | if TYPE_CHECKING:
  |    ^^^^^^^^^^^^^
4 |     def get_value() -> int:
5 |         ...
  |
info: rule `if-type-checking-used` was selected in the configuration file

Mypy: No Diagnostic Emitted

Pyright: No Diagnostic Emitted

Ty: No Diagnostic Emitted