Skip to content

refactor: convert isinstance chains to match/case (part 8)#36869

Merged
asukaminato0721 merged 1 commit into
langgenius:mainfrom
EvanYao826:refactor/isinstance-to-match-case-batch8
May 31, 2026
Merged

refactor: convert isinstance chains to match/case (part 8)#36869
asukaminato0721 merged 1 commit into
langgenius:mainfrom
EvanYao826:refactor/isinstance-to-match-case-batch8

Conversation

@EvanYao826
Copy link
Copy Markdown
Contributor

Summary

Convert remaining isinstance/if-elif chains to structural pattern matching (match/case) in 5 files.

Refs #35902

Changes

File Pattern Cases
core/mcp/server/streamable_http.py MCP request type dispatch 4 (Initialize, ListTools, CallTool, Ping)
core/helper/code_executor/template_transformer.py Value type conversion 3 (str w/ guard, dict, list)
core/app/apps/advanced_chat/generate_response_converter.py Stream response dispatch 3 (MessageEnd, Error, NodeStart/Finish)
core/agent/cot_agent_runner.py History message processing 3 (Assistant, Tool, User)
core/app/apps/base_app_runner.py Content type handling 3 (str, Text, Image)

Motivation

  • Better semantic meaning and readability
  • Catches missing branches at match time
  • Consistent with existing codebase patterns (other files already use match/case)

Test Plan

  • Ruff lint: all checks passed
  • py_compile: all 5 files compile successfully

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)
@EvanYao826 EvanYao826 requested a review from QuantumGhost as a code owner May 31, 2026 13:00
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. refactor labels May 31, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 45.84% 45.84% 0.00%
Strict coverage 45.36% 45.36% 0.00%
Typed symbols 24,722 24,722 0
Untyped symbols 29,518 29,518 0
Modules 2763 2763 0

@asukaminato0721 asukaminato0721 enabled auto-merge May 31, 2026 14:10
@asukaminato0721 asukaminato0721 added this pull request to the merge queue May 31, 2026
@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label May 31, 2026
@asukaminato0721 asukaminato0721 requested a review from Copilot May 31, 2026 14:11
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 isinstance chains to match/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.

Merged via the queue into langgenius:main with commit c6474a2 May 31, 2026
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer refactor size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants