From 94458807ce89b5ef411186d933df55d7b96b17fe Mon Sep 17 00:00:00 2001 From: goodboy Date: Mon, 23 Mar 2026 13:33:39 -0400 Subject: [PATCH] Expose `RuntimeVars` + `get_runtime_vars()` from pkg Export the new `RuntimeVars` struct and `get_runtime_vars()` from `tractor.__init__` and improve the accessor to optionally return the struct form. Deats, - add `RuntimeVars` and `get_runtime_vars` to `__init__.py` exports; alphabetize `_state` imports. - move `get_runtime_vars()` up in `_state.py` to sit right below `_runtime_vars` dict definition. - add `as_dict: bool = True` param so callers can get either the legacy `dict` or the new `RuntimeVars` struct. - drop the old stub fn at bottom of `_state.py`. - rm stale `from .msg.pretty_struct import Struct` comment. (this commit msg was generated in some part by [`claude-code`][claude-code-gh]) [claude-code-gh]: https://github.com/anthropics/claude-code --- tractor/__init__.py | 6 ++++-- tractor/_state.py | 28 ++++++++++++++++++---------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/tractor/__init__.py b/tractor/__init__.py index 6fac747f..374b7875 100644 --- a/tractor/__init__.py +++ b/tractor/__init__.py @@ -41,10 +41,12 @@ from ._supervise import ( ActorNursery as ActorNursery, ) from ._state import ( + RuntimeVars as RuntimeVars, current_actor as current_actor, - is_root_process as is_root_process, current_ipc_ctx as current_ipc_ctx, - debug_mode as debug_mode + debug_mode as debug_mode, + get_runtime_vars as get_runtime_vars, + is_root_process as is_root_process, ) from ._exceptions import ( ContextCancelled as ContextCancelled, diff --git a/tractor/_state.py b/tractor/_state.py index b3458cf7..d3b5851e 100644 --- a/tractor/_state.py +++ b/tractor/_state.py @@ -33,7 +33,6 @@ from typing import ( import platformdirs from trio.lowlevel import current_task -# from .msg.pretty_struct import Struct from msgspec import ( field, Struct, @@ -54,6 +53,7 @@ _def_tpt_proto: TransportProtocolKey = 'tcp' _current_actor: Actor|None = None # type: ignore # noqa _last_actor_terminated: Actor|None = None + # TODO: mk this a `msgspec.Struct`! # -[x] type out all fields obvi! # -[ ] (eventually) mk wire-ready for monitoring? @@ -140,6 +140,23 @@ _runtime_vars: dict[str, Any] = { } +def get_runtime_vars( + as_dict: bool = True, +) -> dict: + ''' + Deliver a **copy** of the current `Actor`'s "runtime variables". + + By default, for historical impl reasons, this delivers the `dict` + form, but the `RuntimeVars` struct should be utilized as possible + for future calls. + + ''' + if as_dict: + return dict(_runtime_vars) + + return RuntimeVars(**_runtime_vars) + + def last_actor() -> Actor|None: ''' Try to return last active `Actor` singleton @@ -301,12 +318,3 @@ def current_ipc_protos() -> list[str]: ''' return _runtime_vars['_enable_tpts'] - - -# !TODO, convert this to the new `RuntimeVars` struct! -def get_runtime_vars() -> dict: - ''' - Deliver a **copy** of the current `Actor`'s "runtime variables". - - ''' - return dict(_runtime_vars)