-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
unnecessary-list-index-lookup doesn't detect list mutation in a nested while loop #6818
Comments
/CC @timmartin We have a test for: for idx, val in enumerate(my_list):
my_list[idx] = 42 Seems like we also need to exclude other forms of assignment which we are likely not doing as of yet. Perhaps by checking for I'm putting this in |
This doesn't seem all that simple to me. I don't think the problem is just a matter of
makes the assumption that if it proceeds through the tree in order and terminates early when it is assigned, then we won't warn on any reference that can happen after the assignment. For / While loop conditions are cases where this assumption is violated, since the condition will be evaluated again after the body of the loop. The simple solution would just be to suppress the warning for the whole loop any time there's a write to the subscript anywhere wihin the body. This will cause some false negatives, but it seems not unreasonable to me; if the for loop is updating the enumerated data, then maybe it's clearer just to do the index lookup every time and be explicit about it. If we want to analyze this properly then I can write some code that detects the for / while loop conditional and then recursively checks inside the body for assignments. However, this seems like we're getting into control flow analysis, which I gather is something that we're trying to solve more generally in Pylint. I'm not sure about writing a whole bunch of ad hoc code for this one case. |
Yeah, let's create a PR now that might cause some false negatives and worry about fixing those later. Better to have false negatives than to have false positives. Would you be willing to prepare that PR? |
Yes, I can create a PR this weekend. |
@DanielNoord is this really a blocker for 2.14.1 ? We have two crashes in the already fixed issues and we can release 2.14.2 with only this as soon as it's merged if required. |
Yeah, I think this is quite a big flaw in the newly added checker. It's not likely that people will update their dependencies in the weekends and its Pentecost. So let's wait to see if we can get a fix merged this weekend. If we can't then let's release without one. |
The ``unnecessary-list-index-lookup`` checker was assuming that if the subscript was written to then it would only invalidate access on later lines, but this is not necessarily the case if an inner loop is nested inside the one being checked. Fixes pylint-dev#6818
…6845) The ``unnecessary-list-index-lookup`` checker was assuming that if the subscript was written to then it would only invalidate access on later lines, but this is not necessarily the case if an inner loop is nested inside the one being checked. Fixes #6818 Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
…ylint-dev#6845) The ``unnecessary-list-index-lookup`` checker was assuming that if the subscript was written to then it would only invalidate access on later lines, but this is not necessarily the case if an inner loop is nested inside the one being checked. Fixes pylint-dev#6818 Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
…6845) The ``unnecessary-list-index-lookup`` checker was assuming that if the subscript was written to then it would only invalidate access on later lines, but this is not necessarily the case if an inner loop is nested inside the one being checked. Fixes #6818 Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
This reverts commit 1f9d749.
This reverts commit 1f9d749.
Bug description
Simple example:
Similar issue has unnecessary-dict-index-lookup
BTW, another simple failure is #6788. These checkers seems to be too naive to be enabled per default (as in the 2.14).
Configuration
Command used
Pylint output
Expected behavior
No R1736.
Pylint version
OS / Environment
No response
Additional dependencies
No response
The text was updated successfully, but these errors were encountered: