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 fields/factories test files (#44881)

Felix Shia7752e4c52d1
diff --git a/api/tests/unit_tests/factories/file_factory/test_builders.py b/api/tests/unit_tests/factories/file_factory/test_builders.py
index 7c5337b9a1a..b109de84951 100644
--- a/api/tests/unit_tests/factories/file_factory/test_builders.py
+++ b/api/tests/unit_tests/factories/file_factory/test_builders.py
@@ -1,13 +1,12 @@
import uuid
-from collections.abc import Callable, Iterator
+from collections.abc import Callable, Iterator, Sequence
from dataclasses import dataclass
from datetime import UTC, datetime
-from unittest.mock import patch
+from unittest.mock import MagicMock, patch
import pytest
from httpx import Response
-from sqlalchemy import Engine
-from sqlalchemy.orm import Session, sessionmaker
+from sqlalchemy.orm import Session
from core.app.entities.app_invoke_entities import InvokeFrom, UserFrom
from core.app.file_access import (
@@ -18,7 +17,7 @@ from core.app.file_access import (
)
from core.workflow.file_reference import build_file_reference, parse_file_reference, resolve_file_record_id
from extensions.storage.storage_type import StorageType
-from factories.file_factory.builders import _resolve_file_type
+from factories.file_factory.builders import FileMappingInput, _resolve_file_type
from factories.file_factory.builders import build_from_mapping as _build_from_mapping
from factories.file_factory.builders import build_from_mappings as _build_from_mappings
from graphon.file import File, FileTransferMethod, FileType, FileUploadConfig
@@ -51,7 +50,13 @@ TEST_CONFIG = FileUploadConfig(
)
-def build_from_mapping(*, mapping, tenant_id, config=None, strict_type_validation=False):
+def build_from_mapping(
+ *,
+ mapping: FileMappingInput,
+ tenant_id: str,
+ config: FileUploadConfig | None = None,
+ strict_type_validation: bool = False,
+) -> File:
return _build_from_mapping(
mapping=mapping,
tenant_id=tenant_id,
@@ -61,7 +66,13 @@ def build_from_mapping(*, mapping, tenant_id, config=None, strict_type_validatio
)
-def build_from_mappings(*, mappings, tenant_id, config=None, strict_type_validation=False):
+def build_from_mappings(
+ *,
+ mappings: Sequence[FileMappingInput],
+ tenant_id: str,
+ config: FileUploadConfig | None = None,
+ strict_type_validation: bool = False,
+) -> Sequence[File]:
return _build_from_mappings(
mappings=mappings,
tenant_id=tenant_id,
@@ -80,12 +91,8 @@ class FileRecords:
# Fixtures
@pytest.fixture(autouse=True)
-def file_records(monkeypatch: pytest.MonkeyPatch, sqlite_engine: Engine) -> Iterator[FileRecords]:
- """Persist authorized file rows and bind builder-owned sessions to SQLite."""
- UploadFile.metadata.create_all(sqlite_engine, tables=[UploadFile.__table__, ToolFile.__table__])
- sqlite_session_maker = sessionmaker(bind=sqlite_engine, expire_on_commit=False)
- monkeypatch.setattr("core.db.session_factory._session_maker", sqlite_session_maker)
-
+def file_records(sqlite_session: Session) -> FileRecords:
+ """Persist authorized file rows through the shared SQLite session."""
upload_file = UploadFile(
tenant_id=TEST_TENANT_ID,
storage_type=StorageType.LOCAL,
@@ -113,15 +120,14 @@ def file_records(monkeypatch: pytest.MonkeyPatch, sqlite_engine: Engine) -> Iter
)
tool_file.id = TEST_TOOL_FILE_ID
- with sqlite_session_maker() as session:
- session.add_all([upload_file, tool_file])
- session.commit()
- yield FileRecords(session=session, upload_file=upload_file, tool_file=tool_file)
+ sqlite_session.add_all([upload_file, tool_file])
+ sqlite_session.commit()
+ return FileRecords(session=sqlite_session, upload_file=upload_file, tool_file=tool_file)
@pytest.fixture
-def mock_http_head():
- def _mock_response(filename, size, content_type):
+def mock_http_head() -> Iterator[MagicMock]:
+ def _mock_response(filename: str, size: int, content_type: str) -> Response:
return Response(
status_code=200,
headers={
@@ -137,7 +143,7 @@ def mock_http_head():
# Helper functions
-def local_file_mapping(file_type="image"):
+def local_file_mapping(file_type: str = "image") -> dict[str, str]:
return {
"transfer_method": "local_file",
"upload_file_id": TEST_UPLOAD_FILE_ID,
@@ -145,7 +151,7 @@ def local_file_mapping(file_type="image"):
}
-def tool_file_mapping(file_type="document"):
+def tool_file_mapping(file_type: str = "document") -> dict[str, str]:
return {
"transfer_method": "tool_file",
"tool_file_id": TEST_TOOL_FILE_ID,
@@ -154,18 +160,20 @@ def tool_file_mapping(file_type="document"):
# Tests
-def test_build_from_mapping_backward_compatibility():
+def test_build_from_mapping_backward_compatibility() -> None:
mapping = local_file_mapping(file_type="image")
file = build_from_mapping(mapping=mapping, tenant_id=TEST_TENANT_ID)
assert isinstance(file, File)
assert file.transfer_method == FileTransferMethod.LOCAL_FILE
assert file.type == FileType.IMAGE
assert resolve_file_record_id(file.reference) == TEST_UPLOAD_FILE_ID
- assert parse_file_reference(file.reference).storage_key is None
+ reference = parse_file_reference(file.reference)
+ assert reference is not None
+ assert reference.storage_key is None
assert file.storage_key == "test_key"
-def test_build_from_mapping_accepts_opaque_reference_for_local_file():
+def test_build_from_mapping_accepts_opaque_reference_for_local_file() -> None:
mapping = {
"transfer_method": "local_file",
"reference": build_file_reference(record_id=TEST_UPLOAD_FILE_ID, storage_key="test_key"),
@@ -180,7 +188,7 @@ def test_build_from_mapping_accepts_opaque_reference_for_local_file():
assert resolve_file_record_id(file.reference) == TEST_UPLOAD_FILE_ID
-def test_build_from_mapping_accepts_opaque_related_id_for_tool_file():
+def test_build_from_mapping_accepts_opaque_related_id_for_tool_file() -> None:
mapping = {
"transfer_method": "tool_file",
"related_id": build_file_reference(record_id=TEST_TOOL_FILE_ID, storage_key="tool_file.pdf"),
@@ -193,11 +201,13 @@ def test_build_from_mapping_accepts_opaque_related_id_for_tool_file():
assert file.transfer_method == FileTransferMethod.TOOL_FILE
assert file.type == FileType.DOCUMENT
assert resolve_file_record_id(file.reference) == TEST_TOOL_FILE_ID
- assert parse_file_reference(file.reference).storage_key is None
+ tool_reference = parse_file_reference(file.reference)
+ assert tool_reference is not None
+ assert tool_reference.storage_key is None
assert file.storage_key == "tool_file.pdf"
-def test_build_from_mapping_prefers_tool_filename_extension_over_mimetype(file_records: FileRecords):
+def test_build_from_mapping_prefers_tool_filename_extension_over_mimetype(file_records: FileRecords) -> None:
file_records.tool_file.name = "report.docx"
file_records.tool_file.file_key = "tools/test_tenant_id/file.bin"
file_records.tool_file.mimetype = "application/octet-stream"
@@ -219,7 +229,7 @@ def test_build_from_mapping_prefers_tool_filename_extension_over_mimetype(file_r
("document", False, "Detected file type does not match"),
],
)
-def test_build_from_local_file_strict_validation(file_type, should_pass, expected_error):
+def test_build_from_local_file_strict_validation(file_type: str, should_pass: bool, expected_error: str) -> None:
mapping = local_file_mapping(file_type=file_type)
if should_pass:
file = build_from_mapping(mapping=mapping, tenant_id=TEST_TENANT_ID, strict_type_validation=True)
@@ -236,7 +246,7 @@ def test_build_from_local_file_strict_validation(file_type, should_pass, expecte
("image", False, "Detected file type does not match"),
],
)
-def test_build_from_tool_file_strict_validation(file_type, should_pass, expected_error):
+def test_build_from_tool_file_strict_validation(file_type: str, should_pass: bool, expected_error: str) -> None:
"""Strict type validation for tool_file."""
mapping = tool_file_mapping(file_type=file_type)
if should_pass:
@@ -247,7 +257,7 @@ def test_build_from_tool_file_strict_validation(file_type, should_pass, expected
build_from_mapping(mapping=mapping, tenant_id=TEST_TENANT_ID, strict_type_validation=True)
-def test_build_from_remote_url(mock_http_head):
+def test_build_from_remote_url(mock_http_head: MagicMock) -> None:
mapping = {
"transfer_method": "remote_url",
"url": TEST_REMOTE_URL,
@@ -260,7 +270,7 @@ def test_build_from_remote_url(mock_http_head):
assert file.size == 2048
-def test_build_from_mappings_accepts_remote_url_with_upload_file_id(monkeypatch: pytest.MonkeyPatch):
+def test_build_from_mappings_accepts_remote_url_with_upload_file_id(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(
"factories.file_factory.builders.helpers.get_signed_file_url",
lambda **_kwargs: "https://example.com/signed-upload-file",
@@ -279,7 +289,7 @@ def test_build_from_mappings_accepts_remote_url_with_upload_file_id(monkeypatch:
assert files[0].storage_key == "test_key"
-def test_build_from_remote_url_prefers_filename_extension_over_mimetype():
+def test_build_from_remote_url_prefers_filename_extension_over_mimetype() -> None:
mapping = {
"transfer_method": "remote_url",
"url": TEST_REMOTE_URL,
@@ -306,7 +316,9 @@ def test_build_from_remote_url_prefers_filename_extension_over_mimetype():
("video", False, "Detected file type does not match the specified type"),
],
)
-def test_build_from_remote_url_strict_validation(mock_http_head, file_type, should_pass, expected_error):
+def test_build_from_remote_url_strict_validation(
+ mock_http_head: MagicMock, file_type: str, should_pass: bool, expected_error: str
+) -> None:
"""Test strict type validation for remote_url."""
mapping = {
"transfer_method": "remote_url",
@@ -321,7 +333,7 @@ def test_build_from_remote_url_strict_validation(mock_http_head, file_type, shou
build_from_mapping(mapping=mapping, tenant_id=TEST_TENANT_ID, strict_type_validation=True)
-def test_build_from_remote_url_without_strict_validation(mock_http_head):
+def test_build_from_remote_url_without_strict_validation(mock_http_head: MagicMock) -> None:
"""Test that remote_url allows type mismatch when strict_type_validation is False."""
mapping = {
"transfer_method": "remote_url",
@@ -334,7 +346,7 @@ def test_build_from_remote_url_without_strict_validation(mock_http_head):
assert file.filename == "remote_test.jpg"
-def test_tool_file_not_found(file_records: FileRecords):
+def test_tool_file_not_found(file_records: FileRecords) -> None:
"""Test ToolFile not found in database."""
file_records.session.delete(file_records.tool_file)
file_records.session.commit()
@@ -344,7 +356,7 @@ def test_tool_file_not_found(file_records: FileRecords):
build_from_mapping(mapping=mapping, tenant_id=TEST_TENANT_ID)
-def test_local_file_not_found(file_records: FileRecords):
+def test_local_file_not_found(file_records: FileRecords) -> None:
"""Test UploadFile not found in database."""
file_records.session.delete(file_records.upload_file)
file_records.session.commit()
@@ -354,7 +366,7 @@ def test_local_file_not_found(file_records: FileRecords):
build_from_mapping(mapping=mapping, tenant_id=TEST_TENANT_ID)
-def test_build_without_type_specification():
+def test_build_without_type_specification() -> None:
"""Test the situation where no file type is specified"""
mapping = {
"transfer_method": "local_file",
@@ -373,7 +385,7 @@ def test_build_without_type_specification():
("video", False, "File validation failed"),
],
)
-def test_file_validation_with_config(file_type, should_pass, expected_error):
+def test_file_validation_with_config(file_type: str, should_pass: bool, expected_error: str) -> None:
"""Test the validation of files and configurations"""
mapping = local_file_mapping(file_type=file_type)
if should_pass:
@@ -384,7 +396,7 @@ def test_file_validation_with_config(file_type, should_pass, expected_error):
build_from_mapping(mapping=mapping, tenant_id=TEST_TENANT_ID, config=TEST_CONFIG)
-def test_invalid_transfer_method():
+def test_invalid_transfer_method() -> None:
"""Test that invalid transfer method raises ValueError."""
mapping = {
"transfer_method": "invalid_method",
@@ -395,7 +407,7 @@ def test_invalid_transfer_method():
build_from_mapping(mapping=mapping, tenant_id=TEST_TENANT_ID)
-def test_invalid_uuid_format():
+def test_invalid_uuid_format() -> None:
"""Test that invalid UUID format raises ValueError."""
mapping = {
"transfer_method": "local_file",
@@ -406,7 +418,7 @@ def test_invalid_uuid_format():
build_from_mapping(mapping=mapping, tenant_id=TEST_TENANT_ID)
-def test_tenant_mismatch(file_records: FileRecords):
+def test_tenant_mismatch(file_records: FileRecords) -> None:
"""Test that tenant mismatch raises security error."""
file_records.upload_file.tenant_id = "different_tenant_id"
file_records.session.commit()
@@ -416,7 +428,7 @@ def test_tenant_mismatch(file_records: FileRecords):
build_from_mapping(mapping=mapping, tenant_id=TEST_TENANT_ID)
-def test_build_from_mapping_scopes_upload_file_to_end_user():
+def test_build_from_mapping_scopes_upload_file_to_end_user() -> None:
scope = FileAccessScope(
tenant_id=TEST_TENANT_ID,
user_id=TEST_END_USER_ID,
@@ -440,7 +452,7 @@ def test_build_from_mapping_scopes_upload_file_to_end_user():
build_from_mapping(mapping=local_file_mapping(), tenant_id=TEST_TENANT_ID)
-def test_build_from_mapping_scopes_tool_file_to_end_user():
+def test_build_from_mapping_scopes_tool_file_to_end_user() -> None:
scope = FileAccessScope(
tenant_id=TEST_TENANT_ID,
user_id=TEST_END_USER_ID,
@@ -464,7 +476,7 @@ def test_build_from_mapping_scopes_tool_file_to_end_user():
build_from_mapping(mapping=tool_file_mapping(), tenant_id=TEST_TENANT_ID)
-def test_build_from_mapping_allows_granted_tool_file_for_other_end_user():
+def test_build_from_mapping_allows_granted_tool_file_for_other_end_user() -> None:
"""Tool files produced in this run must remain readable by later nodes (#41169).
Workflow-as-tool from an agent binds an EndUser scope. Plugin downloads often
@@ -484,7 +496,7 @@ def test_build_from_mapping_allows_granted_tool_file_for_other_end_user():
assert file.type == FileType.DOCUMENT
-def test_disallowed_file_types():
+def test_disallowed_file_types() -> None:
"""Test that disallowed file types are rejected."""
# Config that only allows image and document types
restricted_config = FileUploadConfig(
@@ -497,7 +509,7 @@ def test_disallowed_file_types():
build_from_mapping(mapping=mapping, tenant_id=TEST_TENANT_ID, config=restricted_config)
-def test_disallowed_extensions(file_records: FileRecords):
+def test_disallowed_extensions(file_records: FileRecords) -> None:
"""Test that disallowed file extensions are rejected for custom type."""
file_records.upload_file.extension = "exe"
file_records.upload_file.name = "malicious.exe"
@@ -520,7 +532,7 @@ def test_disallowed_extensions(file_records: FileRecords):
build_from_mapping(mapping=mapping, tenant_id=TEST_TENANT_ID, config=restricted_config)
-def test_custom_file_type_uses_extension_validation_under_strict_mode(file_records: FileRecords):
+def test_custom_file_type_uses_extension_validation_under_strict_mode(file_records: FileRecords) -> None:
"""Custom form uploads are classified by the configured extension list.
Strict MIME matching is skipped for the custom bucket, but the File still
@@ -570,7 +582,7 @@ def test_custom_bucket_preserves_detected_type_for_llm(
mime_type: str,
expected_type: FileType,
strict_type_validation: bool,
-):
+) -> None:
"""Other File Types is an upload bucket, not a prompt-content type.
Graphon LLM nodes skip ``FileType.CUSTOM``, so known files accepted through
@@ -608,7 +620,7 @@ def test_custom_bucket_rejects_disallowed_extension_for_detected_document(
file_records: FileRecords,
strict_type_validation: bool,
allow_documents: bool,
-):
+) -> None:
"""Detecting a document must not bypass the submitted custom bucket's allowlist."""
file_records.upload_file.extension = "pdf"
file_records.upload_file.name = "report.pdf"
@@ -646,7 +658,7 @@ def test_resolve_file_type_custom_bucket_keeps_detected_type(
specified: str | None,
strict: bool,
expected: FileType,
-):
+) -> None:
assert (
_resolve_file_type(
detected_file_type=detected,
@@ -657,7 +669,7 @@ def test_resolve_file_type_custom_bucket_keeps_detected_type(
)
-def test_custom_bucket_document_is_attached_to_llm_prompt(file_records: FileRecords):
+def test_custom_bucket_document_is_attached_to_llm_prompt(file_records: FileRecords) -> None:
"""A Start-node Other File Types PDF must reach LLM prompt conversion (#41236)."""
file_records.upload_file.extension = "pdf"
file_records.upload_file.name = "report.pdf"
@@ -714,7 +726,7 @@ def test_custom_bucket_document_is_attached_to_llm_prompt(file_records: FileReco
def test_custom_bucket_pdf_converts_to_document_prompt_content(
file_records: FileRecords, config_overrides: Callable[..., None]
-):
+) -> None:
"""Detected type must produce document prompt content, not an unsupported placeholder."""
file_records.upload_file.extension = "pdf"
file_records.upload_file.name = "report.pdf"
diff --git a/api/tests/unit_tests/factories/file_factory/test_remote.py b/api/tests/unit_tests/factories/file_factory/test_remote.py
index 7efea00c0ba..23bc3c3dab5 100644
--- a/api/tests/unit_tests/factories/file_factory/test_remote.py
+++ b/api/tests/unit_tests/factories/file_factory/test_remote.py
@@ -11,7 +11,7 @@ from sqlalchemy.orm import Session, sessionmaker
from extensions.storage.storage_type import StorageType
from factories.file_factory import builders
from factories.file_factory.remote import extract_filename, get_remote_file_info
-from graphon.file import FileTransferMethod
+from graphon.file import FileTransferMethod, FileType
from models import UploadFile
from models.enums import CreatorUserRole
@@ -22,8 +22,8 @@ class _FakeResponse:
headers: dict[str, str]
-def _mock_head(monkeypatch: pytest.MonkeyPatch, headers: dict[str, str], status_code: int = 200):
- def _fake_head(method: str, url: str, follow_redirects: bool = True):
+def _mock_head(monkeypatch: pytest.MonkeyPatch, headers: dict[str, str], status_code: int = 200) -> None:
+ def _fake_head(method: str, url: str, follow_redirects: bool = True) -> _FakeResponse:
assert method == "HEAD"
return _FakeResponse(status_code=status_code, headers=headers)
@@ -33,7 +33,7 @@ def _mock_head(monkeypatch: pytest.MonkeyPatch, headers: dict[str, str], status_
class TestGetRemoteFileInfo:
"""Tests for get_remote_file_info focusing on filename extraction rules."""
- def test_inline_no_filename(self, monkeypatch: pytest.MonkeyPatch):
+ def test_inline_no_filename(self, monkeypatch: pytest.MonkeyPatch) -> None:
_mock_head(
monkeypatch,
{
@@ -47,7 +47,7 @@ class TestGetRemoteFileInfo:
assert mime_type == "application/pdf"
assert size == 123
- def test_attachment_no_filename(self, monkeypatch: pytest.MonkeyPatch):
+ def test_attachment_no_filename(self, monkeypatch: pytest.MonkeyPatch) -> None:
_mock_head(
monkeypatch,
{
@@ -61,7 +61,7 @@ class TestGetRemoteFileInfo:
assert mime_type == "application/octet-stream"
assert size == 456
- def test_attachment_quoted_space_filename(self, monkeypatch: pytest.MonkeyPatch):
+ def test_attachment_quoted_space_filename(self, monkeypatch: pytest.MonkeyPatch) -> None:
_mock_head(
monkeypatch,
{
@@ -75,7 +75,7 @@ class TestGetRemoteFileInfo:
assert mime_type == "image/jpeg"
assert size == 789
- def test_attachment_filename_star_percent20(self, monkeypatch: pytest.MonkeyPatch):
+ def test_attachment_filename_star_percent20(self, monkeypatch: pytest.MonkeyPatch) -> None:
_mock_head(
monkeypatch,
{
@@ -87,7 +87,7 @@ class TestGetRemoteFileInfo:
assert filename == "file name.jpg"
assert mime_type == "image/jpeg"
- def test_attachment_filename_star_chinese(self, monkeypatch: pytest.MonkeyPatch):
+ def test_attachment_filename_star_chinese(self, monkeypatch: pytest.MonkeyPatch) -> None:
_mock_head(
monkeypatch,
{
@@ -99,7 +99,7 @@ class TestGetRemoteFileInfo:
assert filename == "测试文件.jpg"
assert mime_type == "image/jpeg"
- def test_filename_from_url_when_no_header(self, monkeypatch: pytest.MonkeyPatch):
+ def test_filename_from_url_when_no_header(self, monkeypatch: pytest.MonkeyPatch) -> None:
_mock_head(
monkeypatch,
{
@@ -113,7 +113,7 @@ class TestGetRemoteFileInfo:
assert mime_type == "text/plain"
assert size == 12
- def test_no_filename_in_url_or_header_generates_uuid_bin(self, monkeypatch: pytest.MonkeyPatch):
+ def test_no_filename_in_url_or_header_generates_uuid_bin(self, monkeypatch: pytest.MonkeyPatch) -> None:
_mock_head(
monkeypatch,
{
@@ -130,95 +130,95 @@ class TestGetRemoteFileInfo:
class TestExtractFilename:
"""Tests for extract_filename focusing on RFC5987 parsing and security."""
- def test_no_content_disposition_uses_url_basename(self):
+ def test_no_content_disposition_uses_url_basename(self) -> None:
"""Test that URL basename is used when no Content-Disposition header."""
result = extract_filename("http://example.com/path/file.txt", None)
assert result == "file.txt"
- def test_no_content_disposition_with_percent_encoded_url(self):
+ def test_no_content_disposition_with_percent_encoded_url(self) -> None:
"""Test that percent-encoded URL basename is decoded."""
result = extract_filename("http://example.com/path/file%20name.txt", None)
assert result == "file name.txt"
- def test_no_content_disposition_empty_url_path(self):
+ def test_no_content_disposition_empty_url_path(self) -> None:
"""Test that empty URL path returns None."""
result = extract_filename("http://example.com/", None)
assert result is None
- def test_simple_filename_header(self):
+ def test_simple_filename_header(self) -> None:
"""Test basic filename extraction from Content-Disposition."""
result = extract_filename("http://example.com/", 'attachment; filename="test.txt"')
assert result == "test.txt"
- def test_quoted_filename_with_spaces(self):
+ def test_quoted_filename_with_spaces(self) -> None:
"""Test filename with spaces in quotes."""
result = extract_filename("http://example.com/", 'attachment; filename="my file.txt"')
assert result == "my file.txt"
- def test_unquoted_filename(self):
+ def test_unquoted_filename(self) -> None:
"""Test unquoted filename."""
result = extract_filename("http://example.com/", "attachment; filename=test.txt")
assert result == "test.txt"
- def test_percent_encoded_filename(self):
+ def test_percent_encoded_filename(self) -> None:
"""Test percent-encoded filename."""
result = extract_filename("http://example.com/", 'attachment; filename="file%20name.txt"')
assert result == "file name.txt"
- def test_rfc5987_filename_star_utf8(self):
+ def test_rfc5987_filename_star_utf8(self) -> None:
"""Test RFC5987 filename* with UTF-8 encoding."""
result = extract_filename("http://example.com/", "attachment; filename*=UTF-8''file%20name.txt")
assert result == "file name.txt"
- def test_rfc5987_filename_star_chinese(self):
+ def test_rfc5987_filename_star_chinese(self) -> None:
"""Test RFC5987 filename* with Chinese characters."""
result = extract_filename(
"http://example.com/", "attachment; filename*=UTF-8''%E6%B5%8B%E8%AF%95%E6%96%87%E4%BB%B6.txt"
)
assert result == "测试文件.txt"
- def test_rfc5987_filename_star_with_language(self):
+ def test_rfc5987_filename_star_with_language(self) -> None:
"""Test RFC5987 filename* with language tag."""
result = extract_filename("http://example.com/", "attachment; filename*=UTF-8'en'file%20name.txt")
assert result == "file name.txt"
- def test_rfc5987_filename_star_fallback_charset(self):
+ def test_rfc5987_filename_star_fallback_charset(self) -> None:
"""Test RFC5987 filename* with fallback charset."""
result = extract_filename("http://example.com/", "attachment; filename*=''file%20name.txt")
assert result == "file name.txt"
- def test_rfc5987_filename_star_malformed_fallback(self):
+ def test_rfc5987_filename_star_malformed_fallback(self) -> None:
"""Test RFC5987 filename* with malformed format falls back to simple unquote."""
result = extract_filename("http://example.com/", "attachment; filename*=malformed%20filename.txt")
assert result == "malformed filename.txt"
- def test_filename_star_takes_precedence_over_filename(self):
+ def test_filename_star_takes_precedence_over_filename(self) -> None:
"""Test that filename* takes precedence over filename."""
test_string = 'attachment; filename="old.txt"; filename*=UTF-8\'\'new.txt"'
result = extract_filename("http://example.com/", test_string)
assert result == "new.txt"
- def test_path_injection_protection(self):
+ def test_path_injection_protection(self) -> None:
"""Test that path injection attempts are blocked by os.path.basename."""
result = extract_filename("http://example.com/", 'attachment; filename="../../../etc/passwd"')
assert result == "passwd"
- def test_path_injection_protection_rfc5987(self):
+ def test_path_injection_protection_rfc5987(self) -> None:
"""Test that path injection attempts in RFC5987 are blocked."""
result = extract_filename("http://example.com/", "attachment; filename*=UTF-8''..%2F..%2F..%2Fetc%2Fpasswd")
assert result == "passwd"
- def test_empty_filename_returns_none(self):
+ def test_empty_filename_returns_none(self) -> None:
"""Test that empty filename returns None."""
result = extract_filename("http://example.com/", 'attachment; filename=""')
assert result is None
- def test_whitespace_only_filename_returns_none(self):
+ def test_whitespace_only_filename_returns_none(self) -> None:
"""Test that whitespace-only filename returns None."""
result = extract_filename("http://example.com/", 'attachment; filename=" "')
assert result is None
- def test_complex_rfc5987_encoding(self):
+ def test_complex_rfc5987_encoding(self) -> None:
"""Test complex RFC5987 encoding with special characters."""
result = extract_filename(
"http://example.com/",
@@ -226,39 +226,39 @@ class TestExtractFilename:
)
assert result == "中文文件 (副本).pdf"
- def test_iso8859_1_encoding(self):
+ def test_iso8859_1_encoding(self) -> None:
"""Test ISO-8859-1 encoding in RFC5987."""
result = extract_filename("http://example.com/", "attachment; filename*=ISO-8859-1''file%20name.txt")
assert result == "file name.txt"
- def test_encoding_error_fallback(self):
+ def test_encoding_error_fallback(self) -> None:
"""Test that encoding errors fall back to safe ASCII filename."""
result = extract_filename("http://example.com/", "attachment; filename*=INVALID-CHARSET''file%20name.txt")
assert result == "file name.txt"
- def test_mixed_quotes_and_encoding(self):
+ def test_mixed_quotes_and_encoding(self) -> None:
"""Test filename with mixed quotes and percent encoding."""
result = extract_filename(
"http://example.com/", 'attachment; filename="file%20with%20quotes%20%26%20encoding.txt"'
)
assert result == "file with quotes & encoding.txt"
- def test_url_with_query_string(self):
+ def test_url_with_query_string(self) -> None:
"""Test that query strings are stripped from URL basename."""
result = extract_filename("http://example.com/path/file.txt?signature=abc123&expires=12345", None)
assert result == "file.txt"
- def test_url_with_hash_fragment(self):
+ def test_url_with_hash_fragment(self) -> None:
"""Test that hash fragments are stripped from URL basename."""
result = extract_filename("http://example.com/path/file.txt#section", None)
assert result == "file.txt"
- def test_url_with_query_and_fragment(self):
+ def test_url_with_query_and_fragment(self) -> None:
"""Test that both query strings and hash fragments are stripped."""
result = extract_filename("http://example.com/path/file.txt?token=xyz#section", None)
assert result == "file.txt"
- def test_signed_url_preserves_filename(self):
+ def test_signed_url_preserves_filename(self) -> None:
"""Test that signed URL parameters don't affect filename extraction."""
result = extract_filename(
"http://storage.example.com/bucket/documents/report.pdf?AWSAccessKeyId=xxx&Signature=yyy&Expires=12345",
@@ -266,27 +266,27 @@ class TestExtractFilename:
)
assert result == "report.pdf"
- def test_percent_encoded_filename_with_query_string(self):
+ def test_percent_encoded_filename_with_query_string(self) -> None:
"""Test percent-encoded filename with query string is decoded correctly."""
result = extract_filename("http://example.com/path/my%20file.txt?download=true", None)
assert result == "my file.txt"
- def test_percent_encoded_filename_with_fragment(self):
+ def test_percent_encoded_filename_with_fragment(self) -> None:
"""Test percent-encoded filename with fragment is decoded correctly."""
result = extract_filename("http://example.com/path/my%20file.txt#page=1", None)
assert result == "my file.txt"
- def test_complex_percent_encoding_with_query(self):
+ def test_complex_percent_encoding_with_query(self) -> None:
"""Test complex percent-encoded filename with query parameters."""
result = extract_filename("http://example.com/docs/%E4%B8%AD%E6%96%87%E6%96%87%E4%BB%B6.pdf?v=1", None)
assert result == "中文文件.pdf"
- def test_url_with_special_chars_in_query(self):
+ def test_url_with_special_chars_in_query(self) -> None:
"""Test that special characters in query string don't affect filename."""
result = extract_filename("http://example.com/file.bin?name=test&path=/some/path", None)
assert result == "file.bin"
- def test_malformed_percent_encoding_safe_fallback(self):
+ def test_malformed_percent_encoding_safe_fallback(self) -> None:
"""Test that malformed percent-encoding is handled safely."""
result = extract_filename("http://example.com/path/file%20name%GG.txt?x=1", None)
# %GG is invalid, should be replaced with replacement character
@@ -295,12 +295,12 @@ class TestExtractFilename:
assert "file" in result
assert ".txt" in result
- def test_empty_path_with_query_returns_none(self):
+ def test_empty_path_with_query_returns_none(self) -> None:
"""Test that empty path with query string returns None."""
result = extract_filename("http://example.com/?query=value", None)
assert result is None
- def test_path_only_with_query_string(self):
+ def test_path_only_with_query_string(self) -> None:
"""Test bare path (not full URL) with query string."""
result = extract_filename("/path/to/file.txt?extra=params", None)
assert result == "file.txt"
@@ -335,7 +335,7 @@ class TestBuildFromDatasourceFile:
def test_extension_passed_without_doubled_dot(
self, monkeypatch: pytest.MonkeyPatch, sqlite_engine: Engine, sqlite_session: Session
- ):
+ ) -> None:
"""Regression: standardize_file_type must receive the extension exactly once-prefixed.
Previously the call was ``standardize_file_type(extension="." + extension, ...)`` while
@@ -343,11 +343,9 @@ class TestBuildFromDatasourceFile:
``lstrip(".")`` inside ``standardize_file_type`` masked the bug from end users, but the
argument shape itself was wrong and showed up in any caller that didn't strip dots.
"""
- captured: dict = {}
-
- def fake_standardize(*, extension: str = "", mime_type: str = ""):
- from graphon.file import FileType
+ captured: dict[str, str] = {}
+ def fake_standardize(*, extension: str = "", mime_type: str = "") -> FileType:
captured["extension"] = extension
captured["mime_type"] = mime_type
return FileType.DOCUMENT
@@ -378,12 +376,10 @@ class TestBuildFromDatasourceFile:
def test_extension_falls_back_to_bin_when_key_has_no_dot(
self, monkeypatch: pytest.MonkeyPatch, sqlite_engine: Engine, sqlite_session: Session
- ):
- captured: dict = {}
-
- def fake_standardize(*, extension: str = "", mime_type: str = ""):
- from graphon.file import FileType
+ ) -> None:
+ captured: dict[str, str] = {}
+ def fake_standardize(*, extension: str = "", mime_type: str = "") -> FileType:
captured["extension"] = extension
return FileType.CUSTOM
@@ -410,7 +406,7 @@ class TestBuildFromDatasourceFile:
def test_datasource_file_is_scoped_to_tenant(
self, monkeypatch: pytest.MonkeyPatch, sqlite_engine: Engine, sqlite_session: Session
- ):
+ ) -> None:
datasource_file = self._make_datasource_file(key="folder/data.csv")
sqlite_session.add(datasource_file)
sqlite_session.commit()
diff --git a/api/tests/unit_tests/factories/file_factory/test_validation.py b/api/tests/unit_tests/factories/file_factory/test_validation.py
index 61337fcf108..9466daf703e 100644
--- a/api/tests/unit_tests/factories/file_factory/test_validation.py
+++ b/api/tests/unit_tests/factories/file_factory/test_validation.py
@@ -41,7 +41,13 @@ def _validate(
("video", ".mp4", [], [], True),
],
)
-def test_bucket_semantics(input_file_type, file_extension, allowed_file_types, allowed_file_extensions, expected):
+def test_bucket_semantics(
+ input_file_type: str,
+ file_extension: str,
+ allowed_file_types: list[FileType],
+ allowed_file_extensions: list[str],
+ expected: bool,
+) -> None:
config = FileUploadConfig(
allowed_file_types=allowed_file_types,
allowed_file_extensions=allowed_file_extensions,
@@ -50,7 +56,7 @@ def test_bucket_semantics(input_file_type, file_extension, allowed_file_types, a
@pytest.mark.parametrize("whitelist_entry", [".png", ".PNG", "png", "PNG", " .Png ", "PnG"])
-def test_extension_match_is_case_and_dot_insensitive(whitelist_entry):
+def test_extension_match_is_case_and_dot_insensitive(whitelist_entry: str) -> None:
config = FileUploadConfig(
allowed_file_types=[FileType.CUSTOM],
allowed_file_extensions=[whitelist_entry],
@@ -58,7 +64,7 @@ def test_extension_match_is_case_and_dot_insensitive(whitelist_entry):
assert _validate(input_file_type="custom", file_extension=".png", config=config) is True
-def test_extension_mismatch_still_rejected_after_normalization():
+def test_extension_mismatch_still_rejected_after_normalization() -> None:
config = FileUploadConfig(
allowed_file_types=[FileType.CUSTOM],
allowed_file_extensions=[".png", ".jpg"],
@@ -66,7 +72,7 @@ def test_extension_mismatch_still_rejected_after_normalization():
assert _validate(input_file_type="custom", file_extension=".pdf", config=config) is False
-def test_mixed_case_whitelist_replicating_real_user_config():
+def test_mixed_case_whitelist_replicating_real_user_config() -> None:
config = FileUploadConfig(
allowed_file_types=[FileType.CUSTOM],
allowed_file_extensions=[".PNG", "png", "JPG", ".WEBP", "SVG", "GIF"],
@@ -75,7 +81,7 @@ def test_mixed_case_whitelist_replicating_real_user_config():
assert _validate(input_file_type="custom", file_extension=ext, config=config) is True
-def test_tool_file_always_passes():
+def test_tool_file_always_passes() -> None:
config = FileUploadConfig(allowed_file_types=[FileType.CUSTOM], allowed_file_extensions=[".pdf"])
assert (
_validate(
@@ -88,7 +94,7 @@ def test_tool_file_always_passes():
)
-def test_transfer_method_gate_for_non_image():
+def test_transfer_method_gate_for_non_image() -> None:
config = FileUploadConfig(
allowed_file_types=[FileType.DOCUMENT],
allowed_file_upload_methods=[FileTransferMethod.LOCAL_FILE],
@@ -113,7 +119,7 @@ def test_transfer_method_gate_for_non_image():
)
-def test_history_replay_matches_round_1_outcome_under_unchanged_config():
+def test_history_replay_matches_round_1_outcome_under_unchanged_config() -> None:
"""A file that passes round 1 must pass history replay when config is unchanged."""
config = FileUploadConfig(
allowed_file_types=[FileType.CUSTOM],
@@ -123,7 +129,7 @@ def test_history_replay_matches_round_1_outcome_under_unchanged_config():
assert _validate(input_file_type="image", file_extension=".png", config=config) is True
-def test_empty_whitelist_in_custom_bucket_denies_by_default():
+def test_empty_whitelist_in_custom_bucket_denies_by_default() -> None:
"""Defensive: when a file lands in the CUSTOM bucket, an empty
allowed_file_extensions list rejects. The UI never submits empty;
this guards DSL / API paths that bypass the UI from accidentally
@@ -136,7 +142,7 @@ def test_empty_whitelist_in_custom_bucket_denies_by_default():
assert _validate(input_file_type="image", file_extension=".png", config=config) is False
-def test_normalize_handles_whitespace_and_empty_consistently():
+def test_normalize_handles_whitespace_and_empty_consistently() -> None:
"""Whitespace-only or empty entries in the whitelist must not match real
extensions (regression guard for _normalize_extension edge cases)."""
for noisy_entry in ("", " ", "\t"):
@@ -147,7 +153,7 @@ def test_normalize_handles_whitespace_and_empty_consistently():
assert _validate(input_file_type="custom", file_extension=".png", config=config) is False
-def test_empty_extension_does_not_spuriously_match_empty_whitelist_entry():
+def test_empty_extension_does_not_spuriously_match_empty_whitelist_entry() -> None:
"""Defensive: even if the whitelist contains an empty / whitespace entry
(e.g., a stray comma in DSL), an extensionless file must not pass via
a both-sides-empty match. Real entries in the same whitelist still match."""
diff --git a/api/tests/unit_tests/fields/test_dataset_fields.py b/api/tests/unit_tests/fields/test_dataset_fields.py
index 9d8c868e1a7..ef318fec568 100644
--- a/api/tests/unit_tests/fields/test_dataset_fields.py
+++ b/api/tests/unit_tests/fields/test_dataset_fields.py
@@ -19,13 +19,15 @@ from models.dataset import (
ExternalKnowledgeBindings,
Pipeline,
)
-from models.enums import DatasetMetadataType, DataSourceType, DocumentCreatedFrom, TagType
+from models.enums import DatasetMetadataType, DataSourceType, DocumentCreatedFrom, IndexingStatus, TagType
from models.model import App, AppMode, IconType, Tag, TagBinding
from services.knowledge.dataset_read_service import load_dataset_detail, load_dataset_details
-def _dataset_detail_payload(**overrides):
- payload = {
+def _dataset_detail_payload(**overrides: object) -> dict[str, object]:
+ tags: list[object] = []
+ doc_metadata: list[object] = []
+ payload: dict[str, object] = {
"id": "ds-1",
"name": "Dataset",
"description": "desc",
@@ -71,7 +73,7 @@ def _dataset_detail_payload(**overrides):
"model_provider_name": None,
"summary_prompt": None,
},
- "tags": [],
+ "tags": tags,
"doc_form": None,
"external_knowledge_info": {
"external_knowledge_id": "knowledge-id",
@@ -80,7 +82,7 @@ def _dataset_detail_payload(**overrides):
"external_knowledge_api_endpoint": "https://example.com",
},
"external_retrieval_model": None,
- "doc_metadata": [],
+ "doc_metadata": doc_metadata,
"built_in_field_enabled": False,
"pipeline_id": None,
"runtime_mode": "general",
@@ -101,11 +103,11 @@ def _dataset_detail_payload(**overrides):
return payload
-def _dump_dataset_detail(payload):
+def _dump_dataset_detail(payload: dict[str, object]) -> dict[str, object]:
return DatasetDetailResponse.model_validate(payload).model_dump(mode="json")
-def test_dataset_detail_preserves_permission_keys():
+def test_dataset_detail_preserves_permission_keys() -> None:
response = _dump_dataset_detail(
_dataset_detail_payload(permission_keys=["dataset.acl.readonly", "dataset.acl.edit"])
)
@@ -113,7 +115,7 @@ def test_dataset_detail_preserves_permission_keys():
assert response["permission_keys"] == ["dataset.acl.readonly", "dataset.acl.edit"]
-def test_dataset_detail_expands_legacy_null_nested_fields():
+def test_dataset_detail_expands_legacy_null_nested_fields() -> None:
response = _dump_dataset_detail(
_dataset_detail_payload(
summary_index_setting=None,
@@ -143,7 +145,7 @@ def test_dataset_detail_expands_legacy_null_nested_fields():
assert response["external_retrieval_model"] is None
-def test_dataset_detail_expands_legacy_null_retrieval_nested_fields():
+def test_dataset_detail_expands_legacy_null_retrieval_nested_fields() -> None:
response = _dump_dataset_detail(
_dataset_detail_payload(
retrieval_model_dict={
@@ -162,11 +164,15 @@ def test_dataset_detail_expands_legacy_null_retrieval_nested_fields():
)
)
- assert response["retrieval_model_dict"]["reranking_model"] == {
+ retrieval_model_dict = response["retrieval_model_dict"]
+ assert isinstance(retrieval_model_dict, dict)
+
+ assert retrieval_model_dict["reranking_model"] == {
"reranking_provider_name": None,
"reranking_model_name": None,
}
- assert response["retrieval_model_dict"]["weights"] == {
+
+ assert retrieval_model_dict["weights"] == {
"weight_type": None,
"keyword_setting": {"keyword_weight": None},
"vector_setting": {
@@ -177,7 +183,7 @@ def test_dataset_detail_expands_legacy_null_retrieval_nested_fields():
}
-def test_dataset_detail_expands_missing_weighted_score_nested_fields():
+def test_dataset_detail_expands_missing_weighted_score_nested_fields() -> None:
response = _dump_dataset_detail(
_dataset_detail_payload(
retrieval_model_dict={
@@ -193,7 +199,10 @@ def test_dataset_detail_expands_missing_weighted_score_nested_fields():
)
)
- assert response["retrieval_model_dict"]["weights"] == {
+ retrieval_model_dict = response["retrieval_model_dict"]
+ assert isinstance(retrieval_model_dict, dict)
+
+ assert retrieval_model_dict["weights"] == {
"weight_type": None,
"keyword_setting": {"keyword_weight": None},
"vector_setting": {
@@ -225,7 +234,7 @@ def test_dataset_detail_serializes_after_session_closes(sqlite_session: Session)
enable_api=False,
is_multimodal=False,
)
- dataset.embedding_available = True
+ dataset.embedding_available = True # pyrefly: ignore[missing-attribute]
decoy_app = App(
id="decoy-app",
tenant_id="tenant-1",
@@ -285,7 +294,7 @@ def _dataset(index: int, account_id: str, **overrides: object) -> Dataset:
return dataset
-def _document(dataset_id: str, *, word_count: int, indexing_status: str, archived: bool = False) -> Document:
+def _document(dataset_id: str, *, word_count: int, indexing_status: IndexingStatus, archived: bool = False) -> Document:
return Document(
tenant_id="tenant-1",
dataset_id=dataset_id,
@@ -331,10 +340,10 @@ def _seed_dataset_page(session: Session) -> list[Dataset]:
session.add_all(
[
- _document(plain.id, word_count=10, indexing_status="completed"),
- _document(plain.id, word_count=20, indexing_status="completed", archived=True),
- _document(plain.id, word_count=5, indexing_status="waiting"),
- _document(piped.id, word_count=7, indexing_status="completed"),
+ _document(plain.id, word_count=10, indexing_status=IndexingStatus.COMPLETED),
+ _document(plain.id, word_count=20, indexing_status=IndexingStatus.COMPLETED, archived=True),
+ _document(plain.id, word_count=5, indexing_status=IndexingStatus.WAITING),
+ _document(piped.id, word_count=7, indexing_status=IndexingStatus.COMPLETED),
]
)
@@ -397,7 +406,9 @@ def _count_selects(session: Session) -> Generator[list[str]]:
engine = session.get_bind()
@event.listens_for(engine, "before_cursor_execute")
- def _before(_conn, _cursor, statement, _parameters, _context, _executemany):
+ def _before(
+ _conn: object, _cursor: object, statement: str, _parameters: object, _context: object, _executemany: object
+ ) -> None:
if statement.lstrip().upper().startswith("SELECT"):
statements.append(statement)
@@ -407,7 +418,7 @@ def _count_selects(session: Session) -> Generator[list[str]]:
event.remove(engine, "before_cursor_execute", _before)
-def test_dataset_detail_prefetch_matches_per_dataset_lookups(sqlite_session: Session):
+def test_dataset_detail_prefetch_matches_per_dataset_lookups(sqlite_session: Session) -> None:
datasets = _seed_dataset_page(sqlite_session)
expected = [_dump(dataset, session=sqlite_session) for dataset in datasets]
@@ -438,7 +449,7 @@ def test_dataset_detail_prefetch_matches_per_dataset_lookups(sqlite_session: Ses
assert prefetched[4]["doc_form"] is None
-def test_dataset_detail_prefetch_keeps_query_count_independent_of_page_size(sqlite_session: Session):
+def test_dataset_detail_prefetch_keeps_query_count_independent_of_page_size(sqlite_session: Session) -> None:
account = Account(name="Ada", email="[email protected]")
account.id = "account-1"
sqlite_session.add(account)
diff --git a/api/tests/unit_tests/fields/test_message_fields.py b/api/tests/unit_tests/fields/test_message_fields.py
index 9f394548ea0..d7a20eb1107 100644
--- a/api/tests/unit_tests/fields/test_message_fields.py
+++ b/api/tests/unit_tests/fields/test_message_fields.py
@@ -3,7 +3,7 @@ from decimal import Decimal
from fields.message_fields import ExploreMessageListItem, MessageListItem, RetrieverResource, WebMessageListItem
-def _retrieved_video_resource():
+def _retrieved_video_resource() -> dict[str, object]:
# Shaped like a real stored retriever_resources entry (core/rag/entities/citation_metadata.py's
# RetrievalSourceMetadata, the write-side model) for a video citation with a start/end offset.
return {
@@ -21,67 +21,79 @@ def _retrieved_video_resource():
}
-def _base_kwargs():
+def _base_kwargs() -> dict[str, object]:
+ inputs: dict[str, object] = {}
+ retriever_resources: list[object] = []
+ agent_thoughts: list[object] = []
+ message_files: list[object] = []
+ extra_contents: list[object] = []
return {
"id": "m1",
"conversation_id": "c1",
- "inputs": {},
+ "inputs": inputs,
"query": "hi",
"answer": "answer",
- "retriever_resources": [],
- "agent_thoughts": [],
- "message_files": [],
+ "retriever_resources": retriever_resources,
+ "agent_thoughts": agent_thoughts,
+ "message_files": message_files,
"status": "normal",
- "extra_contents": [],
+ "extra_contents": extra_contents,
}
class TestExploreMessageListItem:
- def test_retriever_resource_preserves_knowledge_fs_citation_identity(self):
+ def test_retriever_resource_preserves_knowledge_fs_citation_identity(self) -> None:
kwargs = _base_kwargs()
- kwargs["retriever_resources"] = [
+ payload = MessageListItem.model_validate(
{
- "position": 1,
- "document_id": "11111111-1111-4111-8111-111111111111",
- "document_asset_id": "22222222-2222-4222-8222-222222222222",
- "document_revision": 4,
- "document_version": 7,
+ **kwargs,
+ "retriever_resources": [
+ {
+ "position": 1,
+ "document_id": "11111111-1111-4111-8111-111111111111",
+ "document_asset_id": "22222222-2222-4222-8222-222222222222",
+ "document_revision": 4,
+ "document_version": 7,
+ }
+ ],
}
- ]
-
- payload = MessageListItem(**kwargs).model_dump(mode="json")["retriever_resources"][0]
+ ).model_dump(mode="json")["retriever_resources"][0]
assert payload["document_id"] == "11111111-1111-4111-8111-111111111111"
assert payload["document_asset_id"] == "22222222-2222-4222-8222-222222222222"
assert payload["document_revision"] == 4
assert payload["document_version"] == 7
- def test_exposes_metadata_for_history_rehydration(self):
+ def test_exposes_metadata_for_history_rehydration(self) -> None:
# The Explore/installed-app surface must surface message_metadata (incl. reasoning)
# so the chat-with-history client can rehydrate the thinking panel on reload.
- item = ExploreMessageListItem(**_base_kwargs(), metadata={"reasoning": {"llm": "thinking..."}})
+ item = ExploreMessageListItem.model_validate(
+ {**_base_kwargs(), "metadata": {"reasoning": {"llm": "thinking..."}}}
+ )
payload = item.model_dump(mode="json")
assert payload["metadata"] == {"reasoning": {"llm": "thinking..."}}
- def test_metadata_defaults_to_none(self):
- item = ExploreMessageListItem(**_base_kwargs())
+ def test_metadata_defaults_to_none(self) -> None:
+ item = ExploreMessageListItem.model_validate(_base_kwargs())
assert item.model_dump(mode="json")["metadata"] is None
- def test_base_message_list_item_has_no_metadata(self):
+ def test_base_message_list_item_has_no_metadata(self) -> None:
# Guard the public service-API contract: the base item must not leak metadata.
- payload = MessageListItem(**_base_kwargs()).model_dump(mode="json")
+ payload = MessageListItem.model_validate(_base_kwargs()).model_dump(mode="json")
assert "metadata" not in payload
- def test_message_list_item_exposes_usage_fields(self):
- payload = MessageListItem(
- **_base_kwargs(),
- message_tokens=7,
- answer_tokens=11,
- provider_response_latency=1.25,
- total_price=Decimal("0.0001234"),
- currency="USD",
+ def test_message_list_item_exposes_usage_fields(self) -> None:
+ payload = MessageListItem.model_validate(
+ {
+ **_base_kwargs(),
+ "message_tokens": 7,
+ "answer_tokens": 11,
+ "provider_response_latency": 1.25,
+ "total_price": Decimal("0.0001234"),
+ "currency": "USD",
+ }
).model_dump(mode="json")
assert payload["message_tokens"] == 7
@@ -91,12 +103,14 @@ class TestExploreMessageListItem:
assert payload["total_price"] == "0.0001234"
assert payload["currency"] == "USD"
- def test_web_message_list_item_exposes_usage_and_metadata(self):
- payload = WebMessageListItem(
- **_base_kwargs(),
- metadata={"usage": {"total_tokens": 18}},
- message_tokens=7,
- answer_tokens=11,
+ def test_web_message_list_item_exposes_usage_and_metadata(self) -> None:
+ payload = WebMessageListItem.model_validate(
+ {
+ **_base_kwargs(),
+ "metadata": {"usage": {"total_tokens": 18}},
+ "message_tokens": 7,
+ "answer_tokens": 11,
+ }
).model_dump(mode="json")
assert payload["metadata"] == {"usage": {"total_tokens": 18}}
@@ -106,7 +120,7 @@ class TestExploreMessageListItem:
class TestRetrieverResourceDocMetadata:
- def test_doc_metadata_survives_validation_from_a_stored_dict(self):
+ def test_doc_metadata_survives_validation_from_a_stored_dict(self) -> None:
# GET /messages re-validates the raw dicts stored in Message.retriever_resources
# (JSON-backed, written from RetrievalSourceMetadata) against RetrieverResource.
# Before this field existed, ResponseModel's extra="ignore" silently dropped
@@ -125,9 +139,9 @@ class TestRetrieverResourceDocMetadata:
assert payload["title"] == "training.mp4"
assert payload["retriever_from"] == "workflow"
- def test_message_list_item_propagates_doc_metadata_through_retriever_resources(self):
- payload = MessageListItem(
- **{**_base_kwargs(), "retriever_resources": [_retrieved_video_resource()]},
+ def test_message_list_item_propagates_doc_metadata_through_retriever_resources(self) -> None:
+ payload = MessageListItem.model_validate(
+ {**_base_kwargs(), "retriever_resources": [_retrieved_video_resource()]}
).model_dump(mode="json")
assert payload["retriever_resources"][0]["doc_metadata"]["start_seconds"] == 1036.0
diff --git a/api/tests/unit_tests/fields/test_workflow_draft_variable_fields.py b/api/tests/unit_tests/fields/test_workflow_draft_variable_fields.py
index c941310b626..c7b4d15494f 100644
--- a/api/tests/unit_tests/fields/test_workflow_draft_variable_fields.py
+++ b/api/tests/unit_tests/fields/test_workflow_draft_variable_fields.py
@@ -32,7 +32,7 @@ _TEST_NODE_EXEC_ID = str(uuid.uuid4())
class TestWorkflowDraftVariableFields:
- def test_serialize_full_content(self):
+ def test_serialize_full_content(self) -> None:
"""Test that _serialize_full_content uses pre-loaded relationships."""
# Create mock objects with relationships pre-loaded
mock_variable = WorkflowDraftVariable(
@@ -65,7 +65,7 @@ class TestWorkflowDraftVariableFields:
# Verify it used the pre-loaded relationships (no database queries)
mock_file_helpers.get_signed_file_url.assert_called_once_with("test-upload-file-id", as_attachment=True)
- def test_serialize_full_content_handles_none_cases(self):
+ def test_serialize_full_content_handles_none_cases(self) -> None:
"""Test that _serialize_full_content handles None cases properly."""
# Test with no file_id
@@ -74,7 +74,7 @@ class TestWorkflowDraftVariableFields:
result = _serialize_full_content(draft_var)
assert result is None
- def test_serialize_full_content_should_raises_when_file_id_exists_but_file_is_none(self):
+ def test_serialize_full_content_should_raises_when_file_id_exists_but_file_is_none(self) -> None:
# Test with no file_id
draft_var = WorkflowDraftVariable()
draft_var.file_id = str(uuid.uuid4())
@@ -82,7 +82,7 @@ class TestWorkflowDraftVariableFields:
with pytest.raises(AssertionError):
_serialize_full_content(draft_var)
- def test_conversation_variable(self):
+ def test_conversation_variable(self) -> None:
conv_var = WorkflowDraftVariable.new_conversation_variable(
app_id=_TEST_APP_ID, name="conv_var", value=build_segment(1)
)
@@ -110,7 +110,7 @@ class TestWorkflowDraftVariableFields:
expected_with_value["full_content"] = None
assert dump_response(WorkflowDraftVariableResponse, conv_var) == expected_with_value
- def test_create_sys_variable(self):
+ def test_create_sys_variable(self) -> None:
sys_var = WorkflowDraftVariable.new_sys_variable(
app_id=_TEST_APP_ID,
name="sys_var",
@@ -123,7 +123,7 @@ class TestWorkflowDraftVariableFields:
sys_var.last_edited_at = naive_utc_now()
sys_var.visible = True
- expected_without_value = OrderedDict(
+ expected_without_value: OrderedDict[str, object] = OrderedDict(
{
"id": sys_var.id,
"type": sys_var.get_variable_type().value,
@@ -142,7 +142,7 @@ class TestWorkflowDraftVariableFields:
expected_with_value["full_content"] = None
assert dump_response(WorkflowDraftVariableResponse, sys_var) == expected_with_value
- def test_node_variable(self):
+ def test_node_variable(self) -> None:
node_var = WorkflowDraftVariable.new_node_variable(
app_id=_TEST_APP_ID,
node_id="test_node",
@@ -175,7 +175,7 @@ class TestWorkflowDraftVariableFields:
expected_with_value["full_content"] = None
assert dump_response(WorkflowDraftVariableResponse, node_var) == expected_with_value
- def test_node_variable_with_file(self):
+ def test_node_variable_with_file(self) -> None:
node_var = WorkflowDraftVariable.new_node_variable(
app_id=_TEST_APP_ID,
node_id="test_node",
@@ -229,11 +229,11 @@ class TestWorkflowDraftVariableFields:
class TestWorkflowDraftVariableList:
- def test_workflow_draft_variable_list(self):
+ def test_workflow_draft_variable_list(self) -> None:
class TestCase(NamedTuple):
name: str
var_list: WorkflowDraftVariableList
- expected: dict
+ expected: dict[str, object]
node_var = WorkflowDraftVariable.new_node_variable(
app_id=_TEST_APP_ID,
@@ -307,7 +307,7 @@ class TestWorkflowDraftVariableList:
)
-def test_workflow_node_variables_fields():
+def test_workflow_node_variables_fields() -> None:
conv_var = WorkflowDraftVariable.new_conversation_variable(
app_id=_TEST_APP_ID, name="conv_var", value=build_segment(1)
)
@@ -319,7 +319,7 @@ def test_workflow_node_variables_fields():
assert item_dict["value"] == 1
-def test_workflow_file_variable_with_signed_url():
+def test_workflow_file_variable_with_signed_url() -> None:
"""Test that File type variables include signed URLs in API responses."""
from graphon.file import File, FileTransferMethod, FileType
@@ -375,7 +375,7 @@ def test_workflow_file_variable_with_signed_url():
assert "sign=" in remote_url
-def test_workflow_file_variable_remote_url():
+def test_workflow_file_variable_remote_url() -> None:
"""Test that File type variables with REMOTE_URL transfer method return the remote URL."""
from graphon.file import File, FileTransferMethod, FileType
diff --git a/api/tests/unit_tests/pyrefly.toml b/api/tests/unit_tests/pyrefly.toml
index 635aa359f01..4220ca93946 100644
--- a/api/tests/unit_tests/pyrefly.toml
+++ b/api/tests/unit_tests/pyrefly.toml
@@ -22,7 +22,6 @@ project-excludes = [
"controllers/console/app/test_workflow.py",
"controllers/console/app/test_workflow_node_output_inspector.py",
"controllers/console/app/test_wraps.py",
- "fields/test_workflow_draft_variable_fields.py",
"controllers/console/auth/test_activate.py",
"controllers/console/auth/test_email_register.py",
"controllers/console/datasets/rag_pipeline/test_datasource_auth.py",
@@ -257,11 +256,6 @@ project-excludes = [
"core/workflow/nodes/knowledge_retrieval/test_retrieval.py",
"extensions/test_ext_celery.py",
"extensions/test_ext_login.py",
- "factories/file_factory/test_builders.py",
- "factories/file_factory/test_remote.py",
- "factories/file_factory/test_validation.py",
- "fields/test_dataset_fields.py",
- "fields/test_message_fields.py",
"../helpers/unit/libs/_human_input/support.py",
"../e2e_tests/contracts/libs/_human_input/test_form_service.py",
"../e2e_tests/contracts/libs/_human_input/test_models.py",