From 33cc769ca21aa6161c003d8930666b85e570d665 Mon Sep 17 00:00:00 2001 From: h Date: Thu, 27 Aug 2026 23:22:23 +0200 Subject: [PATCH] chore: make ruff format and ty clean Format two mcp modules with the current ruff, and narrow the text-block check in conversation_store through a typed helper so ty stops flagging dict.get on an unnarrowed block. Co-Authored-By: Claude Fable 5 --- src/beaver_gateway/core/conversation_store.py | 13 ++++--- src/beaver_gateway/mcp/lenient.py | 37 +++++++------------ src/beaver_gateway/mcp/types.py | 4 +- 3 files changed, 22 insertions(+), 32 deletions(-) diff --git a/src/beaver_gateway/core/conversation_store.py b/src/beaver_gateway/core/conversation_store.py index 309464b..2eaa6bb 100644 --- a/src/beaver_gateway/core/conversation_store.py +++ b/src/beaver_gateway/core/conversation_store.py @@ -539,6 +539,13 @@ def _splice_assistant_group( return _splice_by_rebuild(stored_group=stored_group, incoming=incoming) +def _is_text_block(blk: object) -> bool: + if not isinstance(blk, dict): + return False + block = cast("dict[str, Any]", blk) + return block.get("type") == "text" and bool(str(block.get("text", "")).strip()) + + def _splice_in_place( *, stored_group: _StoredDisplayTurn, incoming: ParsedTurn ) -> list[dict[str, Any]] | None: @@ -570,11 +577,7 @@ def _splice_in_place( if msg["role"] != "assistant" or not isinstance(content, list): continue positions.extend( - (mi, bi) - for bi, blk in enumerate(content) - if isinstance(blk, dict) - and blk.get("type") == "text" - and str(blk.get("text", "")).strip() + (mi, bi) for bi, blk in enumerate(content) if _is_text_block(blk) ) if len(positions) != len(new_texts): return None diff --git a/src/beaver_gateway/mcp/lenient.py b/src/beaver_gateway/mcp/lenient.py index e5b1f10..4249b59 100644 --- a/src/beaver_gateway/mcp/lenient.py +++ b/src/beaver_gateway/mcp/lenient.py @@ -84,23 +84,19 @@ class LenientStdioTransport(ClientTransport): self, **session_kwargs: Unpack[SessionKwargs] ) -> AsyncIterator[ClientSession]: errlog: TextIO = self.log_file if self.log_file is not None else sys.stderr - async with _lenient_stdio_client( - StdioServerParameters( - command=self.command, - args=self.args, - env=self.env, - cwd=self.cwd, - ), - errlog=errlog, - ) as (read_stream, write_stream), ClientSession( - read_stream, write_stream, **session_kwargs - ) as session: + async with ( + _lenient_stdio_client( + StdioServerParameters( + command=self.command, args=self.args, env=self.env, cwd=self.cwd + ), + errlog=errlog, + ) as (read_stream, write_stream), + ClientSession(read_stream, write_stream, **session_kwargs) as session, + ): yield session def __repr__(self) -> str: - return ( - f"" - ) + return f"" @contextlib.asynccontextmanager @@ -133,9 +129,7 @@ async def _lenient_stdio_client( # noqa: PLR0915 — mirrors mcp.client.stdio.s command=command, args=server.args, env=( - {**env_default, **server.env} - if server.env is not None - else env_default + {**env_default, **server.env} if server.env is not None else env_default ), errlog=errlog, cwd=server.cwd, @@ -164,9 +158,7 @@ async def _lenient_stdio_client( # noqa: PLR0915 — mirrors mcp.client.stdio.s if not stripped: continue try: - message = types.JSONRPCMessage.model_validate_json( - stripped - ) + message = types.JSONRPCMessage.model_validate_json(stripped) except Exception: # noqa: BLE001 — by design, see module doc _log.debug( "lenient stdio: dropped non-JSON line: %r", @@ -194,10 +186,7 @@ async def _lenient_stdio_client( # noqa: PLR0915 — mirrors mcp.client.stdio.s except anyio.ClosedResourceError: await anyio.lowlevel.checkpoint() - async with ( - anyio.create_task_group() as tg, - process, - ): + async with anyio.create_task_group() as tg, process: tg.start_soon(stdout_reader) tg.start_soon(stdin_writer) try: diff --git a/src/beaver_gateway/mcp/types.py b/src/beaver_gateway/mcp/types.py index bd7a7f8..8359b8d 100644 --- a/src/beaver_gateway/mcp/types.py +++ b/src/beaver_gateway/mcp/types.py @@ -62,9 +62,7 @@ class PythonToolMcp(_BaseMcp): tools: tuple[Callable[..., object], ...] -McpServerT = Annotated[ - StdioMcp | HttpMcp | PythonToolMcp, Field(discriminator="kind") -] +McpServerT = Annotated[StdioMcp | HttpMcp | PythonToolMcp, Field(discriminator="kind")] class McpServer: