Fix crash while iteraring over a class attribute#7386
Fix crash while iteraring over a class attribute#7386DanielNoord merged 3 commits intopylint-dev:mainfrom
Conversation
Pull Request Test Coverage Report for Build 2966111299
💛 - Coveralls |
This comment has been minimized.
This comment has been minimized.
orSolocate
left a comment
There was a problem hiding this comment.
@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.
| my_dict[new_key] = 1 # [modified-iterating-dict] | ||
|
|
||
|
|
||
| class MyClass: # pylint: disable=too-few-public-methods |
There was a problem hiding this comment.
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.
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.
| 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.
| 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.
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.
@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.
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 xAstroid 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.
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.
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.
Pierre-Sassoulas
left a comment
There was a problem hiding this comment.
LGTM, let's merge once the discussion with @orSolocate is resolved :)
2b3e733
Co-authored-by: orSolocate <38433858+orSolocate@users.noreply.github.com>
|
@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 <38433858+orSolocate@users.noreply.github.com>
Co-authored-by: orSolocate <38433858+orSolocate@users.noreply.github.com>
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?