Annotate 'yield' inside async functions as errors (PY-16094)

This commit is contained in:
Andrey Vlasovskikh
2015-09-08 15:03:39 +03:00
parent c8a8585c36
commit e2d75292f0
3 changed files with 13 additions and 1 deletions
@@ -21,7 +21,7 @@ import com.jetbrains.python.codeInsight.dataflow.scope.ScopeUtil;
import com.jetbrains.python.psi.*;
/**
* Highlights incorrect return statements: 'return' and 'yield' outside functions, returning values from generators.
* Highlights incorrect return statements: 'return' and 'yield' outside functions, 'yield' inside async functions.
*/
public class ReturnAnnotator extends PyAnnotator {
public void visitPyReturnStatement(final PyReturnStatement node) {
@@ -36,5 +36,8 @@ public class ReturnAnnotator extends PyAnnotator {
if (!(owner instanceof PyFunction || owner instanceof PyLambdaExpression)) {
getHolder().createErrorAnnotation(node, "'yield' outside of function");
}
if (owner instanceof PyFunction && ((PyFunction)owner).isAsync()) {
getHolder().createErrorAnnotation(node, "'yield' inside async function");
}
}
}
@@ -0,0 +1,5 @@
async def foo(x):
await x
<error descr="'yield' inside async function">yield x</error>
<error descr="'yield' inside async function">yield from x</error>
return x
@@ -237,6 +237,10 @@ public class PythonHighlightingTest extends PyTestCase {
doTest(LanguageLevel.PYTHON35, true, true);
}
public void testYieldInsideAsyncDef() {
doTest(LanguageLevel.PYTHON35, false, false);
}
// ---
private void doTest(final LanguageLevel languageLevel, final boolean checkWarnings, final boolean checkInfos) {
PythonLanguageLevelPusher.setForcedLanguageLevel(myFixture.getProject(), languageLevel);