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 @@ -1970,7 +1970,10 @@ def in_type_checking_block(node: nodes.NodeNG) -> bool:
1970
1970
if isinstance (ancestor .test , nodes .Name ):
1971
1971
if ancestor .test .name != "TYPE_CHECKING" :
1972
1972
continue
1973
- maybe_import_from = ancestor .test .lookup (ancestor .test .name )[1 ][0 ]
1973
+ lookup_result = ancestor .test .lookup (ancestor .test .name )[1 ]
1974
+ if not lookup_result :
1975
+ return False
1976
+ maybe_import_from = lookup_result [0 ]
1974
1977
if (
1975
1978
isinstance (maybe_import_from , nodes .ImportFrom )
1976
1979
and maybe_import_from .modname == "typing"
Original file line number Diff line number Diff line change @@ -447,6 +447,16 @@ def test_if_typing_guard() -> None:
447
447
assert utils .is_typing_guard (code [3 ]) is False
448
448
449
449
450
+ def test_in_type_checking_block () -> None :
451
+ code = astroid .extract_node (
452
+ """
453
+ if TYPE_CHECKING: # don't import this!
454
+ import math #@
455
+ """
456
+ )
457
+ assert utils .in_type_checking_block (code ) is False
458
+
459
+
450
460
def test_is_empty_literal () -> None :
451
461
list_node = astroid .extract_node ("a = []" )
452
462
assert utils .is_base_container (list_node .value )
You can’t perform that action at this time.
0 commit comments