Skip to content

Type hint asyncio.Process streams (stdin, stdout, stderr) when created with values other than default None #13714

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

Open
makukha opened this issue Mar 24, 2025 · 3 comments
Labels
stubs: false positive Type checkers report false errors

Comments

@makukha
Copy link

makukha commented Mar 24, 2025

Is it possible to update asyncio stub files so that asyncio.Process.{stdin,stdout,stderr} are resolved as non-None by mypy?

Example:

from asyncio import create_subprocess_exec
from asyncio.subprocess import PIPE

async def main() -> None:
    proc = await create_subprocess_exec('/usr/bin/ls', stdout=PIPE)
    async for line in proc.stdout:
        print(line)

Typing error reported by mypy (desired behaviour is no error):

error: Item "None" of "StreamReader | None" has no attribute "__aiter__" (not async iterable)  [union-attr]

Current asyncio.Process declaration in typeshed/stdlib/asyncio/subprocess.pyi:

class Process:
    stdin: streams.StreamWriter | None
    stdout: streams.StreamReader | None
    stderr: streams.StreamReader | None
@srittau srittau added the stubs: false positive Type checkers report false errors label Mar 25, 2025
@srittau
Copy link
Collaborator

srittau commented Mar 25, 2025

Considering that type variable defaults can now be used, it makes sense to me to make Process generic over the various streams. In that case, it seems possibly to me to make this work properly, but I haven't looked too closely at it.

@makukha
Copy link
Author

makukha commented Mar 29, 2025

Functions affected:

Is it true that if using Process[TIN, TOUT, TERR] generic, every function above will require 8 overload alternatives? If so, this solution is questionable, especially regarding that the same problem appears for subprocess module that is already full of overloads.

@makukha
Copy link
Author

makukha commented Mar 29, 2025

The same problem appears in subprocess module.

from subprocess import PIPE, Popen

def main() -> None:
    popen = Popen('/usr/bin/ls', stdout=PIPE)
    for line in popen.stdout:
        print(line)
error: Item "None" of "IO[bytes] | None" has no attribute "__iter__" (not iterable)  [union-attr]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stubs: false positive Type checkers report false errors
Projects
None yet
Development

No branches or pull requests

2 participants