forked from goodboy/tractor
1
0
Fork 0

Add `MsgTypeError.expected_msg_type`

Which matches with renaming `.payload_msg` -> `.expected_msg` which is
the value we attempt to construct from a vanilla-msgppack
decode-to-`dict` and then construct manually into a `MsgType` using
`.msg.types.from_dict_msg()`. Add a todo to use new `use_pretty` flag
which currently conflicts with `._exceptions.pformat_boxed_type()`
prefix formatting..
runtime_to_msgspec
Tyler Goodlet 2024-04-14 16:32:18 -04:00
parent 2d22713806
commit 2edfed75eb
1 changed files with 19 additions and 10 deletions

View File

@ -43,9 +43,12 @@ from tractor.msg import (
MsgType, MsgType,
Stop, Stop,
Yield, Yield,
pretty_struct,
types as msgtypes, types as msgtypes,
) )
from tractor.msg.pretty_struct import (
iter_fields,
Struct,
)
if TYPE_CHECKING: if TYPE_CHECKING:
from ._context import Context from ._context import Context
@ -82,7 +85,7 @@ class InternalError(RuntimeError):
_ipcmsg_keys: list[str] = [ _ipcmsg_keys: list[str] = [
fi.name fi.name
for fi, k, v for fi, k, v
in pretty_struct.iter_fields(Error) in iter_fields(Error)
] ]
@ -321,7 +324,7 @@ class RemoteActorError(Exception):
assert self.boxed_type is boxed_type assert self.boxed_type is boxed_type
@property @property
def ipc_msg(self) -> pretty_struct.Struct: def ipc_msg(self) -> Struct:
''' '''
Re-render the underlying `._ipc_msg: Msg` as Re-render the underlying `._ipc_msg: Msg` as
a `pretty_struct.Struct` for introspection such that the a `pretty_struct.Struct` for introspection such that the
@ -334,12 +337,12 @@ class RemoteActorError(Exception):
msg_type: MsgType = type(self._ipc_msg) msg_type: MsgType = type(self._ipc_msg)
fields: dict[str, Any] = { fields: dict[str, Any] = {
k: v for _, k, v in k: v for _, k, v in
pretty_struct.iter_fields(self._ipc_msg) iter_fields(self._ipc_msg)
} }
return defstruct( return defstruct(
msg_type.__name__, msg_type.__name__,
fields=fields.keys(), fields=fields.keys(),
bases=(msg_type, pretty_struct.Struct), bases=(msg_type, Struct),
)(**fields) )(**fields)
@property @property
@ -641,11 +644,11 @@ class MsgTypeError(
''' '''
reprol_fields: list[str] = [ reprol_fields: list[str] = [
'payload_msg', 'expected_msg_type',
] ]
extra_body_fields: list[str] = [ extra_body_fields: list[str] = [
'cid', 'cid',
'payload_msg', 'expected_msg',
] ]
@property @property
@ -661,9 +664,7 @@ class MsgTypeError(
return self.msgdata.get('_msg_dict') return self.msgdata.get('_msg_dict')
@property @property
def payload_msg( def expected_msg(self) -> MsgType|None:
self,
) -> MsgType|None:
''' '''
Attempt to construct what would have been the original Attempt to construct what would have been the original
`MsgType`-with-payload subtype (i.e. an instance from the set `MsgType`-with-payload subtype (i.e. an instance from the set
@ -674,9 +675,17 @@ class MsgTypeError(
if msg_dict := self.msg_dict.copy(): if msg_dict := self.msg_dict.copy():
return msgtypes.from_dict_msg( return msgtypes.from_dict_msg(
dict_msg=msg_dict, dict_msg=msg_dict,
# use_pretty=True,
# ^-TODO-^ would luv to use this BUT then the
# `field_prefix` in `pformat_boxed_tb()` cucks it
# all up.. XD
) )
return None return None
@property
def expected_msg_type(self) -> Type[MsgType]|None:
return type(self.expected_msg)
@property @property
def cid(self) -> str: def cid(self) -> str:
# pre-packed using `.from_decode()` constructor # pre-packed using `.from_decode()` constructor