Skip to content

Commit 5e3d046

Browse files
ghislainpiotsonartech
authored andcommitted
SONARPY-3122 S7503 should not raise issues on async comprehensions (#365)
GitOrigin-RevId: 280765d366209ef9fd451c4bbaf994f53454c92f
1 parent c67678a commit 5e3d046

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

python-checks/src/main/java/org/sonar/python/checks/AsyncFunctionNotAsyncCheck.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
package org.sonar.python.checks;
1818

1919
import javax.annotation.Nullable;
20+
2021
import org.sonar.check.Rule;
2122
import org.sonar.plugins.python.api.PythonSubscriptionCheck;
2223
import org.sonar.plugins.python.api.SubscriptionContext;
2324
import org.sonar.plugins.python.api.tree.AwaitExpression;
2425
import org.sonar.plugins.python.api.tree.BaseTreeVisitor;
26+
import org.sonar.plugins.python.api.tree.ComprehensionFor;
2527
import org.sonar.plugins.python.api.tree.ForStatement;
2628
import org.sonar.plugins.python.api.tree.FunctionDef;
2729
import org.sonar.plugins.python.api.tree.ReturnStatement;
@@ -150,6 +152,12 @@ public void visitFunctionDef(FunctionDef functionDef) {
150152
// Skip nested functions
151153
}
152154

155+
@Override
156+
public void visitComprehensionFor(ComprehensionFor tree) {
157+
asyncFeatureFound |= tree.asyncToken() != null;
158+
super.visitComprehensionFor(tree);
159+
}
160+
153161
@Override
154162
protected void scan(@Nullable Tree tree) {
155163
// Stop scanning if we've already found an async feature

python-checks/src/test/resources/checks/asyncFunctionNotAsync.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ async def __anext__(self):
142142
class AsyncResource:
143143
async def __aclose__(self):
144144
print("Releasing resources")
145-
145+
146146
class AsyncAwaitableObject:
147147
async def __await__(self):
148148
yield "something"
@@ -183,3 +183,9 @@ class MyOtherClass(MyClass):
183183
async def my_method(self):
184184
# No issue on overriding methods
185185
do_something()
186+
187+
async def async_comprehension():
188+
return [something async for something in async_iterable()]
189+
190+
async def sync_comprehension(): # Noncompliant
191+
return [something for something in async_iterable()]

0 commit comments

Comments
 (0)