Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive E1126 when index previously assigned to None #2880

Closed
chrisjbillington opened this issue Apr 22, 2019 · 2 comments
Closed

False positive E1126 when index previously assigned to None #2880

chrisjbillington opened this issue Apr 22, 2019 · 2 comments
Labels
Bug 🪲 Control flow Requires control flow understanding Minor 💅 Polishing pylint is always nice Needs PR This issue is accepted, sufficiently specified and now needs an implementation

Comments

@chrisjbillington
Copy link

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]
@PCManticore
Copy link
Contributor

Thanks for opening an issue. This is related to pylint not being able to understand control flow, a tricky topic for a non-human tool.

@PCManticore PCManticore added Bug 🪲 Minor 💅 Polishing pylint is always nice Control flow Requires control flow understanding labels Apr 23, 2019
@Pierre-Sassoulas Pierre-Sassoulas added the Needs PR This issue is accepted, sufficiently specified and now needs an implementation label Jun 30, 2022
@mbyrnepr2
Copy link
Member

This one no longer reproduces and should be fixed as per pylint-dev/astroid#1189.

Tested with:
pylint 2.17.0-dev0
astroid 2.15.0-dev0
Python 3.10.4

@mbyrnepr2 mbyrnepr2 closed this as not planned Won't fix, can't repro, duplicate, stale Feb 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 Control flow Requires control flow understanding Minor 💅 Polishing pylint is always nice Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
Development

No branches or pull requests

4 participants