-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fix crash while iteraring over a class attribute #7386
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Update ``modified_iterating`` checker to fix a crash with ``for`` loops on empty list. | ||
|
|
||
| Closes #7380 |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,5 @@ | ||||||||||||||||||||||||||
| """Tests for iterating-modified messages""" | ||||||||||||||||||||||||||
| # pylint: disable=not-callable,unnecessary-comprehension | ||||||||||||||||||||||||||
| # pylint: disable=not-callable,unnecessary-comprehension,too-few-public-methods | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| import copy | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
@@ -26,7 +26,7 @@ | |||||||||||||||||||||||||
| i = 1 | ||||||||||||||||||||||||||
| for item in my_dict: | ||||||||||||||||||||||||||
| item_list[0] = i # for coverage, see reference at /pull/5628#discussion_r792181642 | ||||||||||||||||||||||||||
| my_dict[i] = 1 # [modified-iterating-dict] | ||||||||||||||||||||||||||
| my_dict[i] = 1 # [modified-iterating-dict] | ||||||||||||||||||||||||||
| i += 1 | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| i = 1 | ||||||||||||||||||||||||||
|
|
@@ -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: | ||||||||||||||||||||||||||
| """Regression test for https://github.com/PyCQA/pylint/issues/7380""" | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def __init__(self) -> None: | ||||||||||||||||||||||||||
| self.attribute = [1, 2, 3] | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def my_method(self): | ||||||||||||||||||||||||||
| """This should raise as we are deleting.""" | ||||||||||||||||||||||||||
| for var in self.attribute: | ||||||||||||||||||||||||||
| del var # [modified-iterating-list] | ||||||||||||||||||||||||||
|
Comment on lines
+104
to
+107
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Also check the false positive case. Fix the
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 class MyClass:
def __init__():
self.attribute = [1,2,3]
def method(self):
for x in self.attribute:
del x
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean like when iterating through a dictionary
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||||||||||||||||||||||
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.