-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Better approach in 'unnecessary-list-index-lookup' to avoid crashes #10511
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
Conversation
if isinstance(node, (nodes.Name, nodes.Call, nodes.Attribute)) or ( | ||
isinstance(node, nodes.UnaryOp) | ||
and isinstance(node.operand, (nodes.Attribute, nodes.Name)) | ||
): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also avoid this call when we have a const or minus a const so my intuition is that it's going to be faster in the common use case, even if we infer more on the strange use case, but I'm not in the mood for a benchmark to prove it.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #10511 +/- ##
==========================================
- Coverage 95.85% 95.85% -0.01%
==========================================
Files 177 177
Lines 19285 19284 -1
==========================================
- Hits 18486 18484 -2
- Misses 799 800 +1
🚀 New features to boost your workflow:
|
🤖 According to the primer, this change has no effect on the checked open source code. 🤖🎉 This comment was generated for commit ca4f7b1 |
return None, HIGH | ||
if isinstance(node, nodes.UnaryOp) and isinstance(node.operand, nodes.Const): | ||
return node.operand.value, HIGH | ||
inferred = utils.safe_infer(node) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
inferred = utils.safe_infer(node) | |
if isinstance(node, (nodes.Name, nodes.Call, nodes.Attribute)) or ( | |
isinstance(node, nodes.UnaryOp) | |
and isinstance(node.operand, (nodes.Attribute, nodes.Name)) | |
): | |
inferred = utils.safe_infer(node) |
Alternatively...
The backport to
To backport manually, run these commands in your terminal: # Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-maintenance/3.3.x maintenance/3.3.x
# Navigate to the new working tree
cd .worktrees/backport-maintenance/3.3.x
# Create a new branch
git switch --create backport-10511-to-maintenance/3.3.x
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 a52dd5851604d72249b29b022f52b742a5730d61
# Push it to GitHub
git push --set-upstream origin backport-10511-to-maintenance/3.3.x
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-maintenance/3.3.x Then, create a pull request where the |
Type of Changes
Description
Got real tired of the crashes in enumerate when inferring the value of start following #7821, #7963, #8069, #8207, #9074, #9078, #9272, so inference most of the time it is.
Fixed crash in 'unnecessary-list-index-lookup' when starting an enumeration using
minus the length of an iterable inside a dict comprehension when the len call was only
made in this dict comprehension, and not elsewhere. Also changed the approach,
to use inference in all cases but the simple ones, so we don't have to fix crashes
one by one for arbitrarily complex expressions in enumerate.
Closes #10510