Skip to content

in_type_checking_block can crash if you haven't imported TYPE_CHECKING #8434

Closed
@jacobtylerwalls

Description

@jacobtylerwalls

Bug description

if TYPE_CHECKING:  # don't import this!
    pass
else:
    import math

Configuration

No response

Command used

pylint a.py

Pylint output

File "/Users/.../pylint/pylint/utils/ast_walker.py", line 96, in walk
    callback(astroid)
  File "/Users/.../pylint/pylint/checkers/variables.py", line 1329, in leave_module
    self._check_imports(not_consumed)
  File "/Users/.../pylint/pylint/checkers/variables.py", line 3071, in _check_imports
    if not in_type_checking_block(stmt):
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/.../pylint/pylint/checkers/utils.py", line 1921, in in_type_checking_block
    maybe_import_from = ancestor.test.lookup(ancestor.test.name)[1][0]
                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: tuple index out of range

Expected behavior

************* Module a
a.py:1:3: E0602: Undefined variable 'TYPE_CHECKING' (undefined-variable)
a.py:4:4: W0611: Unused import math (unused-import)

Pylint version

pylint 3.0.0a6
astroid 2.16.0dev0
Python 3.11.0

OS / Environment

No response

Additional dependencies

No response

Metadata

Metadata

Labels

Crash 💥A bug that makes pylint crashNeeds PRThis issue is accepted, sufficiently specified and now needs an implementation

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions