PY-60714 PyCharm does not understand async iterators

__anext__ method is required to return Awaitable. When getting iterated type, unwrap that awaitable


Merge-request: IJ-MR-150432
Merged-by: Aleksandr Govenko <aleksandr.govenko@jetbrains.com>

(cherry picked from commit 97a070ead70c0ec0f2da08000517c592c5805965)

IJ-MR-150432

GitOrigin-RevId: 21dc20b164e6095e56707ea50bc93bf67564e88e
This commit is contained in:
Aleksandr.Govenko
2024-12-03 16:42:52 +00:00
committed by intellij-monorepo-bot
parent da3a2976a7
commit bdbe78b63a
2 changed files with 18 additions and 5 deletions

View File

@@ -381,10 +381,8 @@ public class PyTargetExpressionImpl extends PyBaseElementImpl<PyTargetExpression
boolean async) {
final Ref<PyType> nextMethodCallType = getNextMethodCallType(type, source, anchor, context, async);
if (nextMethodCallType != null && !nextMethodCallType.isNull()) {
if (nextMethodCallType.get() instanceof PyCollectionType collectionType) {
if (async && "typing.Awaitable".equals(collectionType.getClassQName())) {
return collectionType.getIteratedItemType();
}
if (async) {
return Ref.deref(PyTypingTypeProvider.unwrapCoroutineReturnType(nextMethodCallType.get()));
}
return nextMethodCallType.get();
}

View File

@@ -568,7 +568,7 @@ public class Py3TypeTest extends PyTestCase {
doTest("int",
"""
class AIter(object):
def __anext__(self):
async def __anext__(self):
return 5
class A(object):
def __aiter__(self):
@@ -577,6 +577,21 @@ public class Py3TypeTest extends PyTestCase {
async for expr in a:
print(expr)""");
}
// PY-60714
public void testAsyncIteratorUnwrapsCoroutineFromAnext() {
doTest("bytes", """
class AIterator:
def __aiter__(self):
return self
async def __anext__(self) -> bytes:
return b"a"
async for expr in AIterator():
print(expt)
""");
}
// PY-21655
public void testUsageOfFunctionDecoratedWithAsyncioCoroutine() {