-
-
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
Fix false negative no-member
when the code was a manual
augmented assignments
#7509
Conversation
d454dbe
to
d56ed59
Compare
pylint/checkers/utils.py
Outdated
if not attr: | ||
return False | ||
|
||
return binop.op == "+" and attr.attrname == node.attrname |
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 entire util func is pretty ugly ... all ears for improvements
Pull Request Test Coverage Report for Build 3112831510
π - Coveralls |
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.
Looks pretty good already, thank you. I made a small review, there's an issue with the pipeline to fix too.
pylint/checkers/utils.py
Outdated
return False | ||
|
||
children = [x for x in parent.get_children()] | ||
binops = [x for x in children if isinstance(x, nodes.BinOp)] |
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.
As children is only used in the second list comprehension, imbricating both list comprehension would be more efficient.
I'm on mobile so I can't check get_children api but I wouldn't be surprised if you can get all the children of a certain type directly.
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 see a way to get children of a certain type https://pylint.pycqa.org/projects/astroid/en/latest/api/astroid.nodes.html#astroid.nodes.AugAssign.get_children
but will def combine the list comps
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.
Maybe node_of_classes but I'm not sure it's only the children that are returned. Sorry for suggesting things that I can't test myself right now, I'm on mobile.
pylint/checkers/utils.py
Outdated
|
||
children = [x for x in parent.get_children()] | ||
binops = [x for x in children if isinstance(x, nodes.BinOp)] | ||
if not binops: |
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 can avoid doing the following if in some cases like this:
if not binops: | |
if not binops or binops[0] ! = "+": |
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 thinking!
pylint/checkers/utils.py
Outdated
if not attr: | ||
return False | ||
|
||
return binop.op == "+" and attr.attrname == node.attrname |
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.
return binop.op == "+" and attr.attrname == node.attrname | |
return attr.attrname == node.attrname |
pylint/checkers/utils.py
Outdated
|
||
binop = binops[0] | ||
|
||
attr = 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.
attr = 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'm much more in favor of
var = None
if ...
var = 1
elif ...
var = 2
if not var
but hey I"ll take your suggestion
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 did not consider the case where var is none from the if, I'm not opinionated about this you asked about possible style fix so I tried :)
pylint/checkers/utils.py
Outdated
|
||
if not attr: |
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.
if not attr: | |
else: |
@Pierre-Sassoulas I was thinking: does it make sense to make this a code style check? |
Sounds good ! |
@clavedeluna I'll have a look at converting this into a message tomorrow. You can do so as well if you want to? I'd be happy to guide you in the right direction? |
go right ahead with adding it, I'll look at Pierre's review. I think doing |
whoops... broke the tests |
2146e03
to
8b3de4d
Compare
Alright so maybe I'm missing something obvious but how is is that not always a bool? do I wrap it in bool?? |
This is because |
ah makes sense! I'm getting more and more familiar with the code base so thanks for the tip. |
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.
LGTM, thank you @clavedeluna !
manual
augomented assignmanual
augmented assignments
I'm working a PR that incorporates these changes to get a primer result. Should be done in 30 minutes or something like that. |
I'll let you merge this one @DanielNoord |
See #7514. I had to change quite a bit. Preferably we merge that PR first and then use the utility introduced there in this PR. The robustness we get from checking the new checker on the primer is a huge benefit compared to the functional tests we can think of ourselves. |
I will rebase and re-do this PR once that one is merged |
de151b6
to
0216731
Compare
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.
Thank you for inspiring the work on the augmented assignment checker, and thank you for fixing this !
manual
augmented assignmentsno-member
when the code was a manual
augmented assignments
Type of Changes
Description
Add code to detect
no-member
when an augmented assign is done "manually", that is, when a user does thisobj.value = 1 + obj.value
instead of
obj.value += 1
If
no-member
would be raised in the second case, it should also be raised in the first case, which is why this PR is here!Closes #4562 (altho I can remove this auto-close if we want to keep it open for the
pydantic
false positive case, though I Personally think it should be a separate issue)