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 crash when using enumerate
with start
and a class attribute
#7824
Conversation
@@ -2303,7 +2303,7 @@ def _enumerate_with_start( | |||
def _get_start_value(self, node: nodes.NodeNG) -> tuple[int | None, Confidence]: | |||
confidence = HIGH | |||
|
|||
if isinstance(node, (nodes.Name, nodes.Call)): | |||
if isinstance(node, (nodes.Name, nodes.Call, nodes.Attribute)): | |||
inferred = utils.safe_infer(node) | |||
start_val = inferred.value if inferred else None | |||
confidence = INFERENCE |
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.
in this entire func I'm negotiating between knowing what type of node will be and crashing... would you prefer I make the else the "catch all" instead and ensure we never crash regardless of not knowing what the node could be?
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.
I don't really understand your point and I'm not sure I missed this in an earlier review but the broad else doesn't make a lot of sense.
mypy
only allows it because astroid
isn't typed and therefore every astroid
object is Any
as far as mypy
can see. Doing node.value
is not type safe sadly so we should use instance checks.
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.
Oftentimes if a node is not something we expect we return early. I.e. we use the pattern we talked about in #7787 (comment). It's a way to make sure we have false negatives instead of false positive or crashes. To be clearer listing the node we expect to treat is safer than thinking we know what we'll have to treat. It's really hard to think of all possible python construct around a particular situation. Might be a principle that should be documented / checked in review btw.
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.
Alright so... I think this would meet both of your comments?
def _get_start_value(self, node: nodes.NodeNG) -> tuple[int | None, Confidence]:
if isinstance(node, (nodes.Name, nodes.Call, nodes.Attribute)):
inferred = utils.safe_infer(node)
start_val = inferred.value if inferred else None
return start_val, INFERENCE
if isinstance(node, nodes.UnaryOp):
return node.operand.value, HIGH
if isinstance(node, nodes.Const):
return node.value, HIGH
return None, HIGH
?
Pull Request Test Coverage Report for Build 3544004706
💛 - Coveralls |
c191af8
to
6e6652e
Compare
Primer stdlib error appears to be unrelated and probably hitting other PRs, too |
This comment has been minimized.
This comment has been minimized.
6e6652e
to
e29eeec
Compare
This comment has been minimized.
This comment has been minimized.
Blocked by #7826 |
Could you rebase/merge main now that #7826 is merged please ? :) |
I'm going to add the test case provided in #7821 (comment) and rebase and hopefully we should be good |
e29eeec
to
58a3c91
Compare
This comment has been minimized.
This comment has been minimized.
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.
Good job on adding the second test case.
enumerate
with start
and a class attribute
This comment has been minimized.
This comment has been minimized.
🤖 According to the primer, this change has no effect on the checked open source code. 🤖🎉 This comment was generated for commit f3e5eab |
…te (#7824) Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
… attribute (pylint-dev#7824)" This reverts commit 86b8c64.
Type of Changes
Description
fixes
Closes #7821