From c5d525df08bb9534d1efb3ab5ff9357b52240fe9 Mon Sep 17 00:00:00 2001 From: Marrrrrrrrry <34876935+Marrrrrrrrry@users.noreply.github.com> Date: Wed, 9 Sep 2026 17:42:36 +0800 Subject: [PATCH] feat: declare missing config keys (opendal_scheme, chunk_expire_hours) and add config schema guard - opendal_scheme was read directly by the OpenDAL backend but never declared in DEFAULT_CONFIG: selecting that backend crashed with AttributeError - chunk_expire_hours existed only as getattr fallbacks in two modules - refresh_settings now warns about unknown user_config keys - meta-test scans the codebase so any future settings.X reference without a DEFAULT_CONFIG declaration fails the suite --- apps/base/config.py | 3 ++ core/settings.py | 10 +++++ tests/test_config_schema_guard.py | 68 +++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 tests/test_config_schema_guard.py diff --git a/apps/base/config.py b/apps/base/config.py index 4c13f53..59ec028 100644 --- a/apps/base/config.py +++ b/apps/base/config.py @@ -78,6 +78,9 @@ async def refresh_settings() -> None: """从数据库读取最新配置并应用到运行时。""" config_record = await KeyValue.filter(key="settings").first() settings.user_config = config_record.value if config_record and config_record.value else {} + unknown = settings.unknown_keys(settings.user_config) + if unknown: + logger.warning("配置中存在未登记的键(将被忽略其类型语义,仅透传): %s", unknown) _sync_ip_limits() diff --git a/core/settings.py b/core/settings.py index 0fabae9..a682ae8 100644 --- a/core/settings.py +++ b/core/settings.py @@ -84,6 +84,8 @@ DEFAULT_CONFIG = { "showAdminAddr": 0, "robotsText": "User-agent: *\nDisallow: /", "trustedProxies": [], + "chunk_expire_hours": 24, + "opendal_scheme": "s3", } @@ -107,6 +109,14 @@ class Settings: else: self.user_config[key] = value + def unknown_keys(self, config: dict) -> list[str]: + """Keys in `config` that DEFAULT_CONFIG does not define. + + Unknown keys silently fall through __getattr__ and crash later at the + usage site; callers (refresh_settings) surface them at load time. + """ + return sorted(k for k in config if k not in self.default_config) + def items(self): return {**self.default_config, **self.user_config}.items() diff --git a/tests/test_config_schema_guard.py b/tests/test_config_schema_guard.py new file mode 100644 index 0000000..efa8f2c --- /dev/null +++ b/tests/test_config_schema_guard.py @@ -0,0 +1,68 @@ +"""Schema guard: every settings attribute referenced by application code must +be declared in DEFAULT_CONFIG. + +This is the regression net for the ``opendal_scheme`` class of bug — a config +key read via ``settings.X`` that DEFAULT_CONFIG never declared, which crashes +with AttributeError only when an admin actually selects that code path. +""" +import re +import unittest +from pathlib import Path + +from core.settings import DEFAULT_CONFIG + +REPO_ROOT = Path(__file__).resolve().parent.parent +SCAN_DIRS = ["main.py", "core", "apps"] + +# Attribute references that are Settings' own machinery, not config keys. +NON_CONFIG_ATTRS = {"items", "user_config", "default_config", "unknown_keys"} + +# Filename-extension tokens: matches like "core/settings.py" in docstrings. +FILE_EXT_LIKE = {"py", "pyc", "toml", "ini", "cfg", "yaml", "yml", "md"} + +# (?