refactor: migrate to Annotated type aliases for FastAPI dependencies#261
Merged
igorbenav merged 8 commits intoMay 29, 2026
Merged
Conversation
Create user, tier, rate_limit, and api_keys dependencies.py files. Move inline service factory functions from routes.py into their respective dependencies.py module for consistency.
Replace inline Annotated/Depends with AsyncSessionDep, CurrentUserDep, CurrentSuperUserDep, and per-module service aliases (UserServiceDep, TierServiceDep, RateLimitServiceDep).
Migrate all 8 endpoints from old-style param = Depends() to CurrentUserDep, APIKeyServiceDep, and AsyncSessionDep. Reorder parameters to avoid no-default-after-default issues with key_id: int = Path(...).
Migrate 6 auth endpoints from old-style Depends() and inline Annotated to centralized aliases (OAuth2FormDep, AsyncSessionDep, SessionManagerDep, CurrentSessionDataDep, GoogleOAuthProviderDep, OAuthStateStorageDep). Reorder params to comply with Python no-default-after-default rules.
Add dependency injection overview to endpoints.md (central + per-module alias tables), document the pattern in development.md (custom dependency walkthrough with alias registration), and list the feature in README.md.
…llers
Add section separators (Database/Users/Sessions/OAuth) to
infrastructure/dependencies.py for readability.
Also fix a latent bug surfaced by this migration: /check-auth depended on
get_current_session_data, which raises 401 when there is no session and never
returns None — so its `if not session_data` branch was dead code and the
endpoint returned 401 to anonymous callers, defeating its stated purpose
(letting clients check auth status). Switch it to a new OptionalSessionDataDep
backed by get_session_from_cookie (returns None instead of raising), so
unauthenticated requests now get 200 {authenticated: false}.
Update the check_auth tests to override the dependency the endpoint actually
uses, and add a regression test that exercises the real no-cookie path.
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.
Replace all inline
Depends(...)calls and rawAnnotated[..., Depends(...)]annotations with centralized type aliases. Route signatures are cleaner and new modules can reuse the same aliases without repeating import boilerplate.Changes
Infrastructure
infrastructure/dependencies.py: 9 shared aliases:AsyncSessionDep,CurrentUserDep,CurrentSuperUserDep,OptionalUserDep,SessionManagerDep,CurrentSessionDataDep,OAuth2FormDep,GoogleOAuthProviderDep,OAuthStateStorageDepPer-module service aliases
modules/{user,tier,rate_limit,api_keys}/dependencies.py: service aliases per feature, factory functions moved out of route filesRoute files migrated
Docs
Migration pattern