Repository navigation
fix(api): resolve new pyrefly unreachable-except and unwrap diagnostics - #44053
Open
mesrop2008 wants to merge 18 commits into
Open
mesrop2008 wants to merge 18 commits into
mesrop2008 wants to merge 18 commits into
Conversation
pyrefly 1.4 flags except clauses that can never match and fails to resolve the signature of methods stacked under setup_required. - Collapse except tuples whose members are already covered by ValueError (pydantic ValidationError, JSONDecodeError, UnicodeDecodeError, binascii.Error, DocumentIndexingError) or by AgentAppGeneratorError. - Annotate the CSRF _unauthorized helper as NoReturn and drop the trailing raise that existed only for the type checker. - Remove dead validation branches guarded by required Literal fields (KnowledgeConfig.indexing_technique, PreProcessingRule.id) and the model_construct-based test that targeted one of them. - Cast decorated controller methods at the test boundary before unwrapping them. Fixes langgenius#44038
mesrop2008
requested review from
JohnJyong,
QuantumGhost,
WH-2099 and
laipz8200
as code owners
October 14, 2026 06:59
Contributor
Pyrefly Diffbase → PR--- /tmp/pyrefly_base.txt 2026-11-03 02:37:13.909384149 +0000
+++ /tmp/pyrefly_pr.txt 2026-11-03 02:37:00.933304384 +0000
@@ -184,6 +184,8 @@
--> providers/vdb/vdb-tidb-on-qdrant/src/dify_vdb_tidb_on_qdrant/tidb_service.py:153:35
ERROR Argument `Any | None` is not assignable to parameter `cluster_response` with type `Mapping[str, Any]` in function `TidbService.extract_qdrant_endpoint` [bad-argument-type]
--> providers/vdb/vdb-tidb-on-qdrant/src/dify_vdb_tidb_on_qdrant/tidb_service.py:154:75
+ERROR Could not find name `binascii` [unknown-name]
+ --> libs/oauth.py:101:13
ERROR Argument `Literal['normal']` is not assignable to parameter `status` with type `ConversationStatus | SQLCoreOperations[ConversationStatus]` in function `models.model.Conversation.__init__` [bad-argument-type]
--> tests/e2e_tests/containers/controllers/console/app/test_chat_conversation_status_count_api.py:75:16
ERROR Argument `Literal['app-run']` is not assignable to parameter `triggered_from` with type `SQLCoreOperations[WorkflowRunTriggeredFrom] | WorkflowRunTriggeredFrom` in function `models.workflow.WorkflowRun.__init__` [bad-argument-type]
@@ -1981,7 +1983,7 @@
ERROR Object of class `NoneType` has no attribute `pre_processing_rules` [missing-attribute]
--> tests/helpers/unit/services/document_service_validation.py:1388:9
ERROR Object of class `NoneType` has no attribute `segmentation` [missing-attribute]
- --> tests/helpers/unit/services/document_service_validation.py:1464:9
+ --> tests/helpers/unit/services/document_service_validation.py:1437:9
ERROR Cannot index into `list[Unknown]` [bad-index]
--> tests/helpers/unit/services/hit_service.py:423:20
ERROR Cannot index into `object` [bad-index]
@@ -4540,8 +4542,20 @@
--> tests/unit_tests/core/test_provider_manager.py:569:37
ERROR Argument `list[LoadBalancingModelConfig]` is not assignable to parameter `load_balancing_model_configs` with type `list[_LoadBalancingModelConfigCacheEntry] | None` in function `core.provider_manager.ProviderManager._to_model_settings` [bad-argument-type]
--> tests/unit_tests/core/test_provider_manager.py:570:42
+ERROR Argument `Literal['llm']` is not assignable to parameter `model_type` with type `ModelType | SQLCoreOperations[ModelType]` in function `models.provider.ProviderModelSetting.__init__` [bad-argument-type]
+ --> tests/unit_tests/core/test_provider_manager.py:587:20
+ERROR Argument `Literal['llm']` is not assignable to parameter `model_type` with type `ModelType | SQLCoreOperations[ModelType]` in function `models.provider.LoadBalancingModelConfig.__init__` [bad-argument-type]
+ --> tests/unit_tests/core/test_provider_manager.py:597:24
+ERROR Argument `Literal['llm']` is not assignable to parameter `model_type` with type `ModelType | SQLCoreOperations[ModelType]` in function `models.provider.LoadBalancingModelConfig.__init__` [bad-argument-type]
+ --> tests/unit_tests/core/test_provider_manager.py:606:24
+ERROR Argument `Literal['llm']` is not assignable to parameter `model_type` with type `ModelType | SQLCoreOperations[ModelType]` in function `models.provider.LoadBalancingModelConfig.__init__` [bad-argument-type]
+ --> tests/unit_tests/core/test_provider_manager.py:615:24
+ERROR Argument `list[ProviderModelSetting]` is not assignable to parameter `provider_model_settings` with type `list[_ProviderModelSettingCacheEntry] | None` in function `core.provider_manager.ProviderManager._to_model_settings` [bad-argument-type]
+ --> tests/unit_tests/core/test_provider_manager.py:635:37
+ERROR Argument `list[LoadBalancingModelConfig]` is not assignable to parameter `load_balancing_model_configs` with type `list[_LoadBalancingModelConfigCacheEntry] | None` in function `core.provider_manager.ProviderManager._to_model_settings` [bad-argument-type]
+ --> tests/unit_tests/core/test_provider_manager.py:636:42
WARN Identity comparison `False is True` is always False [unnecessary-comparison]
- --> tests/unit_tests/core/test_provider_manager.py:1309:30
+ --> tests/unit_tests/core/test_provider_manager.py:1387:30
ERROR Class member `DummyTool.get_runtime_parameters` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
--> tests/unit_tests/core/tools/__base/test_tool.py:71:9
ERROR Object of type `ToolInvokeMessage.BlobChunkMessage | ToolInvokeMessage.BlobMessage | ToolInvokeMessage.FileMessage | ToolInvokeMessage.JsonMessage | ToolInvokeMessage.LogMessage | ToolInvokeMessage.RetrieverResourceMessage | ToolInvokeMessage.TextMessage | ToolInvokeMessage.VariableMessage | None` has no attribute `text` [missing-attribute]
|
Contributor
Pyrefly Type Coverage
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #44053 +/- ##
==========================================
+ Coverage 73.31% 82.75% +9.44%
==========================================
Files 6239 6193 -46
Lines 390948 383074 -7874
Branches 79291 78820 -471
==========================================
+ Hits 286615 317006 +30391
+ Misses 98235 60301 -37934
+ Partials 6098 5767 -331
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Add unit tests for the except ValueError paths simplified in the previous commit so the patch is fully covered: - ProviderManager skips load-balancing configs and returns empty provider credentials when the stored JSON is malformed. - parse_file_reference falls back to the raw value for invalid base64 or JSON payloads, and round-trips canonical references. - The datasource credential repository rejects stored documents whose data_source_info cannot be parsed. - Document change_status maps ValueError and DocumentIndexingError to DocumentInvalidActionError.
…hub.com/mesrop2008/dify into fix/pyrefly-redundant-except-and-unwrap
…nt-except-and-unwrap # Conflicts: # api/controllers/console/app/workflow.py # api/controllers/console/datasets/rag_pipeline/rag_pipeline_workflow.py
…nt-except-and-unwrap # Conflicts: # api/controllers/console/app/workflow.py # api/controllers/console/datasets/rag_pipeline/rag_pipeline_workflow.py # api/controllers/console/explore/trial.py # api/tests/unit_tests/controllers/files/test_plugin_file_upload.py # api/tests/unit_tests/core/test_provider_manager.py
…nt-except-and-unwrap
Closed
4 tasks done
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
Fixes #44038
The upcoming pyrefly 1.4 releases (reproduced with
pyrefly==1.4.0.dev3) report 17 warnings inapi/and 10 errors inapi/tests/unit_tests. This PR fixes all of them. Runtime behavior is unchanged: each simplifiedexceptstill catches the same exceptions, and the removed branches could never run.Unreachable
exceptclauses (unreachable-except-clause)ValueErroralready covers pydanticValidationError,json.JSONDecodeError,UnicodeDecodeError,binascii.ErrorandDocumentIndexingError(BaseServiceErrorsubclassesValueError). These were removed from the exception tuples, along with imports that are no longer used:controllers/console/app/workflow.pycontrollers/console/datasets/rag_pipeline/rag_pipeline_workflow.pycore/plugin/plugin_service.pycore/provider_manager.py(2 sites)core/workflow/file_reference.pylibs/oauth.pyrepositories/data_source/credential_repository.pyrepositories/knowledge/document_repository.pyservices/knowledge/documents/adapters.pyservices/tools/tools_transform_service.pyIn
controllers/console/explore/trial.py,AgentAppNotPublishedErrorsubclassesAgentAppGeneratorError, and both already mapped toAppUnavailableError, so the clause is now justexcept AgentAppGeneratorError.Unreachable code (
unreachable)libs/token.py:_unauthorized()always raises, so it is now annotated-> NoReturn. Theraiseafter it, which existed only for the type checker, is removed.services/knowledge/documents/adapters.py: removed theindexing_technique is requiredcheck.KnowledgeConfig.indexing_techniqueis a requiredLiteral["high_quality", "economy"], andconfigalways comes fromKnowledgeConfig.model_validate, so the condition could never be true.services/knowledge/dataset_service.py: removed thepre_processing_rules id is requiredcheck, becausePreProcessingRule.idis a required, non-emptyLiteral. Also removed the matching test intests/unit_tests/services/document_service_validation.py. That test skipped validation withmodel_construct(id=None)to reach the check, and pytest doesn't collect the file anyway because its name doesn't matchtest_*.py.unwrapargument errors (bad-argument-type)When
setup_requiredis stacked on top of another ParamSpec decorator, pyrefly 1.4 cannot resolve the method's signature and infers(ParamSpec(Unknown)) -> Unknown. As suggested in the issue thread, the tests now cast the decorated method before unwrapping it. Production decorator types are unchanged:tests/unit_tests/controllers/console/workspace/test_skills.py: cast to the concrete return type each test uses (Response, ortuple[dict[str, str], int]).tests/unit_tests/controllers/files/test_plugin_file_upload.py: the seven identical unwrap calls now go through a single_post_upload()helper.tests/unit_tests/controllers/inner_api/app/test_file_grants.py: cast toCallable[..., object]beforeinspect.unwrap.Testing
api/project andtests/unit_tests/pyrefly.toml.uv.lock, used by CI): still no diagnostics for theapi/,tests/unit_testsandtests/test_containers_integration_testsconfigs.ruff format --checkandruff checkpass on all changed files, and the pre-commit hooks pass.check_no_new_test_doubles,check_no_new_getattr,check_no_new_controller_sqlalchemy,check_no_new_session_mockandcheck_no_spec_mockpass against currentmain.console/app/test_workflow.py,console/datasets/rag_pipeline,console/explore,console/workspace/test_skills.py,files/test_plugin_file_upload.py,inner_api/app/test_file_grants.py), pluslibs/test_oauth_*.py,libs/test_token.py,core/test_provider_manager.py,repositories/data_source,repositories/knowledge,services/knowledgeandservices/tools/test_tools_transform_service.py.except ValueErrorbranches (97 passed across these files):core/test_provider_manager.py: a load-balancing config with malformed stored JSON is skipped while valid configs are kept and cached, and malformed provider credentials return{}.core/workflow/test_file_reference.py(new): canonical references round-trip, and invalid base64 or JSON payloads fall back to using the raw value as therecord_id.repositories/data_source/test_credential_repository.py: a stored document with unparsabledata_source_infodoes not resolve a credential.services/knowledge/test_dataset_adapters.py:change_statusmaps bothValueErrorandDocumentIndexingErrortoDocumentInvalidActionError.Note:
pyrefly 1.4.0.dev4, released after the issue was filed, adds 27method-assignerrors in unit tests and 1redundant-castwarning inservices/workflow_draft_variable_service.py. They are not part of #44038 and are left for a follow-up.Screenshots
Checklist
make lint && make type-check(backend) andvp staged(frontend) to appease the lint gods