subprocess.Popen: overloads for set stdin/stdout/stderr attributes?
When passing in stdin, stdout, or stderr those attributes are also set on the Popen instances, i.e. not Optional/None.
Given the many overloads there are already (https://github.com/python/typeshed/blob/2d82e1fb8bcedc75be3ae91f911472931c92490a/stdlib/3/subprocess.pyi#L844-L1156) and that this would (AFAIK) have to multiply them, this might not really be feasible/practical though?
Ref: https://github.com/python/typeshed/commit/76b295a66919dcb3719e73011747dbcbc321c1d3 made them Optional by default.
I'm not sure if this is the same as the issue I just ran into, but I'll comment here since it seems like it's probably related:
I'm creating a Popen object with one of the arguments being stdin=subprocess.PIPE. Afterwards, trying to call popen_obj.stdin.write(command) gives a mypy error:
error: Item "None" of "Optional[IO[str]]" has no attribute "write"
Same issue occurs with asyncio.create_subprocess_exec(). Although this one doesn't have any overloads currently, so could be easier to update.
https://github.com/python/typeshed/blob/master/stdlib/3/asyncio/subprocess.pyi#L42
I'm creating a
Popenobject with one of the arguments beingstdin=subprocess.PIPE. Afterwards, trying to callpopen_obj.stdin.write(command)gives a mypy error:error: Item "None" of "Optional[IO[str]]" has no attribute "write"
I hit the similar issue as well, do we have an ETA for this?
My issue was:
error: Item "None" of "Optional[IO[str]]" has no attribute "readline"
Adding if result.stdout is not None: works for the mypy check, but I'm not sure whether it's even needed at all
@jlin880 wrote:
I hit the similar issue as well, do we have an ETA for this?
We don't even have a this for this; that is, nobody has even really sketched a solution, probably because of how repetitive the signatures in the subprocess stubs are (because that was the only way to model the evolution of the signatures from version to version of Python at the time).
Perhaps PEP 692 can be used to eliminate most of the repeated signatures, now that typeshed is allowed to use it (https://github.com/python/typeshed/issues/9710), using a TypedDict for the kwargs to place the version checks directly around the specific fields they control instead of having a huge pile of enormous signatures with tiny differences from each to the next. I hope that can work?
That would make it easier to contemplate solving the problem with stdin/stdout/stderr, though it still seems like it might involve a combinatorial explosion in signatures, and I don't know how you could do that in a maintainable way.