-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 while iteraring over a class attribute #7386
Conversation
Pull Request Test Coverage Report for Build 2966111299
💛 - Coveralls |
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.
@DanielNoord hey man consider these little improvements for this little addition to my checker.
Are you sure an Attribute
will always be something to check here and not a false-positive? I trust your Python knowledge here.
@@ -93,3 +93,15 @@ def update_existing_key(): | |||
for key in my_dict: | |||
new_key = key.lower() | |||
my_dict[new_key] = 1 # [modified-iterating-dict] | |||
|
|||
|
|||
class MyClass: # pylint: disable=too-few-public-methods |
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 is the only class in the test file. you can add the disable flag at the top of the file
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 is personal preference I think. I like disabling at the occurrence as it allows more freedom in checking for some message further down the file.
But I'll change it here 😄
"""Regression test for https://github.com/PyCQA/pylint/issues/7380""" | ||
|
||
def __init__(self) -> None: | ||
self.attribute = [1, 2, 3] |
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.
self.attribute = [1, 2, 3] | |
self.iterated_attributes = [1, 2, 3] |
def my_method(self): | ||
"""This should raise as we are deleting.""" | ||
for var in self.attribute: | ||
del var # [modified-iterating-list] |
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.
def my_method(self): | |
"""This should raise as we are deleting.""" | |
for var in self.attribute: | |
del var # [modified-iterating-list] | |
def modifying_list_(self): | |
# should be fine to modify a copy | |
for var in self.iterated_attributes.copy(): | |
del var # [modified-iterating-list] | |
# should raise a message when modifying | |
for var in self.iterated_attributes: | |
del var # [modified-iterating-list] | |
Also check the false positive case. Fix the .txt
file accordingly.
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 we should hold off on actually fixing any false negatives and positives here. As I said, the current code really doesn't work well with class attributes as we depend on functions like .append
being attributes of the iterable object. This complicates things massively when actually looking for attributes.
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.
@DanielNoord yeah i re-read it and understood that the case which is not working is the class case.
I don't fully understand the append
example, how can you put list.append
in a for loop?
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.
for x in a_list:
a_list.append(x)
The issue here is that append
is an Attribute
node on a_list
just as attribute
is an Attribute
node in the following example:
class MyClass:
def __init__():
self.attribute = [1,2,3]
def method(self):
for x in self.attribute:
del x
Astroid
only recognises that they are nodes that come after a period, not that they are attributes in the traditional sense of the word.
This complicates the logic we're currently using in the checker. I tried to fix it, but it turned out to be a pretty large refactor which probably should be taken in multiple steps for ease of reviewing.
To make sure we no longer crash I wanted to push this fix at least.
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.
You mean like when iterating through a dictionary d
: iterating on d.keys
is an Attribute
which is actually a fix the our error of iterating through d
? something like that? so your current solution will throw false-postive now?
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 you'll understand what you mean when you add the example I gave in the opening post to the functional tests. If you run pytest
then you'll see that some of the isinstance(node, nodes.Attribute)
test you are using to pick up d.append
will now also pick up MyClass.attribute
. This causes some issues and methods such as _modified_iterating_list_cond
will need to be changed to allow checking both type of 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.
LGTM, let's merge once the discussion with @orSolocate is resolved :)
2b3e733
Co-authored-by: orSolocate <[email protected]>
@Pierre-Sassoulas you guys can merge it. You can open a new issue to add support for class attributes with my checker and assign me :) |
🤖 According to the primer, this change has no effect on the checked open source code. 🤖🎉 This comment was generated for commit 339abe2 |
Co-authored-by: orSolocate <[email protected]>
Co-authored-by: orSolocate <[email protected]>
doc/whatsnew/<current release.rst>
.Type of Changes
Description
Closes #7380
@orSolocate I haven't created an issue for this but:
Currently doesn't raise like it should.
This would require quite a bit of refactoring in the current checker as it isn't really suited for checking class attributes. I'm not sure and there is no obligation, but perhaps you find this interesting and want to look into it?