Skip to content

Commit 373acca

Browse files
committed
cleanup: simplify defensive guards + remove stale migration comments
1 parent 7cf9c78 commit 373acca

7 files changed

Lines changed: 12 additions & 17 deletions

File tree

‎framework/hosting/simple_module_hosting/_inertia_shared.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ def build_i18n_block(scope: Scope, request: Request) -> dict:
2424
from what was last served on this session.
2525
* Full page loads and locale transitions ship the complete dict.
2626
"""
27+
# Test fixtures sometimes build a bare FastAPI with a partial app.state.sm
28+
# stub (e.g. permissions-only, no i18n); guard both lookups to keep them usable.
2729
sm = getattr(request.app.state, "sm", None)
2830
registry = getattr(sm, "i18n_registry", None) if sm is not None else None
2931
locale = getattr(request.state, "locale", None)

‎framework/hosting/simple_module_hosting/app_builder.py‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,6 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
214214

215215
mount_module_static_dirs(app, modules)
216216

217-
# Typed singleton container. Loose app.state.* keys remain for the
218-
# duration of the staged migration; consumers read from app.state.sm.*.
219217
app.state.sm = Services(
220218
settings=settings,
221219
db=db_state,

‎framework/hosting/simple_module_hosting/i18n_deps.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ async def get_translator(request: Request) -> Translator:
1616
1717
``request.state.locale`` is populated by LocaleMiddleware.
1818
"""
19-
registry = request.app.state.sm.i18n_registry
20-
default_locale = request.app.state.sm.settings.i18n_default_locale
19+
sm = request.app.state.sm
20+
default_locale = sm.settings.i18n_default_locale
2121
locale = getattr(request.state, "locale", default_locale)
22-
return Translator(registry, locale=locale, default_locale=default_locale)
22+
return Translator(sm.i18n_registry, locale=locale, default_locale=default_locale)
2323

2424

2525
TranslatorDep = Annotated[Translator, Depends(get_translator)]

‎framework/hosting/tests/test_app.py‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ async def test_returns_fastapi_instance(self, settings: Settings):
1717
assert isinstance(app, FastAPI)
1818

1919
async def test_app_state_has_registries(self, app: FastAPI):
20-
# After Task 10, loose keys are removed. Access via app.state.sm.* instead.
2120
assert hasattr(app.state, "sm")
2221
sm = app.state.sm
2322
assert sm.menu_registry is not None
@@ -85,7 +84,6 @@ async def test_app_state_has_sm_services(
8584

8685
sm = app.state.sm
8786
assert isinstance(sm, Services)
88-
# All registries and state are accessed via app.state.sm.* after Task 10.
8987
assert sm.settings is not None
9088
assert sm.db is not None
9189
assert sm.event_bus is not None

‎modules/auth/tests/test_deps.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import importlib.resources
66
from pathlib import Path
7+
from types import SimpleNamespace
78
from unittest.mock import MagicMock
89

910
import pytest
@@ -53,7 +54,7 @@ async def test_raises_403_when_missing_permission(self, app):
5354
check_fn = dep.dependency
5455

5556
request = MagicMock()
56-
request.app.state.perm_registry = app.state.sm.permissions
57+
request.app.state.sm = SimpleNamespace(permissions=app.state.sm.permissions)
5758

5859
user = UserContext(id="u1", email="u@test.com", name="User", roles=["viewer"])
5960

@@ -67,7 +68,7 @@ async def test_admin_bypasses_permission_check(self, app):
6768
check_fn = dep.dependency
6869

6970
request = MagicMock()
70-
request.app.state.perm_registry = app.state.sm.permissions
71+
request.app.state.sm = SimpleNamespace(permissions=app.state.sm.permissions)
7172

7273
admin_user = UserContext(id="a1", email="admin@test.com", name="Admin", roles=["admin"])
7374
await check_fn(request, _translator(), admin_user)
@@ -80,7 +81,7 @@ async def test_multiple_permissions_any_match(self, app):
8081
check_fn = dep.dependency
8182

8283
request = MagicMock()
83-
request.app.state.perm_registry = app.state.sm.permissions
84+
request.app.state.sm = SimpleNamespace(permissions=app.state.sm.permissions)
8485

8586
admin = UserContext(id="a1", email="a@t.com", name="Admin", roles=["admin"])
8687
await check_fn(request, _translator(), admin)
@@ -90,7 +91,7 @@ async def test_non_admin_without_permission_fails(self, app):
9091
check_fn = dep.dependency
9192

9293
request = MagicMock()
93-
request.app.state.perm_registry = app.state.sm.permissions
94+
request.app.state.sm = SimpleNamespace(permissions=app.state.sm.permissions)
9495

9596
user = UserContext(id="u1", email="u@t.com", name="User", roles=["user"])
9697
with pytest.raises(HTTPException) as exc_info:

‎modules/dashboard/dashboard/stats.py‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,7 @@ async def _count_products(db: AsyncSession) -> int:
9595
def _get_module_info(app: FastAPI) -> list[dict[str, str]]:
9696
# Reads from the module list discovered once at startup, avoiding
9797
# expensive entry-point rescans on every request.
98-
modules = app.state.sm.modules if hasattr(app.state, "sm") else None
99-
if modules is None:
100-
return []
101-
return [{"name": m.meta.name, "status": "loaded"} for m in modules]
98+
return [{"name": m.meta.name, "status": "loaded"} for m in app.state.sm.modules]
10299

103100

104101
async def _run_health_checks(app: FastAPI) -> list[dict[str, str]]:

‎modules/users/users/roles_cache.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ async def get_roles_cache(app: FastAPI) -> list[RoleSummary]:
5252
covers scenarios where startup ran before the ``users_role`` table had any
5353
rows. Once populated, subsequent calls are O(1) attribute reads.
5454
"""
55-
users = getattr(app.state, "users", None)
56-
cached = users.roles_cache if users is not None else []
55+
cached = app.state.users.roles_cache
5756
if cached:
5857
return cached
5958
return await refresh_roles_cache(app)

0 commit comments

Comments
 (0)