Skip to content
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

Merged
merged 3 commits into from
Aug 31, 2022

Conversation

DanielNoord
Copy link
Collaborator

  • Add yourself to CONTRIBUTORS if you are a new contributor.
  • Add a ChangeLog entry describing what your PR does.
  • If it's a new feature, or an important bug fix, add a What's New entry in
    doc/whatsnew/<current release.rst>.
  • Write a good description on what the PR does.

Type of Changes

Type
🐛 Bug fix

Description

Closes #7380

@orSolocate I haven't created an issue for this but:

class MyClass:
    def __init__(self) -> None:
        self.attribute = [1, 2, 3]

   def my_other_method(self):
        """This should raise as we are appending."""
        for var in self.attribute:
            self.attribute.append(var)  # [modified-iterating-list]

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?

@DanielNoord DanielNoord added Needs backport Needs to be cherry-picked on the current patch version by a pylint's maintainer Crash 💥 A bug that makes pylint crash labels Aug 31, 2022
@DanielNoord DanielNoord added this to the 2.15.1 milestone Aug 31, 2022
@coveralls
Copy link

coveralls commented Aug 31, 2022

Pull Request Test Coverage Report for Build 2966111299

  • 4 of 4 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.0008%) to 95.318%

Totals Coverage Status
Change from base Build 2962079951: 0.0008%
Covered Lines: 16939
Relevant Lines: 17771

💛 - Coveralls

@github-actions

This comment has been minimized.

bleudev
bleudev previously approved these changes Aug 31, 2022
Copy link
Contributor

@orSolocate orSolocate left a 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
Copy link
Contributor

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

Copy link
Collaborator Author

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]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.attribute = [1, 2, 3]
self.iterated_attributes = [1, 2, 3]

Comment on lines +104 to +107
def my_method(self):
"""This should raise as we are deleting."""
for var in self.attribute:
del var # [modified-iterating-list]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Collaborator Author

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.

Copy link
Contributor

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?

Copy link
Collaborator Author

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.

Copy link
Contributor

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?

Copy link
Collaborator Author

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.

doc/whatsnew/fragments/7380.bugfix Outdated Show resolved Hide resolved
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a 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 :)

DanielNoord and others added 2 commits August 31, 2022 20:17
Co-authored-by: orSolocate <38433858+orSolocate@users.noreply.github.com>
@orSolocate
Copy link
Contributor

@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 :)

@github-actions
Copy link
Contributor

🤖 According to the primer, this change has no effect on the checked open source code. 🤖🎉

This comment was generated for commit 339abe2

@DanielNoord DanielNoord merged commit bcd4ca3 into pylint-dev:main Aug 31, 2022
@DanielNoord DanielNoord deleted the crash branch August 31, 2022 20:06
@Pierre-Sassoulas Pierre-Sassoulas added Backported and removed Needs backport Needs to be cherry-picked on the current patch version by a pylint's maintainer labels Sep 1, 2022
Pierre-Sassoulas pushed a commit to Pierre-Sassoulas/pylint that referenced this pull request Sep 1, 2022
Co-authored-by: orSolocate <38433858+orSolocate@users.noreply.github.com>
Pierre-Sassoulas pushed a commit that referenced this pull request Sep 6, 2022
Co-authored-by: orSolocate <38433858+orSolocate@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backported Crash 💥 A bug that makes pylint crash
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AttributeError: 'Attribute' object has no attribute 'name'
5 participants