-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Python: Modernize iter not returning self query #19554
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
Merged
joefarebrother
merged 10 commits into
github:main
from
joefarebrother:python-qual-iter-not-return-self
Jun 13, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bedd44a
Update query and add case for iter(self.__next__, None)
joefarebrother 7b452a1
Add case for wrappers
joefarebrother f27057a
Update qhelp
joefarebrother 06504f2
Update tests
joefarebrother 44a678a
remove redundant import
joefarebrother b15fec0
Fix qhelp and tests
joefarebrother e933a27
Add changenote
joefarebrother c070d04
Fix qhelp
joefarebrother f3a5608
Apply review suggestions - remove methodOfClass, fix qhelp typo; addi…
joefarebrother 73f2770
Fix handling for some wrappers + add test case
joefarebrother File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
python/ql/integration-tests/query-suite/python-code-quality.qls.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
python/ql/src/change-notes/2025-05-23-iter-not-return-self.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
category: minorAnalysis | ||
--- | ||
* The `py/iter-returns-non-self` query has been modernized, and no longer alerts for certain cases where an equivalent iterator is returned. |
1 change: 0 additions & 1 deletion
1
python/ql/test/query-tests/Functions/general/IterReturnsNonSelf.expected
This file was deleted.
Oops, something went wrong.
2 changes: 2 additions & 0 deletions
2
python/ql/test/query-tests/Functions/iterators/IterReturnsNonSelf.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
| test.py:5:5:5:23 | Function __iter__ | Iter method of iterator $@ does not return `self`. | test.py:1:1:1:11 | Class Bad1 | Bad1 | | ||
| test.py:51:5:51:23 | Function __iter__ | Iter method of iterator $@ does not return `self`. | test.py:42:1:42:21 | Class FalsePositive1 | FalsePositive1 | |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
class Bad1: | ||
def __next__(self): | ||
return 0 | ||
|
||
def __iter__(self): # BAD: Iter does not return self | ||
yield 0 | ||
|
||
class Good1: | ||
def __next__(self): | ||
return 0 | ||
|
||
def __iter__(self): # GOOD: iter returns self | ||
return self | ||
|
||
class Good2: | ||
def __init__(self): | ||
self._it = iter([0,0,0]) | ||
|
||
def __next__(self): | ||
return next(self._it) | ||
|
||
def __iter__(self): # GOOD: iter and next are wrappers around a field | ||
return self._it.__iter__() | ||
|
||
class Good3: | ||
def __init__(self): | ||
self._it = iter([0,0,0]) | ||
|
||
def __next__(self): | ||
return self._it.__next__() | ||
|
||
def __iter__(self): # GOOD: iter and next are wrappers around a field | ||
return self._it | ||
|
||
class Good4: | ||
def __next__(self): | ||
return 0 | ||
|
||
def __iter__(self): # GOOD: this is an equivalent iterator to `self`. | ||
return iter(self.__next__, None) | ||
|
||
class FalsePositive1: | ||
def __init__(self): | ||
self._it = None | ||
|
||
def __next__(self): | ||
if self._it is None: | ||
self._it = iter(self) | ||
return next(self._it) | ||
|
||
def __iter__(self): # SPURIOUS, GOOD: implementation of next ensures the iterator is equivalent to the one returned by iter, but this is not detected. | ||
yield 0 | ||
yield 0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.