← 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
"""
2
Tests
the
typing
.
TYPE_CHECKING
constant
.
3
"""
4
5
# Specification: https://typing.readthedocs.io/en/latest/spec/directives.html#type-checking
6
7
from
typing
import
TYPE_CHECKING
,
assert_type
8
9
10
if
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`
12
13
if
TYPE_CHECKING
:
14
b
:
list
[
int
]
=
[
1
,
2
,
3
]
15
else
:
16
b
:
list
[
str
]
=
[
"a"
,
"b"
,
"c"
]
17
18
assert_type
(
b
,
list
[
int
])