From 12b015cec4294da3d834add8bf28d245f0890839 Mon Sep 17 00:00:00 2001 From: goodboy Date: Tue, 7 Apr 2026 14:58:37 -0400 Subject: [PATCH] Tighten logging and annotations in `_web_bs` Split multi-value log msgs onto separate f-str lines, add `!r` to URL and error format refs, and fix `response_type` annotation from bare `type` to `Type[Struct]`. Also, - Use `X|Y` union style (no spaces). - Add `-> None` return hint to `proxy_msgs()`. - Single backticks in `fixture` docstring ref. - Expand `Callable` return type across lines. (this commit msg was generated in some part by [`claude-code`][claude-code-gh]) [claude-code-gh]: https://github.com/anthropics/claude-code --- piker/data/_web_bs.py | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/piker/data/_web_bs.py b/piker/data/_web_bs.py index fde04e71..dee510f8 100644 --- a/piker/data/_web_bs.py +++ b/piker/data/_web_bs.py @@ -168,7 +168,7 @@ async def _reconnect_forever( nobsws: NoBsWs, reset_after: int, # msg recv timeout before reset attempt - fixture: AsyncContextManager | None = None, + fixture: AsyncContextManager|None = None, task_status: TaskStatus = trio.TASK_STATUS_IGNORED, ) -> None: @@ -185,7 +185,7 @@ async def _reconnect_forever( async def proxy_msgs( ws: WebSocketConnection, rent_cs: trio.CancelScope, # parent cancel scope - ): + ) -> None: ''' Receive (under `timeout` deadline) all msgs from from underlying websocket and relay them to (calling) parent task via ``trio`` @@ -206,7 +206,7 @@ async def _reconnect_forever( except nobsws.recon_errors: log.exception( f'{src_mod}\n' - f'{url} connection bail with:' + f'{url!r} connection failed\n' ) with trio.CancelScope(shield=True): await trio.sleep(0.5) @@ -269,7 +269,7 @@ async def _reconnect_forever( nobsws._ws = ws log.info( f'{src_mod}\n' - f'Connection success: {url}' + f'Connection success: {url!r}' ) # begin relay loop to forward msgs @@ -341,7 +341,7 @@ async def _reconnect_forever( async def open_autorecon_ws( url: str, - fixture: AsyncContextManager | None = None, + fixture: AsyncContextManager|None = None, # time in sec between msgs received before # we presume connection might need a reset. @@ -361,7 +361,7 @@ async def open_autorecon_ws( and restarts the full http(s) handshake on catches of certain connetivity errors, or some user defined recv timeout. - You can provide a ``fixture`` async-context-manager which will be + You can provide a `fixture` async-context-manager which will be entered/exitted around each connection reset; eg. for (re)requesting subscriptions without requiring streaming setup code to rerun. @@ -402,7 +402,8 @@ async def open_autorecon_ws( except NoBsWs.recon_errors as con_err: log.warning( f'Entire ws-channel disconnect due to,\n' - f'con_err: {con_err!r}\n' + f'\n' + f'{con_err!r}\n' ) @@ -424,7 +425,7 @@ class JSONRPCResult(Struct): async def open_jsonrpc_session( url: str, start_id: int = 0, - response_type: type = JSONRPCResult, + response_type: Type[Struct] = JSONRPCResult, msg_recv_timeout: float = float('inf'), # ^NOTE, since only `deribit` is using this jsonrpc stuff atm # and options mkts are generally "slow moving".. @@ -435,7 +436,10 @@ async def open_jsonrpc_session( # broken and never restored with wtv init sequence is required to # re-establish a working req-resp session. -) -> Callable[[str, dict], dict]: +) -> Callable[ + [str, dict], + dict, +]: ''' Init a json-RPC-over-websocket connection to the provided `url`. @@ -531,14 +535,18 @@ async def open_jsonrpc_session( 'id': mid, } if not rpc_results.get(mid): log.warning( - f'Unexpected ws msg: {json.dumps(msg, indent=4)}' + f'Unexpected ws msg?\n' + f'{json.dumps(msg, indent=4)}' ) case { 'method': _, 'params': _, }: - log.debug(f'Recieved\n{msg}') + log.debug( + f'Recieved\n' + f'{msg!r}' + ) case { 'error': error @@ -554,12 +562,15 @@ async def open_jsonrpc_session( result['event'].set() log.error( f'JSONRPC request failed\n' - f'req: {req_msg}\n' - f'resp: {error}\n' + f'req: {req_msg!r}\n' + f'resp: {error!r}\n' ) case _: - log.warning(f'Unhandled JSON-RPC msg!?\n{msg}') + log.warning( + f'Unhandled JSON-RPC msg!?\n' + f'{msg!r}' + ) tn.start_soon(recv_task) yield json_rpc