Skip to content

fix: pass an optional request provider to choose_sa_engine for modern-di 4.0 - #57

Closed
lesnik512 wants to merge 2 commits into
mainfrom
modern-di-4
Closed

lesnik512 wants to merge 2 commits into
mainfrom
modern-di-4

Conversation

@lesnik512

@lesnik512 lesnik512 commented Oct 5, 2026 •

Copy link
Copy Markdown
Member

Refs modern-python/modern-di#579.
Closes #56

Summary

modern-di 4.0 makes context values required. choose_sa_engine took request: litestar.Request[...] | None = None, filled from the integration's required litestar_request_provider, so resolving dynamic_engine outside an HTTP request (tests/test_db_routing.py::test_no_request_uses_primary) raises ContextValueNotSetError on 4.0.

Following the migration guide, Dependencies now declares its own optional_request (ContextProvider(litestar.Request, scope=Scope.REQUEST, bound_type=None, default=None)) and passes it to dynamic_engine explicitly.

default= on ContextProvider exists only in modern-di 4, so this PR needs modern-di 4. It lands together with bumping the modern-di-litestar pin to the release that allows modern-di 4. That release does not exist yet, so the pin is unchanged here.

CI on the current pin

CI fails until the pin bump, because modern-di-litestar>=3,<4 resolves modern-di 3.5.0, whose ContextProvider has no default parameter:

  • lint: ty reports error[unknown-argument]: Argument `default` does not match any known parameter of `ContextProvider.__init__` at app/ioc.py:26.
  • pytest: importing app.ioc raises TypeError: ContextProvider.__init__() got an unexpected keyword argument 'default'.

Verification

Local, Python 3.14, Postgres 14 in Docker, against the modern-di main wheel (the 4.0 candidate) installed over the synced env and run with uv run --no-sync: ruff format/check and ty clean, pytest 31 passed, coverage 100%. Before this change test_no_request_uses_primary failed with ContextValueNotSetError.

Stays draft until modern-di 4.0.0 and a modern-di-litestar release that allows it are on PyPI.

@lesnik512

Copy link
Copy Markdown
Member Author

This was generated by AI.

Closing: modern-python/modern-di#602 lets a nullable or defaulted parameter fall back when its context value is unset, so the template's existing request: Request | None = None works on modern-di 4.0 without an extra provider. Checked: main at 0dd4326 against modern-di main 8cd5c07 passes ty and all 31 tests (100% coverage), including test_no_request_uses_primary.

@lesnik512 lesnik512 closed this Oct 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prepare for modern-di 4.0: optional request parameter in db routing

1 participant