File tree 3 files changed +17
-1
lines changed
3 files changed +17
-1
lines changed Original file line number Diff line number Diff line change
1
+ Fix a crash when ``TYPE_CHECKING`` is used without importing it.
2
+
3
+ Closes #8434
Original file line number Diff line number Diff line change @@ -1918,7 +1918,10 @@ def in_type_checking_block(node: nodes.NodeNG) -> bool:
1918
1918
if isinstance (ancestor .test , nodes .Name ):
1919
1919
if ancestor .test .name != "TYPE_CHECKING" :
1920
1920
continue
1921
- maybe_import_from = ancestor .test .lookup (ancestor .test .name )[1 ][0 ]
1921
+ lookup_result = ancestor .test .lookup (ancestor .test .name )[1 ]
1922
+ if not lookup_result :
1923
+ return False
1924
+ maybe_import_from = lookup_result [0 ]
1922
1925
if (
1923
1926
isinstance (maybe_import_from , nodes .ImportFrom )
1924
1927
and maybe_import_from .modname == "typing"
Original file line number Diff line number Diff line change @@ -446,6 +446,16 @@ def test_if_typing_guard() -> None:
446
446
assert utils .is_typing_guard (code [3 ]) is False
447
447
448
448
449
+ def test_in_type_checking_block () -> None :
450
+ code = astroid .extract_node (
451
+ """
452
+ if TYPE_CHECKING: # don't import this!
453
+ import math #@
454
+ """
455
+ )
456
+ assert utils .in_type_checking_block (code ) is False
457
+
458
+
449
459
def test_is_empty_literal () -> None :
450
460
list_node = astroid .extract_node ("a = []" )
451
461
assert utils .is_base_container (list_node .value )
You can’t perform that action at this time.
0 commit comments