Skip to content

Commit a28a650

Browse files
committed
Add explanatory comment for type narrowing guard in _handle_response
1 parent a9548e8 commit a28a650

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/mcp/shared/session.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,12 @@ async def _handle_response(self, message: SessionMessage) -> None:
462462
"""
463463
root = message.message.root
464464

465-
# Type guard: this method is only called for responses/errors
466-
if not isinstance(root, JSONRPCResponse | JSONRPCError): # pragma: no cover
467-
return
465+
# This check is always true at runtime: the caller (_receive_loop) only invokes
466+
# this method in the else branch after checking for JSONRPCRequest and
467+
# JSONRPCNotification. However, the type checker can't infer this from the
468+
# method signature, so we need this guard for type narrowing.
469+
if not isinstance(root, JSONRPCResponse | JSONRPCError):
470+
return # pragma: no cover
468471

469472
response_id: RequestId = root.id
470473

0 commit comments

Comments
 (0)