-
-
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2450,17 +2450,20 @@ def _enumerate_with_start( | |||||||||||||
| return False, confidence | ||||||||||||||
|
|
||||||||||||||
| def _get_start_value(self, node: nodes.NodeNG) -> tuple[int | None, Confidence]: | ||||||||||||||
| 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) | ||||||||||||||
| # inferred can be an astroid.base.Instance as in 'enumerate(x, int(y))' or | ||||||||||||||
| # not correctly inferred (None) | ||||||||||||||
| start_val = inferred.value if isinstance(inferred, nodes.Const) else None | ||||||||||||||
| return start_val, INFERENCE | ||||||||||||||
| if isinstance(node, nodes.UnaryOp): | ||||||||||||||
| return node.operand.value, HIGH | ||||||||||||||
| # Most common use cases are a constant integer or minus a constant integer. We | ||||||||||||||
| # don't need inference for that. If that's not the case, we assume arbitrary | ||||||||||||||
| # complexity and we use inference. | ||||||||||||||
| if isinstance(node, nodes.Const): | ||||||||||||||
| return node.value, HIGH | ||||||||||||||
| return None, HIGH | ||||||||||||||
| if isinstance(node, nodes.UnaryOp) and isinstance(node.operand, nodes.Const): | ||||||||||||||
| return node.operand.value, HIGH | ||||||||||||||
| inferred = utils.safe_infer(node) | ||||||||||||||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Alternatively... |
||||||||||||||
| if isinstance(inferred, nodes.Const): | ||||||||||||||
| return inferred.value, INFERENCE | ||||||||||||||
| # inferred can be an 'astroid.base.Instance' in 'enumerate(x, int(y))', | ||||||||||||||
| # for example. We're doing nothing in this case for now, as extracting | ||||||||||||||
| # the value is costly. | ||||||||||||||
|
|
||||||||||||||
| # At this point the most likely cases is that the node is uninferable | ||||||||||||||
| # But we don't have to check if it's actually uninferable. | ||||||||||||||
| return None, INFERENCE | ||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.