-
Notifications
You must be signed in to change notification settings - Fork 31
Create rule S7524 async for
should be used with asynchronous iterators in async
functions
#5116
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
Open
github-actions
wants to merge
3
commits into
master
Choose a base branch
from
rule/add-RSPEC-S7524
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
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
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 @@ | ||
{ | ||
} |
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,25 @@ | ||
{ | ||
"title": "\"async for\" should be used for asynchronous iterators", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant/Issue", | ||
"constantCost": "5min" | ||
}, | ||
"tags": [ | ||
"async" | ||
], | ||
"defaultSeverity": "Major", | ||
"ruleSpecification": "RSPEC-7524", | ||
"sqKey": "S7524", | ||
"scope": "All", | ||
"defaultQualityProfiles": ["Sonar way"], | ||
"quickfix": "covered", | ||
"code": { | ||
"impacts": { | ||
"MAINTAINABILITY": "LOW", | ||
"RELIABILITY": "LOW" | ||
}, | ||
"attribute": "CONVENTIONAL" | ||
} | ||
} |
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,94 @@ | ||
This rule raises an issue when a synchronous `for` loop is used with an iterator implementing the asynchronous iteration protocol. | ||
|
||
== Why is this an issue? | ||
|
||
When working inside an `async` function, you should use `async for` to iterate over asynchronous iterators. An asynchronous iterator is an object that provides `+__aiter__+` and `+__anext__+` methods and should be used instead of a synchronous `for` loop. | ||
|
||
=== What is the potential impact? | ||
|
||
Not following the proper async pattern can lead to: | ||
|
||
* **Incorrect Logic**: It shows a misunderstanding of the asynchronous nature of the data source. | ||
* **Inconsistent async usage**: Mixing synchronous and asynchronous iteration patterns makes code harder to read and maintain. | ||
|
||
== How to fix it | ||
|
||
Use the `async for` statement to iterate over asynchronous iterators inside an `async` function. | ||
|
||
=== Code examples | ||
|
||
==== Noncompliant code example | ||
|
||
[source,python,diff-id=1,diff-type=noncompliant] | ||
---- | ||
class MyIterator: | ||
def __init__(self): | ||
... | ||
|
||
def __iter__(self): | ||
... | ||
|
||
def __next__(self): | ||
... | ||
|
||
def __aiter__(self): | ||
... | ||
|
||
async def __anext__(self): | ||
... | ||
|
||
async def main(): | ||
for _ in MyIterator(): # Noncompliant | ||
... | ||
---- | ||
|
||
==== Compliant solution | ||
|
||
[source,python,diff-id=1,diff-type=compliant] | ||
---- | ||
class MyIterator: | ||
def __init__(self): | ||
... | ||
|
||
def __iter__(self): | ||
... | ||
|
||
def __next__(self): | ||
... | ||
|
||
def __aiter__(self): | ||
... | ||
|
||
async def __anext__(self): | ||
... | ||
|
||
async def main(): | ||
async for _ in MyIterator(): # Compliant | ||
... | ||
---- | ||
|
||
ifdef::env-github,rspecator-view[] | ||
|
||
== Implementation Specification | ||
(visible only on this page) | ||
|
||
=== Message | ||
|
||
Use "async for" for asynchronous iterators | ||
|
||
Consider implementing quickfix to replace the `for` keyword with `async for`. | ||
|
||
=== Highlighting | ||
|
||
* Primary location: The `for` keyword of the loop. | ||
* Secondary locations: The `async` keyword of the enclosing function | ||
* Secondary locations: The `+__aiter__+` and `+__anext__+` methods if we can get the location | ||
endif::env-github,rspecator-view[] | ||
|
||
== Resources | ||
|
||
=== Documentation | ||
|
||
* Python Documentation - https://docs.python.org/3/reference/compound_stmts.html#the-async-for-statement[The async for statement] | ||
* Python Documentation - https://docs.python.org/3/reference/datamodel.html#the-iterator-protocol[The iterator protocol] | ||
* Python Documentation - https://docs.python.org/3/reference/datamodel.html#asynchronous-iterators[Asynchronous Iterators] |
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.
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 with Low the severity should be
Minor