Skip to content

Refactor FastAPI app initialization and update comments - #32

Closed
Keerthi-10-05 wants to merge 2 commits into
DevSidd2006:mainfrom
Keerthi-10-05:patch-1
Closed

Keerthi-10-05 wants to merge 2 commits into
DevSidd2006:mainfrom
Keerthi-10-05:patch-1

Conversation

@Keerthi-10-05

@Keerthi-10-05 Keerthi-10-05 commented Oct 1, 2026 •

Copy link
Copy Markdown

Refactor FastAPI application initialization to use create_app() for improved configuration flexibility. Update comments and docstrings for clarity.

Description

Please include a summary of the change and which issue it fixes. Include relevant motivation and context.

Fixes # (issue)

Screenshots (if visual change)

For any UI/visual change, add before & after screenshots or a short GIF.

Before After

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring / Code cleanup

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Include instructions so we can reproduce.

  • Automated tests run locally (pytest tests/ -v)
  • Tested in mock mode without external API keys (LLM_PROVIDER=mock)
  • UI tested across pages/ (if applicable)

Checklist:

  • I have performed a self-review of my own code
  • New and existing unit tests pass locally with my changes
  • I have not committed any secrets, API keys, credentials, or personal candidate data
  • AI tools are welcome, but I've reviewed and verified all AI-assisted content myself
  • For visual/UI changes, I've included before/after screenshots or a GIF above

Summary by Sourcery

Refactor FastAPI initialization to support independently configured applications while preserving existing deployment and testing entry points.

Enhancements:

  • Refactor FastAPI application construction around an explicit factory while retaining the documented module-level application entry point.
  • Preserve configurable application wiring seams and lazy provider initialization across API services.
  • Expand and clarify application endpoint, static hosting, deployment, and health-check documentation.

Refactor FastAPI application initialization to use create_app() for improved configuration flexibility. Update comments and docstrings for clarity.
@sourcery-ai

sourcery-ai Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Refactors api/app.py around an explicit create_app(settings) factory while preserving the existing uvicorn/test entry point and runtime behavior, and substantially improves comments and docstrings describing configuration, wiring, endpoints, and static hosting.

Flow diagram for FastAPI application factory initialization

flowchart TD
    Start["Import api.app"] --> Factory["create_app(settings)"]
    Factory --> Resolve{"settings provided?"}
    Resolve -->|Yes| UseSettings["Use explicit AppSettings"]
    Resolve -->|No| CachedSettings["get_settings()"]
    UseSettings --> Build["Build FastAPI application"]
    CachedSettings --> Build
    Build --> Wiring["Initialize app.state wiring seams"]
    Wiring --> Middleware["install_middleware(app, settings)"]
    Middleware --> Routes["include_router(...)"]
    Routes --> Endpoints["Register health and root endpoints"]
    Endpoints --> Static{"serve_static_pages enabled?"}
    Static -->|Yes| Mount["Mount pages at /app"]
    Static -->|No| Return["Return app"]
    Mount --> Return
    Return --> Export["Export module-level app"]
Loading

File-Level Changes

Change Details Files
Clarify the application factory contract and configuration behavior.
  • Document the use of create_app() with optional explicit settings.
  • Preserve the module-level app = create_app() deployment and test entry point.
  • Make the None settings fallback explicit rather than relying on truthiness.
  • Expose the selected settings through app.state.settings.
api/app.py
Reorganize and clarify application assembly and dependency-injection wiring.
  • Group and document state wiring seams for registries and agent factories.
  • Add structured sections for middleware, routes, health, root, and static-page setup.
  • Preserve lazy provider resolution and per-request wiring behavior.
  • Retain all API routes, health/root behavior, and optional static page mounting.
api/app.py
Improve inline documentation for hosting, health, and service-summary behavior.
  • Explain same-origin static hosting and WebSocket requirements.
  • Document that health checks avoid external providers, databases, and credentials.
  • Document the root endpoint's credential-free configuration summary and persistence indicator.
api/app.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@github-actions

github-actions Bot commented Oct 1, 2026

Copy link
Copy Markdown

👋 Thanks for opening this PR! A maintainer will review it soon.

In the meantime, please make sure you've read CONTRIBUTING.md and filled out the PR template, including screenshots for any visual changes.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="api/app.py" line_range="139" />
<code_context>
     app.state.leaderboard_factory = None
+
+    # Make the application settings available through app.state.
     app.state.settings = settings

+    # ------------------------------------------------------------------
</code_context>
<issue_to_address>
**issue (bug_risk):** When a caller creates an explicitly configured app and uses it without entering the ASGI lifespan, the first request builds `app.state.container` with the process-wide `get_settings()` result instead of the explicit settings stored on `app.state.settings`. Request dependencies then use the wrong API/auth/provider/persistence configuration.

**Triggers:** When an embedded app or test uses `TestClient(create_app(custom_settings))` without a context manager, so the lifespan has not initialized the container.

**Suggested fix:** Update `_container_from_app` to use `getattr(app.state, "settings", get_settings())` when building the fallback container.
</issue_to_address>

Sourcery assessment

Approval pending. 1 finding to address first.

Blocking findings: api/app.py:139


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread api/app.py
app.state.leaderboard_factory = None

# Make the application settings available through app.state.
app.state.settings = settings

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (bug_risk): When a caller creates an explicitly configured app and uses it without entering the ASGI lifespan, the first request builds app.state.container with the process-wide get_settings() result instead of the explicit settings stored on app.state.settings. Request dependencies then use the wrong API/auth/provider/persistence configuration.

Triggers: When an embedded app or test uses TestClient(create_app(custom_settings)) without a context manager, so the lifespan has not initialized the container.

Suggested fix: Update _container_from_app to use getattr(app.state, "settings", get_settings()) when building the fallback container.

@Keerthi-10-05

Copy link
Copy Markdown
Author

Hi, all available checks are passing, but the Vercel deployment is showing “Authorization required to deploy.” Clicking the Vercel check redirects me to a Vercel login page. Could a maintainer please authorize the deployment if required? Thanks!

@DevSidd2006 DevSidd2006 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Update _container_from_app to use getattr(app.state, "settings", get_settings()) when building the fallback container.

After this you are good to go.

@DevSidd2006 DevSidd2006 added the enhancement New feature or request label Oct 1, 2026
@DevSidd2006
DevSidd2006 self-requested a review October 28, 2026 15:49

@DevSidd2006 DevSidd2006 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks for the contribution! The create_app() factory and module-level app = create_app() entry point are already present on main, so the main goal of this PR has been addressed.

Additionally, the latest merge from main appears to have concatenated both versions of api/app.py, resulting in duplicated routers, endpoints, comments, and malformed docstrings. The file currently fails to compile.

Since the intended refactor is already implemented and repairing this PR would mostly remove duplicated code, I’m closing it. If there are any remaining improvements—such as the explicit settings is None check—please feel free to submit them in a new, focused PR. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants