Skip to content

False positive E1126 when index previously assigned to None #2880

@chrisjbillington

Description

@chrisjbillington

The following code:

N = 10
order = [0] * N
j = None
for i in range(N):
    if j is not None:
        order[j] += 1 # <--- E1126 for this line
    else:
        j = i

gives:

test.py:6:0: E1126: Sequence index is not an int, slice, or instance with __index__ (invalid-sequence-index)

I can see that this is because j was initially set to None, which would not be a valid index. However the code is actually correct, as a human can see, since it checks for None before using the index.

I'm not sure if there's anything pylint can do abut this, since I'm not sure how sophisticated it aims to be in checking this sort of thing. But reporting this anyway for what it's worth - feel free to close if this is outside the scope of pylint.

pylint 2.2.2
astroid 2.1.0
Python 3.7.3 (default, Mar 26 2019, 21:43:19) 
[GCC 8.2.1 20181127]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bug 🪲Control flowRequires control flow understandingMinor 💅Polishing pylint is always niceNeeds PRThis issue is accepted, sufficiently specified and now needs an implementation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions