refactor: convert isinstance chains to match/case (part 8)#36869
Merged
asukaminato0721 merged 1 commit intoMay 31, 2026
Merged
Conversation
Refs langgenius#35902 Convert remaining isinstance/if-elif chains to structural pattern matching: - core/mcp/server/streamable_http.py: request type dispatch (4-case) - core/helper/code_executor/template_transformer.py: value type conversion (3-case) - core/app/apps/advanced_chat/generate_response_converter.py: stream response dispatch (3-case) - core/agent/cot_agent_runner.py: history message processing (3-case) - core/app/apps/base_app_runner.py: content type handling (3-case)
Contributor
Pyrefly Type Coverage
|
asukaminato0721
approved these changes
May 31, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR continues the repo-wide refactor to replace isinstance / if-elif dispatch chains with Python structural pattern matching (match/case) across remaining hotspots, improving readability and making dispatch logic more explicit.
Changes:
- Refactored MCP request dispatch in the Streamable HTTP server from
isinstancechains tomatch/case. - Refactored several response/content type dispatch sections (template post-processing, streaming response conversion, agent history processing, multimodal content handling) to use
match/case. - Preserved existing default/fallback behavior in each location (including explicit
_fallbacks where previously present).
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| api/core/mcp/server/streamable_http.py | Switch MCP request type routing to match/case for clearer dispatch. |
| api/core/helper/code_executor/template_transformer.py | Use match/case for recursive conversion of scientific-notation strings. |
| api/core/app/apps/base_app_runner.py | Use match/case for streaming multimodal content aggregation/handling. |
| api/core/app/apps/advanced_chat/generate_response_converter.py | Use match/case for stream response chunk conversion routing. |
| api/core/agent/cot_agent_runner.py | Use match/case for history prompt message processing by message type. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Convert remaining
isinstance/if-elifchains to structural pattern matching (match/case) in 5 files.Refs #35902
Changes
core/mcp/server/streamable_http.pycore/helper/code_executor/template_transformer.pycore/app/apps/advanced_chat/generate_response_converter.pycore/agent/cot_agent_runner.pycore/app/apps/base_app_runner.pyMotivation
match/case)Test Plan
py_compile: all 5 files compile successfully