Skip to content

else-if checker throwing an IndexError #2816

@niothiel

Description

@niothiel

Steps to reproduce

  1. Create a file with the contents:
print(f'{"foo" if True else "N/A"}')
if "foo" == "bar":
    pass
elif "bar" == "baz":
    pass
  1. Configure pylint to include the pylint.extensions.check_elif extension
  2. Run pylint {file}.py

Current behavior

The elif checker throws an IndexError:

Traceback (most recent call last):
  File ".../bin/pylint", line 11, in <module>
    sys.exit(run_pylint())
  File ".../lib/python3.7/site-packages/pylint/__init__.py", line 20, in run_pylint
    Run(sys.argv[1:])
  File ".../lib/python3.7/site-packages/pylint/lint.py", line 1628, in __init__
    linter.check(args)
  File ".../lib/python3.7/site-packages/pylint/lint.py", line 943, in check
    self._do_check(files_or_modules)
  File ".../lib/python3.7/site-packages/pylint/lint.py", line 1075, in _do_check
    self.check_astroid_module(ast_node, walker, rawcheckers, tokencheckers)
  File ".../lib/python3.7/site-packages/pylint/lint.py", line 1158, in check_astroid_module
    walker.walk(ast_node)
  File ".../lib/python3.7/site-packages/pylint/utils.py", line 1303, in walk
    self.walk(child)
  File ".../lib/python3.7/site-packages/pylint/utils.py", line 1303, in walk
    self.walk(child)
  File ".../lib/python3.7/site-packages/pylint/utils.py", line 1300, in walk
    cb(astroid)
  File ".../lib/python3.7/site-packages/pylint/extensions/check_elif.py", line 63, in visit_if
    if not self._elifs[self._if_counter]:
IndexError: list index out of range

Expected behavior

The checker completes successfully.

pylint --version output

pylint 2.3.1
astroid 2.2.5
Python 3.7.1 (default, Dec 13 2018, 10:54:03) 
[Clang 10.0.0 (clang-1000.10.44.4)]

Basic Analysis

It looks like there is a conflict with elif and how the f-string is parsed for if statements. I've observed that either excluding the elif or removing the f-string, pylint completes successfully. I'm not sure if this is best reported here or against astroid or some other place entirely. Please feel free to move as appropriate. :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions