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 logging context and formatter tests (#43615)

Eddy ZHANG916661df59ef
diff --git a/api/tests/unit_tests/core/logging/test_context.py b/api/tests/unit_tests/core/logging/test_context.py
index 2458d57ef75..84e4e109104 100644
--- a/api/tests/unit_tests/core/logging/test_context.py
+++ b/api/tests/unit_tests/core/logging/test_context.py
@@ -1,6 +1,7 @@
"""Tests for logging context module."""
import uuid
+from collections.abc import Iterator
from contextvars import copy_context
import pytest
@@ -16,7 +17,7 @@ from core.logging.context import (
@pytest.fixture(autouse=True)
-def _reset_logging_context():
+def _reset_logging_context() -> Iterator[None]:
clear_request_context()
yield
clear_request_context()
@@ -25,21 +26,21 @@ def _reset_logging_context():
class TestLoggingContext:
"""Tests for the logging context functions."""
- def test_init_creates_request_id(self):
+ def test_init_creates_request_id(self) -> None:
"""init_request_context should create a 10-char request ID."""
init_request_context()
request_id = get_request_id()
assert len(request_id) == 10
assert all(c in "0123456789abcdef" for c in request_id)
- def test_init_creates_trace_id(self):
+ def test_init_creates_trace_id(self) -> None:
"""init_request_context should create a 32-char trace ID."""
init_request_context()
trace_id = get_trace_id()
assert len(trace_id) == 32
assert all(c in "0123456789abcdef" for c in trace_id)
- def test_trace_id_derived_from_request_id(self):
+ def test_trace_id_derived_from_request_id(self) -> None:
"""trace_id should be deterministically derived from request_id."""
init_request_context()
request_id = get_request_id()
@@ -49,7 +50,7 @@ class TestLoggingContext:
expected_trace = uuid.uuid5(uuid.NAMESPACE_DNS, request_id).hex
assert trace_id == expected_trace
- def test_clear_resets_context(self):
+ def test_clear_resets_context(self) -> None:
"""clear_request_context should reset both IDs to empty strings."""
init_request_context()
assert get_request_id() != ""
@@ -59,13 +60,13 @@ class TestLoggingContext:
assert get_request_id() == ""
assert get_trace_id() == ""
- def test_default_values_are_empty(self):
+ def test_default_values_are_empty(self) -> None:
"""Default values should be empty strings before init."""
clear_request_context()
assert get_request_id() == ""
assert get_trace_id() == ""
- def test_multiple_inits_create_different_ids(self):
+ def test_multiple_inits_create_different_ids(self) -> None:
"""Each init should create new unique IDs."""
init_request_context()
first_request_id = get_request_id()
@@ -78,7 +79,7 @@ class TestLoggingContext:
assert first_request_id != second_request_id
assert first_trace_id != second_trace_id
- def test_context_isolation(self):
+ def test_context_isolation(self) -> None:
"""Context should be isolated per-call (no thread leakage in same thread)."""
init_request_context()
id1 = get_request_id()
@@ -90,7 +91,7 @@ class TestLoggingContext:
# IDs should be different
assert id1 != id2
- def test_set_identity_context(self):
+ def test_set_identity_context(self) -> None:
set_identity_context(tenant_id="tenant-1", user_id="user-1", user_type="end_user")
identity = get_identity_context()
@@ -98,14 +99,14 @@ class TestLoggingContext:
assert identity.user_id == "user-1"
assert identity.user_type == "end_user"
- def test_set_identity_context_replaces_all_fields(self):
+ def test_set_identity_context_replaces_all_fields(self) -> None:
set_identity_context(tenant_id="tenant-1", user_id="user-1", user_type="account")
set_identity_context(user_id="user-2", user_type="end_user")
assert get_identity_context() == ("", "user-2", "end_user")
- def test_identity_context_is_copied_as_primitive_values(self):
+ def test_identity_context_is_copied_as_primitive_values(self) -> None:
set_identity_context(tenant_id="tenant-1", user_id="user-1", user_type="end_user")
copied_context = copy_context()
@@ -114,14 +115,14 @@ class TestLoggingContext:
assert get_identity_context() == ("", "", "")
assert copied_context.run(get_identity_context) == ("tenant-1", "user-1", "end_user")
- def test_init_clears_existing_identity_context(self):
+ def test_init_clears_existing_identity_context(self) -> None:
set_identity_context(tenant_id="tenant-1", user_id="user-1", user_type="end_user")
init_request_context()
assert get_identity_context() == ("", "", "")
- def test_clear_resets_identity_context(self):
+ def test_clear_resets_identity_context(self) -> None:
set_identity_context(tenant_id="tenant-1", user_id="user-1", user_type="end_user")
clear_request_context()
diff --git a/api/tests/unit_tests/core/logging/test_structured_formatter.py b/api/tests/unit_tests/core/logging/test_structured_formatter.py
index 94b91d205e0..c2e718c15ba 100644
--- a/api/tests/unit_tests/core/logging/test_structured_formatter.py
+++ b/api/tests/unit_tests/core/logging/test_structured_formatter.py
@@ -7,7 +7,7 @@ import orjson
class TestStructuredJSONFormatter:
- def test_basic_log_format(self):
+ def test_basic_log_format(self) -> None:
from core.logging.structured_formatter import StructuredJSONFormatter
formatter = StructuredJSONFormatter(service_name="test-service")
@@ -31,7 +31,7 @@ class TestStructuredJSONFormatter:
assert "ts" in log_dict
assert log_dict["ts"].endswith("Z")
- def test_severity_mapping(self):
+ def test_severity_mapping(self) -> None:
from core.logging.structured_formatter import StructuredJSONFormatter
formatter = StructuredJSONFormatter()
@@ -58,7 +58,7 @@ class TestStructuredJSONFormatter:
log_dict = orjson.loads(output)
assert log_dict["severity"] == expected_severity, f"Level {level} should map to {expected_severity}"
- def test_error_with_stack_trace(self):
+ def test_error_with_stack_trace(self) -> None:
from core.logging.structured_formatter import StructuredJSONFormatter
formatter = StructuredJSONFormatter()
@@ -85,7 +85,7 @@ class TestStructuredJSONFormatter:
assert "stack_trace" in log_dict
assert "ValueError: Test error" in log_dict["stack_trace"]
- def test_no_stack_trace_for_info(self):
+ def test_no_stack_trace_for_info(self) -> None:
from core.logging.structured_formatter import StructuredJSONFormatter
formatter = StructuredJSONFormatter()
@@ -110,7 +110,7 @@ class TestStructuredJSONFormatter:
assert "stack_trace" not in log_dict
- def test_trace_context_included(self):
+ def test_trace_context_included(self) -> None:
from core.logging.structured_formatter import StructuredJSONFormatter
formatter = StructuredJSONFormatter()
@@ -132,7 +132,7 @@ class TestStructuredJSONFormatter:
assert log_dict["trace_id"] == "5b8aa5a2d2c872e8321cf37308d69df2"
assert log_dict["span_id"] == "051581bf3bb55c45"
- def test_identity_context_included(self):
+ def test_identity_context_included(self) -> None:
from core.logging.structured_formatter import StructuredJSONFormatter
formatter = StructuredJSONFormatter()
@@ -157,7 +157,7 @@ class TestStructuredJSONFormatter:
assert log_dict["identity"]["user_id"] == "u-admin-007"
assert log_dict["identity"]["user_type"] == "admin"
- def test_no_identity_when_empty(self):
+ def test_no_identity_when_empty(self) -> None:
from core.logging.structured_formatter import StructuredJSONFormatter
formatter = StructuredJSONFormatter()
@@ -176,7 +176,7 @@ class TestStructuredJSONFormatter:
assert "identity" not in log_dict
- def test_attributes_included(self):
+ def test_attributes_included(self) -> None:
from core.logging.structured_formatter import StructuredJSONFormatter
formatter = StructuredJSONFormatter()
@@ -197,7 +197,7 @@ class TestStructuredJSONFormatter:
assert log_dict["attributes"]["order_id"] == "ord-123"
assert log_dict["attributes"]["amount"] == 99.99
- def test_message_with_args(self):
+ def test_message_with_args(self) -> None:
from core.logging.structured_formatter import StructuredJSONFormatter
formatter = StructuredJSONFormatter()
@@ -216,7 +216,7 @@ class TestStructuredJSONFormatter:
assert log_dict["message"] == "User john logged in from 192.168.1.1"
- def test_timestamp_format(self):
+ def test_timestamp_format(self) -> None:
from core.logging.structured_formatter import StructuredJSONFormatter
formatter = StructuredJSONFormatter()
@@ -240,7 +240,7 @@ class TestStructuredJSONFormatter:
# Should have milliseconds
assert "." in ts
- def test_fallback_for_non_serializable_attributes(self):
+ def test_fallback_for_non_serializable_attributes(self) -> None:
from core.logging.structured_formatter import StructuredJSONFormatter
formatter = StructuredJSONFormatter()
diff --git a/api/tests/unit_tests/pyrefly.toml b/api/tests/unit_tests/pyrefly.toml
index 273425cf467..1e1ced9ef1d 100644
--- a/api/tests/unit_tests/pyrefly.toml
+++ b/api/tests/unit_tests/pyrefly.toml
@@ -374,9 +374,7 @@ project-excludes = [
"core/llm_generator/output_parser/test_structured_output.py",
"core/llm_generator/test_llm_generator.py",
"core/llm_generator/test_llm_generator_missing.py",
- "core/logging/test_context.py",
"core/logging/test_filters.py",
- "core/logging/test_structured_formatter.py",
"core/logging/test_trace_helpers.py",
"core/mcp/auth/test_auth_flow.py",
"core/mcp/client/test_session.py",