PY-21048 Fixed: Strange inspection in return values of async function

Unwrap __coroutine in PyTypeCheckerInspection when matching expected and actual return types
This commit is contained in:
Semyon Proshev
2016-10-13 17:32:54 +03:00
parent d407a181ac
commit ccec504ffb
3 changed files with 19 additions and 1 deletions
@@ -91,7 +91,7 @@ public class PyTypeCheckerInspection extends PyInspection {
if (annotation != null || typeCommentAnnotation != null) {
final PyExpression returnExpr = node.getExpression();
final PyType actual = returnExpr != null ? myTypeEvalContext.getType(returnExpr) : PyNoneType.INSTANCE;
final PyType expected = myTypeEvalContext.getReturnType(function);
final PyType expected = getExpectedReturnType(function);
if (!PyTypeChecker.match(expected, actual, myTypeEvalContext)) {
final String expectedName = PythonDocumentationProvider.getTypeName(expected, myTypeEvalContext);
final String actualName = PythonDocumentationProvider.getTypeName(actual, myTypeEvalContext);
@@ -105,6 +105,17 @@ public class PyTypeCheckerInspection extends PyInspection {
}
}
@Nullable
private PyType getExpectedReturnType(@NotNull PyFunction function) {
final PyType returnType = myTypeEvalContext.getReturnType(function);
if (returnType instanceof PyCollectionType && PyNames.FAKE_COROUTINE.equals(returnType.getName())) {
return ((PyCollectionType)returnType).getElementTypes(myTypeEvalContext).get(0);
}
return returnType;
}
@Override
public void visitPyFunction(PyFunction node) {
final PyAnnotation annotation = node.getAnnotation();
@@ -0,0 +1,2 @@
async def test(self, a) -> int:
return 123
@@ -158,4 +158,9 @@ public class Py3TypeCheckerInspectionTest extends PyTestCase {
public void testComprehensionsOverAsyncGenerator() {
doTest();
}
// PY-21048
public void testAsyncFunctionReturnType() {
doTest();
}
}