Код: Выделить всё
async for foo in bar:
...
Код: Выделить всё
import pytest
class TestImplementation:
def __aiter__(self):
return self
async def __anext__(self):
raise StopAsyncIteration
@pytest.mark.asyncio # note use of pytest-asyncio marker
async def test_async_for():
async for _ in TestImplementation():
pass
Код: Выделить всё
=================================== FAILURES ===================================
________________________________ test_async_for ________________________________
@pytest.mark.asyncio
async def test_async_for():
> async for _ in TestImplementation():
E TypeError: 'async for' received an invalid object from __aiter__: TestImplementation
...: TypeError
===================== 1 failed, ... passed in 2.89 seconds ======================
- Объект должен реализовать метод __aiter__... возвращающий объект асинхронного итератора.
- Объект асинхронного итератора должен реализовать метод __anext__... возвращающий ожидаемый объект.
- Чтобы остановить итерацию __anext__ должен вызвать исключение StopAsyncIteration.
Это не работает в последних выпущенных версиях Python (3.5.1), py.test (2.9.2) и pytest-asyncio (0.4.1).
Подробнее здесь: https://stackoverflow.com/questions/380 ... s-iterator
Мобильная версия