← Back to index

directives_type_checking.py

True Positive
False Positive
False Negative
Optional (detected)
Warning or Info
TP: 0
FP: 1
FN: 0
Optional: 0 / 0
1"""
2Tests the typing.TYPE_CHECKING constant.
3"""
4
5# Specification: https://typing.readthedocs.io/en/latest/spec/directives.html#type-checking
6
7from typing import TYPE_CHECKING, assert_type
8
9
10if not TYPE_CHECKING:
11 a: int = "" # This should not generate an error
Unexpected error [invalid-assignment] Object of type `Literal[""]` is not assignable to `int`
13if TYPE_CHECKING:
14 b: list[int] = [1, 2, 3]
15else:
16 b: list[str] = ["a", "b", "c"]
18assert_type(b, list[int])