Skip to content

Commit 38dfb57

Browse files
Improve asyncio.subprocess stubs (#5327)
1 parent 1826072 commit 38dfb57

File tree

3 files changed

+113
-23
lines changed

3 files changed

+113
-23
lines changed

stdlib/asyncio/base_events.pyi

+12-6
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,9 @@ class BaseEventLoop(AbstractEventLoop, metaclass=ABCMeta):
315315
protocol_factory: _ProtocolFactory,
316316
cmd: Union[bytes, str],
317317
*,
318-
stdin: Any = ...,
319-
stdout: Any = ...,
320-
stderr: Any = ...,
318+
stdin: Union[int, IO[Any], None] = ...,
319+
stdout: Union[int, IO[Any], None] = ...,
320+
stderr: Union[int, IO[Any], None] = ...,
321321
universal_newlines: Literal[False] = ...,
322322
shell: Literal[True] = ...,
323323
bufsize: Literal[0] = ...,
@@ -329,10 +329,16 @@ class BaseEventLoop(AbstractEventLoop, metaclass=ABCMeta):
329329
async def subprocess_exec(
330330
self,
331331
protocol_factory: _ProtocolFactory,
332+
program: Any,
332333
*args: Any,
333-
stdin: Any = ...,
334-
stdout: Any = ...,
335-
stderr: Any = ...,
334+
stdin: Union[int, IO[Any], None] = ...,
335+
stdout: Union[int, IO[Any], None] = ...,
336+
stderr: Union[int, IO[Any], None] = ...,
337+
universal_newlines: Literal[False] = ...,
338+
shell: Literal[True] = ...,
339+
bufsize: Literal[0] = ...,
340+
encoding: None = ...,
341+
errors: None = ...,
336342
**kwargs: Any,
337343
) -> _TransProtPair: ...
338344
def add_reader(self, fd: FileDescriptorLike, callback: Callable[..., Any], *args: Any) -> None: ...

stdlib/asyncio/events.pyi

+19-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ from asyncio.transports import BaseTransport
99
from asyncio.unix_events import AbstractChildWatcher
1010
from socket import AddressFamily, SocketKind, _Address, _RetAddress, socket
1111
from typing import IO, Any, Awaitable, Callable, Dict, Generator, List, Optional, Sequence, Tuple, TypeVar, Union, overload
12+
from typing_extensions import Literal
1213

1314
if sys.version_info >= (3, 7):
1415
from contextvars import Context
@@ -399,19 +400,31 @@ class AbstractEventLoop(metaclass=ABCMeta):
399400
protocol_factory: _ProtocolFactory,
400401
cmd: Union[bytes, str],
401402
*,
402-
stdin: Any = ...,
403-
stdout: Any = ...,
404-
stderr: Any = ...,
403+
stdin: Union[int, IO[Any], None] = ...,
404+
stdout: Union[int, IO[Any], None] = ...,
405+
stderr: Union[int, IO[Any], None] = ...,
406+
universal_newlines: Literal[False] = ...,
407+
shell: Literal[True] = ...,
408+
bufsize: Literal[0] = ...,
409+
encoding: None = ...,
410+
errors: None = ...,
411+
text: Literal[False, None] = ...,
405412
**kwargs: Any,
406413
) -> _TransProtPair: ...
407414
@abstractmethod
408415
async def subprocess_exec(
409416
self,
410417
protocol_factory: _ProtocolFactory,
418+
program: Any,
411419
*args: Any,
412-
stdin: Any = ...,
413-
stdout: Any = ...,
414-
stderr: Any = ...,
420+
stdin: Union[int, IO[Any], None] = ...,
421+
stdout: Union[int, IO[Any], None] = ...,
422+
stderr: Union[int, IO[Any], None] = ...,
423+
universal_newlines: Literal[False] = ...,
424+
shell: Literal[True] = ...,
425+
bufsize: Literal[0] = ...,
426+
encoding: None = ...,
427+
errors: None = ...,
415428
**kwargs: Any,
416429
) -> _TransProtPair: ...
417430
@abstractmethod

stdlib/asyncio/subprocess.pyi

+82-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
import subprocess
12
import sys
3+
from _typeshed import AnyPath
24
from asyncio import events, protocols, streams, transports
3-
from typing import IO, Any, Optional, Tuple, Union
5+
from typing import IO, Any, Callable, Optional, Tuple, Union
6+
from typing_extensions import Literal
47

58
if sys.version_info >= (3, 8):
6-
from os import PathLike
7-
8-
_ExecArg = Union[str, bytes, PathLike[str], PathLike[bytes]]
9+
_ExecArg = AnyPath
910
else:
10-
_ExecArg = Union[str, bytes] # Union used instead of AnyStr due to mypy issue #1236
11+
_ExecArg = Union[str, bytes]
1112

1213
PIPE: int
1314
STDOUT: int
@@ -41,12 +42,30 @@ class Process:
4142

4243
if sys.version_info >= (3, 10):
4344
async def create_subprocess_shell(
44-
cmd: Union[str, bytes], # Union used instead of AnyStr due to mypy issue #1236
45+
cmd: Union[str, bytes],
4546
stdin: Union[int, IO[Any], None] = ...,
4647
stdout: Union[int, IO[Any], None] = ...,
4748
stderr: Union[int, IO[Any], None] = ...,
4849
limit: int = ...,
49-
**kwds: Any,
50+
*,
51+
# These parameters are forced to these values by BaseEventLoop.subprocess_shell
52+
universal_newlines: Literal[False] = ...,
53+
shell: Literal[True] = ...,
54+
bufsize: Literal[0] = ...,
55+
encoding: None = ...,
56+
errors: None = ...,
57+
text: Literal[False, None] = ...,
58+
# These parameters are taken by subprocess.Popen, which this ultimately delegates to
59+
executable: Optional[AnyPath] = ...,
60+
preexec_fn: Optional[Callable[[], Any]] = ...,
61+
close_fds: bool = ...,
62+
cwd: Optional[AnyPath] = ...,
63+
env: Optional[subprocess._ENV] = ...,
64+
startupinfo: Optional[Any] = ...,
65+
creationflags: int = ...,
66+
restore_signals: bool = ...,
67+
start_new_session: bool = ...,
68+
pass_fds: Any = ...,
5069
) -> Process: ...
5170
async def create_subprocess_exec(
5271
program: _ExecArg,
@@ -55,18 +74,53 @@ if sys.version_info >= (3, 10):
5574
stdout: Union[int, IO[Any], None] = ...,
5675
stderr: Union[int, IO[Any], None] = ...,
5776
limit: int = ...,
58-
**kwds: Any,
77+
# These parameters are forced to these values by BaseEventLoop.subprocess_shell
78+
universal_newlines: Literal[False] = ...,
79+
shell: Literal[True] = ...,
80+
bufsize: Literal[0] = ...,
81+
encoding: None = ...,
82+
errors: None = ...,
83+
# These parameters are taken by subprocess.Popen, which this ultimately delegates to
84+
text: Optional[bool] = ...,
85+
executable: Optional[AnyPath] = ...,
86+
preexec_fn: Optional[Callable[[], Any]] = ...,
87+
close_fds: bool = ...,
88+
cwd: Optional[AnyPath] = ...,
89+
env: Optional[subprocess._ENV] = ...,
90+
startupinfo: Optional[Any] = ...,
91+
creationflags: int = ...,
92+
restore_signals: bool = ...,
93+
start_new_session: bool = ...,
94+
pass_fds: Any = ...,
5995
) -> Process: ...
6096

6197
else:
6298
async def create_subprocess_shell(
63-
cmd: Union[str, bytes], # Union used instead of AnyStr due to mypy issue #1236
99+
cmd: Union[str, bytes],
64100
stdin: Union[int, IO[Any], None] = ...,
65101
stdout: Union[int, IO[Any], None] = ...,
66102
stderr: Union[int, IO[Any], None] = ...,
67103
loop: Optional[events.AbstractEventLoop] = ...,
68104
limit: int = ...,
69-
**kwds: Any,
105+
*,
106+
# These parameters are forced to these values by BaseEventLoop.subprocess_shell
107+
universal_newlines: Literal[False] = ...,
108+
shell: Literal[True] = ...,
109+
bufsize: Literal[0] = ...,
110+
encoding: None = ...,
111+
errors: None = ...,
112+
text: Literal[False, None] = ...,
113+
# These parameters are taken by subprocess.Popen, which this ultimately delegates to
114+
executable: Optional[AnyPath] = ...,
115+
preexec_fn: Optional[Callable[[], Any]] = ...,
116+
close_fds: bool = ...,
117+
cwd: Optional[AnyPath] = ...,
118+
env: Optional[subprocess._ENV] = ...,
119+
startupinfo: Optional[Any] = ...,
120+
creationflags: int = ...,
121+
restore_signals: bool = ...,
122+
start_new_session: bool = ...,
123+
pass_fds: Any = ...,
70124
) -> Process: ...
71125
async def create_subprocess_exec(
72126
program: _ExecArg,
@@ -76,5 +130,22 @@ else:
76130
stderr: Union[int, IO[Any], None] = ...,
77131
loop: Optional[events.AbstractEventLoop] = ...,
78132
limit: int = ...,
79-
**kwds: Any,
133+
# These parameters are forced to these values by BaseEventLoop.subprocess_shell
134+
universal_newlines: Literal[False] = ...,
135+
shell: Literal[True] = ...,
136+
bufsize: Literal[0] = ...,
137+
encoding: None = ...,
138+
errors: None = ...,
139+
# These parameters are taken by subprocess.Popen, which this ultimately delegates to
140+
text: Optional[bool] = ...,
141+
executable: Optional[AnyPath] = ...,
142+
preexec_fn: Optional[Callable[[], Any]] = ...,
143+
close_fds: bool = ...,
144+
cwd: Optional[AnyPath] = ...,
145+
env: Optional[subprocess._ENV] = ...,
146+
startupinfo: Optional[Any] = ...,
147+
creationflags: int = ...,
148+
restore_signals: bool = ...,
149+
start_new_session: bool = ...,
150+
pass_fds: Any = ...,
80151
) -> Process: ...

0 commit comments

Comments
 (0)