Add __aiter__ and __anext__ methods for asyncio.StreamReader (#4286)

The following code produces an error in mypy:

    import asyncio
    from asyncio.subprocess import PIPE

    async def main() -> None:
        proc = await asyncio.create_subprocess_shell("ls -l", stdout=PIPE)
        assert proc.stdout is not None
        async for line in proc.stdout:
            print(line.decode())
        await proc.wait()

    asyncio.run(main())

$ mypy --strict file.py
file.py:8: error: "StreamReader" has no attribute "__aiter__" (not async iterable)

This commits fixes this by adding __aiter__/__anext__ methods that are
needed for async iterator protocol.
This commit is contained in:
Denis Laxalde
2020-06-28 20:57:49 +02:00
committed by GitHub
parent ed04d33def
commit 0dd3258ed2

View File

@@ -1,5 +1,5 @@
import sys
from typing import Any, Awaitable, Callable, Iterable, Optional, Tuple, Union
from typing import Any, AsyncIterator, Awaitable, Callable, Iterable, Optional, Tuple, Union
from . import events
from . import protocols
@@ -106,3 +106,5 @@ class StreamReader:
async def readuntil(self, separator: bytes = ...) -> bytes: ...
async def read(self, n: int = ...) -> bytes: ...
async def readexactly(self, n: int) -> bytes: ...
def __aiter__(self) -> AsyncIterator[bytes]: ...
async def __anext__(self) -> bytes: ...