refactor: convert if isinstance chains to match case#36846
Merged
asukaminato0721 merged 3 commits intoMay 31, 2026
Merged
Conversation
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
Contributor
Pyrefly Diffbase → 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]
|
Contributor
There was a problem hiding this comment.
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
isinstancedispatch withmatch/casein MCP request handling, task pipeline error mapping, and several stream response converters. - Converted user/identity type branching (Account vs EndUser) to
match/casein workflow response conversion and logging identity extraction. - Converted prompt-message type branching to
match/casein 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.
Contributor
Pyrefly Type Coverage
|
asukaminato0721
previously approved these changes
May 31, 2026
asukaminato0721
approved these changes
May 31, 2026
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
Refactors
if/elif isinstancetype-dispatch chains into Pythonmatch/casestatements.Motivation
As described in #35902, using
match caseprovides:Changes
Converted isinstance chains to match/case in 13 files:
api/core/mcp/server/streamable_http.py- MCP request type dispatchapi/core/app/apps/advanced_chat/generate_task_pipeline.py- User type dispatchapi/core/app/apps/advanced_chat/generate_response_converter.py- Stream response dispatchapi/core/app/apps/agent_chat/generate_response_converter.py- Stream response dispatchapi/core/app/apps/chat/generate_response_converter.py- Stream response dispatchapi/core/app/apps/completion/generate_response_converter.py- Stream response dispatchapi/core/app/apps/common/workflow_response_converter.py- User type dispatchapi/core/app/apps/base_app_runner.py- Message content type dispatchapi/core/app/task_pipeline/based_generate_task_pipeline.py- Error type dispatchapi/core/agent/cot_agent_runner.py- Prompt message type dispatchapi/core/agent/cot_completion_agent_runner.py- Prompt message type dispatchapi/core/logging/filters.py- User type dispatchapi/models/workflow.py- Variable type and pause reason dispatchTesting
#35902