Skip to content

Commit

Permalink
Merge pull request #76 from python-trio/misc-fixes
Browse files Browse the repository at this point in the history
Update stubs for upstream updates we haven't taken yet
  • Loading branch information
oremanj committed Mar 29, 2023
2 parents 73ba636 + 0373f91 commit 8d98579
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 50 deletions.
5 changes: 2 additions & 3 deletions allowlist.txt
Expand Up @@ -111,22 +111,21 @@ trio.socket.fromshare
trio.socket.ntohl

# Not present in stub (but maybe should be?)
trio.DTLSChannel
trio.DTLSEndpoint
trio.MultiError.derive
trio.NeedHandshakeError
trio.Path.hardlink_to
trio.Path.is_mount
trio.Path.link_to
trio.Path.readlink
trio.Process.aclose
trio.Process.encoding
trio.Process.errors
trio.Process.universal_newlines
trio.TrioDeprecationWarning
trio.lowlevel.FdStream.close
trio.lowlevel.open_process

# NoPublicConstructor
trio.DTLSChannel.__init__
trio.MemoryReceiveChannel.__init__
trio.MemorySendChannel.__init__
trio.Nursery.__init__
Expand Down
105 changes: 58 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,59 @@ 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: 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], metaclass=ABCMeta):
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 +751,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 +760,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 +778,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 +798,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 +823,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
71 changes: 71 additions & 0 deletions trio-stubs/lowlevel.pyi
Expand Up @@ -7,15 +7,19 @@ from typing import (
ContextManager,
Coroutine,
Generic,
Mapping,
NoReturn,
Optional,
Sequence,
Union,
Sequence,
TypeVar,
Tuple,
overload,
)
from _typeshed import StrOrBytesPath
from trio_typing import Nursery, takes_callable_and_args
from typing_extensions import Literal, Protocol
from mypy_extensions import VarArg
import trio
import outcome
Expand Down Expand Up @@ -165,6 +169,73 @@ 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

class _HasFileno(Protocol):
def fileno(self) -> int: ...

_Redirect = Union[int, _HasFileno, None]

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 = ...,
) -> trio.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] = ...,
) -> trio.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] = ...,
) -> trio.Process: ...

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

0 comments on commit 8d98579

Please sign in to comment.