Skip to content

armgit / dify

Public Imported from GitHub
0

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.

refactor(api): enable strict typing for utils unit tests (#44331)

Eddy ZHANG507313ada516
diff --git a/api/tests/unit_tests/pyrefly.toml b/api/tests/unit_tests/pyrefly.toml
index eebf7861ce3..65bcafc864a 100644
--- a/api/tests/unit_tests/pyrefly.toml
+++ b/api/tests/unit_tests/pyrefly.toml
@@ -599,12 +599,6 @@ project-excludes = [
"test_pytest_dify.py",
"tools/test_api_tool.py",
"tools/test_mcp_tool.py",
- "utils/encryption/test_system_encryption.py",
- "utils/http_parser/test_oauth_convert_request_to_raw_data.py",
- "utils/position_helper/test_position_helper.py",
- "utils/structured_output_parser/test_structured_output_parser.py",
- "utils/test_text_processing.py",
- "utils/yaml/test_yaml_utils.py",
]
[errors]
diff --git a/api/tests/unit_tests/utils/encryption/test_system_encryption.py b/api/tests/unit_tests/utils/encryption/test_system_encryption.py
index e76edc377fc..aa5b2ffb97b 100644
--- a/api/tests/unit_tests/utils/encryption/test_system_encryption.py
+++ b/api/tests/unit_tests/utils/encryption/test_system_encryption.py
@@ -1,6 +1,7 @@
import base64
import hashlib
-from unittest.mock import patch
+from collections.abc import Callable
+from unittest.mock import MagicMock, patch
import pytest
from Crypto.Cipher import AES
@@ -20,34 +21,34 @@ from core.tools.utils.system_encryption import (
class TestSystemEncrypter:
"""Test cases for SystemEncrypter class"""
- def test_init_with_secret_key(self):
+ def test_init_with_secret_key(self) -> None:
"""Test initialization with provided secret key"""
secret_key = "test_secret_key"
encrypter = SystemEncrypter(secret_key=secret_key)
expected_key = hashlib.sha256(secret_key.encode()).digest()
assert encrypter.key == expected_key
- def test_init_with_none_secret_key(self, config_overrides):
+ def test_init_with_none_secret_key(self, config_overrides: Callable[..., None]) -> None:
"""Test initialization with None secret key falls back to config"""
config_overrides(SECRET_KEY="config_secret")
encrypter = SystemEncrypter(secret_key=None)
expected_key = hashlib.sha256(b"config_secret").digest()
assert encrypter.key == expected_key
- def test_init_with_empty_secret_key(self):
+ def test_init_with_empty_secret_key(self) -> None:
"""Test initialization with empty secret key"""
encrypter = SystemEncrypter(secret_key="")
expected_key = hashlib.sha256(b"").digest()
assert encrypter.key == expected_key
- def test_init_without_secret_key_uses_config(self, config_overrides):
+ def test_init_without_secret_key_uses_config(self, config_overrides: Callable[..., None]) -> None:
"""Test initialization without secret key uses config"""
config_overrides(SECRET_KEY="default_secret")
encrypter = SystemEncrypter()
expected_key = hashlib.sha256(b"default_secret").digest()
assert encrypter.key == expected_key
- def test_encrypt_params_basic(self):
+ def test_encrypt_params_basic(self) -> None:
"""Test basic parameters encryption"""
encrypter = SystemEncrypter("test_secret")
params = {"client_id": "test_id", "client_secret": "test_secret"}
@@ -62,7 +63,7 @@ class TestSystemEncrypter:
except Exception:
pytest.fail("Encrypted result is not valid base64")
- def test_encrypt_params_empty_dict(self):
+ def test_encrypt_params_empty_dict(self) -> None:
"""Test encryption with empty dictionary"""
encrypter = SystemEncrypter("test_secret")
params = {}
@@ -71,7 +72,7 @@ class TestSystemEncrypter:
assert isinstance(encrypted, str)
assert len(encrypted) > 0
- def test_encrypt_params_complex_data(self):
+ def test_encrypt_params_complex_data(self) -> None:
"""Test encryption with complex data structures"""
encrypter = SystemEncrypter("test_secret")
params = {
@@ -88,7 +89,7 @@ class TestSystemEncrypter:
assert isinstance(encrypted, str)
assert len(encrypted) > 0
- def test_encrypt_params_unicode_data(self):
+ def test_encrypt_params_unicode_data(self) -> None:
"""Test encryption with unicode data"""
encrypter = SystemEncrypter("test_secret")
params = {"client_id": "test_id", "client_secret": "test_secret", "description": "This is a test case 🚀"}
@@ -97,7 +98,7 @@ class TestSystemEncrypter:
assert isinstance(encrypted, str)
assert len(encrypted) > 0
- def test_encrypt_params_large_data(self):
+ def test_encrypt_params_large_data(self) -> None:
"""Test encryption with large data"""
encrypter = SystemEncrypter("test_secret")
params = {
@@ -109,19 +110,19 @@ class TestSystemEncrypter:
assert isinstance(encrypted, str)
assert len(encrypted) > 0
- def test_encrypt_params_invalid_input(self):
+ def test_encrypt_params_invalid_input(self) -> None:
"""Test encryption with invalid input types"""
encrypter = SystemEncrypter("test_secret")
with pytest.raises(Exception, match="Encryption failed: 'NoneType' object is not iterable"):
- encrypter.encrypt_params(None)
+ encrypter.encrypt_params(None) # pyrefly: ignore[bad-argument-type]
with pytest.raises(
Exception, match=("Encryption failed: dictionary update sequence element \\#0 has length 1; 2 is required")
):
- encrypter.encrypt_params("not_a_dict")
+ encrypter.encrypt_params("not_a_dict") # pyrefly: ignore[bad-argument-type]
- def test_decrypt_params_basic(self):
+ def test_decrypt_params_basic(self) -> None:
"""Test basic parameters decryption"""
encrypter = SystemEncrypter("test_secret")
original_params = {"client_id": "test_id", "client_secret": "test_secret"}
@@ -131,7 +132,7 @@ class TestSystemEncrypter:
assert decrypted == original_params
- def test_decrypt_params_empty_dict(self):
+ def test_decrypt_params_empty_dict(self) -> None:
"""Test decryption of empty dictionary"""
encrypter = SystemEncrypter("test_secret")
original_params = {}
@@ -141,7 +142,7 @@ class TestSystemEncrypter:
assert decrypted == original_params
- def test_decrypt_params_complex_data(self):
+ def test_decrypt_params_complex_data(self) -> None:
"""Test decryption with complex data structures"""
encrypter = SystemEncrypter("test_secret")
original_params = {
@@ -159,7 +160,7 @@ class TestSystemEncrypter:
assert decrypted == original_params
- def test_decrypt_params_unicode_data(self):
+ def test_decrypt_params_unicode_data(self) -> None:
"""Test decryption with unicode data"""
encrypter = SystemEncrypter("test_secret")
original_params = {
@@ -173,7 +174,7 @@ class TestSystemEncrypter:
assert decrypted == original_params
- def test_decrypt_params_large_data(self):
+ def test_decrypt_params_large_data(self) -> None:
"""Test decryption with large data"""
encrypter = SystemEncrypter("test_secret")
original_params = {
@@ -186,14 +187,14 @@ class TestSystemEncrypter:
assert decrypted == original_params
- def test_decrypt_params_invalid_base64(self):
+ def test_decrypt_params_invalid_base64(self) -> None:
"""Test decryption with invalid base64 data"""
encrypter = SystemEncrypter("test_secret")
with pytest.raises(EncryptionError):
encrypter.decrypt_params("invalid_base64!")
- def test_decrypt_params_empty_string(self):
+ def test_decrypt_params_empty_string(self) -> None:
"""Test decryption with empty string"""
encrypter = SystemEncrypter("test_secret")
@@ -202,21 +203,21 @@ class TestSystemEncrypter:
assert "encrypted_data cannot be empty" in str(exc_info.value)
- def test_decrypt_params_non_string_input(self):
+ def test_decrypt_params_non_string_input(self) -> None:
"""Test decryption with non-string input"""
encrypter = SystemEncrypter("test_secret")
with pytest.raises(ValueError, match="encrypted_data must be a string") as exc_info:
- encrypter.decrypt_params(123)
+ encrypter.decrypt_params(123) # pyrefly: ignore[bad-argument-type]
assert "encrypted_data must be a string" in str(exc_info.value)
with pytest.raises(ValueError, match="encrypted_data must be a string") as exc_info:
- encrypter.decrypt_params(None)
+ encrypter.decrypt_params(None) # pyrefly: ignore[bad-argument-type]
assert "encrypted_data must be a string" in str(exc_info.value)
- def test_decrypt_params_too_short_data(self):
+ def test_decrypt_params_too_short_data(self) -> None:
"""Test decryption with too short encrypted data"""
encrypter = SystemEncrypter("test_secret")
@@ -228,7 +229,7 @@ class TestSystemEncrypter:
assert "Invalid encrypted data format" in str(exc_info.value)
- def test_decrypt_params_corrupted_data(self):
+ def test_decrypt_params_corrupted_data(self) -> None:
"""Test decryption with corrupted data"""
encrypter = SystemEncrypter("test_secret")
@@ -238,7 +239,7 @@ class TestSystemEncrypter:
with pytest.raises(EncryptionError):
encrypter.decrypt_params(corrupted_data)
- def test_decrypt_params_wrong_key(self):
+ def test_decrypt_params_wrong_key(self) -> None:
"""Test decryption with wrong key"""
encrypter1 = SystemEncrypter("secret1")
encrypter2 = SystemEncrypter("secret2")
@@ -249,12 +250,12 @@ class TestSystemEncrypter:
with pytest.raises(EncryptionError):
encrypter2.decrypt_params(encrypted)
- def test_encryption_decryption_consistency(self):
+ def test_encryption_decryption_consistency(self) -> None:
"""Test that encryption and decryption are consistent"""
encrypter = SystemEncrypter("test_secret")
test_cases = [
- {},
+ dict[str, object](),
{"simple": "value"},
{"client_id": "id", "client_secret": "secret"},
{"complex": {"nested": {"deep": "value"}}},
@@ -268,7 +269,7 @@ class TestSystemEncrypter:
decrypted = encrypter.decrypt_params(encrypted)
assert decrypted == original_params, f"Failed for case: {original_params}"
- def test_encryption_randomness(self):
+ def test_encryption_randomness(self) -> None:
"""Test that encryption produces different results for same input"""
encrypter = SystemEncrypter("test_secret")
params = {"client_id": "test_id", "client_secret": "test_secret"}
@@ -284,7 +285,7 @@ class TestSystemEncrypter:
decrypted2 = encrypter.decrypt_params(encrypted2)
assert decrypted1 == decrypted2 == params
- def test_different_secret_keys_produce_different_results(self):
+ def test_different_secret_keys_produce_different_results(self) -> None:
"""Test that different secret keys produce different encrypted results"""
encrypter1 = SystemEncrypter("secret1")
encrypter2 = SystemEncrypter("secret2")
@@ -303,7 +304,7 @@ class TestSystemEncrypter:
assert decrypted1 == decrypted2 == params
@patch("core.tools.utils.system_encryption.get_random_bytes")
- def test_encrypt_params_crypto_error(self, mock_get_random_bytes):
+ def test_encrypt_params_crypto_error(self, mock_get_random_bytes: MagicMock) -> None:
"""Test encryption when crypto operation fails"""
mock_get_random_bytes.side_effect = Exception("Crypto error")
@@ -316,7 +317,7 @@ class TestSystemEncrypter:
assert "Encryption failed" in str(exc_info.value)
@patch("core.tools.utils.system_encryption.TypeAdapter")
- def test_encrypt_params_serialization_error(self, mock_type_adapter):
+ def test_encrypt_params_serialization_error(self, mock_type_adapter: MagicMock) -> None:
"""Test encryption when JSON serialization fails"""
mock_type_adapter.return_value.dump_json.side_effect = Exception("Serialization error")
@@ -328,7 +329,7 @@ class TestSystemEncrypter:
assert "Encryption failed" in str(exc_info.value)
- def test_decrypt_params_invalid_json(self):
+ def test_decrypt_params_invalid_json(self) -> None:
"""Test decryption with invalid JSON data"""
encrypter = SystemEncrypter("test_secret")
@@ -344,7 +345,7 @@ class TestSystemEncrypter:
with pytest.raises(EncryptionError):
encrypter.decrypt_params(encoded)
- def test_key_derivation_consistency(self):
+ def test_key_derivation_consistency(self) -> None:
"""Test that key derivation is consistent"""
secret_key = "test_secret"
encrypter1 = SystemEncrypter(secret_key)
@@ -359,7 +360,7 @@ class TestSystemEncrypter:
class TestFactoryFunctions:
"""Test cases for factory functions"""
- def test_create_system_encrypter_with_secret(self):
+ def test_create_system_encrypter_with_secret(self) -> None:
"""Test factory function with secret key"""
secret_key = "test_secret"
encrypter = create_system_encrypter(secret_key)
@@ -368,7 +369,7 @@ class TestFactoryFunctions:
expected_key = hashlib.sha256(secret_key.encode()).digest()
assert encrypter.key == expected_key
- def test_create_system_encrypter_without_secret(self, config_overrides):
+ def test_create_system_encrypter_without_secret(self, config_overrides: Callable[..., None]) -> None:
"""Test factory function without secret key"""
config_overrides(SECRET_KEY="config_secret")
encrypter = create_system_encrypter()
@@ -377,7 +378,7 @@ class TestFactoryFunctions:
expected_key = hashlib.sha256(b"config_secret").digest()
assert encrypter.key == expected_key
- def test_create_system_encrypter_with_none_secret(self, config_overrides):
+ def test_create_system_encrypter_with_none_secret(self, config_overrides: Callable[..., None]) -> None:
"""Test factory function with None secret key"""
config_overrides(SECRET_KEY="config_secret")
encrypter = create_system_encrypter(None)
@@ -390,7 +391,7 @@ class TestFactoryFunctions:
class TestGlobalEncrypterInstance:
"""Test cases for global encrypter instance"""
- def test_get_system_encrypter_singleton(self):
+ def test_get_system_encrypter_singleton(self) -> None:
"""Test that get_system_encrypter returns singleton instance"""
# Clear the global instance first
import core.tools.utils.system_encryption
@@ -403,7 +404,7 @@ class TestGlobalEncrypterInstance:
assert encrypter1 is encrypter2
assert isinstance(encrypter1, SystemEncrypter)
- def test_get_system_encrypter_uses_config(self, config_overrides):
+ def test_get_system_encrypter_uses_config(self, config_overrides: Callable[..., None]) -> None:
"""Test that global encrypter uses config"""
# Clear the global instance first
import core.tools.utils.system_encryption
@@ -420,7 +421,7 @@ class TestGlobalEncrypterInstance:
class TestConvenienceFunctions:
"""Test cases for convenience functions"""
- def test_encrypt_system_params(self):
+ def test_encrypt_system_params(self) -> None:
"""Test encrypt_system_params convenience function"""
params = {"client_id": "test_id", "client_secret": "test_secret"}
@@ -429,7 +430,7 @@ class TestConvenienceFunctions:
assert isinstance(encrypted, str)
assert len(encrypted) > 0
- def test_decrypt_system_params(self):
+ def test_decrypt_system_params(self) -> None:
"""Test decrypt_system_params convenience function"""
params = {"client_id": "test_id", "client_secret": "test_secret"}
@@ -438,10 +439,10 @@ class TestConvenienceFunctions:
assert decrypted == params
- def test_convenience_functions_consistency(self):
+ def test_convenience_functions_consistency(self) -> None:
"""Test that convenience functions work consistently"""
test_cases = [
- {},
+ dict[str, object](),
{"simple": "value"},
{"client_id": "id", "client_secret": "secret"},
{"complex": {"nested": {"deep": "value"}}},
@@ -454,30 +455,30 @@ class TestConvenienceFunctions:
decrypted = decrypt_system_params(encrypted)
assert decrypted == original_params, f"Failed for case: {original_params}"
- def test_convenience_functions_with_errors(self):
+ def test_convenience_functions_with_errors(self) -> None:
"""Test convenience functions with error conditions"""
# Test encryption with invalid input
with pytest.raises(Exception, match="Encryption failed: 'NoneType' object is not iterable"):
- encrypt_system_params(None)
+ encrypt_system_params(None) # pyrefly: ignore[bad-argument-type]
# Test decryption with invalid input
with pytest.raises(ValueError, match="encrypted_data cannot be empty"):
decrypt_system_params("")
with pytest.raises(ValueError, match="encrypted_data must be a string"):
- decrypt_system_params(None)
+ decrypt_system_params(None) # pyrefly: ignore[bad-argument-type]
class TestErrorHandling:
"""Test cases for error handling"""
- def test_encryption_error_inheritance(self):
+ def test_encryption_error_inheritance(self) -> None:
"""Test that EncryptionError is a proper exception"""
error = EncryptionError("Test error")
assert isinstance(error, Exception)
assert str(error) == "Test error"
- def test_encryption_error_with_cause(self):
+ def test_encryption_error_with_cause(self) -> None:
"""Test EncryptionError with cause"""
original_error = ValueError("Original error")
error = EncryptionError("Wrapper error")
@@ -487,7 +488,7 @@ class TestErrorHandling:
assert str(error) == "Wrapper error"
assert error.__cause__ is original_error
- def test_error_messages_are_informative(self):
+ def test_error_messages_are_informative(self) -> None:
"""Test that error messages are informative"""
encrypter = SystemEncrypter("test_secret")
@@ -498,7 +499,7 @@ class TestErrorHandling:
# Test non-string error
with pytest.raises(ValueError, match="encrypted_data must be a string") as exc_info:
- encrypter.decrypt_params(123)
+ encrypter.decrypt_params(123) # pyrefly: ignore[bad-argument-type]
assert "encrypted_data must be a string" in str(exc_info.value)
# Test invalid format error
@@ -511,7 +512,7 @@ class TestErrorHandling:
class TestEdgeCases:
"""Test cases for edge cases and boundary conditions"""
- def test_very_long_secret_key(self):
+ def test_very_long_secret_key(self) -> None:
"""Test with very long secret key"""
long_secret = "x" * 10000
encrypter = SystemEncrypter(long_secret)
@@ -525,7 +526,7 @@ class TestEdgeCases:
decrypted = encrypter.decrypt_params(encrypted)
assert decrypted == params
- def test_special_characters_in_secret_key(self):
+ def test_special_characters_in_secret_key(self) -> None:
"""Test with special characters in secret key"""
special_secret = "!@#$%^&*()_+-=[]{}|;':\",./<>?`~test🚀"
encrypter = SystemEncrypter(special_secret)
@@ -535,13 +536,13 @@ class TestEdgeCases:
decrypted = encrypter.decrypt_params(encrypted)
assert decrypted == params
- def test_empty_values_in_params(self):
+ def test_empty_values_in_params(self) -> None:
"""Test with empty values in params"""
params = {
"client_id": "",
"client_secret": "",
- "empty_dict": {},
- "empty_list": [],
+ "empty_dict": dict[str, object](),
+ "empty_list": list[object](),
"empty_string": "",
"zero": 0,
"false": False,
@@ -553,7 +554,7 @@ class TestEdgeCases:
decrypted = encrypter.decrypt_params(encrypted)
assert decrypted == params
- def test_deeply_nested_params(self):
+ def test_deeply_nested_params(self) -> None:
"""Test with deeply nested params"""
params = {"level1": {"level2": {"level3": {"level4": {"level5": {"deep_value": "found"}}}}}}
@@ -562,7 +563,7 @@ class TestEdgeCases:
decrypted = encrypter.decrypt_params(encrypted)
assert decrypted == params
- def test_params_with_all_json_types(self):
+ def test_params_with_all_json_types(self) -> None:
"""Test with all JSON-supported data types"""
params = {
"string": "test_string",
@@ -585,7 +586,7 @@ class TestEdgeCases:
class TestPerformance:
"""Test cases for performance considerations"""
- def test_large_params(self):
+ def test_large_params(self) -> None:
"""Test with large params"""
large_value = "x" * 100000 # 100KB
params = {"client_id": "test_id", "large_data": large_value}
@@ -595,7 +596,7 @@ class TestPerformance:
decrypted = encrypter.decrypt_params(encrypted)
assert decrypted == params
- def test_many_fields_params(self):
+ def test_many_fields_params(self) -> None:
"""Test with many fields in params"""
params = {f"field_{i}": f"value_{i}" for i in range(1000)}
@@ -604,7 +605,7 @@ class TestPerformance:
decrypted = encrypter.decrypt_params(encrypted)
assert decrypted == params
- def test_repeated_encryption_decryption(self):
+ def test_repeated_encryption_decryption(self) -> None:
"""Test repeated encryption and decryption operations"""
encrypter = SystemEncrypter("test_secret")
params = {"client_id": "test_id", "client_secret": "test_secret"}
diff --git a/api/tests/unit_tests/utils/http_parser/test_oauth_convert_request_to_raw_data.py b/api/tests/unit_tests/utils/http_parser/test_oauth_convert_request_to_raw_data.py
index 293ac253f5d..1fa7c863835 100644
--- a/api/tests/unit_tests/utils/http_parser/test_oauth_convert_request_to_raw_data.py
+++ b/api/tests/unit_tests/utils/http_parser/test_oauth_convert_request_to_raw_data.py
@@ -7,7 +7,7 @@ from werkzeug.test import EnvironBuilder
from core.plugin.impl.oauth import OAuthHandler
-def test_oauth_convert_request_to_raw_data():
+def test_oauth_convert_request_to_raw_data() -> None:
oauth_handler = OAuthHandler()
builder = EnvironBuilder(
method="GET",
@@ -22,7 +22,7 @@ def test_oauth_convert_request_to_raw_data():
assert b"\r\n\r\n" in raw_request_bytes
-def test_oauth_convert_request_to_raw_data_with_query_params():
+def test_oauth_convert_request_to_raw_data_with_query_params() -> None:
oauth_handler = OAuthHandler()
builder = EnvironBuilder(
method="GET",
@@ -38,7 +38,7 @@ def test_oauth_convert_request_to_raw_data_with_query_params():
assert b"\r\n\r\n" in raw_request_bytes
-def test_oauth_convert_request_to_raw_data_with_post_body():
+def test_oauth_convert_request_to_raw_data_with_post_body() -> None:
oauth_handler = OAuthHandler()
builder = EnvironBuilder(
method="POST",
@@ -55,7 +55,7 @@ def test_oauth_convert_request_to_raw_data_with_post_body():
assert b"param1=value1&param2=value2" in raw_request_bytes
-def test_oauth_convert_request_to_raw_data_with_json_body():
+def test_oauth_convert_request_to_raw_data_with_json_body() -> None:
oauth_handler = OAuthHandler()
json_data = {"code": "abc123", "state": "xyz789", "grant_type": "authorization_code"}
builder = EnvironBuilder(
diff --git a/api/tests/unit_tests/utils/position_helper/test_position_helper.py b/api/tests/unit_tests/utils/position_helper/test_position_helper.py
index dc94dbdb3d0..c36df218049 100644
--- a/api/tests/unit_tests/utils/position_helper/test_position_helper.py
+++ b/api/tests/unit_tests/utils/position_helper/test_position_helper.py
@@ -42,7 +42,7 @@ def prepare_empty_commented_positions_yaml(tmp_path: Path, monkeypatch: pytest.M
return str(tmp_path)
-def test_position_helper(prepare_example_positions_yaml):
+def test_position_helper(prepare_example_positions_yaml: str) -> None:
position_map = get_position_map(folder_path=prepare_example_positions_yaml, file_name="example_positions.yaml")
assert len(position_map) == 4
assert position_map == {
@@ -53,14 +53,14 @@ def test_position_helper(prepare_example_positions_yaml):
}
-def test_position_helper_with_all_commented(prepare_empty_commented_positions_yaml):
+def test_position_helper_with_all_commented(prepare_empty_commented_positions_yaml: str) -> None:
position_map = get_position_map(
folder_path=prepare_empty_commented_positions_yaml, file_name="example_positions_all_commented.yaml"
)
assert position_map == {}
-def test_excluded_position_data(prepare_example_positions_yaml):
+def test_excluded_position_data(prepare_example_positions_yaml: str) -> None:
position_map = get_position_map(folder_path=prepare_example_positions_yaml, file_name="example_positions.yaml")
pin_list = ["forth", "first"]
include_set = set()
@@ -92,7 +92,7 @@ def test_excluded_position_data(prepare_example_positions_yaml):
assert sorted_data == ["forth", "first", "second", "third", "extra1", "extra2"]
-def test_included_position_data(prepare_example_positions_yaml):
+def test_included_position_data(prepare_example_positions_yaml: str) -> None:
position_map = get_position_map(folder_path=prepare_example_positions_yaml, file_name="example_positions.yaml")
pin_list = ["forth", "first"]
include_set = {"forth", "first"}
diff --git a/api/tests/unit_tests/utils/structured_output_parser/test_structured_output_parser.py b/api/tests/unit_tests/utils/structured_output_parser/test_structured_output_parser.py
index c166a946d92..372a8a25ec8 100644
--- a/api/tests/unit_tests/utils/structured_output_parser/test_structured_output_parser.py
+++ b/api/tests/unit_tests/utils/structured_output_parser/test_structured_output_parser.py
@@ -1,10 +1,13 @@
from decimal import Decimal
+from typing import Literal, NotRequired
from unittest.mock import MagicMock, patch
import pytest
+from typing_extensions import TypedDict
from core.llm_generator.output_parser.errors import OutputParserError
from core.llm_generator.output_parser.structured_output import invoke_llm_with_structured_output
+from graphon.model_runtime.entities.common_entities import I18nObject
from graphon.model_runtime.entities.llm_entities import (
LLMResult,
LLMResultChunk,
@@ -19,7 +22,31 @@ from graphon.model_runtime.entities.message_entities import (
TextPromptMessageContent,
UserPromptMessage,
)
-from graphon.model_runtime.entities.model_entities import AIModelEntity, ModelType
+from graphon.model_runtime.entities.model_entities import AIModelEntity, ModelType, ParameterRule, ParameterType
+
+
+class _StructuredOutputCase(TypedDict):
+ name: str
+ provider: str
+ model_name: str
+ support_structure_output: bool
+ stream: bool
+ json_schema: dict[str, object]
+ expected_llm_response: LLMResult | list[LLMResultChunk] | list[MagicMock]
+ expected_result_type: type[LLMResultWithStructuredOutput] | Literal["generator"] | None
+ should_raise: bool
+ parameter_rules: NotRequired[list[ParameterRule]]
+ expected_response_format: NotRequired[str]
+ expected_error: NotRequired[type[Exception]]
+
+
+def _response_format_rule(*options: str) -> ParameterRule:
+ return ParameterRule(
+ name="response_format",
+ label=I18nObject(en_US="Response Format"),
+ type=ParameterType.STRING,
+ options=list(options),
+ )
def create_mock_usage(prompt_tokens: int = 10, completion_tokens: int = 5) -> LLMUsage:
@@ -49,7 +76,7 @@ def get_model_entity(provider: str, model_name: str, support_structure_output: b
model_schema.model_provider = provider
model_schema.model_name = model_name
model_schema.support_structure_output = support_structure_output
- model_schema.parameter_rules = []
+ model_schema.parameter_rules = list[object]()
return model_schema
@@ -58,14 +85,14 @@ def get_model_instance() -> MagicMock:
"""Create a mock ModelInstance for testing"""
mock_instance = MagicMock()
mock_instance.provider = "openai"
- mock_instance.credentials = {}
+ mock_instance.credentials = dict[str, object]()
return mock_instance
-def test_structured_output_parser():
+def test_structured_output_parser() -> None:
"""Test cases for invoke_llm_with_structured_output function"""
- testcases = [
+ testcases: list[_StructuredOutputCase] = [
# Test case 1: Model with native structured output support, non-streaming
{
"name": "native_structured_output_non_streaming",
@@ -246,9 +273,8 @@ def test_structured_output_parser():
"support_structure_output": True,
"stream": False,
"json_schema": {"type": "object", "properties": {"result": {"type": "string"}}},
- "parameter_rules": [
- MagicMock(name="response_format", options=["json_schema"], required=False),
- ],
+ "parameter_rules": [_response_format_rule("json_schema")],
+ "expected_response_format": "json_schema",
"expected_llm_response": LLMResult(
model="gpt-4o",
message=AssistantPromptMessage(content='{"result": "success"}'),
@@ -265,9 +291,8 @@ def test_structured_output_parser():
"support_structure_output": False,
"stream": False,
"json_schema": {"type": "object", "properties": {"output": {"type": "string"}}},
- "parameter_rules": [
- MagicMock(name="response_format", options=["JSON"], required=False),
- ],
+ "parameter_rules": [_response_format_rule("JSON")],
+ "expected_response_format": "JSON",
"expected_llm_response": LLMResult(
model="claude-3-sonnet",
message=AssistantPromptMessage(content='{"output": "result"}'),
@@ -298,7 +323,9 @@ def test_structured_output_parser():
if case["should_raise"]:
# Test error cases
- with pytest.raises(case["expected_error"]): # noqa: PT012
+ expected_error = case.get("expected_error")
+ assert expected_error is not None
+ with pytest.raises(expected_error): # noqa: PT012
if case["stream"]:
result_generator = invoke_llm_with_structured_output(
provider=case["provider"],
@@ -356,7 +383,9 @@ def test_structured_output_parser():
assert isinstance(last_chunk.structured_output, dict)
else:
# Test non-streaming results
- assert isinstance(result, case["expected_result_type"])
+ expected_type = case["expected_result_type"]
+ assert isinstance(expected_type, type)
+ assert isinstance(result, expected_type)
assert result.model == case["model_name"]
assert result.structured_output is not None
assert isinstance(result.structured_output, dict)
@@ -368,10 +397,12 @@ def test_structured_output_parser():
assert call_args.kwargs["stream"] == case["stream"]
assert "user" not in call_args.kwargs
assert "temperature" in call_args.kwargs["model_parameters"]
+ if "expected_response_format" in case:
+ assert call_args.kwargs["model_parameters"]["response_format"] == case["expected_response_format"]
assert "max_tokens" in call_args.kwargs["model_parameters"]
-def test_parse_structured_output_edge_cases():
+def test_parse_structured_output_edge_cases() -> None:
"""Test edge cases for structured output parsing"""
# Test case with list that contains dict (reasoning model scenario)
@@ -422,7 +453,7 @@ def test_parse_structured_output_edge_cases():
assert result.structured_output == {"thought": "reasoning process"}
-def test_model_specific_schema_preparation():
+def test_model_specific_schema_preparation() -> None:
"""Test schema preparation for different model types"""
# Test Gemini model
diff --git a/api/tests/unit_tests/utils/test_text_processing.py b/api/tests/unit_tests/utils/test_text_processing.py
index 5f6ccbcdff6..3ca91456785 100644
--- a/api/tests/unit_tests/utils/test_text_processing.py
+++ b/api/tests/unit_tests/utils/test_text_processing.py
@@ -72,5 +72,5 @@ from core.tools.utils.text_processing_utils import remove_leading_symbols
("\n换行", "\n换行"),
],
)
-def test_remove_leading_symbols(input_text, expected_output):
+def test_remove_leading_symbols(input_text: str, expected_output: str) -> None:
assert remove_leading_symbols(input_text) == expected_output
diff --git a/api/tests/unit_tests/utils/yaml/test_yaml_utils.py b/api/tests/unit_tests/utils/yaml/test_yaml_utils.py
index fd4e4a3664d..ac192cb6c50 100644
--- a/api/tests/unit_tests/utils/yaml/test_yaml_utils.py
+++ b/api/tests/unit_tests/utils/yaml/test_yaml_utils.py
@@ -56,7 +56,7 @@ def prepare_invalid_yaml_file(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -
return str(file_path)
-def test_load_yaml_non_existing_file():
+def test_load_yaml_non_existing_file() -> None:
with pytest.raises(FileNotFoundError):
_load_yaml_file(file_path=NON_EXISTING_YAML_FILE)
@@ -64,7 +64,7 @@ def test_load_yaml_non_existing_file():
_load_yaml_file(file_path="")
-def test_load_valid_yaml_file(prepare_example_yaml_file):
+def test_load_valid_yaml_file(prepare_example_yaml_file: str) -> None:
yaml_data = _load_yaml_file(file_path=prepare_example_yaml_file)
assert len(yaml_data) > 0
assert yaml_data["age"] == 30
@@ -75,7 +75,7 @@ def test_load_valid_yaml_file(prepare_example_yaml_file):
assert yaml_data.get("non_existed_key") is None
-def test_load_invalid_yaml_file(prepare_invalid_yaml_file):
+def test_load_invalid_yaml_file(prepare_invalid_yaml_file: str) -> None:
# yaml syntax error
with pytest.raises(YAMLError):
_load_yaml_file(file_path=prepare_invalid_yaml_file)