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 configs unit tests (#44294)

mechmaster203fa361c40fe6
diff --git a/api/tests/unit_tests/configs/test_dify_config.py b/api/tests/unit_tests/configs/test_dify_config.py
index dd9f3313534..e345c79f368 100644
--- a/api/tests/unit_tests/configs/test_dify_config.py
+++ b/api/tests/unit_tests/configs/test_dify_config.py
@@ -1,4 +1,6 @@
-from typing import override
+from collections.abc import Callable
+from pathlib import Path
+from typing import cast, override
import pytest
from flask import Flask
@@ -65,14 +67,15 @@ class _IsolatedDifyConfig(DifyConfig):
def _make_config(**values: object) -> DifyConfig:
# Pydantic exposes a generated field-wise signature, while this helper intentionally forwards heterogeneous input.
- return _IsolatedDifyConfig(**values) # pyrefly: ignore[bad-argument-type]
+ factory = cast(Callable[..., _IsolatedDifyConfig], _IsolatedDifyConfig)
+ return factory(**values)
def test_skill_package_default_limit_matches_documentation() -> None:
assert _make_config().UPLOAD_SKILL_FILE_SIZE_LIMIT == 50
-def test_dify_config_keeps_secret_key_empty_when_missing(tmp_path) -> None:
+def test_dify_config_keeps_secret_key_empty_when_missing(tmp_path: Path) -> None:
config = _make_config(OPENDAL_FS_ROOT=str(tmp_path))
assert config.SECRET_KEY == ""
@@ -80,14 +83,14 @@ def test_dify_config_keeps_secret_key_empty_when_missing(tmp_path) -> None:
assert not (tmp_path / ".dify_secret_key").exists()
-def test_dify_config_preserves_explicit_secret_key(tmp_path) -> None:
+def test_dify_config_preserves_explicit_secret_key(tmp_path: Path) -> None:
config = _make_config(SECRET_KEY="explicit", OPENDAL_FS_ROOT=str(tmp_path))
assert config.SECRET_KEY == "explicit"
assert not (tmp_path / ".dify_secret_key").exists()
-def test_dify_config():
+def test_dify_config() -> None:
config = _make_config(
HTTP_REQUEST_MAX_WRITE_TIMEOUT="30",
HTTP_REQUEST_MAX_READ_TIMEOUT="300",
@@ -213,7 +216,7 @@ def test_new_user_default_models_reject_duplicate_model_types() -> None:
_ = config.NEW_USER_DEFAULT_MODEL_LIST
-def test_http_timeout_defaults():
+def test_http_timeout_defaults() -> None:
"""Test that HTTP timeout defaults are correctly set"""
config = _make_config()
@@ -223,13 +226,13 @@ def test_http_timeout_defaults():
assert config.HTTP_REQUEST_MAX_WRITE_TIMEOUT == 600
-def test_internal_files_url_falls_back_to_server_console_api_url():
+def test_internal_files_url_falls_back_to_server_console_api_url() -> None:
config = _make_config(SERVER_CONSOLE_API_URL="http://api:5001")
assert config.INTERNAL_FILES_URL == "http://api:5001"
-def test_internal_files_url_prefers_explicit_value():
+def test_internal_files_url_prefers_explicit_value() -> None:
config = _make_config(
INTERNAL_FILES_URL="http://files-internal:5001",
SERVER_CONSOLE_API_URL="http://api:5001",
@@ -238,13 +241,13 @@ def test_internal_files_url_prefers_explicit_value():
assert config.INTERNAL_FILES_URL == "http://files-internal:5001"
-def test_empty_files_url_overrides_console_api_url_for_relative_browser_uris():
+def test_empty_files_url_overrides_console_api_url_for_relative_browser_uris() -> None:
config = _make_config(FILES_URL="", CONSOLE_API_URL="http://api:5001")
assert config.FILES_URL == ""
-def test_flask_configs():
+def test_flask_configs() -> None:
flask_app = Flask("app")
flask_app.config.from_mapping(
_make_config(
@@ -296,14 +299,14 @@ def test_flask_configs():
assert str(URL(str(config["CODE_EXECUTION_ENDPOINT"])) / "v1") == "http://127.0.0.1:8194/v1"
-def test_inner_api_config_exist():
+def test_inner_api_config_exist() -> None:
config = _make_config(INNER_API_KEY="test-inner-api-key")
assert config.INNER_API is False
assert isinstance(config.INNER_API_KEY, str)
assert len(config.INNER_API_KEY) > 0
-def test_db_extras_options_merging():
+def test_db_extras_options_merging() -> None:
"""Test that DB_EXTRAS options are merged with the default timezone startup option."""
config = _make_config(DB_EXTRAS="options=-c search_path=myschema")
@@ -312,7 +315,7 @@ def test_db_extras_options_merging():
assert "timezone=UTC" in options
-def test_db_session_timezone_override_can_disable_app_level_timezone_injection():
+def test_db_session_timezone_override_can_disable_app_level_timezone_injection() -> None:
config = _make_config(
DB_EXTRAS="options=-c search_path=myschema",
DB_SESSION_TIMEZONE_OVERRIDE="",
@@ -323,7 +326,7 @@ def test_db_session_timezone_override_can_disable_app_level_timezone_injection()
}
-def test_pubsub_redis_url_default():
+def test_pubsub_redis_url_default() -> None:
config = _make_config(
REDIS_HOST="redis.example.com",
REDIS_PORT="6380",
@@ -337,25 +340,25 @@ def test_pubsub_redis_url_default():
assert config.PUBSUB_REDIS_CHANNEL_TYPE == "pubsub"
-def test_pubsub_redis_url_override():
+def test_pubsub_redis_url_override() -> None:
config = _make_config(PUBSUB_REDIS_URL="redis://pubsub-host:6381/5")
assert config.normalized_pubsub_redis_url == "redis://pubsub-host:6381/5"
-def test_pubsub_redis_url_required_when_default_unavailable():
+def test_pubsub_redis_url_required_when_default_unavailable() -> None:
config = _make_config(REDIS_HOST="")
with pytest.raises(ValueError, match="PUBSUB_REDIS_URL must be set"):
_ = config.normalized_pubsub_redis_url
-def test_dify_config_exposes_redis_key_prefix_default():
+def test_dify_config_exposes_redis_key_prefix_default() -> None:
config = _make_config()
assert config.REDIS_KEY_PREFIX == ""
-def test_dify_config_accepts_redis_key_prefix():
+def test_dify_config_accepts_redis_key_prefix() -> None:
config = _make_config(REDIS_KEY_PREFIX="enterprise-a")
assert config.REDIS_KEY_PREFIX == "enterprise-a"
@@ -384,13 +387,13 @@ def test_dify_config_accepts_redis_key_prefix():
],
)
def test_celery_broker_url_with_special_chars_password(
- broker_url,
- expected_host,
- expected_port,
- expected_username,
- expected_password,
- expected_db,
-):
+ broker_url: str,
+ expected_host: str,
+ expected_port: int,
+ expected_username: str | None,
+ expected_password: str | None,
+ expected_db: str,
+) -> None:
"""Test that CELERY_BROKER_URL with various formats are handled correctly."""
from kombu.utils.url import parse_url
diff --git a/api/tests/unit_tests/configs/test_env_consistency.py b/api/tests/unit_tests/configs/test_env_consistency.py
index 1afdd307b8c..372ef970d59 100644
--- a/api/tests/unit_tests/configs/test_env_consistency.py
+++ b/api/tests/unit_tests/configs/test_env_consistency.py
@@ -108,13 +108,13 @@ def _docker_config_set() -> set[str]:
return docker_config_set
-def test_api_env_keys_exist_in_docker_env_examples():
+def test_api_env_keys_exist_in_docker_env_examples() -> None:
diff = _api_config_set() - _docker_config_set() - BASE_API_AND_DOCKER_CONFIG_SET_DIFF
assert not diff, f"API and Docker config sets are different with keys: {sorted(diff)}"
-def test_api_env_keys_exist_in_docker_compose_env_examples():
+def test_api_env_keys_exist_in_docker_compose_env_examples() -> None:
diff = _api_config_set() - _docker_config_set() - BASE_API_AND_DOCKER_COMPOSE_CONFIG_SET_DIFF
assert not diff, f"API and Docker Compose config sets are different with keys: {sorted(diff)}"
diff --git a/api/tests/unit_tests/configs/test_nacos_http_client.py b/api/tests/unit_tests/configs/test_nacos_http_client.py
index 5712ddb077e..b8190da79f0 100644
--- a/api/tests/unit_tests/configs/test_nacos_http_client.py
+++ b/api/tests/unit_tests/configs/test_nacos_http_client.py
@@ -5,14 +5,14 @@ import httpx
from configs.remote_settings_sources.nacos.http_request import NacosHttpClient
-def _ok_response(text: str = "ok", json_data: dict | None = None) -> httpx.Response:
+def _ok_response(text: str = "ok", json_data: dict[str, object] | None = None) -> httpx.Response:
request = httpx.Request("GET", "http://nacos.test")
if json_data is not None:
return httpx.Response(200, json=json_data, request=request)
return httpx.Response(200, text=text, request=request)
-def test_http_request_passes_bounded_timeout():
+def test_http_request_passes_bounded_timeout() -> None:
client = NacosHttpClient()
with patch("configs.remote_settings_sources.nacos.http_request.httpx.request") as mock_request:
mock_request.return_value = _ok_response()
@@ -24,7 +24,7 @@ def test_http_request_passes_bounded_timeout():
assert timeout.connect is not None
-def test_http_request_returns_graceful_message_on_timeout():
+def test_http_request_returns_graceful_message_on_timeout() -> None:
client = NacosHttpClient()
with patch(
"configs.remote_settings_sources.nacos.http_request.httpx.request",
@@ -36,7 +36,7 @@ def test_http_request_returns_graceful_message_on_timeout():
assert "timed out" in result.lower()
-def test_get_access_token_passes_bounded_timeout():
+def test_get_access_token_passes_bounded_timeout() -> None:
client = NacosHttpClient()
client.username = "user"
client.password = "pass"
diff --git a/api/tests/unit_tests/pyrefly.toml b/api/tests/unit_tests/pyrefly.toml
index 54db056fd5a..d253ed29a8c 100644
--- a/api/tests/unit_tests/pyrefly.toml
+++ b/api/tests/unit_tests/pyrefly.toml
@@ -14,9 +14,6 @@ project-excludes = [
"commands/test_generate_swagger_specs.py",
"commands/test_lint_response_contracts.py",
"commands/test_upgrade_db.py",
- "configs/test_dify_config.py",
- "configs/test_env_consistency.py",
- "configs/test_nacos_http_client.py",
"repositories/test_app_definition_query_repository_agent.py",
"controllers/common/test_app_access.py",
"controllers/common/test_errors.py",