Skip to content

refactor: convert isinstance chains to match/case (part 3)#36242

Merged
asukaminato0721 merged 2 commits into
langgenius:mainfrom
EvanYao826:refactor/isinstance-to-matchcase-v3
May 16, 2026
Merged

refactor: convert isinstance chains to match/case (part 3)#36242
asukaminato0721 merged 2 commits into
langgenius:mainfrom
EvanYao826:refactor/isinstance-to-matchcase-v3

Conversation

@EvanYao826
Copy link
Copy Markdown
Contributor

@EvanYao826 EvanYao826 commented May 16, 2026

Summary

#35902

Convert multi-branch isinstance/if-elif chains to structural pattern matching (match/case) for better readability and exhaustiveness checking.

Changes

8 files converted:

  • trace-mlflow/mlflow_trace.py — trace dispatch (7 branches) + _parse_prompts (3 branches)
  • trace-tencent/tencent_trace.py — trace dispatch (7 branches)
  • core/tools/mcp_tool/tool.py — MCP content type dispatch with nested match (4 branches)
  • advanced_chat/generate_task_pipeline.py — stream response dispatch (4 branches)
  • workflow/generate_task_pipeline.py — stream response dispatch (4 branches)
  • core/base/tts/app_generator_tts_publisher.py — message content type dispatch
  • workflow_app_runner.py — graph event dispatch (10 branches)
  • enterprise/enterprise_trace.py — trace dispatch (10 branches)

Benefits

  1. Exhaustiveness checkingcase _: pass makes it explicit when a type is unhandled
  2. Better readability — structural pattern matching is cleaner than long if/elif chains
  3. Missing branch detection — match/case makes it easier to spot missing type handling

Convert multi-branch isinstance/if-elif chains to structural pattern
matching (match/case) for better readability and exhaustiveness checking.

Files converted:
- api/providers/trace/trace-mlflow/mlflow_trace.py (trace dispatch + _parse_prompts)
- api/providers/trace/trace-tencent/tencent_trace.py (trace dispatch)
- api/core/tools/mcp_tool/tool.py (content type dispatch)
- api/core/app/apps/advanced_chat/generate_task_pipeline.py (stream response dispatch)
- api/core/app/apps/workflow/generate_task_pipeline.py (stream response dispatch)
- api/core/base/tts/app_generator_tts_publisher.py (message content dispatch)
- api/core/app/apps/workflow_app_runner.py (graph event dispatch)
- api/enterprise/telemetry/enterprise_trace.py (trace dispatch)

Fixes langgenius#35902
@EvanYao826 EvanYao826 requested a review from QuantumGhost as a code owner May 16, 2026 02:30
@dosubot dosubot Bot added size:XL This PR changes 500-999 lines, ignoring generated files. refactor labels May 16, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 0.00% 43.70% +43.70%
Strict coverage 0.00% 43.22% +43.22%
Typed symbols 0 22,131 +22,131
Untyped symbols 0 28,829 +28,829
Modules 0 2557 +2,557

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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@asukaminato0721
Copy link
Copy Markdown
Contributor

@dosu how is this pr

@dosubot
Copy link
Copy Markdown

dosubot Bot commented May 16, 2026

This PR looks good. Here's a quick assessment:

✅ No issues detected. The conversion is safe and well-executed:

  • Python version compatibility — Dify requires Python 3.12+ [1], so match/case (introduced in 3.10) is fully supported.
  • Behavioral equivalence — The case ClassName() pattern is functionally identical to isinstance(obj, ClassName) for simple type dispatch. The PR correctly preserves nested isinstance checks within case bodies where more complex conditional logic is needed .
  • Consistent conversion pattern — All 8 files follow the same mechanical 1:1 mapping (isinstancecase ClassName(), elsecase _:), which reduces the risk of subtle bugs .
  • Aligns with issue goals — This is part 3 of the systematic refactoring tracked in #35902, targeting files with long isinstance chains (7–11 branches) for maximum readability impact.

The refactoring improves readability and makes unhandled types explicit via case _: without changing any runtime behavior.

To reply, just mention @dosu.


Share context across your team and agents. Try Dosu.

Leave Feedback Learn about dify with Dosu

@asukaminato0721 asukaminato0721 added this pull request to the merge queue May 16, 2026
@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label May 16, 2026
Merged via the queue into langgenius:main with commit e7e6fe8 May 16, 2026
31 of 32 checks passed
zhangtaodemama added a commit to zhangtaodemama/langgenius-dify-bfaadcb0c706 that referenced this pull request May 16, 2026
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:XL This PR changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants