Skip to content

Add __aiter__ and __anext__ methods for asyncio.StreamReader #4286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 28, 2020

Conversation

dlax
Copy link
Contributor

@dlax dlax commented Jun 28, 2020

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.

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.
@JelleZijlstra JelleZijlstra merged commit 0dd3258 into python:master Jun 28, 2020
@dlax dlax deleted the asyncio-streams-iter branch June 28, 2020 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants