High
Fastapi
Use dependency injection
Official
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
```
Add This Rule
Sign in to add this rule to your workspace
Sign in with GitHubDetails
- Severity
- High
- Category
- Fastapi
- Used in
- 4 rulesets