Drop `Optional` style from runtime mod

ctx_cancel_semantics_and_overruns
Tyler Goodlet 2023-05-25 16:00:05 -04:00
parent a0276f41c2
commit 6495688730
1 changed files with 29 additions and 24 deletions

View File

@ -30,7 +30,6 @@ import sys
from typing import ( from typing import (
Any, Any,
Callable, Callable,
Optional,
Union, Union,
TYPE_CHECKING, TYPE_CHECKING,
) )
@ -101,7 +100,7 @@ async def _invoke(
cancel_scope = trio.CancelScope() cancel_scope = trio.CancelScope()
# activated cancel scope ref # activated cancel scope ref
cs: Optional[trio.CancelScope] = None cs: trio.CancelScope | None = None
ctx = actor.get_context( ctx = actor.get_context(
chan, chan,
@ -468,16 +467,20 @@ class Actor:
msg_buffer_size: int = 2**6 msg_buffer_size: int = 2**6
# nursery placeholders filled in by `async_main()` after fork # nursery placeholders filled in by `async_main()` after fork
_root_n: Optional[trio.Nursery] = None _root_n: trio.Nursery | None = None
_service_n: Optional[trio.Nursery] = None _service_n: trio.Nursery | None = None
_server_n: Optional[trio.Nursery] = None _server_n: trio.Nursery | None = None
# Information about `__main__` from parent # Information about `__main__` from parent
_parent_main_data: dict[str, str] _parent_main_data: dict[str, str]
_parent_chan_cs: Optional[trio.CancelScope] = None _parent_chan_cs: trio.CancelScope | None = None
# syncs for setup/teardown sequences # syncs for setup/teardown sequences
_server_down: Optional[trio.Event] = None _server_down: trio.Event | None = None
# user toggled crash handling (including monkey-patched in
# `trio.open_nursery()` via `.trionics._supervisor` B)
_debug_mode: bool = False
# if started on ``asycio`` running ``trio`` in guest mode # if started on ``asycio`` running ``trio`` in guest mode
_infected_aio: bool = False _infected_aio: bool = False
@ -493,8 +496,8 @@ class Actor:
enable_modules: list[str] = [], enable_modules: list[str] = [],
uid: str | None = None, uid: str | None = None,
loglevel: str | None = None, loglevel: str | None = None,
arbiter_addr: Optional[tuple[str, int]] = None, arbiter_addr: tuple[str, int] | None = None,
spawn_method: Optional[str] = None spawn_method: str | None = None
) -> None: ) -> None:
''' '''
This constructor is called in the parent actor **before** the spawning This constructor is called in the parent actor **before** the spawning
@ -554,9 +557,8 @@ class Actor:
] = {} ] = {}
self._listeners: list[trio.abc.Listener] = [] self._listeners: list[trio.abc.Listener] = []
self._parent_chan: Optional[Channel] = None self._parent_chan: Channel | None = None
self._forkserver_info: Optional[ self._forkserver_info: tuple | None = None
tuple[Any, Any, Any, Any, Any]] = None
self._actoruid2nursery: dict[ self._actoruid2nursery: dict[
tuple[str, str], tuple[str, str],
ActorNursery | None, ActorNursery | None,
@ -647,7 +649,7 @@ class Actor:
self._no_more_peers = trio.Event() # unset self._no_more_peers = trio.Event() # unset
chan = Channel.from_stream(stream) chan = Channel.from_stream(stream)
uid: Optional[tuple[str, str]] = chan.uid uid: tuple[str, str] | None = chan.uid
log.runtime(f"New connection to us {chan}") log.runtime(f"New connection to us {chan}")
# send/receive initial handshake response # send/receive initial handshake response
@ -695,7 +697,7 @@ class Actor:
# append new channel # append new channel
self._peers[uid].append(chan) self._peers[uid].append(chan)
local_nursery: Optional[ActorNursery] = None # noqa local_nursery: ActorNursery | None = None # noqa
disconnected: bool = False disconnected: bool = False
# Begin channel management - respond to remote requests and # Begin channel management - respond to remote requests and
@ -947,8 +949,8 @@ class Actor:
async def _from_parent( async def _from_parent(
self, self,
parent_addr: Optional[tuple[str, int]], parent_addr: tuple[str, int] | None,
) -> tuple[Channel, Optional[tuple[str, int]]]: ) -> tuple[Channel, tuple[str, int] | None]:
try: try:
# Connect back to the parent actor and conduct initial # Connect back to the parent actor and conduct initial
# handshake. From this point on if we error, we # handshake. From this point on if we error, we
@ -961,7 +963,7 @@ class Actor:
# Initial handshake: swap names. # Initial handshake: swap names.
await self._do_handshake(chan) await self._do_handshake(chan)
accept_addr: Optional[tuple[str, int]] = None accept_addr: tuple[str, int] | None = None
if self._spawn_method == "trio": if self._spawn_method == "trio":
# Receive runtime state from our parent # Receive runtime state from our parent
@ -1020,7 +1022,7 @@ class Actor:
self._server_down = trio.Event() self._server_down = trio.Event()
try: try:
async with trio.open_nursery() as server_n: async with trio.open_nursery() as server_n:
l: list[trio.abc.Listener] = await server_n.start( listeners: list[trio.abc.Listener] = await server_n.start(
partial( partial(
trio.serve_tcp, trio.serve_tcp,
self._stream_handler, self._stream_handler,
@ -1031,10 +1033,13 @@ class Actor:
host=accept_host, host=accept_host,
) )
) )
sockets: list[trio.socket] = [
getattr(listener, 'socket', 'unknown socket')
for listener in listeners
]
log.runtime( log.runtime(
"Started tcp server(s) on" f'Started tcp server(s) on {sockets}')
f" {[getattr(l, 'socket', 'unknown socket') for l in l]}") self._listeners.extend(listeners)
self._listeners.extend(l)
task_status.started(server_n) task_status.started(server_n)
finally: finally:
# signal the server is down since nursery above terminated # signal the server is down since nursery above terminated
@ -1215,7 +1220,7 @@ class Actor:
self._server_n.cancel_scope.cancel() self._server_n.cancel_scope.cancel()
@property @property
def accept_addr(self) -> Optional[tuple[str, int]]: def accept_addr(self) -> tuple[str, int] | None:
''' '''
Primary address to which the channel server is bound. Primary address to which the channel server is bound.
@ -1267,7 +1272,7 @@ class Actor:
async def async_main( async def async_main(
actor: Actor, actor: Actor,
accept_addr: Optional[tuple[str, int]] = None, accept_addr: tuple[str, int] | None = None,
# XXX: currently ``parent_addr`` is only needed for the # XXX: currently ``parent_addr`` is only needed for the
# ``multiprocessing`` backend (which pickles state sent to # ``multiprocessing`` backend (which pickles state sent to
@ -1276,7 +1281,7 @@ async def async_main(
# change this to a simple ``is_subactor: bool`` which will # change this to a simple ``is_subactor: bool`` which will
# be False when running as root actor and True when as # be False when running as root actor and True when as
# a subactor. # a subactor.
parent_addr: Optional[tuple[str, int]] = None, parent_addr: tuple[str, int] | None = None,
task_status: TaskStatus[None] = trio.TASK_STATUS_IGNORED, task_status: TaskStatus[None] = trio.TASK_STATUS_IGNORED,
) -> None: ) -> None: