refactor: own the lifespan composition, drop fastapi private import#20
Merged
Conversation
setup_di chained its lifespan onto the app via the private fastapi.routing._merge_lifespan_context — a FastAPI-internals dependency that could break silently on upgrade, with no test isolating it. Replace it with a small _compose_lifespan that keeps the original lifespan as the outer context, opens the container inside it, and passes the original's yielded state through. This uses only the public app.router.lifespan_context; the simplification is valid because our nested manager always yields None. Pin the contract the private helper provided with a characterization test: setup_di over an app that already has a state-yielding lifespan — its startup/shutdown both fire, its state reaches requests, and the container opens/closes. Behavior unchanged; 100% coverage. Promotes the change into architecture/container-lifecycle.md per the planning convention. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Candidate 2 from the architecture review — contain (here, remove) the private-API lifespan coupling. Behavior unchanged.
What
setup_dichained its lifespan viafastapi.routing._merge_lifespan_context— a private FastAPI symbol that could break silently on upgrade, with no test isolating the risk.Research showed the helper's only real job is merging two ASGI state dicts, and our
_lifespan_manageralways yieldsNone. So a correct composition collapses to ~4 lines using only the publicapp.router.lifespan_context:Original stays the outer context (same startup/shutdown order, same reopen-across-cycles behavior); the container opens inside it.
Wins (review's terms)
Tests
test_setup_di_composes_with_existing_lifespan: an app with a state-yielding lifespan — asserts startup + shutdown both fire, the original's state reachesrequest.state, and the container opens/closes. Added first as a characterization test (green on the old_mergecode), stays green after the refactor — pinning the exact contract the private helper provided. The state-passthrough path was previously untested.Notes
caston the composed lifespan mirrors FastAPI's own_merge_lifespan_context(which carries aty: ignorefor the same reason):LifespanisCM[None] | CM[Mapping]and can't express ourCM[Mapping | None].architecture/container-lifecycle.mdpromoted per the planning convention.Verification
just test-ci→ 100% coverage, 8 passed.just lint-ci→ ruff / ty / eof /check-planningclean.🤖 Generated with Claude Code