Repository navigation
Refactor FastAPI app initialization and update comments - #32
Keerthi-10-05 wants to merge 2 commits into
Conversation
Refactor FastAPI application initialization to use create_app() for improved configuration flexibility. Update comments and docstrings for clarity.
Reviewer's GuideRefactors Flow diagram for FastAPI application factory initializationflowchart 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"]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
👋 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. |
There was a problem hiding this comment.
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
| app.state.leaderboard_factory = None | ||
|
|
||
| # Make the application settings available through app.state. | ||
| app.state.settings = settings |
There was a problem hiding this comment.
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.
|
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
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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!
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.
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Include instructions so we can reproduce.
pytest tests/ -v)LLM_PROVIDER=mock)pages/(if applicable)Checklist:
Summary by Sourcery
Refactor FastAPI initialization to support independently configured applications while preserving existing deployment and testing entry points.
Enhancements: