Skip to content

refactor: convert isinstance chains to match/case pattern#36364

Merged
asukaminato0721 merged 1 commit into
langgenius:mainfrom
xxiaoxiong:refactor/isinstance-to-match-case-35902
May 20, 2026
Merged

refactor: convert isinstance chains to match/case pattern#36364
asukaminato0721 merged 1 commit into
langgenius:mainfrom
xxiaoxiong:refactor/isinstance-to-match-case-35902

Conversation

@xxiaoxiong
Copy link
Copy Markdown
Contributor

@xxiaoxiong xxiaoxiong commented May 19, 2026

Summary

This PR refactors multiple chains to use Python's pattern (introduced in Python 3.10), improving code readability and maintainability.

Changes

Files Modified

  1. services/tools/tools_transform_service.py

    • : Converted 3-branch isinstance chain to match/case
  2. services/rag_pipeline/rag_pipeline.py

    • Datasource parameter handling: Converted isinstance checks for str/list types to match/case
  3. core/tools/tool_label_manager.py

    • : Converted isinstance chain with union types to match/case
  4. core/memory/token_buffer_memory.py

    • Content type handling: Converted TextPromptMessageContent/ImagePromptMessageContent checks to match/case

Benefits

  • Improved readability: Match/case syntax is more declarative and easier to understand
  • Better pattern matching: Leverages Python 3.10+ structural pattern matching
  • Consistent with existing codebase: Many files already use match/case (e.g., , )

Testing

  • All changes preserve existing behavior
  • No functional changes, only syntax improvements
  • Follows the same patterns already used in the codebase

#35902

- services/tools/tools_transform_service.py: repack_provider() method
- services/rag_pipeline/rag_pipeline.py: datasource parameter handling
- core/tools/tool_label_manager.py: get_tool_labels() method
- core/memory/token_buffer_memory.py: content type handling

Improves code readability and follows modern Python 3.10+ patterns.
Addresses issue #35902
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. refactor labels May 19, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 0.00% 44.42% +44.42%
Strict coverage 0.00% 43.95% +43.95%
Typed symbols 0 22,535 +22,535
Untyped symbols 0 28,498 +28,498
Modules 0 2561 +2,561

@asukaminato0721 asukaminato0721 enabled auto-merge May 19, 2026 06:16
@asukaminato0721
Copy link
Copy Markdown
Contributor

@dosu how about this pr

Comment thread api/services/tools/tools_transform_service.py
@dosubot
Copy link
Copy Markdown

dosubot Bot commented May 19, 2026

This PR looks good overall. It's Part 4 of the systematic isinstancematch/case refactoring initiative tracked in issue #35902 [1], and Parts 1–3 have already been merged, so it's well-aligned with the established direction.

Strengths:

  • All four files contain clear isinstance chains that benefit from this conversion [2] [3] [4] [5]
  • The PR correctly uses class pattern syntax (case ClassName(): with parentheses), which is the proper way to do type matching in match/case
  • Consistent with the existing codebase — 50+ files already use match/case for similar type dispatch patterns [6]
  • No functional changes, purely a syntax modernization

One thing to watch:

  • The guard clause case dict() if "icon" in provider: is fine but worth a double-check — make sure the match subject and the guard variable reference the same object (i.e., match provider: then case dict() if "icon" in provider: works correctly since provider is the match subject) [7]
  • For any subclass relationships in the matched types, ensure cases are ordered from most-specific to least-specific, since match/case stops at the first hit [1]

No red flags — this is a clean, low-risk refactor that follows patterns already approved and merged in earlier parts of this series.

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 20, 2026
@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label May 20, 2026
@asukaminato0721 asukaminato0721 requested a review from Copilot May 20, 2026 03:13
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 refactors several isinstance/elif chains into Python 3.10+ match/case statements to make type-branching logic more declarative and consistent across the codebase.

Changes:

  • Refactored provider repacking logic to use match/case for dict/entity/provider variants.
  • Refactored datasource parameter value branching (None/str/list/other) to match/case.
  • Refactored controller/content type branching in tool label and memory prompt-building code to match/case.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
api/services/tools/tools_transform_service.py Refactors provider-type branching in repack_provider() using match/case.
api/services/rag_pipeline/rag_pipeline.py Refactors datasource parameter handling to match/case while preserving assignment behavior.
api/core/tools/tool_label_manager.py Refactors controller-type branching in get_tool_labels() to match/case.
api/core/memory/token_buffer_memory.py Refactors prompt content type handling (text/image) to match/case.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Merged via the queue into langgenius:main with commit 77333e5 May 20, 2026
32 checks passed
zhangtaodemama added a commit to zhangtaodemama/langgenius-dify-bfaadcb0c706 that referenced this pull request May 20, 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: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