Skip to content

refactor: migrate to Annotated type aliases for FastAPI dependencies#261

Merged
igorbenav merged 8 commits into
benavlabs:mainfrom
emiliano-gandini-outeda:annotated-deps
May 29, 2026
Merged

refactor: migrate to Annotated type aliases for FastAPI dependencies#261
igorbenav merged 8 commits into
benavlabs:mainfrom
emiliano-gandini-outeda:annotated-deps

Conversation

@emiliano-gandini-outeda
Copy link
Copy Markdown
Collaborator

@emiliano-gandini-outeda emiliano-gandini-outeda commented May 27, 2026

Replace all inline Depends(...) calls and raw Annotated[..., 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, OAuthStateStorageDep

Per-module service aliases

  • modules/{user,tier,rate_limit,api_keys}/dependencies.py: service aliases per feature, factory functions moved out of route files

Route files migrated

Module Endpoints Notes
user/routes.py 11 Switch to aliases, param reordering
tier/routes.py 2 Switch to TierServiceDep
rate_limit/routes.py 4 Switch to RateLimitServiceDep
api_keys/routes.py 8 Switch to aliases, reorder key_id: Path
auth/routes.py 6 Switch to aliases, reorder Query params

Docs

  • endpoints.md: DI intro section with alias reference tables
  • development.md: custom dependency walkthrough with alias registration example
  • README.md: feature bullet for Annotated type aliases

Migration pattern

# Before
async def get_item(
    db: AsyncSession = Depends(async_session),
    service: ItemService = Depends(get_item_service),
) -> dict[str, Any]:
    ...
# After
async def get_item(
    db: AsyncSessionDep,
    service: ItemServiceDep,
) -> dict[str, Any]:
    ...

emiliano-gandini-outeda and others added 8 commits May 27, 2026 11:31
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>
@igorbenav igorbenav merged commit 768cddb into benavlabs:main May 29, 2026
3 checks passed
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