Skip to content

fastapi lifespan - #29

Merged
heinrich10 merged 3 commits into
mainfrom
feat/fastapi_lifespan
Jun 2, 2026
Merged

fastapi lifespan#29
heinrich10 merged 3 commits into
mainfrom
feat/fastapi_lifespan

Conversation

@heinrich10

Copy link
Copy Markdown
Owner

What?

Adds a FastAPI lifespan context manager to handle application startup and shutdown events. Specifically:

  • Moves init_logger() from import-time in pyfastapi/__init__.py to the lifespan startup phase.
  • Disposes the SQLAlchemy engine on shutdown via get_engine().dispose().

Why?

Import-time side effects (like init_logger() in pyfastapi/__init__.py) are fragile:

  • They run during module import, not when the application actually starts.
  • They can be triggered prematurely by test collection, IDE introspection, or background tooling.
  • There was no graceful shutdown hook to release database connections.

Using a lifespan context manager is the idiomatic FastAPI pattern for startup/shutdown logic and eliminates these issues.

How?

  • Added @asynccontextmanager async def lifespan(app: FastAPI) in pyfastapi/main.py that:
    • Calls init_logger() before yielding (startup).
    • Calls get_engine().dispose() after yielding (shutdown).
  • Passed lifespan=lifespan to the FastAPI() constructor.
  • Removed the init_logger() call from pyfastapi/__init__.py, leaving it as a clean re-export of app.

Testing?

  • uv run pytest passes (65/65 tests).
  • uv run poe lint passes (flake8 + mypy).
  • uv run poe coverage meets existing coverage.
  • TestClient triggers the lifespan context automatically, so integration tests exercise the startup path.

Screenshots (optional)

N/A

Anything Else?

  • No breaking changes. run.py and TestClient usage remain unchanged.
  • architecture.md and AGENTS.md were updated to reflect this change.

AI Summary (optional)

This pull request introduces significant improvements to the documentation and application startup/shutdown logic for the pyfastapi project. The main highlights are the addition of a comprehensive AGENTS.md file 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:

  • Added a detailed AGENTS.md file 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:

  • Refactored application startup and shutdown logic to use FastAPI's lifespan context manager in main.py. This ensures that init_logger() is called on startup and the database engine is properly disposed of on shutdown, improving resource management and reliability.
  • Removed the eager logger initialization from pyfastapi/__init__.py, as this is now handled within the lifespan context.

@github-actions

github-actions Bot commented Jun 2, 2026

Copy link
Copy Markdown

☂️ Python Coverage

current status: ✅

Overall Coverage

Lines Covered Coverage Threshold Status
1177 1158 98% 0% 🟢

New Files

No new covered files...

Modified Files

File Coverage Status
pyfastapi/init.py 100% 🟢
pyfastapi/main.py 100% 🟢
TOTAL 100% 🟢

updated for commit: d8c3e61 by action🐍

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 lifespan async 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 clean app re-export.
  • Added contributor/agent-facing documentation in AGENTS.md describing 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.

Comment thread pyfastapi/main.py Outdated
Comment thread AGENTS.md Outdated
Comment thread AGENTS.md Outdated
heinrich10 and others added 2 commits June 2, 2026 13:45
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@heinrich10
heinrich10 merged commit 46f6205 into main Jun 2, 2026
1 check passed
@heinrich10
heinrich10 deleted the feat/fastapi_lifespan branch June 2, 2026 05:47
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.

2 participants