-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
bugfix - assignment-from-none
False Positive #7853
#7886
Conversation
That is interesting:
|
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 should definitely not touch safe_infer
for something happening in a particular checker, this is going to affect a lot of code. What we can do is make astroid better for a specific piece of code it parse, or make the checker handle a specific piece of code it analyses.
assignment-from-none
False Positive #7853assignment-from-none
False Positive #7853
I uploaded a fix that refactors the There were some unnecessary things in the original function, like the use of a |
π€ Effect of this PR on checked open source code: π€ Effect on sentry:
This comment was generated for commit b992e12 |
EDIT: Not sure where it fails, when I run The When I run the |
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'm still not sure whether I like this approach. It feels like we're complicating the safe_infer
util for a very specific use case.
We could also consider doing the double infer call in the checker itself. We would need to duplicate a little bit of safe_infer
inside the checker, but at least the safe_infer
function remains somewhat legible.
Perhaps other maintainers have an opinion here?
@@ -0,0 +1,3 @@ | |||
# Default ignored files |
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.
This shouldn't be committed.
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.
yeah my bad, will remove it in my next commit
@@ -0,0 +1,3 @@ | |||
Fix False Positive issue regarding a complex ``assignment-from-none``. |
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 think this might need some more specific details, but we can figure that out 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.
Agree. yeah we just don't know what is the final solution yet.
function_node = safe_infer(node.value.func) | ||
funcs = (nodes.FunctionDef, astroid.UnboundMethod, astroid.BoundMethod) | ||
if not isinstance(function_node, funcs): | ||
ASTROID_FUNC_TYPES = ( |
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.
Let's make this a frozen set at either the Module or Class level.
astroid.BoundMethod, | ||
) | ||
return_nodes = [] | ||
function_nodes: Any | None = safe_infer_multiple(node.value.func) |
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.
function_nodes: Any | None = safe_infer_multiple(node.value.func) | |
function_nodes = safe_infer_multiple(node.value.func) |
astroid.UnboundMethod, | ||
astroid.BoundMethod, | ||
) | ||
return_nodes = [] |
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.
Can we add annotation here?
if not isinstance(function_node, funcs): | ||
ASTROID_FUNC_TYPES = ( | ||
nodes.FunctionDef, | ||
astroid.UnboundMethod, |
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.
Ideally these get imported from bases
.
# Make sure that it's a valid function that we can analyze. | ||
# Ordered from less expensive to more expensive checks. | ||
if ( | ||
not function_node.is_function |
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.
Is this really necessary?
Edit: Ah I see that most of this is just indenting. Hmm, well we could also fix this in a follow up, but it feels unnecessary.
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.
The purpose of refactoring safe_infer
was only to separate it to functions. Any logic should not have been modified.
context: InferenceContext | None = None, | ||
*, | ||
compare_constants: bool = False, | ||
) -> Any | None: |
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.
Why is this Any
?
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 asked myself the same question. But this is what mypy
accepted. :S
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.
Yes, that's because mypy
doesn't do any checking on astroid
. It should be InferenceResult
.
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 might need to extend safe_infer
, but I consider it to be an API. It's used too much in pylint as well as in lib depending on it, to be open to modification.
I agree that this function is super fragile. I had a really hard time making all the functional tests pass.. The purpose of the change was just to refactor |
@DanielNoord - I think I didn't understand the suggestion here, could you explain more on that please? |
The issue is that for some nodes we want to do a double inference right? So not call |
EDIT: first of all thank you for reviewing the code and for your insights, they've always been very significant. Even though you didn't like the spirit of this PR initially..
Not necessary twice, for our case which is a function infer, you want """
""" In this case you want to check |
Can't we get away with calling |
|
We already have a cached util for getting all inferred values, it's |
Thanks for helping think through these issues. I think changing the assignment-from-none checker to use |
Type of Changes
Description
See comment about this PR in this comment.
Closes #7853