Skip to content

Commit

Permalink
Fix tests and type errors.
Browse files Browse the repository at this point in the history
The `cast` for the FeatureFlagRouters is necessary for Injector to
correctly resolve and inject instances of the type. The actual type is
`Self | FeatureFlagRouter[...]`, but the usages of the injected type do
not rely on `Self`, which messes up how Injector resolves dependencies.
  • Loading branch information
aholmes committed Oct 17, 2024
1 parent d5d2242 commit 10384df
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
10 changes: 7 additions & 3 deletions src/web/Ligare/web/middleware/feature_flags/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def get_config_type() -> type[AbstractConfig]:
def _provide_feature_flag_router(
self, injector: Injector
) -> FeatureFlagRouter[FeatureFlag]:
return injector.get(self._t_feature_flag)
return cast(FeatureFlagRouter[FeatureFlag], injector.get(self._t_feature_flag))


class DBFeatureFlagRouterModule(FeatureFlagRouterModule[DBFeatureFlag]):
Expand All @@ -86,7 +86,9 @@ def __init__(self) -> None:
def _provide_db_feature_flag_router(
self, injector: Injector
) -> FeatureFlagRouter[DBFeatureFlag]:
return injector.get(self._t_feature_flag)
return cast(
FeatureFlagRouter[DBFeatureFlag], injector.get(self._t_feature_flag)
)

@singleton
@provider
Expand All @@ -105,7 +107,9 @@ def __init__(self) -> None:
def _provide_caching_feature_flag_router(
self, injector: Injector
) -> FeatureFlagRouter[CachingFeatureFlag]:
return injector.get(self._t_feature_flag)
return cast(
FeatureFlagRouter[CachingFeatureFlag], injector.get(self._t_feature_flag)
)


P = ParamSpec("P")
Expand Down
20 changes: 12 additions & 8 deletions src/web/test/unit/middleware/test_feature_flags_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ def _user_session_app_init_hook(
bases=[],
)
)
application_modules.append(CachingFeatureFlagRouterModule)
application_modules.append(FeatureFlagMiddlewareModule())
application_modules.append(
FeatureFlagMiddlewareModule(CachingFeatureFlagRouterModule)
)

def test__FeatureFlagMiddleware__feature_flag_api_GET_requires_user_session_when_flask_login_is_configured(
self,
Expand All @@ -108,8 +109,9 @@ def app_init_hook(
application_configs: list[type[AbstractConfig]],
application_modules: list[Module | type[Module]],
):
application_modules.append(CachingFeatureFlagRouterModule)
application_modules.append(FeatureFlagMiddlewareModule())
application_modules.append(
FeatureFlagMiddlewareModule(CachingFeatureFlagRouterModule)
)

openapi_mock_controller.begin()
app = next(
Expand Down Expand Up @@ -142,8 +144,9 @@ def app_init_hook(
):
if not flask_login_is_configured:
application_modules.clear()
application_modules.append(CachingFeatureFlagRouterModule)
application_modules.append(FeatureFlagMiddlewareModule())
application_modules.append(
FeatureFlagMiddlewareModule(CachingFeatureFlagRouterModule)
)

def client_init_hook(app: CreateAppResult[FlaskApp]):
caching_feature_flag_router = app.app_injector.flask_injector.injector.get(
Expand Down Expand Up @@ -363,8 +366,9 @@ def app_init_hook(
):
if not flask_login_is_configured:
application_modules.clear()
application_modules.append(CachingFeatureFlagRouterModule)
application_modules.append(FeatureFlagMiddlewareModule())
application_modules.append(
FeatureFlagMiddlewareModule(CachingFeatureFlagRouterModule)
)

openapi_mock_controller.begin()
app = next(
Expand Down

0 comments on commit 10384df

Please sign in to comment.