fastapi lifespan - #29
Merged
Merged
Conversation
☂️ Python Coverage
Overall Coverage
New FilesNo new covered files... Modified Files
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors pyfastapi application lifecycle handling to use FastAPI’s lifespan mechanism, moving logger initialization out of import-time and adding a shutdown hook to dispose the SQLAlchemy engine.
Changes:
- Added a FastAPI
lifespanasync context manager that initializes logging on startup and disposes the SQLAlchemy engine on shutdown. - Removed import-time logger initialization from
pyfastapi/__init__.py, leaving it as a cleanappre-export. - Added contributor/agent-facing documentation in
AGENTS.mddescribing architecture, tooling, and conventions.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| pyfastapi/main.py | Introduces lifespan and wires it into FastAPI(...) for startup/shutdown behavior. |
| pyfastapi/init.py | Removes import-time side effects and re-exports app. |
| AGENTS.md | Adds project architecture/conventions documentation (with a couple of wording inaccuracies noted). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.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.
What?
Adds a FastAPI
lifespancontext manager to handle application startup and shutdown events. Specifically:init_logger()from import-time inpyfastapi/__init__.pyto thelifespanstartup phase.get_engine().dispose().Why?
Import-time side effects (like
init_logger()inpyfastapi/__init__.py) are fragile:Using a
lifespancontext manager is the idiomatic FastAPI pattern for startup/shutdown logic and eliminates these issues.How?
@asynccontextmanager async def lifespan(app: FastAPI)inpyfastapi/main.pythat:init_logger()before yielding (startup).get_engine().dispose()after yielding (shutdown).lifespan=lifespanto theFastAPI()constructor.init_logger()call frompyfastapi/__init__.py, leaving it as a clean re-export ofapp.Testing?
uv run pytestpasses (65/65 tests).uv run poe lintpasses (flake8 + mypy).uv run poe coveragemeets existing coverage.TestClienttriggers the lifespan context automatically, so integration tests exercise the startup path.Screenshots (optional)
N/A
Anything Else?
run.pyandTestClientusage remain unchanged.architecture.mdandAGENTS.mdwere updated to reflect this change.AI Summary (optional)
This pull request introduces significant improvements to the documentation and application startup/shutdown logic for the
pyfastapiproject. The main highlights are the addition of a comprehensiveAGENTS.mdfile describing the project's architecture and conventions, and a refactor to ensure logging and database engine lifecycle management are handled correctly using FastAPI's lifespan events.Documentation Enhancements:
AGENTS.mdfile providing an overview of the project's architecture, technology stack, directory structure, data model, dependency injection patterns, testing strategy, code style, CI/CD, and deployment considerations. This serves as a valuable reference for contributors and AI coding agents.Application Lifecycle Management:
lifespancontext manager inmain.py. This ensures thatinit_logger()is called on startup and the database engine is properly disposed of on shutdown, improving resource management and reliability.pyfastapi/__init__.py, as this is now handled within thelifespancontext.