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

Fix potential-index-error false positive when iterable contains starred element #10097

Merged
merged 3 commits into from
Nov 24, 2024

Conversation

zenlyj
Copy link
Contributor

@zenlyj zenlyj commented Nov 24, 2024

Type of Changes

Type
🐛 Bug fix
✨ New feature
🔨 Refactoring
📜 Docs

Description

The current implementation calculates the length of an indexed iterable by using its literal length. However, this approach fails when the iterable contains a starred element that unpacks into multiple elements.

For example:

lst = [1, 2, 3]
print([*lst][1])  # error reported as checker assumes indexed iterable has 1 element instead of 3.

This PR addresses the issue by extending the check to account for the inferred length of starred nodes within the indexed iterable.

Closes #10076

@zenlyj zenlyj added the False Positive 🦟 A message is emitted but nothing is wrong with the code label Nov 24, 2024
Copy link

codecov bot commented Nov 24, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.80%. Comparing base (68cb5b3) to head (9e865b4).
Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main   #10097   +/-   ##
=======================================
  Coverage   95.80%   95.80%           
=======================================
  Files         174      174           
  Lines       18962    18973   +11     
=======================================
+ Hits        18166    18177   +11     
  Misses        796      796           
Files with missing lines Coverage Δ
pylint/checkers/variables.py 97.27% <100.00%> (+0.02%) ⬆️
---- 🚨 Try these New Features:

This comment has been minimized.

@zenlyj zenlyj force-pushed the fix/10076-false-positive branch from d627b12 to 9e865b4 Compare November 24, 2024 06:59
Copy link
Contributor

🤖 According to the primer, this change has no effect on the checked open source code. 🤖🎉

This comment was generated for commit 9e865b4

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

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

Looks great, thank you!

@DanielNoord DanielNoord merged commit 3e9e613 into pylint-dev:main Nov 24, 2024
46 checks passed
github-actions bot pushed a commit that referenced this pull request Nov 24, 2024
Pierre-Sassoulas pushed a commit that referenced this pull request Nov 24, 2024
…rred element (#10097) (#10098)

(cherry picked from commit 3e9e613)

Co-authored-by: Zen Lee <53538590+zenlyj@users.noreply.github.com>
@zenlyj zenlyj deleted the fix/10076-false-positive branch November 25, 2024 10:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport maintenance/3.3.x False Positive 🦟 A message is emitted but nothing is wrong with the code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rule E0643 behaves inconsistently when indexing tuples
3 participants