mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-31 21:13:49 +08:00
Refactor: reformat all code for lefthook using ruff and gofmt (#16585)
This commit is contained in:
@@ -67,10 +67,11 @@ def _load_bot_api(monkeypatch, *, accessible, calls):
|
||||
async def _gen():
|
||||
yield 'data: {"event":"message","data":{"content":"ok"}}\n\n'
|
||||
yield 'data: {"event":"message_end","data":{"content":"ok"}}\n\n'
|
||||
|
||||
return _gen()
|
||||
|
||||
_stub(monkeypatch, "quart", Response=lambda *a, **k: SimpleNamespace(headers=SimpleNamespace(add_header=lambda *aa, **kk: None)), request=SimpleNamespace())
|
||||
_stub(monkeypatch, "api.apps", AUTH_BETA="beta", login_required=lambda *_a, **_k: (lambda func: func))
|
||||
_stub(monkeypatch, "api.apps", AUTH_BETA="beta", login_required=lambda *_a, **_k: lambda func: func)
|
||||
_stub(monkeypatch, "agent.canvas", Canvas=lambda *a, **k: SimpleNamespace(get_component_input_form=lambda _n: {}, get_prologue=lambda: "", get_mode=lambda: "agent"))
|
||||
_stub(monkeypatch, "api.db.db_models", APIToken=SimpleNamespace(query=lambda **_k: [SimpleNamespace(tenant_id="attacker-tenant")]))
|
||||
_stub(monkeypatch, "api.db.services.api_service", API4ConversationService=SimpleNamespace())
|
||||
|
||||
@@ -64,7 +64,8 @@ def _load_agent_api(monkeypatch, *, storage_get):
|
||||
return SimpleNamespace(payload=payload, headers={})
|
||||
|
||||
_stub(
|
||||
monkeypatch, "api.apps",
|
||||
monkeypatch,
|
||||
"api.apps",
|
||||
current_user=SimpleNamespace(id="tenant-1"),
|
||||
login_required=lambda func: func,
|
||||
)
|
||||
@@ -72,7 +73,8 @@ def _load_agent_api(monkeypatch, *, storage_get):
|
||||
_stub(monkeypatch, "api.db", CanvasCategory=SimpleNamespace())
|
||||
_stub(monkeypatch, "api.db.db_models", Task=SimpleNamespace())
|
||||
_stub(
|
||||
monkeypatch, "api.db.services.api_service",
|
||||
monkeypatch,
|
||||
"api.db.services.api_service",
|
||||
API4ConversationService=SimpleNamespace(
|
||||
get_by_id=lambda _id: (False, None),
|
||||
save=lambda **_k: True,
|
||||
@@ -81,7 +83,8 @@ def _load_agent_api(monkeypatch, *, storage_get):
|
||||
),
|
||||
)
|
||||
_stub(
|
||||
monkeypatch, "api.db.services.canvas_service",
|
||||
monkeypatch,
|
||||
"api.db.services.canvas_service",
|
||||
CanvasTemplateService=SimpleNamespace(),
|
||||
UserCanvasService=SimpleNamespace(accessible=lambda *_a, **_k: True, query=lambda **_k: []),
|
||||
completion=lambda *_a, **_k: None,
|
||||
@@ -92,17 +95,23 @@ def _load_agent_api(monkeypatch, *, storage_get):
|
||||
_stub(monkeypatch, "api.db.services.knowledgebase_service", KnowledgebaseService=SimpleNamespace())
|
||||
_stub(monkeypatch, "api.db.services.pipeline_operation_log_service", PipelineOperationLogService=SimpleNamespace())
|
||||
_stub(
|
||||
monkeypatch, "api.db.services.task_service",
|
||||
CANVAS_DEBUG_DOC_ID="", TaskService=SimpleNamespace(), queue_dataflow=lambda *_a, **_k: None,
|
||||
monkeypatch,
|
||||
"api.db.services.task_service",
|
||||
CANVAS_DEBUG_DOC_ID="",
|
||||
TaskService=SimpleNamespace(),
|
||||
queue_dataflow=lambda *_a, **_k: None,
|
||||
)
|
||||
_stub(
|
||||
monkeypatch, "api.db.services.user_service",
|
||||
TenantService=SimpleNamespace(), UserService=SimpleNamespace(get_by_id=lambda *_a, **_k: (False, None)),
|
||||
monkeypatch,
|
||||
"api.db.services.user_service",
|
||||
TenantService=SimpleNamespace(),
|
||||
UserService=SimpleNamespace(get_by_id=lambda *_a, **_k: (False, None)),
|
||||
)
|
||||
_stub(monkeypatch, "api.db.services.user_canvas_version", UserCanvasVersionService=SimpleNamespace())
|
||||
|
||||
_stub(
|
||||
monkeypatch, "api.utils.api_utils",
|
||||
monkeypatch,
|
||||
"api.utils.api_utils",
|
||||
construct_json_result=lambda **kw: {"kind": "json", **kw},
|
||||
get_data_error_result=lambda message="", code=0, data=False: {"kind": "data_error", "message": message},
|
||||
get_error_data_result=lambda *_a, **_k: {"kind": "error"},
|
||||
@@ -114,11 +123,13 @@ def _load_agent_api(monkeypatch, *, storage_get):
|
||||
# Used as `@validate_request(...)` decorator factory at module level, so it
|
||||
# must return an identity decorator (the lenient fallback would return None
|
||||
# and `@None` raises TypeError during import).
|
||||
validate_request=lambda *_a, **_k: (lambda func: func),
|
||||
validate_request=lambda *_a, **_k: lambda func: func,
|
||||
)
|
||||
_stub(
|
||||
monkeypatch, "common.settings",
|
||||
retriever=SimpleNamespace(), kg_retriever=SimpleNamespace(),
|
||||
monkeypatch,
|
||||
"common.settings",
|
||||
retriever=SimpleNamespace(),
|
||||
kg_retriever=SimpleNamespace(),
|
||||
# download_attachment reads settings.STORAGE_IMPL.get after
|
||||
# `from common import settings` rebinds the module's `settings` name.
|
||||
STORAGE_IMPL=SimpleNamespace(get=storage_get),
|
||||
@@ -135,7 +146,8 @@ def _load_agent_api(monkeypatch, *, storage_get):
|
||||
|
||||
_stub(monkeypatch, "common.misc_utils", get_uuid=lambda: "uuid", thread_pool_exec=_thread_pool_exec)
|
||||
_stub(
|
||||
monkeypatch, "api.utils.web_utils",
|
||||
monkeypatch,
|
||||
"api.utils.web_utils",
|
||||
CONTENT_TYPE_MAP={"markdown": "text/markdown"},
|
||||
apply_safe_file_response_headers=lambda *_a, **_k: None,
|
||||
)
|
||||
|
||||
@@ -80,7 +80,7 @@ def _load_openai_api(monkeypatch):
|
||||
"api.utils.api_utils",
|
||||
get_error_data_result=lambda *a, **k: {"code": 102},
|
||||
get_request_json=lambda: {},
|
||||
validate_request=lambda *_a, **_k: (lambda func: func),
|
||||
validate_request=lambda *_a, **_k: lambda func: func,
|
||||
)
|
||||
_stub(monkeypatch, "common.constants", RetCode=SimpleNamespace(ARGUMENT_ERROR=102), StatusEnum=SimpleNamespace(VALID=SimpleNamespace(value="1")))
|
||||
_stub(monkeypatch, "common.metadata_utils", convert_conditions=lambda *_a, **_k: None, meta_filter=lambda *_a, **_k: [])
|
||||
@@ -107,6 +107,7 @@ async def _aiter(events):
|
||||
def _collect_sse(module, events, **kwargs):
|
||||
"""Run the SSE generator over `events` and return parsed JSON chunks
|
||||
(the trailing `[DONE]` sentinel excluded)."""
|
||||
|
||||
async def run():
|
||||
out = []
|
||||
async for raw in module._stream_chat_completion_sse(_aiter(events), **kwargs):
|
||||
@@ -205,11 +206,7 @@ def test_reasoning_content_streamed_separately(monkeypatch):
|
||||
]
|
||||
chunks = _collect_sse(module, events, need_reference=False, **_BASE_KWARGS)
|
||||
|
||||
reasoning = "".join(
|
||||
c["choices"][0]["delta"].get("reasoning_content")
|
||||
for c in chunks
|
||||
if c != "[DONE]" and isinstance(c["choices"][0]["delta"].get("reasoning_content"), str)
|
||||
)
|
||||
reasoning = "".join(c["choices"][0]["delta"].get("reasoning_content") for c in chunks if c != "[DONE]" and isinstance(c["choices"][0]["delta"].get("reasoning_content"), str))
|
||||
content = "".join(p for p in _content_pieces(chunks) if isinstance(p, str))
|
||||
assert reasoning == "thinking"
|
||||
assert content == "answer"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#
|
||||
#
|
||||
# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@@ -84,6 +84,7 @@ class _FakeKGRetriever:
|
||||
|
||||
def _load_dify_retrieval(monkeypatch, *, kb, accessible, request_body, tenant_id, chunks=None):
|
||||
"""Load dify_retrieval_api.py with minimum stubs to exercise the retrieval handler."""
|
||||
|
||||
def _add_tenant_id_to_kwargs(func):
|
||||
async def wrapper(**kwargs):
|
||||
kwargs["tenant_id"] = tenant_id
|
||||
|
||||
@@ -99,16 +99,8 @@ def _load_update_document_name_only_module(monkeypatch, *, file_lookup):
|
||||
fine_grained_tokenize=lambda tokens: tokens,
|
||||
)
|
||||
|
||||
module_path = (
|
||||
Path(__file__).resolve().parents[5]
|
||||
/ "api"
|
||||
/ "apps"
|
||||
/ "services"
|
||||
/ "document_api_service.py"
|
||||
)
|
||||
spec = importlib.util.spec_from_file_location(
|
||||
"test_update_document_name_only_module", module_path
|
||||
)
|
||||
module_path = Path(__file__).resolve().parents[5] / "api" / "apps" / "services" / "document_api_service.py"
|
||||
spec = importlib.util.spec_from_file_location("test_update_document_name_only_module", module_path)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
monkeypatch.setitem(sys.modules, "test_update_document_name_only_module", module)
|
||||
spec.loader.exec_module(module)
|
||||
|
||||
Reference in New Issue
Block a user