From d2f6428e46fae1d6eff2d9f5db02d460e86d1f2c Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Thu, 18 Apr 2024 15:17:50 -0400 Subject: [PATCH] Annotate nursery and portal methods for `CallerInfo` scanning --- tractor/_portal.py | 27 +++++++++++++++++++++------ tractor/_supervise.py | 9 ++++++++- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/tractor/_portal.py b/tractor/_portal.py index e4db93a..052dd8e 100644 --- a/tractor/_portal.py +++ b/tractor/_portal.py @@ -161,17 +161,18 @@ class Portal: self._expect_result = await self.actor.start_remote_task( self.channel, nsf=NamespacePath(f'{ns}:{func}'), - kwargs=kwargs + kwargs=kwargs, + portal=self, ) async def _return_once( self, ctx: Context, - ) -> dict[str, Any]: + ) -> Return: assert ctx._remote_func_type == 'asyncfunc' # single response - msg: dict = await ctx._recv_chan.receive() + msg: Return = await ctx._recv_chan.receive() return msg async def result(self) -> Any: @@ -247,6 +248,8 @@ class Portal: purpose. ''' + __runtimeframe__: int = 1 # noqa + chan: Channel = self.channel if not chan.connected(): log.runtime( @@ -324,16 +327,18 @@ class Portal: internals! ''' + __runtimeframe__: int = 1 # noqa nsf = NamespacePath( f'{namespace_path}:{function_name}' ) - ctx = await self.actor.start_remote_task( + ctx: Context = await self.actor.start_remote_task( chan=self.channel, nsf=nsf, kwargs=kwargs, + portal=self, ) - ctx._portal = self - msg = await self._return_once(ctx) + ctx._portal: Portal = self + msg: Return = await self._return_once(ctx) return _unwrap_msg( msg, self.channel, @@ -384,6 +389,7 @@ class Portal: self.channel, nsf=nsf, kwargs=kwargs, + portal=self, ) ctx._portal = self return _unwrap_msg( @@ -398,6 +404,14 @@ class Portal: **kwargs, ) -> AsyncGenerator[MsgStream, None]: + ''' + Legacy one-way streaming API. + + TODO: re-impl on top `Portal.open_context()` + an async gen + around `Context.open_stream()`. + + ''' + __runtimeframe__: int = 1 # noqa if not inspect.isasyncgenfunction(async_gen_func): if not ( @@ -411,6 +425,7 @@ class Portal: self.channel, nsf=NamespacePath.from_ref(async_gen_func), kwargs=kwargs, + portal=self, ) ctx._portal = self diff --git a/tractor/_supervise.py b/tractor/_supervise.py index be81e4e..dc65cc6 100644 --- a/tractor/_supervise.py +++ b/tractor/_supervise.py @@ -131,7 +131,12 @@ class ActorNursery: "main task" besides the runtime. ''' - loglevel = loglevel or self._actor.loglevel or get_loglevel() + __runtimeframe__: int = 1 # noqa + loglevel: str = ( + loglevel + or self._actor.loglevel + or get_loglevel() + ) # configure and pass runtime state _rtv = _state._runtime_vars.copy() @@ -209,6 +214,7 @@ class ActorNursery: the actor is terminated. ''' + __runtimeframe__: int = 1 # noqa mod_path: str = fn.__module__ if name is None: @@ -257,6 +263,7 @@ class ActorNursery: directly without any far end graceful ``trio`` cancellation. ''' + __runtimeframe__: int = 1 # noqa self.cancelled = True # TODO: impl a repr for spawn more compact