From 8f79c37b9989fda00787b1db1a986a762355b467 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Thu, 23 Mar 2023 16:29:31 -0400 Subject: [PATCH] Generalize `MktPair.from_msg()` handling Accept a msg with any of: - `.src: Asset` and `.dst: Asset` - `.src: str` and `.dst: str` - `.src: Asset` and `.dst: str` but not the final combo tho XD Also, fix `.key` to properly cast any `.src: Asset` to string! --- piker/accounting/_mktinfo.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/piker/accounting/_mktinfo.py b/piker/accounting/_mktinfo.py index 19657541..19f1b87e 100644 --- a/piker/accounting/_mktinfo.py +++ b/piker/accounting/_mktinfo.py @@ -236,16 +236,31 @@ class MktPair(Struct, frozen=True): ''' dst_asset_msg = msg.pop('dst') + src_asset_msg = msg.pop('src') + if isinstance(dst_asset_msg, str): + src: str = str(src_asset_msg) + assert isinstance(src, str) return cls.from_fqme( dst_asset_msg, + src=src, **msg, ) - # NOTE: we call `.copy()` here to ensure - # type casting! - dst = Asset(**dst_asset_msg).copy() - return cls(dst=dst, **msg).copy() + else: + # NOTE: we call `.copy()` here to ensure + # type casting! + dst = Asset(**dst_asset_msg).copy() + if not isinstance(src_asset_msg, str): + src = Asset(**src_asset_msg).copy() + else: + src = str(src_asset_msg) + + return cls( + dst=dst, + src=src, + **msg, + ).copy() @property def resolved(self) -> bool: @@ -292,7 +307,8 @@ class MktPair(Struct, frozen=True): ''' return maybe_cons_tokens( - [str(self.dst), self.src], + [str(self.dst), + str(self.src)], delim_char='', )