High Fastapi

Use dependency injection

Rule Description

Use FastAPI's Depends() for dependency injection:
- Database sessions
- Authentication
- Configuration
- External services

```python
from fastapi import Depends
from sqlalchemy.ext.asyncio import AsyncSession

async def get_db() -> AsyncGenerator[AsyncSession, None]:
async with async_session() as session:
yield session

async def get_current_user(
token: str = Depends(oauth2_scheme),
db: AsyncSession = Depends(get_db)
) -> User:
...

@app.get("/me")
async def get_me(user: User = Depends(get_current_user)):
return user
```

Included in Rulesets

Django Best Practices
36 rules • 1 standard
View
FastAPI Guidelines
36 rules • 1 standard
View
Python Standards
33 rules • 2 standards
View
Code Review Standards
58 rules • 0 standards
View

Add This Rule

Sign in to add this rule to your workspace

Sign in with GitHub

Details

Severity
High
Category
Fastapi
Used in
4 rulesets