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.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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(
|
||||
async with (
|
||||
_lenient_stdio_client(
|
||||
StdioServerParameters(
|
||||
command=self.command,
|
||||
args=self.args,
|
||||
env=self.env,
|
||||
cwd=self.cwd,
|
||||
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:
|
||||
) as (read_stream, write_stream),
|
||||
ClientSession(read_stream, write_stream, **session_kwargs) as session,
|
||||
):
|
||||
yield session
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return (
|
||||
f"<LenientStdioTransport(command={self.command!r}, args={self.args!r})>"
|
||||
)
|
||||
return f"<LenientStdioTransport(command={self.command!r}, args={self.args!r})>"
|
||||
|
||||
|
||||
@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:
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user