Skip to content

Commit 17db562

Browse files
committed
Wrap the return type from type providers for async function into the coroutine type (PY-16094)
It is suggested in python/typing#119 and most likely will become a part of PEP 484 after Python 3.5.
1 parent 5382632 commit 17db562

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

‎python/src/com/jetbrains/python/psi/impl/PyFunctionImpl.java‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ public PyDecoratorList getDecoratorList() {
182182
@Nullable
183183
@Override
184184
public PyType getReturnType(@NotNull TypeEvalContext context, @NotNull TypeEvalContext.Key key) {
185+
final PyType type = getReturnType(context);
186+
return isAsync() ? createCoroutineType(type) : type;
187+
}
188+
189+
@Nullable
190+
private PyType getReturnType(@NotNull TypeEvalContext context) {
185191
for (PyTypeProvider typeProvider : Extensions.getExtensions(PyTypeProvider.EP_NAME)) {
186192
final Ref<PyType> returnTypeRef = typeProvider.getReturnType(this, context);
187193
if (returnTypeRef != null) {
@@ -204,9 +210,6 @@ public PyType getReturnType(@NotNull TypeEvalContext context, @NotNull TypeEvalC
204210
}
205211
return getReturnStatementType(context);
206212
}
207-
else if (isAsync()) {
208-
return createCoroutineType(null);
209-
}
210213
return null;
211214
}
212215

@@ -367,16 +370,15 @@ public PyType getReturnStatementType(TypeEvalContext typeEvalContext) {
367370
}
368371
return null;
369372
}
370-
final PyType returnType = visitor.result();
371-
if (isAsync()) {
372-
return createCoroutineType(returnType);
373-
}
374-
return returnType;
373+
return visitor.result();
375374
}
376375

377376
@Nullable
378377
private PyType createCoroutineType(@Nullable PyType returnType) {
379378
final PyBuiltinCache cache = PyBuiltinCache.getInstance(this);
379+
if (returnType instanceof PyClassLikeType && PyNames.FAKE_COROUTINE.equals(((PyClassLikeType)returnType).getClassQName())) {
380+
return returnType;
381+
}
380382
final PyClass generator = cache.getClass(PyNames.FAKE_COROUTINE);
381383
return generator != null ? new PyCollectionTypeImpl(generator, false, Collections.singletonList(returnType)) : null;
382384
}

‎python/testSrc/com/jetbrains/python/Py3TypeTest.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,21 @@ public void run() {
102102
});
103103
}
104104

105+
// Not in PEP 484 as for now, see https://github.com/ambv/typehinting/issues/119
106+
public void testCoroutineReturnTypeAnnotation() {
107+
runWithLanguageLevel(LanguageLevel.PYTHON35, new Runnable() {
108+
@Override
109+
public void run() {
110+
doTest("int",
111+
"async def foo() -> int: ...\n" +
112+
"\n" +
113+
"async def bar():\n" +
114+
" expr = await foo()\n");
115+
}
116+
});
117+
118+
}
119+
105120
private void doTest(final String expectedType, final String text) {
106121
myFixture.configureByText(PythonFileType.INSTANCE, text);
107122
final PyExpression expr = myFixture.findElementByText("expr", PyExpression.class);

0 commit comments

Comments
 (0)