Skip to content

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

Merged
merged 1 commit into from
Aug 22, 2025

Conversation

Pierre-Sassoulas
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas commented Aug 22, 2025

Type of Changes

Type
🐛 Bug fix

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

@Pierre-Sassoulas Pierre-Sassoulas added this to the 3.3.9 milestone Aug 22, 2025
Comment on lines -2453 to -2456
if isinstance(node, (nodes.Name, nodes.Call, nodes.Attribute)) or (
isinstance(node, nodes.UnaryOp)
and isinstance(node.operand, (nodes.Attribute, nodes.Name))
):
Copy link
Member Author

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.

Copy link

codecov bot commented Aug 22, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.85%. Comparing base (a49e11a) to head (ca4f7b1).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            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     
Files with missing lines Coverage Δ
pylint/checkers/refactoring/refactoring_checker.py 98.06% <100.00%> (-0.11%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

🤖 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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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...

@Pierre-Sassoulas Pierre-Sassoulas merged commit a52dd58 into main Aug 22, 2025
48 checks passed
@Pierre-Sassoulas Pierre-Sassoulas deleted the fix-enumerate-crash-10510 branch August 22, 2025 08:17
@pylint-backport-bot
Copy link
Contributor

The backport to maintenance/3.3.x failed:

The process '/usr/bin/git' failed with exit code 1

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 base branch is maintenance/3.3.x and the compare/head branch is backport-10511-to-maintenance/3.3.x.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport maintenance/3.3.x Crash 💥 A bug that makes pylint crash
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AstroidError crash on extremely specific enumerate to dictionary comprehension.
2 participants