Skip to content

Commit

Permalink
Update stubs for upstream updates we haven't taken yet: lowlevel.open…
Browse files Browse the repository at this point in the history
…_process(), nursery.start(run_process, ...), run(strict_exception_groups=...), DTLS
  • Loading branch information
oremanj committed Mar 28, 2023
1 parent 73ba636 commit 2363837
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 47 deletions.
99 changes: 52 additions & 47 deletions trio-stubs/__init__.pyi
Expand Up @@ -183,6 +183,7 @@ def run(
clock: Optional[trio.abc.Clock] = ...,
instruments: Sequence[trio.abc.Instrument] = ...,
restrict_keyboard_interrupt_to_checkpoints: bool = ...,
strict_exception_groups: bool = ...,
) -> T: ...

# _timeouts
Expand Down Expand Up @@ -336,6 +337,53 @@ class SocketListener(trio.abc.Listener[SocketStream]):
async def accept(self) -> SocketStream: ...
async def aclose(self) -> None: ...

# _dtls
class DTLSEndpoint:
socket: trio.socket.SocketType
incoming_packets_buffer: int
def __init__(self, socket: trio.socket.SocketType, *, incoming_packets_buffer: int = ...) -> None: ...
# ssl_context is an OpenSSL.SSL.Context from PyOpenSSL, but we can't
# import that name because we don't depend on PyOpenSSL
def connect(self, address: address: Union[Tuple[Any, ...], str, bytes], ssl_context: Any) -> DTLSChannel: ...
@takes_callable_and_args
async def serve(
self,
ssl_context: Any,
async_fn: Union[
Callable[..., Awaitable[Any]],
Callable[[DTLSChannel, VarArg()], Awaitable[Any]],
],
*args: Any,
task_status: TaskStatus[None] = ...,
) -> NoReturn: ...
def close(self) -> None: ...
def __enter__(self) -> DTLSEndpoint: ...
def __exit__(
self,
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None: ...

class DTLSChannel(_NotConstructible, trio.abc.Channel[bytes]):
endpoint: DTLSEndpoint
peer_address: Union[Tuple[Any, ...], str, bytes]
async def do_handshake(self, *, initial_retransmit_timeout: float = ...) -> None: ...
async def send(self, data: bytes) -> None: ...
async def receive(self) -> bytes: ...
def set_ciphertext_mtu(self, new_mtu: int) -> None: ...
def get_cleartext_mtu(self) -> int: ...
def statistics(self) -> Any: ...
async def aclose(self) -> None: ...
def close(self) -> None: ...
def __enter__(self) -> DTLSChannel: ...
def __exit__(
self,
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None: ...

# _file_io

# The actual Trio I/O classes use metaprogramming to forward their methods
Expand Down Expand Up @@ -697,7 +745,7 @@ class _HasFileno(Protocol):

_Redirect = Union[int, _HasFileno, None]

class Process(trio.abc.AsyncResource, _NotConstructible, metaclass=ABCMeta):
class Process(_NotConstructible, metaclass=ABCMeta):
stdin: Optional[trio.abc.SendStream]
stdout: Optional[trio.abc.ReceiveStream]
stderr: Optional[trio.abc.ReceiveStream]
Expand All @@ -706,7 +754,6 @@ class Process(trio.abc.AsyncResource, _NotConstructible, metaclass=ABCMeta):
pid: int
@property
def returncode(self) -> Optional[int]: ...
async def aclose(self) -> None: ...
async def wait(self) -> int: ...
def poll(self) -> Optional[int]: ...
def send_signal(self, sig: signal.Signals) -> None: ...
Expand All @@ -725,22 +772,10 @@ class Process(trio.abc.AsyncResource, _NotConstructible, metaclass=ABCMeta):
# bytes as stdin

if sys.platform == "win32":
async def open_process(
command: Union[StrOrBytesPath, Sequence[StrOrBytesPath]],
*,
stdin: _Redirect = ...,
stdout: _Redirect = ...,
stderr: _Redirect = ...,
close_fds: bool = ...,
shell: bool = ...,
cwd: Optional[StrOrBytesPath] = ...,
env: Optional[Mapping[str, str]] = ...,
startupinfo: subprocess.STARTUPINFO = ...,
creationflags: int = ...,
) -> Process: ...
async def run_process(
command: Union[StrOrBytesPath, Sequence[StrOrBytesPath]],
*,
task_status: TaskStatus[Process] = ...,
stdin: Union[bytes, _Redirect] = ...,
capture_stdout: bool = ...,
capture_stderr: bool = ...,
Expand All @@ -757,42 +792,11 @@ if sys.platform == "win32":
) -> subprocess.CompletedProcess[bytes]: ...

else:
@overload
async def open_process(
command: StrOrBytesPath,
*,
stdin: _Redirect = ...,
stdout: _Redirect = ...,
stderr: _Redirect = ...,
close_fds: bool = ...,
shell: Literal[True],
cwd: Optional[StrOrBytesPath] = ...,
env: Optional[Mapping[str, str]] = ...,
preexec_fn: Optional[Callable[[], Any]] = ...,
restore_signals: bool = ...,
start_new_session: bool = ...,
pass_fds: Sequence[int] = ...,
) -> Process: ...
@overload
async def open_process(
command: Sequence[StrOrBytesPath],
*,
stdin: _Redirect = ...,
stdout: _Redirect = ...,
stderr: _Redirect = ...,
close_fds: bool = ...,
shell: bool = ...,
cwd: Optional[StrOrBytesPath] = ...,
env: Optional[Mapping[str, str]] = ...,
preexec_fn: Optional[Callable[[], Any]] = ...,
restore_signals: bool = ...,
start_new_session: bool = ...,
pass_fds: Sequence[int] = ...,
) -> Process: ...
@overload
async def run_process(
command: StrOrBytesPath,
*,
task_status: TaskStatus[Process] = ...,
stdin: Union[bytes, _Redirect] = ...,
capture_stdout: bool = ...,
capture_stderr: bool = ...,
Expand All @@ -813,6 +817,7 @@ else:
async def run_process(
command: Sequence[StrOrBytesPath],
*,
task_status: TaskStatus[Process] = ...,
stdin: Union[bytes, _Redirect] = ...,
capture_stdout: bool = ...,
capture_stderr: bool = ...,
Expand Down
62 changes: 62 additions & 0 deletions trio-stubs/lowlevel.pyi
Expand Up @@ -165,6 +165,68 @@ def start_thread_soon(
fn: Callable[[], T], deliver: Callable[[outcome.Outcome[T]], None]
) -> None: ...

# _subprocess

# There's a lot of duplication here because mypy doesn't
# have a good way to represent overloads that differ only
# slightly. A cheat sheet:
# - on Windows, command is Union[str, Sequence[str]];
# on Unix, command is str if shell=True and Sequence[str] otherwise
# - on Windows, there are startupinfo and creationflags options;
# on Unix, there are preexec_fn, restore_signals, start_new_session, and pass_fds
# - run_process() has the signature of open_process() plus arguments
# capture_stdout, capture_stderr, check, deliver_cancel, and the ability to pass
# bytes as stdin

if sys.platform == "win32":
async def open_process(
command: Union[StrOrBytesPath, Sequence[StrOrBytesPath]],
*,
stdin: _Redirect = ...,
stdout: _Redirect = ...,
stderr: _Redirect = ...,
close_fds: bool = ...,
shell: bool = ...,
cwd: Optional[StrOrBytesPath] = ...,
env: Optional[Mapping[str, str]] = ...,
startupinfo: subprocess.STARTUPINFO = ...,
creationflags: int = ...,
) -> Process: ...

else:
@overload
async def open_process(
command: StrOrBytesPath,
*,
stdin: _Redirect = ...,
stdout: _Redirect = ...,
stderr: _Redirect = ...,
close_fds: bool = ...,
shell: Literal[True],
cwd: Optional[StrOrBytesPath] = ...,
env: Optional[Mapping[str, str]] = ...,
preexec_fn: Optional[Callable[[], Any]] = ...,
restore_signals: bool = ...,
start_new_session: bool = ...,
pass_fds: Sequence[int] = ...,
) -> Process: ...
@overload
async def open_process(
command: Sequence[StrOrBytesPath],
*,
stdin: _Redirect = ...,
stdout: _Redirect = ...,
stderr: _Redirect = ...,
close_fds: bool = ...,
shell: bool = ...,
cwd: Optional[StrOrBytesPath] = ...,
env: Optional[Mapping[str, str]] = ...,
preexec_fn: Optional[Callable[[], Any]] = ...,
restore_signals: bool = ...,
start_new_session: bool = ...,
pass_fds: Sequence[int] = ...,
) -> Process: ...

# _unix_pipes
class FdStream(trio.abc.Stream):
def __init__(self, fd: int): ...
Expand Down

0 comments on commit 2363837

Please sign in to comment.