Skip to content

refactor: convert if isinstance chains to match case#36846

Merged
asukaminato0721 merged 3 commits into
langgenius:mainfrom
duongynhi000005-oss:refactor/isinstance-to-match-case
May 31, 2026
Merged

refactor: convert if isinstance chains to match case#36846
asukaminato0721 merged 3 commits into
langgenius:mainfrom
duongynhi000005-oss:refactor/isinstance-to-match-case

Conversation

@duongynhi000005-oss
Copy link
Copy Markdown
Contributor

@duongynhi000005-oss duongynhi000005-oss commented May 30, 2026

Summary

Refactors if/elif isinstance type-dispatch chains into Python match/case statements.

Motivation

As described in #35902, using match case provides:

  1. Better detection of missing branches
  2. Clearer semantic meaning for type dispatch

Changes

Converted isinstance chains to match/case in 13 files:

  • api/core/mcp/server/streamable_http.py - MCP request type dispatch
  • api/core/app/apps/advanced_chat/generate_task_pipeline.py - User type dispatch
  • api/core/app/apps/advanced_chat/generate_response_converter.py - Stream response dispatch
  • api/core/app/apps/agent_chat/generate_response_converter.py - Stream response dispatch
  • api/core/app/apps/chat/generate_response_converter.py - Stream response dispatch
  • api/core/app/apps/completion/generate_response_converter.py - Stream response dispatch
  • api/core/app/apps/common/workflow_response_converter.py - User type dispatch
  • api/core/app/apps/base_app_runner.py - Message content type dispatch
  • api/core/app/task_pipeline/based_generate_task_pipeline.py - Error type dispatch
  • api/core/agent/cot_agent_runner.py - Prompt message type dispatch
  • api/core/agent/cot_completion_agent_runner.py - Prompt message type dispatch
  • api/core/logging/filters.py - User type dispatch
  • api/models/workflow.py - Variable type and pause reason dispatch

Testing

  • All files pass Python syntax validation
  • All files pass ruff linting

#35902

Refactors if/elif isinstance type-dispatch chains into Python match/case
statements for better semantics and to catch missing branches at a glance.

Files changed:
- api/core/mcp/server/streamable_http.py
- api/core/app/apps/advanced_chat/generate_task_pipeline.py
- api/core/app/apps/advanced_chat/generate_response_converter.py
- api/core/app/apps/agent_chat/generate_response_converter.py
- api/core/app/apps/chat/generate_response_converter.py
- api/core/app/apps/completion/generate_response_converter.py
- api/core/app/apps/common/workflow_response_converter.py
- api/core/app/apps/base_app_runner.py
- api/core/app/task_pipeline/based_generate_task_pipeline.py
- api/core/agent/cot_agent_runner.py
- api/core/agent/cot_completion_agent_runner.py
- api/core/logging/filters.py
- api/models/workflow.py

Closes langgenius#35902
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. refactor labels May 30, 2026
@asukaminato0721 asukaminato0721 requested a review from Copilot May 31, 2026 11:55
@github-actions
Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-05-31 11:57:06.263484216 +0000
+++ /tmp/pyrefly_pr.txt	2026-05-31 11:56:56.967465752 +0000
@@ -2109,9 +2109,9 @@
 ERROR Argument `Flask` is not assignable to parameter `app` with type `DifyApp` in function `extensions.ext_fastopenapi.init_app` [bad-argument-type]
   --> tests/unit_tests/controllers/console/test_fastopenapi_version.py:23:30
 ERROR Argument `SimpleNamespace` is not assignable to parameter `form` with type `Form` in function `controllers.console.human_input_form._jsonify_form_definition` [bad-argument-type]
-  --> tests/unit_tests/controllers/console/test_human_input_form.py:37:41
+  --> tests/unit_tests/controllers/console/test_human_input_form.py:35:41
 ERROR Argument `SimpleNamespace` is not assignable to parameter `form` with type `Form` in function `controllers.console.human_input_form.ConsoleHumanInputFormApi._ensure_console_access` [bad-argument-type]
-  --> tests/unit_tests/controllers/console/test_human_input_form.py:49:57
+  --> tests/unit_tests/controllers/console/test_human_input_form.py:47:57
 ERROR Object of class `Flask` has no attribute `login_manager` [missing-attribute]
   --> tests/unit_tests/controllers/console/test_workspace_account.py:29:5
 ERROR `SimpleNamespace | object` is not assignable to attribute `_current_tenant` with type `Tenant | None` [bad-assignment]
@@ -2120,8 +2120,6 @@
   --> tests/unit_tests/controllers/console/test_workspace_members.py:16:5
 ERROR `SimpleNamespace` is not assignable to attribute `_current_tenant` with type `Tenant | None` [bad-assignment]
   --> tests/unit_tests/controllers/console/test_workspace_members.py:73:43
-ERROR `in` is not supported between `Literal['count']` and `None` [not-iterable]
-   --> tests/unit_tests/controllers/console/test_wraps.py:187:16
 ERROR `SimpleNamespace` is not assignable to attribute `db` with type `SQLAlchemy` [bad-assignment]
   --> tests/unit_tests/controllers/files/test_image_preview.py:23:17
 ERROR `SimpleNamespace` is not assignable to attribute `request` with type `Request` [bad-assignment]
@@ -2348,6 +2346,28 @@
    --> tests/unit_tests/controllers/service_api/dataset/rag_pipeline/test_rag_pipeline_workflow.py:328:33
 ERROR Missing argument `response_mode` in function `services.rag_pipeline.entity.pipeline_service_api_entities.PipelineRunApiEntity.__init__` [missing-argument]
    --> tests/unit_tests/controllers/service_api/dataset/rag_pipeline/test_rag_pipeline_workflow.py:328:33
+ERROR Argument `list[dict[str, Any]] | None` is not assignable to parameter `obj` with type `Sized` in function `len` [bad-argument-type]
+  --> tests/unit_tests/controllers/service_api/dataset/test_dataset_segment.py:52:20
+ERROR `None` is not subscriptable [unsupported-operation]
+  --> tests/unit_tests/controllers/service_api/dataset/test_dataset_segment.py:75:16
+ERROR `None` is not subscriptable [unsupported-operation]
+  --> tests/unit_tests/controllers/service_api/dataset/test_dataset_segment.py:76:16
+ERROR Missing argument `content` in function `controllers.common.controller_schemas.ChildChunkCreatePayload.__init__` [missing-argument]
+   --> tests/unit_tests/controllers/service_api/dataset/test_dataset_segment.py:120:36
+ERROR Argument value `Literal[0]` violates Pydantic `ge` constraint `Literal[1]` for field `limit` [bad-argument-type]
+   --> tests/unit_tests/controllers/service_api/dataset/test_dataset_segment.py:160:33
+ERROR Argument value `Literal[0]` violates Pydantic `ge` constraint `Literal[1]` for field `page` [bad-argument-type]
+   --> tests/unit_tests/controllers/service_api/dataset/test_dataset_segment.py:165:33
+ERROR Argument `list[DocumentSegment] | None` is not assignable to parameter `obj` with type `Sized` in function `len` [bad-argument-type]
+   --> tests/unit_tests/controllers/service_api/dataset/test_dataset_segment.py:295:20
+ERROR Missing argument `tenant_id` in function `services.dataset_service.SegmentService.get_segments` [missing-argument]
+   --> tests/unit_tests/controllers/service_api/dataset/test_dataset_segment.py:304:54
+ERROR Argument `list[str] | None` is not assignable to parameter `obj` with type `Sized` in function `len` [bad-argument-type]
+   --> tests/unit_tests/controllers/service_api/dataset/test_dataset_segment.py:597:20
+ERROR Object of class `NoneType` has no attribute `name` [missing-attribute]
+   --> tests/unit_tests/controllers/service_api/dataset/test_document.py:238:16
+ERROR Object of class `NoneType` has no attribute `indexing_status` [missing-attribute]
+   --> tests/unit_tests/controllers/service_api/dataset/test_document.py:239:16
 ERROR Missing argument `app_model` in function `protected_view` [missing-argument]
    --> tests/unit_tests/controllers/service_api/test_wraps.py:160:36
 ERROR `object` is not assignable to attribute `request` with type `Request` [bad-assignment]

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 multiple Python type-dispatch blocks from if/elif isinstance(...) chains to match/case statements to make intent clearer and help surface missing branches during future edits.

Changes:

  • Replaced isinstance dispatch with match/case in MCP request handling, task pipeline error mapping, and several stream response converters.
  • Converted user/identity type branching (Account vs EndUser) to match/case in workflow response conversion and logging identity extraction.
  • Converted prompt-message type branching to match/case in CoT agent runners.

Reviewed changes

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

Show a summary per file
File Description
api/core/mcp/server/streamable_http.py Refactors MCP request type dispatch to match/case.
api/core/app/apps/advanced_chat/generate_task_pipeline.py Refactors user type dispatch (Account vs EndUser) in pipeline init to match/case.
api/core/app/apps/advanced_chat/generate_response_converter.py Refactors stream sub-response dispatch to match/case (including node start/finish handling).
api/core/app/apps/agent_chat/generate_response_converter.py Refactors stream sub-response dispatch to match/case.
api/core/app/apps/chat/generate_response_converter.py Refactors stream sub-response dispatch to match/case.
api/core/app/apps/completion/generate_response_converter.py Refactors stream sub-response dispatch to match/case (preserving metadata normalization).
api/core/app/apps/common/workflow_response_converter.py Refactors creator user type mapping to match/case in workflow finish responses.
api/core/app/apps/base_app_runner.py Refactors mixed content type handling in streaming invoke results to match/case.
api/core/app/task_pipeline/based_generate_task_pipeline.py Refactors error type mapping in handle_error to match/case.
api/core/agent/cot_agent_runner.py Refactors prompt message type dispatch to match/case while preserving scratchpad assembly logic.
api/core/agent/cot_completion_agent_runner.py Refactors historic prompt message type dispatch to match/case.
api/core/logging/filters.py Refactors identity extraction branching to match/case.
api/models/workflow.py Refactors variable type dispatch for environment variable decryption and pause reason conversion to match/case.

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

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 31, 2026

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,725 24,725 0
Untyped symbols 29,523 29,523 0
Modules 2764 2764 0

@asukaminato0721 asukaminato0721 enabled auto-merge May 31, 2026 14:17
@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label May 31, 2026
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.

Merged via the queue into langgenius:main with commit 8e5f090 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