Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
refactor(api): enable strict typing for schema resolver tests (#44050)
Eddy ZHANG341660f9b060
diff --git a/api/tests/unit_tests/core/schemas/test_registry.py b/api/tests/unit_tests/core/schemas/test_registry.pyindex 8a3aa5030e2..ac1a6f465d8 100644--- a/api/tests/unit_tests/core/schemas/test_registry.py+++ b/api/tests/unit_tests/core/schemas/test_registry.py@@ -8,7 +8,7 @@ from core.schemas.registry import SchemaRegistryclass TestSchemaRegistry:- def test_initialization(self, tmp_path: Path):+ def test_initialization(self, tmp_path: Path) -> None:base_dir = tmp_path / "schemas"base_dir.mkdir()registry = SchemaRegistry(str(base_dir))@@ -16,19 +16,19 @@ class TestSchemaRegistry:assert registry.versions == {}assert registry.metadata == {}- def test_default_registry_singleton(self):+ def test_default_registry_singleton(self) -> None:registry1 = SchemaRegistry.default_registry()registry2 = SchemaRegistry.default_registry()assert registry1 is registry2assert isinstance(registry1, SchemaRegistry)- def test_load_all_versions_non_existent_dir(self, tmp_path: Path):+ def test_load_all_versions_non_existent_dir(self, tmp_path: Path) -> None:base_dir = tmp_path / "non_existent"registry = SchemaRegistry(str(base_dir))registry.load_all_versions()assert registry.versions == {}- def test_load_all_versions_filtering(self, tmp_path: Path):+ def test_load_all_versions_filtering(self, tmp_path: Path) -> None:base_dir = tmp_path / "schemas"base_dir.mkdir()(base_dir / "not_a_version_dir").mkdir()@@ -41,7 +41,7 @@ class TestSchemaRegistry:mock_load.assert_called_once()assert mock_load.call_args[0][0] == "v1"- def test_load_version_dir_filtering(self, tmp_path: Path):+ def test_load_version_dir_filtering(self, tmp_path: Path) -> None:version_dir = tmp_path / "v1"version_dir.mkdir()(version_dir / "schema1.json").write_text("{}")@@ -53,19 +53,19 @@ class TestSchemaRegistry:mock_load.assert_called_once()assert mock_load.call_args[0][1] == "schema1"- def test_load_version_dir_non_existent(self, tmp_path: Path):+ def test_load_version_dir_non_existent(self, tmp_path: Path) -> None:version_dir = tmp_path / "non_existent"registry = SchemaRegistry(str(tmp_path))registry._load_version_dir("v1", version_dir)assert "v1" not in registry.versions- def test_load_schema_success(self, tmp_path: Path):+ def test_load_schema_success(self, tmp_path: Path) -> None:schema_path = tmp_path / "test.json"schema_content = {"title": "Test Schema", "description": "A test schema"}schema_path.write_text(json.dumps(schema_content))registry = SchemaRegistry(str(tmp_path))- registry.versions["v1"] = {}+ registry.versions["v1"] = dict[str, object]()registry._load_schema("v1", "test", schema_path)assert registry.versions["v1"]["test"] == schema_content@@ -73,29 +73,29 @@ class TestSchemaRegistry:assert registry.metadata[uri]["title"] == "Test Schema"assert registry.metadata[uri]["version"] == "v1"- def test_load_schema_invalid_json(self, tmp_path: Path, caplog: pytest.LogCaptureFixture):+ def test_load_schema_invalid_json(self, tmp_path: Path, caplog: pytest.LogCaptureFixture) -> None:schema_path = tmp_path / "invalid.json"schema_path.write_text("invalid json")registry = SchemaRegistry(str(tmp_path))- registry.versions["v1"] = {}+ registry.versions["v1"] = dict[str, object]()registry._load_schema("v1", "invalid", schema_path)assert "Failed to load schema v1/invalid" in caplog.text- def test_load_schema_os_error(self, tmp_path: Path, caplog: pytest.LogCaptureFixture):+ def test_load_schema_os_error(self, tmp_path: Path, caplog: pytest.LogCaptureFixture) -> None:schema_path = tmp_path / "error.json"schema_path.write_text("{}")registry = SchemaRegistry(str(tmp_path))- registry.versions["v1"] = {}+ registry.versions["v1"] = dict[str, object]()with patch("builtins.open", side_effect=OSError("Read error")):registry._load_schema("v1", "error", schema_path)assert "Failed to load schema v1/error" in caplog.text- def test_get_schema(self):+ def test_get_schema(self) -> None:registry = SchemaRegistry("/tmp")registry.versions = {"v1": {"test": {"type": "object"}}}@@ -108,19 +108,19 @@ class TestSchemaRegistry:# Missing versionassert registry.get_schema("https://dify.ai/schemas/v2/test.json") is None- def test_list_versions(self):+ def test_list_versions(self) -> None:registry = SchemaRegistry("/tmp")registry.versions = {"v2": {}, "v1": {}}assert registry.list_versions() == ["v1", "v2"]- def test_list_schemas(self):+ def test_list_schemas(self) -> None:registry = SchemaRegistry("/tmp")registry.versions = {"v1": {"b": {}, "a": {}}}assert registry.list_schemas("v1") == ["a", "b"]assert registry.list_schemas("v2") == []- def test_get_all_schemas_for_version(self):+ def test_get_all_schemas_for_version(self) -> None:registry = SchemaRegistry("/tmp")registry.versions = {"v1": {"test": {"title": "Test Label"}}}@@ -131,7 +131,7 @@ class TestSchemaRegistry:assert results[0]["schema"] == {"title": "Test Label"}# Default label if title missing- registry.versions["v1"]["no_title"] = {}+ registry.versions["v1"]["no_title"] = dict[str, object]()results = registry.get_all_schemas_for_version("v1")item = next(r for r in results if r["name"] == "no_title")assert item["label"] == "no_title"diff --git a/api/tests/unit_tests/core/schemas/test_resolver.py b/api/tests/unit_tests/core/schemas/test_resolver.pyindex d53b01364eb..a08ad424273 100644--- a/api/tests/unit_tests/core/schemas/test_resolver.py+++ b/api/tests/unit_tests/core/schemas/test_resolver.py@@ -19,24 +19,33 @@ from core.schemas.resolver import ()+def _nested_schema(depth: int) -> dict[str, object]:+ """Return {"type": "object", "properties": {"level_0": {... "level_<depth-1>": {...}}}} without refs."""+ schema: dict[str, object] = {"type": "object", "properties": {}}+ for i in reversed(range(depth)):+ schema = {"type": "object", "properties": {f"level_{i}": schema}}+ return schema++class TestSchemaResolver:"""Test cases for schema reference resolution"""- def setup_method(self):+ def setup_method(self) -> None:"""Setup method to initialize test resources"""self.registry = SchemaRegistry.default_registry()# Clear cache before each testSchemaResolver.clear_cache()- def teardown_method(self):+ def teardown_method(self) -> None:"""Cleanup after each test"""SchemaResolver.clear_cache()- def test_simple_ref_resolution(self):+ def test_simple_ref_resolution(self) -> None:"""Test resolving a simple $ref to a complete schema"""schema_with_ref = {"$ref": "https://dify.ai/schemas/v1/qa_structure.json"}resolved = resolve_dify_schema_refs(schema_with_ref)+ assert isinstance(resolved, dict)# Should be resolved to the actual qa_structure schemaassert resolved["type"] == "object"@@ -49,7 +58,7 @@ class TestSchemaResolver:assert "$schema" not in resolvedassert "version" not in resolved- def test_nested_object_with_refs(self):+ def test_nested_object_with_refs(self) -> None:"""Test resolving $refs within nested object structures"""nested_schema = {"type": "object",@@ -60,6 +69,7 @@ class TestSchemaResolver:}resolved = resolve_dify_schema_refs(nested_schema)+ assert isinstance(resolved, dict)# Original structure should be preservedassert resolved["type"] == "object"@@ -77,7 +87,7 @@ class TestSchemaResolver:assert "$schema" not in file_schemaassert "version" not in file_schema- def test_array_items_ref_resolution(self):+ def test_array_items_ref_resolution(self) -> None:"""Test resolving $refs in array items"""array_schema = {"type": "array",@@ -86,6 +96,7 @@ class TestSchemaResolver:}resolved = resolve_dify_schema_refs(array_schema)+ assert isinstance(resolved, dict)# Array structure should be preservedassert resolved["type"] == "array"@@ -96,7 +107,7 @@ class TestSchemaResolver:assert items_schema["type"] == "array"assert items_schema["title"] == "General Structure"- def test_non_dify_ref_unchanged(self):+ def test_non_dify_ref_unchanged(self) -> None:"""Test that non-Dify $refs are left unchanged"""external_ref_schema = {"type": "object",@@ -107,6 +118,7 @@ class TestSchemaResolver:}resolved = resolve_dify_schema_refs(external_ref_schema)+ assert isinstance(resolved, dict)# External $ref should remain unchangedassert resolved["properties"]["external_data"]["$ref"] == "https://example.com/external-schema.json"@@ -115,7 +127,7 @@ class TestSchemaResolver:assert resolved["properties"]["dify_data"]["type"] == "object"assert resolved["properties"]["dify_data"]["title"] == "File"- def test_no_refs_schema_unchanged(self):+ def test_no_refs_schema_unchanged(self) -> None:"""Test that schemas without $refs are returned unchanged"""simple_schema = {"type": "object",@@ -127,6 +139,7 @@ class TestSchemaResolver:}resolved = resolve_dify_schema_refs(simple_schema)+ assert isinstance(resolved, dict)# Should be identical to inputassert resolved == simple_schema@@ -135,10 +148,10 @@ class TestSchemaResolver:assert resolved["properties"]["items"]["items"]["type"] == "number"assert resolved["required"] == ["name"]- def test_recursion_depth_protection(self):+ def test_recursion_depth_protection(self) -> None:"""Test that excessive recursion depth is prevented"""# Create a moderately nested structure- deep_schema = {"$ref": "https://dify.ai/schemas/v1/qa_structure.json"}+ deep_schema: dict[str, object] = {"$ref": "https://dify.ai/schemas/v1/qa_structure.json"}# Wrap it in fewer layers to make the test more reasonablefor _ in range(2):@@ -146,6 +159,7 @@ class TestSchemaResolver:# Should handle normal cases fine with reasonable depthresolved = resolve_dify_schema_refs(deep_schema, max_depth=25)+ assert isinstance(resolved, dict)assert resolved is not Noneassert resolved["type"] == "object"@@ -154,7 +168,7 @@ class TestSchemaResolver:resolve_dify_schema_refs(deep_schema, max_depth=5)assert exc_info.value.max_depth == 5- def test_circular_reference_detection(self):+ def test_circular_reference_detection(self) -> None:"""Test that circular references are detected and handled"""# Mock registry with circular referencemock_registry = MagicMock()@@ -165,11 +179,12 @@ class TestSchemaResolver:schema = {"$ref": "https://dify.ai/schemas/v1/circular.json"}resolved = resolve_dify_schema_refs(schema, registry=mock_registry)+ assert isinstance(resolved, dict)# Should mark circular referenceassert "$circular_ref" in resolved- def test_schema_not_found_handling(self):+ def test_schema_not_found_handling(self) -> None:"""Test handling of missing schemas"""# Mock registry that returns None for unknown schemasmock_registry = MagicMock()@@ -177,11 +192,12 @@ class TestSchemaResolver:schema = {"$ref": "https://dify.ai/schemas/v1/unknown.json"}resolved = resolve_dify_schema_refs(schema, registry=mock_registry)+ assert isinstance(resolved, dict)# Should keep the original $ref when schema not foundassert resolved["$ref"] == "https://dify.ai/schemas/v1/unknown.json"- def test_primitive_types_unchanged(self):+ def test_primitive_types_unchanged(self) -> None:"""Test that primitive types are returned unchanged"""assert resolve_dify_schema_refs("string") == "string"assert resolve_dify_schema_refs(123) == 123@@ -189,7 +205,7 @@ class TestSchemaResolver:assert resolve_dify_schema_refs(None) is Noneassert resolve_dify_schema_refs(3.14) == 3.14- def test_cache_functionality(self):+ def test_cache_functionality(self) -> None:"""Test that caching works correctly"""schema = {"$ref": "https://dify.ai/schemas/v1/file.json"}@@ -215,16 +231,16 @@ class TestSchemaResolver:resolved3 = resolve_dify_schema_refs(schema)assert resolved3 == resolved1- def test_thread_safety(self):+ def test_thread_safety(self) -> None:"""Test that the resolver is thread-safe"""schema = {"type": "object","properties": {f"prop_{i}": {"$ref": "https://dify.ai/schemas/v1/file.json"} for i in range(10)},}- results = []+ results: list[object] = []- def resolve_in_thread():+ def resolve_in_thread() -> bool:try:result = resolve_dify_schema_refs(schema)results.append(result)@@ -243,7 +259,7 @@ class TestSchemaResolver:first_result = results[0]assert all(r == first_result for r in results if not isinstance(r, Exception))- def test_mixed_nested_structures(self):+ def test_mixed_nested_structures(self) -> None:"""Test resolving refs in complex mixed structures"""complex_schema = {"type": "object",@@ -268,6 +284,7 @@ class TestSchemaResolver:}resolved = resolve_dify_schema_refs(complex_schema, max_depth=20)+ assert isinstance(resolved, dict)# Check structure is preservedassert resolved["type"] == "object"@@ -284,7 +301,7 @@ class TestSchemaResolver:class TestUtilityFunctions:"""Test utility functions"""- def test_is_dify_schema_ref(self):+ def test_is_dify_schema_ref(self) -> None:"""Test _is_dify_schema_ref function"""# Valid Dify refsassert _is_dify_schema_ref("https://dify.ai/schemas/v1/file.json")@@ -300,7 +317,7 @@ class TestUtilityFunctions:assert not _is_dify_schema_ref(123)assert not _is_dify_schema_ref(["list"])- def test_has_dify_refs(self):+ def test_has_dify_refs(self) -> None:"""Test _has_dify_refs function"""# Schemas with Dify refsassert _has_dify_refs({"$ref": "https://dify.ai/schemas/v1/file.json"})@@ -339,7 +356,7 @@ class TestUtilityFunctions:assert not _has_dify_refs(True)assert not _has_dify_refs(None)- def test_has_dify_refs_hybrid_vs_recursive(self):+ def test_has_dify_refs_hybrid_vs_recursive(self) -> None:"""Test that hybrid and recursive detection give same results"""test_schemas = [# No refs@@ -373,7 +390,7 @@ class TestUtilityFunctions:123,True,None,- [],+ list[object](),]for schema in test_schemas:@@ -382,7 +399,7 @@ class TestUtilityFunctions:assert hybrid_result == recursive_result, f"Mismatch for schema: {schema}"- def test_parse_dify_schema_uri(self):+ def test_parse_dify_schema_uri(self) -> None:"""Test parse_dify_schema_uri function"""# Valid URIsassert parse_dify_schema_uri("https://dify.ai/schemas/v1/file.json") == ("v1", "file")@@ -394,7 +411,7 @@ class TestUtilityFunctions:assert parse_dify_schema_uri("invalid") == ("", "")assert parse_dify_schema_uri("") == ("", "")- def test_remove_metadata_fields(self):+ def test_remove_metadata_fields(self) -> None:"""Test _remove_metadata_fields function"""schema = {"$id": "should be removed",@@ -402,7 +419,7 @@ class TestUtilityFunctions:"version": "should be removed","type": "object","title": "should remain",- "properties": {},+ "properties": dict[str, object](),}cleaned = _remove_metadata_fields(schema)@@ -421,7 +438,7 @@ class TestUtilityFunctions:class TestSchemaResolverClass:"""Test SchemaResolver class specifically"""- def test_resolver_initialization(self):+ def test_resolver_initialization(self) -> None:"""Test resolver initialization"""# Default initializationresolver = SchemaResolver()@@ -434,7 +451,7 @@ class TestSchemaResolverClass:assert resolver.max_depth == 5assert resolver.registry is custom_registry- def test_cache_sharing(self):+ def test_cache_sharing(self) -> None:"""Test that cache is shared between resolver instances"""SchemaResolver.clear_cache()@@ -453,7 +470,7 @@ class TestSchemaResolverClass:assert result1 == result2- def test_resolver_with_list_schema(self):+ def test_resolver_with_list_schema(self) -> None:"""Test resolver with list as root schema"""list_schema = [{"$ref": "https://dify.ai/schemas/v1/file.json"},@@ -472,7 +489,7 @@ class TestSchemaResolverClass:assert resolved[2]["type"] == "object"assert resolved[2]["title"] == "Q&A Structure"- def test_cache_performance(self):+ def test_cache_performance(self) -> None:"""Test that repeated references share cached schema lookups."""SchemaResolver.clear_cache()@@ -496,7 +513,7 @@ class TestSchemaResolverClass:assert result1 == result2mock_get.assert_called_once_with("https://dify.ai/schemas/v1/file.json")- def test_fast_path_performance_no_refs(self):+ def test_fast_path_performance_no_refs(self) -> None:"""Test that schemas without $refs use fast path and avoid deep copying"""# Create a moderately complex schema without any $refs (typical plugin output_schema)no_refs_schema = {@@ -558,7 +575,7 @@ class TestSchemaResolverClass:# The main benefit is avoiding deep copy and BFS, not necessarily being 5x fasterassert avg_fast_time < avg_slow_time * 2 # Should not be more than 2x slower- def test_batch_processing_performance(self):+ def test_batch_processing_performance(self) -> None:"""Test performance improvement for batch processing of schemas without refs"""# Simulate the plugin tool scenario: many schemas, most without refsschemas_without_refs = [@@ -582,16 +599,10 @@ class TestSchemaResolverClass:avg_time_per_schema = batch_time / len(schemas_without_refs)assert avg_time_per_schema < 0.001- def test_has_dify_refs_performance(self):+ def test_has_dify_refs_performance(self) -> None:"""Test that _has_dify_refs is fast for large schemas without refs"""# Create a very large schema without refs- large_schema = {"type": "object", "properties": {}}-- # Add many nested properties- current = large_schema- for i in range(100):- current["properties"][f"level_{i}"] = {"type": "object", "properties": {}}- current = current["properties"][f"level_{i}"]+ large_schema = _nested_schema(depth=100)# _has_dify_refs should be fast even for large schemastimes = []@@ -607,7 +618,7 @@ class TestSchemaResolverClass:assert not has_refsassert avg_time < 0.01 # Should complete in less than 10ms- def test_hybrid_vs_recursive_performance(self):+ def test_hybrid_vs_recursive_performance(self) -> None:"""Test performance comparison between hybrid and recursive detection"""# Create test schemas of different types and sizestest_cases = [@@ -637,7 +648,7 @@ class TestSchemaResolverClass:"expected": False,},# Case 3: Large schema without refs- {"name": "large_no_refs", "schema": {"type": "object", "properties": {}}, "expected": False},+ {"name": "large_no_refs", "schema": _nested_schema(depth=50), "expected": False},# Case 4: Schema with Dify refs{"name": "with_dify_refs",@@ -661,12 +672,6 @@ class TestSchemaResolverClass:},]- # Add deep nesting to large schema- current = test_cases[2]["schema"]- for i in range(50):- current["properties"][f"level_{i}"] = {"type": "object", "properties": {}}- current = current["properties"][f"level_{i}"]-# Performance comparisonfor test_case in test_cases:schema = test_case["schema"]@@ -710,7 +715,7 @@ class TestSchemaResolverClass:# so allow a small absolute noise budget on top of the relative limit.assert avg_hybrid < (avg_recursive * relative_slowdown_limit) + absolute_noise_budget_seconds- def test_string_matching_edge_cases(self):+ def test_string_matching_edge_cases(self) -> None:"""Test edge cases for string-based detection"""# Case 1: False positive potential - $ref in descriptionschema_false_positive = {diff --git a/api/tests/unit_tests/core/schemas/test_schema_manager.py b/api/tests/unit_tests/core/schemas/test_schema_manager.pyindex e93c172a901..b87f29ecd3b 100644--- a/api/tests/unit_tests/core/schemas/test_schema_manager.py+++ b/api/tests/unit_tests/core/schemas/test_schema_manager.py@@ -1,18 +1,18 @@from pathlib import Path-from unittest.mock import patch+from unittest.mock import MagicMock, patchfrom core.schemas.registry import SchemaRegistryfrom core.schemas.schema_manager import SchemaManager-def test_init_with_provided_registry(tmp_path: Path):+def test_init_with_provided_registry(tmp_path: Path) -> None:registry = SchemaRegistry(str(tmp_path))manager = SchemaManager(registry=registry)assert manager.registry == registry@patch("core.schemas.schema_manager.SchemaRegistry.default_registry")-def test_init_with_default_registry(mock_default_registry, tmp_path: Path):+def test_init_with_default_registry(mock_default_registry: MagicMock, tmp_path: Path) -> None:registry = SchemaRegistry(str(tmp_path))mock_default_registry.return_value = registry@@ -22,11 +22,11 @@ def test_init_with_default_registry(mock_default_registry, tmp_path: Path):assert manager.registry == registry-def test_get_all_schema_definitions(tmp_path: Path):+def test_get_all_schema_definitions(tmp_path: Path) -> None:registry = SchemaRegistry(str(tmp_path))expected_definitions = [- {"name": "schema1", "label": "schema1", "schema": {}},- {"name": "schema2", "label": "schema2", "schema": {}},+ {"name": "schema1", "label": "schema1", "schema": dict[str, object]()},+ {"name": "schema2", "label": "schema2", "schema": dict[str, object]()},]registry.versions = {"v1": {"other": {}}, "v2": {"schema1": {}, "schema2": {}}}@@ -36,7 +36,7 @@ def test_get_all_schema_definitions(tmp_path: Path):assert result == expected_definitions-def test_get_schema_by_name_success(tmp_path: Path):+def test_get_schema_by_name_success(tmp_path: Path) -> None:registry = SchemaRegistry(str(tmp_path))mock_schema = {"type": "object"}registry.versions = {"v1": {"my_schema": mock_schema}, "v2": {"my_schema": {"type": "string"}}}@@ -47,7 +47,7 @@ def test_get_schema_by_name_success(tmp_path: Path):assert result == {"name": "my_schema", "schema": mock_schema}-def test_get_schema_by_name_not_found(tmp_path: Path):+def test_get_schema_by_name_not_found(tmp_path: Path) -> None:registry = SchemaRegistry(str(tmp_path))manager = SchemaManager(registry=registry)@@ -56,7 +56,7 @@ def test_get_schema_by_name_not_found(tmp_path: Path):assert result is None-def test_list_available_schemas(tmp_path: Path):+def test_list_available_schemas(tmp_path: Path) -> None:registry = SchemaRegistry(str(tmp_path))expected_schemas = ["schema1", "schema2"]registry.versions = {"v1": {"schema2": {}, "schema1": {}}, "v2": {"other": {}}}@@ -67,7 +67,7 @@ def test_list_available_schemas(tmp_path: Path):assert result == expected_schemas-def test_list_available_versions(tmp_path: Path):+def test_list_available_versions(tmp_path: Path) -> None:registry = SchemaRegistry(str(tmp_path))expected_versions = ["v1", "v2"]registry.versions = {"v2": {}, "v1": {}}diff --git a/api/tests/unit_tests/pyrefly.toml b/api/tests/unit_tests/pyrefly.tomlindex e524fc4f9bd..e6be3f41096 100644--- a/api/tests/unit_tests/pyrefly.toml+++ b/api/tests/unit_tests/pyrefly.toml@@ -356,9 +356,6 @@ project-excludes = ["core/repositories/test_sqlalchemy_workflow_node_execution_repository.py","core/repositories/test_workflow_node_execution_conflict_handling.py","core/repositories/test_workflow_node_execution_truncation.py",- "core/schemas/test_registry.py",- "core/schemas/test_resolver.py",- "core/schemas/test_schema_manager.py","core/telemetry/test_facade.py","core/telemetry/test_gateway_integration.py","core/test_file.py",