Skip to content

chore: pass session explicitly in ApiModeration - #39885

Merged
asukaminato0721 merged 1 commit into
langgenius:mainfrom
HandSonic:chore/moderation-explicit-session
Aug 3, 2026
Merged

asukaminato0721 merged 1 commit into
langgenius:mainfrom
HandSonic:chore/moderation-explicit-session

Conversation

@HandSonic

Copy link
Copy Markdown
Contributor

Summary

For #37403 — make the DB session an explicit parameter in the moderation module, following the pattern from #37402.

  • ApiModeration._get_api_based_extension now takes session: scoped_session and uses it for the query, instead of relying on the implicit global db.session inside the leaf function.
  • Callers (validate_config and _get_config_by_requestor) pass db.session at the call site, so the session dependency of the leaf query is visible in the signature.
  • The other moderation providers (keywords, openai_moderation) do not query the database, so the factory boundary and their signatures stay unchanged.
  • No behavior change.

Testing

  • uv run --project api pytest api/tests/unit_tests/core/moderation -q → 208 passed
  • ruff check / ruff format --check on touched files → pass
  • pyrefly check on touched files → 0 errors

From CodeBuddy Code

Make the DB session an explicit parameter of
ApiModeration._get_api_based_extension instead of relying on the
implicit global db.session inside the query function, following the
pattern from langgenius#37402. Callers (validate_config and
_get_config_by_requestor) now pass db.session at the call site, so the
session dependency of the leaf query is visible in the signature.

The other moderation providers (keywords, openai_moderation) do not
query the database, so the factory boundary and their signatures stay
unchanged.

langgenius#37403
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. refactor labels Aug 2, 2026
@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Aug 3, 2026
@github-actions

github-actions Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-08-03 02:18:39.069353980 +0000
+++ /tmp/pyrefly_pr.txt	2026-08-03 02:18:29.392275322 +0000
@@ -4525,7 +4525,11 @@
 ERROR Argument `_FakeModelRuntime` is not assignable to parameter `runtime` with type `ModelRuntime` in function `core.plugin.impl.model_runtime_factory.create_model_type_instance` [bad-argument-type]
    --> tests/unit_tests/core/model_runtime/test_model_provider_factory.py:422:21
 ERROR Missing argument `text` in function `core.moderation.api.api.ModerationOutputParams.__init__` [missing-argument]
-  --> tests/unit_tests/core/moderation/api/test_api.py:58:35
+  --> tests/unit_tests/core/moderation/api/test_api.py:49:35
+ERROR Argument `Session` is not assignable to parameter `session` with type `scoped_session[Unknown]` in function `core.moderation.api.api.ApiModeration._get_api_based_extension` [bad-argument-type]
+   --> tests/unit_tests/core/moderation/api/test_api.py:189:78
+ERROR Argument `Session` is not assignable to parameter `session` with type `scoped_session[Unknown]` in function `core.moderation.api.api.ApiModeration._get_api_based_extension` [bad-argument-type]
+   --> tests/unit_tests/core/moderation/api/test_api.py:192:76
 ERROR Argument `Literal['invalid']` is not assignable to parameter `config` with type `dict[str, Any] | None` in function `core.moderation.base.Moderation.__init__` [bad-argument-type]
     --> tests/unit_tests/core/moderation/test_sensitive_word_filter.py:1282:92
 ERROR Class member `ConcreteTraceInstance.trace` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
@@ -7565,25 +7569,25 @@
 ERROR Argument `SimpleNamespace` is not assignable to parameter `dataset_process_rule` with type `DatasetProcessRule | None` in function `services.dataset_service.DocumentService.save_document_with_dataset_id` [bad-argument-type]
    --> tests/unit_tests/services/test_dataset_service_document.py:952:38
 ERROR Argument `SimpleNamespace` is not assignable to parameter `dataset_process_rule` with type `DatasetProcessRule | None` in function `services.dataset_service.DocumentService.save_document_with_dataset_id` [bad-argument-type]
-    --> tests/unit_tests/services/test_dataset_service_document.py:1034:38
+    --> tests/unit_tests/services/test_dataset_service_document.py:1033:38
 ERROR Argument `SimpleNamespace` is not assignable to parameter `dataset_process_rule` with type `DatasetProcessRule | None` in function `services.dataset_service.DocumentService.save_document_with_dataset_id` [bad-argument-type]
-    --> tests/unit_tests/services/test_dataset_service_document.py:1086:38
+    --> tests/unit_tests/services/test_dataset_service_document.py:1085:38
 ERROR Argument `SimpleNamespace` is not assignable to parameter `dataset` with type `Dataset` in function `services.dataset_service.DocumentService.update_document_with_dataset_id` [bad-argument-type]
-    --> tests/unit_tests/services/test_dataset_service_document.py:1242:17
+    --> tests/unit_tests/services/test_dataset_service_document.py:1241:17
 ERROR Argument `SimpleNamespace` is not assignable to parameter `dataset` with type `Dataset` in function `services.dataset_service.DocumentService.update_document_with_dataset_id` [bad-argument-type]
-    --> tests/unit_tests/services/test_dataset_service_document.py:1275:21
+    --> tests/unit_tests/services/test_dataset_service_document.py:1274:21
 ERROR Argument `SimpleNamespace` is not assignable to parameter `dataset` with type `Dataset` in function `services.dataset_service.DocumentService.update_document_with_dataset_id` [bad-argument-type]
-    --> tests/unit_tests/services/test_dataset_service_document.py:1303:21
+    --> tests/unit_tests/services/test_dataset_service_document.py:1302:21
 ERROR Argument `SimpleNamespace` is not assignable to parameter `dataset` with type `Dataset` in function `services.dataset_service.DocumentService.update_document_with_dataset_id` [bad-argument-type]
-    --> tests/unit_tests/services/test_dataset_service_document.py:1324:21
+    --> tests/unit_tests/services/test_dataset_service_document.py:1323:21
 ERROR Argument `SimpleNamespace` is not assignable to parameter `dataset` with type `Dataset` in function `services.dataset_service.DocumentService.update_document_with_dataset_id` [bad-argument-type]
-    --> tests/unit_tests/services/test_dataset_service_document.py:1364:17
+    --> tests/unit_tests/services/test_dataset_service_document.py:1363:17
 ERROR Object of class `NoneType` has no attribute `rules` [missing-attribute]
-    --> tests/unit_tests/services/test_dataset_service_document.py:1743:22
+    --> tests/unit_tests/services/test_dataset_service_document.py:1742:22
 ERROR Argument `SimpleNamespace` is not assignable to parameter `dataset_process_rule` with type `DatasetProcessRule | None` in function `services.dataset_service.DocumentService.save_document_with_dataset_id` [bad-argument-type]
-    --> tests/unit_tests/services/test_dataset_service_document.py:1873:42
+    --> tests/unit_tests/services/test_dataset_service_document.py:1872:42
 ERROR Argument `SimpleNamespace` is not assignable to parameter `dataset_process_rule` with type `DatasetProcessRule | None` in function `services.dataset_service.DocumentService.save_document_with_dataset_id` [bad-argument-type]
-    --> tests/unit_tests/services/test_dataset_service_document.py:1897:42
+    --> tests/unit_tests/services/test_dataset_service_document.py:1896:42
 ERROR Argument `SimpleNamespace` is not assignable to parameter `knowledge_config` with type `KnowledgeConfig` in function `services.dataset_service.DocumentService.save_document_with_dataset_id` [bad-argument-type]
    --> tests/unit_tests/services/test_dataset_service_lock_not_owned.py:125:26
 ERROR Argument `SimpleNamespace` is not assignable to parameter `dataset` with type `Dataset` in function `services.dataset_service.SegmentService.create_child_chunk` [bad-argument-type]

@github-actions

github-actions Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 57.32% 57.32% -0.00%
Strict coverage 56.85% 56.85% -0.00%
Typed symbols 37,560 37,557 -3
Untyped symbols 28,199 28,199 0
Modules 3092 3092 0

@asukaminato0721
asukaminato0721 added this pull request to the merge queue Aug 3, 2026
Merged via the queue into langgenius:main with commit bb68a05 Aug 3, 2026
34 checks passed
qielanci pushed a commit to qielanci/dify that referenced this pull request Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer refactor size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants