simple-module-core 0.0.43__tar.gz → 0.0.45__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/PKG-INFO +1 -1
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/pyproject.toml +1 -1
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/__main__.py +10 -3
- simple_module_core-0.0.45/simple_module_core/_migration_doctor.py +56 -0
- simple_module_core-0.0.45/simple_module_core/diagnostics/_durable_names.py +71 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_expression_index.py +30 -5
- simple_module_core-0.0.45/simple_module_core/diagnostics/_migration.py +96 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_module.py +2 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/dotenv.py +23 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/invalidation.py +1 -1
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/secret_fields.py +13 -2
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/settings_base.py +1 -1
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/settings_hydrate.py +6 -1
- simple_module_core-0.0.45/simple_module_core/singleflight.py +96 -0
- simple_module_core-0.0.45/tests/test_core_settings_source_cache.py +45 -0
- simple_module_core-0.0.45/tests/test_core_singleflight.py +100 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_diagnostics.py +19 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_expression_index_diagnostics.py +36 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_sm003_page_render.py +2 -2
- simple_module_core-0.0.45/tests/test_sm034_durable_names.py +53 -0
- simple_module_core-0.0.43/simple_module_core/diagnostics/_migration.py +0 -45
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/.gitignore +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/LICENSE +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/README.md +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/__init__.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/_data_doctor.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/_i18n_flatten.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/_i18n_translator.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/_permission_grants.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/_request_hooks.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/activity_feed.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/app_state.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/audit_links.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/body_limits.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/correlation.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/cors.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/csp.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/data_compatibility.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/datetimes.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/deprecation.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/design_packs.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/diagnostics/__init__.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_coupling.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_css.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_db_extensions.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_dependencies.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_heavy_task_imports.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_i18n.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_inertia_api.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_js_workspace.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_pages.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_runner.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_spa_mount.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_tenancy.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_types.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/discovery.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/durable_events.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/environments.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/events.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/exceptions.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/feature_flags.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/health.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/i18n.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/i18n_overrides.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/invalidation_refresh.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/leases.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/mail.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/menu.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/module.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/module_meta.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/module_settings.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/path_rules.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/permission_denied.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/permissions.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/public_routes.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/py.typed +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/rate_limit.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/redirect_safety.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/resources.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/services.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/setup_steps.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/spa_mount.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/stored_settings.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/tenancy.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/tenant_events.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/user_directory.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/user_events.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/versioning.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_audit_links.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_auth_provider_selection.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_core_app_state.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_core_data_compatibility_doctor.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_core_datetimes.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_core_inline_delivery_context.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_core_invalidation_refresh.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_core_leases.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_core_lifecycle_docs.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_core_mail_resolution.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_core_module_settings.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_core_user_directory.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_csp_registry.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_css_diagnostics.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_css_scanner.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_db_extensions_diagnostics.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_deprecation_helper.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_design_packs.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_diagnostics_tenancy.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_discovery.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_discovery_missing_deps.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_doctor_cli_overrides.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_dotenv.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_durable_events.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_environments.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_events.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_events_inline.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_feature_flags.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_feature_flags_decorator.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_health_registry.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_heavy_task_imports_diagnostics.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_i18n.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_i18n_diagnostics.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_i18n_overrides.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_invalidation.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_invalidation_handlers.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_menu.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_module_base.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_module_diagnostics.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_path_rule_registries.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_permission_sources.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_permissions.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_permissions_admin_principal.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_public_routes.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_public_routes_rate_precedence.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_redirect_safety.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_services.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_settings_base.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_sm009_dynamic_coupling.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_tenant_exists.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_tenant_roles.py +0 -0
- {simple_module_core-0.0.43 → simple_module_core-0.0.45}/tests/test_versioning.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: simple_module_core
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.45
|
|
4
4
|
Summary: Module-system primitives for the simple_module framework — ModuleBase, discovery, diagnostics, events
|
|
5
5
|
Project-URL: Homepage, https://github.com/antosubash/simple_module_python
|
|
6
6
|
Project-URL: Repository, https://github.com/antosubash/simple_module_python
|
|
@@ -25,6 +25,7 @@ import sys
|
|
|
25
25
|
from pathlib import Path
|
|
26
26
|
|
|
27
27
|
from simple_module_core._data_doctor import data_compatibility_diagnostics
|
|
28
|
+
from simple_module_core._migration_doctor import migration_diagnostics
|
|
28
29
|
from simple_module_core.diagnostics import (
|
|
29
30
|
Diagnostic,
|
|
30
31
|
DiagnosticLevel,
|
|
@@ -39,7 +40,7 @@ from simple_module_core.discovery import (
|
|
|
39
40
|
select_auth_provider,
|
|
40
41
|
topological_sort,
|
|
41
42
|
)
|
|
42
|
-
from simple_module_core.dotenv import find_env_file, parse_dotenv
|
|
43
|
+
from simple_module_core.dotenv import absolutize_sqlite_url, find_env_file, parse_dotenv
|
|
43
44
|
from simple_module_core.exceptions import InvalidModuleError
|
|
44
45
|
|
|
45
46
|
|
|
@@ -77,8 +78,13 @@ def _load_i18n_settings_from_env() -> tuple[list[str] | None, str]:
|
|
|
77
78
|
|
|
78
79
|
|
|
79
80
|
def _database_url() -> str:
|
|
80
|
-
"""``SM_DATABASE_URL``, defaulting like ``Settings.database_url``.
|
|
81
|
-
|
|
81
|
+
"""``SM_DATABASE_URL``, defaulting and anchored like ``Settings.database_url``.
|
|
82
|
+
|
|
83
|
+
A relative SQLite path is resolved against the project root, as the app
|
|
84
|
+
resolves it, so doctor run from ``host/`` reads the app's database.
|
|
85
|
+
"""
|
|
86
|
+
url = os.environ.get("SM_DATABASE_URL", "sqlite+aiosqlite:///./app.db")
|
|
87
|
+
return absolutize_sqlite_url(url, anchor=find_env_file().parent)
|
|
82
88
|
|
|
83
89
|
|
|
84
90
|
def _database_dialect() -> str:
|
|
@@ -188,6 +194,7 @@ def main() -> int:
|
|
|
188
194
|
database_dialect=_database_dialect(),
|
|
189
195
|
)
|
|
190
196
|
diagnostics.extend(override_findings)
|
|
197
|
+
diagnostics.extend(migration_diagnostics(modules, _database_url(), find_env_file().parent))
|
|
191
198
|
diagnostics.extend(data_compatibility_diagnostics(modules, _database_url()))
|
|
192
199
|
# A missing dependency only warns at a dev boot, but production refuses
|
|
193
200
|
# to start with it (GH #501), so doctor reports it as what it will be.
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"""Doctor's migration checks (SM010, SM011) against ``SM_DATABASE_URL`` (#895)."""
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
import importlib.util
|
|
5
|
+
from collections.abc import Sequence
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from simple_module_core.diagnostics import Diagnostic, DiagnosticLevel
|
|
9
|
+
from simple_module_core.diagnostics._migration import MigrationDiagnostics, module_table_names
|
|
10
|
+
from simple_module_core.module import ModuleBase
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _alembic_ini(project_root: Path) -> Path | None:
|
|
14
|
+
"""``alembic.ini`` at the project root, else ``host/alembic.ini``, as the CLI resolves it."""
|
|
15
|
+
for candidate in (project_root / "alembic.ini", project_root / "host" / "alembic.ini"):
|
|
16
|
+
if candidate.is_file():
|
|
17
|
+
return candidate
|
|
18
|
+
return None
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def migration_diagnostics(
|
|
22
|
+
modules: Sequence[ModuleBase], database_url: str, project_root: Path
|
|
23
|
+
) -> list[Diagnostic]:
|
|
24
|
+
# Core-only installations have neither Alembic nor a database runtime.
|
|
25
|
+
if importlib.util.find_spec("simple_module_db") is None:
|
|
26
|
+
return []
|
|
27
|
+
ini = _alembic_ini(project_root)
|
|
28
|
+
if ini is None:
|
|
29
|
+
return []
|
|
30
|
+
from simple_module_db.migration_state import read_migration_state
|
|
31
|
+
|
|
32
|
+
try:
|
|
33
|
+
state = asyncio.run(read_migration_state(database_url, ini))
|
|
34
|
+
except Exception:
|
|
35
|
+
# Unreachable is a warning, as for SM033: doctor also runs where no
|
|
36
|
+
# database is up. A revision file that fails to import lands here too.
|
|
37
|
+
# The error text can carry credentials, so it is not shown.
|
|
38
|
+
return [
|
|
39
|
+
Diagnostic(
|
|
40
|
+
level=DiagnosticLevel.WARNING,
|
|
41
|
+
code="SM010",
|
|
42
|
+
message="Could not read the migration state (database or Alembic revisions); "
|
|
43
|
+
"SM010 and SM011 were not checked",
|
|
44
|
+
module_name="migrations",
|
|
45
|
+
suggestion="Check SM_DATABASE_URL and that the database is running, and that "
|
|
46
|
+
"the revisions load (`make migration-history`).",
|
|
47
|
+
)
|
|
48
|
+
]
|
|
49
|
+
if state is None:
|
|
50
|
+
return []
|
|
51
|
+
checks = MigrationDiagnostics()
|
|
52
|
+
behind = checks.check_heads(state.current, state.heads)
|
|
53
|
+
if behind:
|
|
54
|
+
# Behind head, a missing table may simply be in a pending revision.
|
|
55
|
+
return behind
|
|
56
|
+
return checks.check_table_coverage(module_table_names(modules), set(state.tables))
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"""SM034: flag a ``subscribe_durable(...)`` call without an explicit ``name=``.
|
|
2
|
+
|
|
3
|
+
A durable handler's outbox rows are keyed by its name, which defaults to the
|
|
4
|
+
handler's import path. Moving or renaming the handler module silently strands
|
|
5
|
+
every undelivered row (it retries as an unknown handler, then goes ``dead``).
|
|
6
|
+
Uses ``ast`` and never imports the module being checked.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import ast
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
from typing import TYPE_CHECKING
|
|
14
|
+
|
|
15
|
+
from simple_module_core.diagnostics._types import Diagnostic, DiagnosticLevel
|
|
16
|
+
|
|
17
|
+
if TYPE_CHECKING:
|
|
18
|
+
from simple_module_core.module import ModuleBase
|
|
19
|
+
|
|
20
|
+
_SKIP_DIRS = frozenset({"tests", "node_modules", "__pycache__"})
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _is_unnamed_subscribe(node: ast.AST) -> bool:
|
|
24
|
+
if not isinstance(node, ast.Call):
|
|
25
|
+
return False
|
|
26
|
+
func = node.func
|
|
27
|
+
called = func.attr if isinstance(func, ast.Attribute) else getattr(func, "id", None)
|
|
28
|
+
if called != "subscribe_durable":
|
|
29
|
+
return False
|
|
30
|
+
# A ``**kwargs`` spread may carry ``name``; give it the benefit of the doubt.
|
|
31
|
+
return not any(kw.arg is None or kw.arg == "name" for kw in node.keywords)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _source_files(src_dir: Path) -> list[Path]:
|
|
35
|
+
return sorted(
|
|
36
|
+
p
|
|
37
|
+
for p in src_dir.rglob("*.py")
|
|
38
|
+
if not _SKIP_DIRS.intersection(p.relative_to(src_dir).parts)
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def check_durable_names(mod: ModuleBase, src_dir: Path) -> list[Diagnostic]:
|
|
43
|
+
"""Warn on each ``subscribe_durable`` call in the package that omits ``name=``."""
|
|
44
|
+
diags: list[Diagnostic] = []
|
|
45
|
+
for path in _source_files(src_dir):
|
|
46
|
+
try:
|
|
47
|
+
tree = ast.parse(path.read_text(), filename=str(path))
|
|
48
|
+
except (OSError, SyntaxError, UnicodeDecodeError):
|
|
49
|
+
continue
|
|
50
|
+
for node in ast.walk(tree):
|
|
51
|
+
if not _is_unnamed_subscribe(node):
|
|
52
|
+
continue
|
|
53
|
+
diags.append(
|
|
54
|
+
Diagnostic(
|
|
55
|
+
level=DiagnosticLevel.WARNING,
|
|
56
|
+
code="SM034",
|
|
57
|
+
message=(
|
|
58
|
+
"subscribe_durable(...) has no name= — the default name is the "
|
|
59
|
+
"handler's import path, so moving or renaming the handler strands "
|
|
60
|
+
"its undelivered outbox rows"
|
|
61
|
+
),
|
|
62
|
+
module_name=mod.meta.name,
|
|
63
|
+
file=f"{path}:{node.lineno}",
|
|
64
|
+
suggestion=(
|
|
65
|
+
"Pass name= with a constant from constants.py whose value is the "
|
|
66
|
+
"current default name (module.path.function), so delivery state "
|
|
67
|
+
"survives refactors."
|
|
68
|
+
),
|
|
69
|
+
)
|
|
70
|
+
)
|
|
71
|
+
return diags
|
|
@@ -25,13 +25,37 @@ if TYPE_CHECKING:
|
|
|
25
25
|
logger = logging.getLogger(__name__)
|
|
26
26
|
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
_ORDERING_MODIFIERS = frozenset({"asc_op", "desc_op", "nulls_first_op", "nulls_last_op"})
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def unwrap_ordering(expr: Any) -> Any:
|
|
32
|
+
"""``expr`` without its ``desc()``/``asc()``/``nulls_first()``/``nulls_last()`` wrappers."""
|
|
33
|
+
while getattr(getattr(expr, "modifier", None), "__name__", None) in _ORDERING_MODIFIERS:
|
|
34
|
+
expr = expr.element
|
|
35
|
+
return expr
|
|
36
|
+
|
|
30
37
|
|
|
31
|
-
|
|
32
|
-
|
|
38
|
+
def _is_column(expr: Any) -> bool:
|
|
39
|
+
"""A column, possibly wrapped in ``desc()``/``asc()``/``nulls_last()``.
|
|
40
|
+
|
|
41
|
+
SQLite reflects ``created_at DESC`` as the column ``created_at`` and Alembic
|
|
42
|
+
compares it as one (GH #869), so an ordering keeps an element a column. A
|
|
43
|
+
plain ``Column`` is attached to its table; ``sa.column("x")`` is a named,
|
|
44
|
+
non-literal column; ``text()``, ``literal_column()``, a function or a bare
|
|
45
|
+
string label are expressions.
|
|
33
46
|
"""
|
|
34
|
-
|
|
47
|
+
expr = unwrap_ordering(expr)
|
|
48
|
+
if hasattr(expr, "element"):
|
|
49
|
+
# Still wraps something once the ordering is gone: a label over an
|
|
50
|
+
# arbitrary expression (``lower(email).label("x")``) or ``sa.desc("col")``'s
|
|
51
|
+
# bare-string label. A column wraps nothing.
|
|
52
|
+
return False
|
|
53
|
+
return getattr(expr, "table", None) is not None or getattr(expr, "is_literal", None) is False
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def index_is_expression_based(index: Any) -> bool:
|
|
57
|
+
"""True when any element of ``index`` is an expression rather than a plain column."""
|
|
58
|
+
return not all(_is_column(expr) for expr in index.expressions)
|
|
35
59
|
|
|
36
60
|
|
|
37
61
|
def find_expression_indexes(tables: Iterable[Any]) -> list[tuple[str, str]]:
|
|
@@ -103,4 +127,5 @@ __all__ = [
|
|
|
103
127
|
"find_expression_indexes",
|
|
104
128
|
"index_is_expression_based",
|
|
105
129
|
"module_all_tables",
|
|
130
|
+
"unwrap_ordering",
|
|
106
131
|
]
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"""Alembic migration-state diagnostics (SM010, SM011)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Iterable, Set
|
|
6
|
+
from typing import TYPE_CHECKING
|
|
7
|
+
|
|
8
|
+
from simple_module_core.diagnostics._expression_index import module_all_tables
|
|
9
|
+
from simple_module_core.diagnostics._types import Diagnostic, DiagnosticLevel
|
|
10
|
+
|
|
11
|
+
if TYPE_CHECKING:
|
|
12
|
+
from simple_module_core.module import ModuleBase
|
|
13
|
+
|
|
14
|
+
_CODE_BEHIND = "SM010"
|
|
15
|
+
_MODULE_NAME = "migrations"
|
|
16
|
+
_SUGGEST_MIGRATE = "Run: make migrate"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def module_table_names(modules: Iterable[ModuleBase]) -> set[str]:
|
|
20
|
+
"""Names of the tables declared in each module's ``models`` (the set SM026 checks)."""
|
|
21
|
+
return {table.name for mod in modules for table in module_all_tables(mod)}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class MigrationDiagnostics:
|
|
25
|
+
"""Validates database migration state."""
|
|
26
|
+
|
|
27
|
+
def check_heads(self, current: Set[str], heads: Set[str]) -> list[Diagnostic]:
|
|
28
|
+
"""SM010 for a database missing any head; the boot check's ``heads - current`` rule.
|
|
29
|
+
|
|
30
|
+
A never-migrated database is a WARNING: a production boot sends it to
|
|
31
|
+
``/setup`` rather than refusing it. One behind on any branch is an ERROR,
|
|
32
|
+
because a production boot refuses it.
|
|
33
|
+
"""
|
|
34
|
+
missing = heads - current
|
|
35
|
+
if not missing:
|
|
36
|
+
return []
|
|
37
|
+
if not current:
|
|
38
|
+
return [
|
|
39
|
+
Diagnostic(
|
|
40
|
+
level=DiagnosticLevel.WARNING,
|
|
41
|
+
code=_CODE_BEHIND,
|
|
42
|
+
message="Database has never been migrated (no alembic_version)",
|
|
43
|
+
module_name=_MODULE_NAME,
|
|
44
|
+
suggestion=_SUGGEST_MIGRATE,
|
|
45
|
+
)
|
|
46
|
+
]
|
|
47
|
+
return [
|
|
48
|
+
Diagnostic(
|
|
49
|
+
level=DiagnosticLevel.ERROR,
|
|
50
|
+
code=_CODE_BEHIND,
|
|
51
|
+
message=(
|
|
52
|
+
f"Database is behind: missing heads {', '.join(map(repr, sorted(missing)))}"
|
|
53
|
+
),
|
|
54
|
+
module_name=_MODULE_NAME,
|
|
55
|
+
suggestion=_SUGGEST_MIGRATE,
|
|
56
|
+
)
|
|
57
|
+
]
|
|
58
|
+
|
|
59
|
+
def check_revision_mismatch(
|
|
60
|
+
self,
|
|
61
|
+
current_revision: str | None,
|
|
62
|
+
head_revision: str | None,
|
|
63
|
+
) -> list[Diagnostic]:
|
|
64
|
+
"""SM010: Error if database is not at the migration head."""
|
|
65
|
+
if current_revision == head_revision:
|
|
66
|
+
return []
|
|
67
|
+
return [
|
|
68
|
+
Diagnostic(
|
|
69
|
+
level=DiagnosticLevel.ERROR,
|
|
70
|
+
code=_CODE_BEHIND,
|
|
71
|
+
message=(f"Database at revision {current_revision!r}, expected {head_revision!r}"),
|
|
72
|
+
module_name=_MODULE_NAME,
|
|
73
|
+
suggestion=_SUGGEST_MIGRATE,
|
|
74
|
+
)
|
|
75
|
+
]
|
|
76
|
+
|
|
77
|
+
def check_table_coverage(
|
|
78
|
+
self,
|
|
79
|
+
module_tables: set[str],
|
|
80
|
+
migrated_tables: set[str],
|
|
81
|
+
) -> list[Diagnostic]:
|
|
82
|
+
"""SM011: Warning for module tables a database at every head does not have."""
|
|
83
|
+
missing = module_tables - migrated_tables
|
|
84
|
+
return [
|
|
85
|
+
Diagnostic(
|
|
86
|
+
level=DiagnosticLevel.WARNING,
|
|
87
|
+
code="SM011",
|
|
88
|
+
message=(
|
|
89
|
+
f"Table '{table}' is declared in models but missing from the migrated "
|
|
90
|
+
"database, so no migration creates it"
|
|
91
|
+
),
|
|
92
|
+
module_name=_MODULE_NAME,
|
|
93
|
+
suggestion=f'Run: make migration msg="add {table}"',
|
|
94
|
+
)
|
|
95
|
+
for table in sorted(missing)
|
|
96
|
+
]
|
{simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_module.py
RENAMED
|
@@ -9,6 +9,7 @@ from typing import TYPE_CHECKING
|
|
|
9
9
|
from simple_module_core.diagnostics._coupling import check_framework_module_coupling
|
|
10
10
|
from simple_module_core.diagnostics._css import check_module_css
|
|
11
11
|
from simple_module_core.diagnostics._dependencies import check_missing_dependencies
|
|
12
|
+
from simple_module_core.diagnostics._durable_names import check_durable_names
|
|
12
13
|
from simple_module_core.diagnostics._heavy_task_imports import check_heavy_task_imports
|
|
13
14
|
from simple_module_core.diagnostics._inertia_api import check_inertia_api_calls
|
|
14
15
|
from simple_module_core.diagnostics._js_workspace import check_js_workspace_files
|
|
@@ -48,6 +49,7 @@ class ModuleDiagnostics:
|
|
|
48
49
|
diagnostics.extend(check_inertia_api_calls(mod, src_dir))
|
|
49
50
|
diagnostics.extend(check_module_css(mod, src_dir))
|
|
50
51
|
diagnostics.extend(check_heavy_task_imports(mod, src_dir))
|
|
52
|
+
diagnostics.extend(check_durable_names(mod, src_dir))
|
|
51
53
|
|
|
52
54
|
return diagnostics
|
|
53
55
|
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
"""Minimal ``.env`` parser + env-var helpers — dependency-free.
|
|
2
2
|
|
|
3
|
+
Also holds ``absolutize_sqlite_url``, which anchors a relative SQLite
|
|
4
|
+
``SM_DATABASE_URL`` at the project root for both the app and the doctor CLI.
|
|
5
|
+
|
|
3
6
|
Used in places that can't or shouldn't pull in ``pydantic-settings`` (the
|
|
4
7
|
diagnostics CLI runs before the host package is imported; the users-module
|
|
5
8
|
bootstrap runs after settings are constructed and needs to read values that
|
|
@@ -139,3 +142,23 @@ def env_bool(name: str, default: bool = False) -> bool:
|
|
|
139
142
|
if raw in BOOL_LITERALS_FALSE:
|
|
140
143
|
return False
|
|
141
144
|
return default
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def absolutize_sqlite_url(url: str, *, anchor: Path) -> str:
|
|
148
|
+
"""Rewrite a relative sqlite path to an absolute one under ``anchor``.
|
|
149
|
+
|
|
150
|
+
``sqlite+aiosqlite:///./host/app.db`` means "relative to the project
|
|
151
|
+
root" by convention, but SQLAlchemy resolves it against the process cwd
|
|
152
|
+
— correct in the web process (which chdirs) and silently wrong in every
|
|
153
|
+
CLI run from a subdirectory. Absolute paths (``:////...``), ``:memory:``,
|
|
154
|
+
SQLite URI-mode paths (``file:...?uri=true``), and non-sqlite URLs pass
|
|
155
|
+
through untouched.
|
|
156
|
+
"""
|
|
157
|
+
if not url.startswith("sqlite"):
|
|
158
|
+
return url
|
|
159
|
+
scheme, sep, rest = url.partition(":///")
|
|
160
|
+
if not sep or not rest or rest.startswith(("/", ":memory:", "file:")):
|
|
161
|
+
return url
|
|
162
|
+
path_part, query_sep, query = rest.partition("?")
|
|
163
|
+
resolved = (anchor / path_part).resolve()
|
|
164
|
+
return f"{scheme}:///{resolved}{query_sep}{query}"
|
|
@@ -86,7 +86,7 @@ class Invalidation:
|
|
|
86
86
|
channel: str
|
|
87
87
|
#: Which entry, or ``None`` for "the whole channel". A string because it has
|
|
88
88
|
#: to survive JSON; a consumer keyed by something else converts on the way
|
|
89
|
-
#: back in (see ``users.session_revocation``).
|
|
89
|
+
#: back in (see ``users.accounts.session_revocation``).
|
|
90
90
|
key: str | None = None
|
|
91
91
|
#: The publishing process's :attr:`InvalidationBus.origin`. Lets a bus
|
|
92
92
|
#: ignore its own broadcast coming back off the transport, which it has
|
|
@@ -9,7 +9,7 @@ longer depends on the ``settings`` plugin being installed or enabled.
|
|
|
9
9
|
from __future__ import annotations
|
|
10
10
|
|
|
11
11
|
import re
|
|
12
|
-
from typing import Any, get_args, get_origin
|
|
12
|
+
from typing import Any, Final, get_args, get_origin
|
|
13
13
|
from urllib.parse import urlsplit
|
|
14
14
|
|
|
15
15
|
from pydantic import BaseModel, SecretBytes, SecretStr
|
|
@@ -20,7 +20,15 @@ from pydantic import BaseModel, SecretBytes, SecretStr
|
|
|
20
20
|
_SECRET_PATTERNS = re.compile(
|
|
21
21
|
r"(password|secret|api[_-]?key|private[_-]?key|token[_-]?secret)", re.I
|
|
22
22
|
)
|
|
23
|
+
|
|
23
24
|
SECRET_MASK = "••••••••"
|
|
25
|
+
# Keys whose stored value must never leave the settings API, the raw
|
|
26
|
+
# key/value browse screen or the audit trail. ``host.secret_key`` is the session-signing key: the
|
|
27
|
+
# hosting layer generates and persists it here when SM_SECRET_KEY is unset, and
|
|
28
|
+
# anyone who can read it can forge a session cookie for any account. Masked on
|
|
29
|
+
# the way out only — the boot-time reader goes straight to SQL, so hiding it
|
|
30
|
+
# here costs nothing.
|
|
31
|
+
SENSITIVE_KEYS: Final = frozenset({"host.secret_key"})
|
|
24
32
|
|
|
25
33
|
# Value types that cannot carry credential material, so a secret-ish *name*
|
|
26
34
|
# on one of them is a false positive rather than something to hide.
|
|
@@ -92,7 +100,10 @@ def conceals_secret(name: str, value: Any, value_type: str | None = None) -> boo
|
|
|
92
100
|
``broker_url``, while that field's password-free default is still shown.
|
|
93
101
|
"""
|
|
94
102
|
return (
|
|
95
|
-
name in
|
|
103
|
+
name in SENSITIVE_KEYS
|
|
104
|
+
or name in _TYPED_SECRET_KEYS
|
|
105
|
+
or is_named_secret(name, value_type)
|
|
106
|
+
or embeds_credential(value)
|
|
96
107
|
)
|
|
97
108
|
|
|
98
109
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"""Base class for DB-backed settings that must not read the environment.
|
|
2
2
|
|
|
3
3
|
Module and host settings are hydrated from the settings store at boot and
|
|
4
|
-
hot-swapped through ``settings.reload.apply_changes_and_reload``. The DB is
|
|
4
|
+
hot-swapped through ``settings.module_settings.reload.apply_changes_and_reload``. The DB is
|
|
5
5
|
meant to be the only source of truth for them.
|
|
6
6
|
|
|
7
7
|
Subclassing ``BaseSettings`` and simply omitting ``env_prefix`` does **not**
|
{simple_module_core-0.0.43 → simple_module_core-0.0.45}/simple_module_core/settings_hydrate.py
RENAMED
|
@@ -18,6 +18,7 @@ from __future__ import annotations
|
|
|
18
18
|
|
|
19
19
|
import logging
|
|
20
20
|
import types
|
|
21
|
+
from functools import cache
|
|
21
22
|
from typing import Any, Literal, Protocol, Union, get_args, get_origin
|
|
22
23
|
|
|
23
24
|
from pydantic import BaseModel
|
|
@@ -105,6 +106,7 @@ def input_type_for_field(cls: type[BaseSettings], field_name: str) -> tuple[str,
|
|
|
105
106
|
return "json", nullable
|
|
106
107
|
|
|
107
108
|
|
|
109
|
+
@cache
|
|
108
110
|
def env_supplied_fields(cls: type[BaseSettings]) -> frozenset[str]:
|
|
109
111
|
"""Fields the class's own non-init sources (env, ``.env``, secrets) answer.
|
|
110
112
|
|
|
@@ -114,6 +116,9 @@ def env_supplied_fields(cls: type[BaseSettings]) -> frozenset[str]:
|
|
|
114
116
|
model validator can reject the env-only view (a production broker check)
|
|
115
117
|
that the DB-merged one would pass.
|
|
116
118
|
|
|
119
|
+
Cached per class for the process lifetime: environment, dotenv and secret
|
|
120
|
+
files must be configured before first use; changes require a restart.
|
|
121
|
+
|
|
117
122
|
A class with no ``env_prefix`` reports nothing from its sources: a plain
|
|
118
123
|
``BaseSettings`` reads bare, case-insensitive names, so a field called
|
|
119
124
|
``user``, ``home`` or ``path`` would be pinned by the process environment
|
|
@@ -128,7 +133,7 @@ def env_supplied_fields(cls: type[BaseSettings]) -> frozenset[str]:
|
|
|
128
133
|
seeded = getattr(cls, "env_seeded_fields", None)
|
|
129
134
|
if callable(seeded):
|
|
130
135
|
found.update(seeded())
|
|
131
|
-
return frozenset(found
|
|
136
|
+
return frozenset(found & cls.model_fields.keys())
|
|
132
137
|
|
|
133
138
|
|
|
134
139
|
def _source_supplied_fields(cls: type[BaseSettings]) -> set[str]:
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"""Serialize cache fills per key, and fill caches without storing stale reads.
|
|
2
|
+
|
|
3
|
+
``SingleFlight`` serializes concurrent misses for one key; callers recheck their
|
|
4
|
+
cache inside the lock. Own one instance per application/cache. No background
|
|
5
|
+
task outlives its caller: failed or cancelled fills release the lock and the
|
|
6
|
+
next waiter retries normally.
|
|
7
|
+
|
|
8
|
+
``fill`` is the epoch-checked read loop shared by caches that an invalidation
|
|
9
|
+
can drop mid-read: a read only gets stored if no invalidation landed while it
|
|
10
|
+
ran.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import asyncio
|
|
16
|
+
from collections.abc import AsyncGenerator, Awaitable, Callable, Hashable
|
|
17
|
+
from contextlib import asynccontextmanager
|
|
18
|
+
from dataclasses import dataclass, field
|
|
19
|
+
from enum import Enum
|
|
20
|
+
from typing import Final, Literal
|
|
21
|
+
|
|
22
|
+
# Epoch-checked fills retry at most this many reads; continuous invalidation
|
|
23
|
+
# must not keep a request looping.
|
|
24
|
+
MAX_FILL_ATTEMPTS = 3
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class Miss(Enum):
|
|
28
|
+
"""Type of :data:`MISS`; a cache lookup's "nothing cached" answer."""
|
|
29
|
+
|
|
30
|
+
MISS = "miss"
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
MISS: Final = Miss.MISS
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _nothing_cached() -> Literal[Miss.MISS]:
|
|
37
|
+
return MISS
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
async def fill[T](
|
|
41
|
+
read: Callable[[], Awaitable[T]],
|
|
42
|
+
*,
|
|
43
|
+
epoch: Callable[[], int],
|
|
44
|
+
store: Callable[[T], None],
|
|
45
|
+
cached: Callable[[], T | Literal[Miss.MISS]] = _nothing_cached,
|
|
46
|
+
) -> T:
|
|
47
|
+
"""Read through *read* and *store* the result unless invalidated meanwhile.
|
|
48
|
+
|
|
49
|
+
*epoch* returns the cache's invalidation counter (bumped by every forget);
|
|
50
|
+
a read whose epoch changed while it ran is not stored and is retried, after
|
|
51
|
+
rechecking *cached* (return :data:`MISS` when nothing is cached; ``None``
|
|
52
|
+
is a valid cached value). At most :data:`MAX_FILL_ATTEMPTS` reads.
|
|
53
|
+
|
|
54
|
+
After the last attempt the last read is returned uncached. It is **not**
|
|
55
|
+
guaranteed to be newer than an invalidation that landed during it: it is
|
|
56
|
+
equivalent to an uncached read racing a concurrent change, so the caller
|
|
57
|
+
answers this one request with it, and it is never cached.
|
|
58
|
+
"""
|
|
59
|
+
attempts = 0
|
|
60
|
+
while True:
|
|
61
|
+
hit = cached()
|
|
62
|
+
if hit is not MISS:
|
|
63
|
+
return hit
|
|
64
|
+
started = epoch()
|
|
65
|
+
value = await read()
|
|
66
|
+
attempts += 1
|
|
67
|
+
if epoch() == started:
|
|
68
|
+
store(value)
|
|
69
|
+
return value
|
|
70
|
+
if attempts >= MAX_FILL_ATTEMPTS:
|
|
71
|
+
return value
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
@dataclass
|
|
75
|
+
class _Flight:
|
|
76
|
+
lock: asyncio.Lock = field(default_factory=asyncio.Lock)
|
|
77
|
+
users: int = 0
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
class SingleFlight:
|
|
81
|
+
def __init__(self) -> None:
|
|
82
|
+
self._locks: dict[Hashable, _Flight] = {}
|
|
83
|
+
|
|
84
|
+
@asynccontextmanager
|
|
85
|
+
async def hold(self, key: Hashable) -> AsyncGenerator[None]:
|
|
86
|
+
flight = self._locks.get(key)
|
|
87
|
+
if flight is None:
|
|
88
|
+
flight = self._locks[key] = _Flight()
|
|
89
|
+
flight.users += 1
|
|
90
|
+
try:
|
|
91
|
+
async with flight.lock:
|
|
92
|
+
yield
|
|
93
|
+
finally:
|
|
94
|
+
flight.users -= 1
|
|
95
|
+
if not flight.users:
|
|
96
|
+
del self._locks[key]
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""Settings source metadata is a process snapshot, isolated by settings class."""
|
|
2
|
+
|
|
3
|
+
from pydantic import Field
|
|
4
|
+
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
5
|
+
from simple_module_core.settings_hydrate import env_supplied_fields
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def test_source_fields_are_computed_once_per_class(tmp_path, monkeypatch):
|
|
9
|
+
dotenv = tmp_path / "settings.env"
|
|
10
|
+
dotenv.write_text("CACHE_FILE=file\n")
|
|
11
|
+
monkeypatch.setenv("CACHE_PROCESS", "process")
|
|
12
|
+
calls = []
|
|
13
|
+
|
|
14
|
+
class Config(BaseSettings):
|
|
15
|
+
model_config = SettingsConfigDict(env_prefix="CACHE_", env_file=dotenv)
|
|
16
|
+
process: str = ""
|
|
17
|
+
file: str = ""
|
|
18
|
+
seeded: str = ""
|
|
19
|
+
alias: str = Field("", alias="CACHE_ALIAS")
|
|
20
|
+
|
|
21
|
+
@classmethod
|
|
22
|
+
def settings_customise_sources(cls, *args, **kwargs):
|
|
23
|
+
calls.append("sources")
|
|
24
|
+
return super().settings_customise_sources(*args, **kwargs)
|
|
25
|
+
|
|
26
|
+
@classmethod
|
|
27
|
+
def env_seeded_fields(cls):
|
|
28
|
+
calls.append("seeded")
|
|
29
|
+
return {"seeded", "unknown"}
|
|
30
|
+
|
|
31
|
+
monkeypatch.setenv("CACHE_ALIAS", "alias")
|
|
32
|
+
assert env_supplied_fields(Config) == {"process", "file", "seeded", "alias"}
|
|
33
|
+
assert isinstance(env_supplied_fields(Config), frozenset)
|
|
34
|
+
dotenv.unlink()
|
|
35
|
+
monkeypatch.delenv("CACHE_PROCESS")
|
|
36
|
+
monkeypatch.delenv("CACHE_ALIAS")
|
|
37
|
+
assert env_supplied_fields(Config) == {"process", "file", "seeded", "alias"}
|
|
38
|
+
assert calls == ["sources", "seeded"]
|
|
39
|
+
|
|
40
|
+
class Other(Config):
|
|
41
|
+
@classmethod
|
|
42
|
+
def env_seeded_fields(cls):
|
|
43
|
+
return set()
|
|
44
|
+
|
|
45
|
+
assert env_supplied_fields(Other) == set()
|