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 redis extension tests (#43873)

Eddy ZHANG3cba699821a6
diff --git a/api/tests/unit_tests/extensions/test_redis.py b/api/tests/unit_tests/extensions/test_redis.py
index 4de90d0afb1..8f5f03924cc 100644
--- a/api/tests/unit_tests/extensions/test_redis.py
+++ b/api/tests/unit_tests/extensions/test_redis.py
@@ -1,4 +1,4 @@
-from collections.abc import Mapping
+from collections.abc import Callable, Mapping
from dataclasses import dataclass, field
import pytest
@@ -26,10 +26,10 @@ class _RecordingRedisClient:
def _record(self, method: str, *args: object, **kwargs: object) -> None:
self.calls.append((method, args, kwargs))
- def register_script(self, script: str):
+ def register_script(self, script: str) -> Callable[..., str]:
self._record("register_script", script)
- def execute(*, keys, args, client):
+ def execute(*, keys: object, args: object, client: object) -> str:
self._record("script", keys, args, client)
return "result"
@@ -61,7 +61,7 @@ class _RecordingRedisClient:
@pytest.fixture(autouse=True)
-def _redis_config(config_overrides) -> None:
+def _redis_config(config_overrides: Callable[..., None]) -> None:
config_overrides(
REDIS_USERNAME=None,
REDIS_PASSWORD=None,
@@ -83,7 +83,7 @@ def _redis_config(config_overrides) -> None:
class TestGetConnectionHealthParams:
- def test_includes_all_health_params(self):
+ def test_includes_all_health_params(self) -> None:
params = _get_connection_health_params()
assert "retry" in params
@@ -91,14 +91,14 @@ class TestGetConnectionHealthParams:
assert "socket_connect_timeout" in params
assert "health_check_interval" in params
assert isinstance(params["retry"], Retry)
- assert params["retry"]._retries == 3
+ assert params["retry"]._retries == 3 # pyrefly: ignore[missing-attribute]
assert params["socket_timeout"] == 5.0
assert params["socket_connect_timeout"] == 5.0
assert params["health_check_interval"] == 30
class TestGetClusterConnectionHealthParams:
- def test_excludes_health_check_interval(self):
+ def test_excludes_health_check_interval(self) -> None:
params = _get_cluster_connection_health_params()
assert "retry" in params
@@ -108,7 +108,7 @@ class TestGetClusterConnectionHealthParams:
class TestGetBaseRedisParams:
- def test_includes_retry_and_health_params(self):
+ def test_includes_retry_and_health_params(self) -> None:
params = _get_base_redis_params()
assert "retry" in params
@@ -124,52 +124,52 @@ class TestGetBaseRedisParams:
class TestParseRedisNodes:
- def test_trims_nodes(self):
+ def test_trims_nodes(self) -> None:
assert _parse_redis_nodes("redis-a:6379, redis-b:6380") == [("redis-a", 6379), ("redis-b", 6380)]
- def test_supports_bracketed_ipv6(self):
+ def test_supports_bracketed_ipv6(self) -> None:
assert _parse_redis_nodes("[2001:db8::10]:6379") == [("2001:db8::10", 6379)]
class TestRedisFallback:
- def test_redis_fallback_success(self):
+ def test_redis_fallback_success(self) -> None:
@redis_fallback(default_return=None)
- def test_func():
+ def test_func() -> str:
return "success"
assert test_func() == "success"
- def test_redis_fallback_error(self):
+ def test_redis_fallback_error(self) -> None:
@redis_fallback(default_return="fallback")
- def test_func():
+ def test_func() -> None:
raise RedisError("Redis error")
assert test_func() == "fallback"
- def test_redis_fallback_none_default(self):
+ def test_redis_fallback_none_default(self) -> None:
@redis_fallback()
- def test_func():
+ def test_func() -> None:
raise RedisError("Redis error")
assert test_func() is None
- def test_redis_fallback_with_args(self):
+ def test_redis_fallback_with_args(self) -> None:
@redis_fallback(default_return=0)
- def test_func(x, y):
+ def test_func(x: int, y: int) -> None:
raise RedisError("Redis error")
assert test_func(1, 2) == 0
- def test_redis_fallback_with_kwargs(self):
- @redis_fallback(default_return={})
- def test_func(x=None, y=None):
+ def test_redis_fallback_with_kwargs(self) -> None:
+ @redis_fallback(default_return=dict[str, object]())
+ def test_func(x: int | None = None, y: int | None = None) -> None:
raise RedisError("Redis error")
assert test_func(x=1, y=2) == {}
- def test_redis_fallback_preserves_function_metadata(self):
+ def test_redis_fallback_preserves_function_metadata(self) -> None:
@redis_fallback(default_return=None)
- def test_func():
+ def test_func() -> None:
"""Test function docstring"""
pass
@@ -178,21 +178,21 @@ class TestRedisFallback:
class TestRedisKeyPrefixHelpers:
- def test_normalize_redis_key_prefix_trims_whitespace(self):
+ def test_normalize_redis_key_prefix_trims_whitespace(self) -> None:
assert _normalize_redis_key_prefix(" enterprise-a ") == "enterprise-a"
- def test_normalize_redis_key_prefix_treats_whitespace_only_as_empty(self):
+ def test_normalize_redis_key_prefix_treats_whitespace_only_as_empty(self) -> None:
assert _normalize_redis_key_prefix(" ") == ""
- def test_serialize_redis_name_returns_original_when_prefix_empty(self):
+ def test_serialize_redis_name_returns_original_when_prefix_empty(self) -> None:
assert _serialize_redis_name("model_lb_index:test", "") == "model_lb_index:test"
- def test_serialize_redis_name_adds_single_colon_separator(self):
+ def test_serialize_redis_name_adds_single_colon_separator(self) -> None:
assert _serialize_redis_name("model_lb_index:test", "enterprise-a") == "enterprise-a:model_lb_index:test"
class TestRedisClientWrapperKeyPrefix:
- def test_wrapper_registered_script_prefixes_key_arguments(self, config_overrides):
+ def test_wrapper_registered_script_prefixes_key_arguments(self, config_overrides: Callable[..., None]) -> None:
raw_client = _RecordingRedisClient()
wrapper = RedisClientWrapper()
wrapper.initialize(raw_client) # type: ignore[arg-type]
@@ -206,7 +206,7 @@ class TestRedisClientWrapperKeyPrefix:
("script", (("enterprise-a:device_code:abc",), ["argument"], raw_client), {}),
]
- def test_wrapper_get_prefixes_string_keys(self, config_overrides):
+ def test_wrapper_get_prefixes_string_keys(self, config_overrides: Callable[..., None]) -> None:
client = _RecordingRedisClient()
wrapper = RedisClientWrapper()
wrapper.initialize(client) # type: ignore[arg-type]
@@ -216,7 +216,7 @@ class TestRedisClientWrapperKeyPrefix:
assert client.calls == [("get", ("enterprise-a:oauth_state:abc",), {})]
- def test_wrapper_delete_prefixes_multiple_keys(self, config_overrides):
+ def test_wrapper_delete_prefixes_multiple_keys(self, config_overrides: Callable[..., None]) -> None:
client = _RecordingRedisClient()
wrapper = RedisClientWrapper()
wrapper.initialize(client) # type: ignore[arg-type]
@@ -226,7 +226,7 @@ class TestRedisClientWrapperKeyPrefix:
assert client.calls == [("delete", ("enterprise-a:key:a", "enterprise-a:key:b"), {})]
- def test_wrapper_lock_prefixes_lock_name(self, config_overrides):
+ def test_wrapper_lock_prefixes_lock_name(self, config_overrides: Callable[..., None]) -> None:
client = _RecordingRedisClient()
wrapper = RedisClientWrapper()
wrapper.initialize(client) # type: ignore[arg-type]
@@ -239,7 +239,7 @@ class TestRedisClientWrapperKeyPrefix:
assert args == ("enterprise-a:resource-lock",)
assert kwargs["timeout"] == 10
- def test_wrapper_hash_operations_prefix_key_name(self, config_overrides):
+ def test_wrapper_hash_operations_prefix_key_name(self, config_overrides: Callable[..., None]) -> None:
client = _RecordingRedisClient()
wrapper = RedisClientWrapper()
wrapper.initialize(client) # type: ignore[arg-type]
@@ -257,7 +257,7 @@ class TestRedisClientWrapperKeyPrefix:
("hexists", ("enterprise-a:hash:key", "field"), {}),
]
- def test_wrapper_zadd_prefixes_sorted_set_name(self, config_overrides):
+ def test_wrapper_zadd_prefixes_sorted_set_name(self, config_overrides: Callable[..., None]) -> None:
client = _RecordingRedisClient()
wrapper = RedisClientWrapper()
wrapper.initialize(client) # type: ignore[arg-type]
@@ -270,7 +270,7 @@ class TestRedisClientWrapperKeyPrefix:
assert args == ("enterprise-a:zset:key", {"member": 1})
assert kwargs["nx"] is False
- def test_wrapper_preserves_keys_when_prefix_is_empty(self, config_overrides):
+ def test_wrapper_preserves_keys_when_prefix_is_empty(self, config_overrides: Callable[..., None]) -> None:
client = _RecordingRedisClient()
wrapper = RedisClientWrapper()
wrapper.initialize(client) # type: ignore[arg-type]
diff --git a/api/tests/unit_tests/pyrefly.toml b/api/tests/unit_tests/pyrefly.toml
index 2b860d6cc4c..b6d3d9a4dd8 100644
--- a/api/tests/unit_tests/pyrefly.toml
+++ b/api/tests/unit_tests/pyrefly.toml
@@ -612,7 +612,6 @@ project-excludes = [
"extensions/test_ext_request_logging.py",
"extensions/test_ext_socketio.py",
"extensions/test_pubsub_channel.py",
- "extensions/test_redis.py",
"extensions/test_set_secretkey.py",
"extensions/test_workflow_warm_shutdown.py",
"factories/test_build_from_mapping.py",