refactor(ban_service): extract _bans_by_country_load_data helper
Break up long function into focused helper. Load data logic separate from aggregation.
This commit is contained in:
@@ -115,7 +115,30 @@ When adding a new response model to `backend/app/models/`:
|
||||
|
||||
---
|
||||
|
||||
## 8. Related Documents
|
||||
## 8. TypedDict for Error Metadata
|
||||
|
||||
Error response metadata uses `ErrorMetadata` (a `TypedDict` with `total=False`) instead of generic `dict[str, str | int | float | bool | None]`. This enables type-safe field access in exception handlers and type checkers can verify correct field usage.
|
||||
|
||||
```python
|
||||
# BAD — generic dict, no type narrowing
|
||||
def get_error_metadata(self) -> dict[str, str | int | float | bool | None]:
|
||||
return {"jail_name": self.name}
|
||||
|
||||
# GOOD — TypedDict, type checker knows exact fields
|
||||
def get_error_metadata(self) -> ErrorMetadata:
|
||||
return {"jail_name": self.name}
|
||||
```
|
||||
|
||||
When accessing error metadata in exception handlers, the type checker can now verify which keys are present:
|
||||
|
||||
```python
|
||||
metadata = exc.get_error_metadata()
|
||||
jail_name = metadata["jail_name"] # type checker verifies "jail_name" exists
|
||||
```
|
||||
|
||||
`ErrorMetadata` is defined in `backend/app/models/response.py` and imported via `TYPE_CHECKING` blocks in `exceptions.py` and `main.py` to avoid circular dependencies at runtime.
|
||||
|
||||
## 9. Related Documents
|
||||
|
||||
- [Architekture.md](Architekture.md) — system architecture and data flow
|
||||
- [Backend-Development.md](Backend-Development.md) — Python coding conventions, Pydantic usage
|
||||
|
||||
Reference in New Issue
Block a user