summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/whatsnew/fragments/8434.bugfix3
-rw-r--r--pylint/checkers/utils.py5
-rw-r--r--tests/checkers/unittest_utils.py10
3 files changed, 17 insertions, 1 deletions
diff --git a/doc/whatsnew/fragments/8434.bugfix b/doc/whatsnew/fragments/8434.bugfix
new file mode 100644
index 000000000..b5a8bddcb
--- /dev/null
+++ b/doc/whatsnew/fragments/8434.bugfix
@@ -0,0 +1,3 @@
+Fix a crash when ``TYPE_CHECKING`` is used without importing it.
+
+Closes #8434
diff --git a/pylint/checkers/utils.py b/pylint/checkers/utils.py
index 0cee91f17..c68f88388 100644
--- a/pylint/checkers/utils.py
+++ b/pylint/checkers/utils.py
@@ -1970,7 +1970,10 @@ def in_type_checking_block(node: nodes.NodeNG) -> bool:
if isinstance(ancestor.test, nodes.Name):
if ancestor.test.name != "TYPE_CHECKING":
continue
- maybe_import_from = ancestor.test.lookup(ancestor.test.name)[1][0]
+ lookup_result = ancestor.test.lookup(ancestor.test.name)[1]
+ if not lookup_result:
+ return False
+ maybe_import_from = lookup_result[0]
if (
isinstance(maybe_import_from, nodes.ImportFrom)
and maybe_import_from.modname == "typing"
diff --git a/tests/checkers/unittest_utils.py b/tests/checkers/unittest_utils.py
index b2cbcb590..a0dfe299b 100644
--- a/tests/checkers/unittest_utils.py
+++ b/tests/checkers/unittest_utils.py
@@ -447,6 +447,16 @@ def test_if_typing_guard() -> None:
assert utils.is_typing_guard(code[3]) is False
+def test_in_type_checking_block() -> None:
+ code = astroid.extract_node(
+ """
+ if TYPE_CHECKING: # don't import this!
+ import math #@
+ """
+ )
+ assert utils.in_type_checking_block(code) is False
+
+
def test_is_empty_literal() -> None:
list_node = astroid.extract_node("a = []")
assert utils.is_base_container(list_node.value)