mirror of
https://github.com/rjNemo/fastapi
synced 2026-06-08 11:36:40 +00:00
* Make compatible with pydantic v1 * Remove unused import * Remove unused ignores * Update pydantic version * Fix minor formatting issue * ⏪ Revert removing iterate_in_threadpool * ✨ Add backwards compatibility with Pydantic 0.32.2 with deprecation warnings * ✅ Update tests to not break when using Pydantic < 1.0.0 * 📝 Update docs for Pydantic version 1.0.0 * 📌 Update Pydantic range version to support from 0.32.2 * 🎨 Format test imports * ✨ Add support for Pydantic < 1.2 for populate_validators * ✨ Add backwards compatibility for Pydantic < 1.2.0 with required fields * 📌 Relax requirement for Pydantic to < 2.0.0 🎉 🚀 * 💚 Update pragma coverage for older Pydantic versions
36 lines
1.2 KiB
Python
36 lines
1.2 KiB
Python
from typing import Any, Sequence
|
|
|
|
from fastapi.utils import PYDANTIC_1
|
|
from pydantic import ValidationError, create_model
|
|
from pydantic.error_wrappers import ErrorList
|
|
from starlette.exceptions import HTTPException as StarletteHTTPException
|
|
from starlette.requests import Request
|
|
from starlette.websockets import WebSocket
|
|
|
|
|
|
class HTTPException(StarletteHTTPException):
|
|
def __init__(
|
|
self, status_code: int, detail: Any = None, headers: dict = None
|
|
) -> None:
|
|
super().__init__(status_code=status_code, detail=detail)
|
|
self.headers = headers
|
|
|
|
|
|
RequestErrorModel = create_model("Request")
|
|
WebSocketErrorModel = create_model("WebSocket")
|
|
|
|
|
|
class RequestValidationError(ValidationError):
|
|
def __init__(self, errors: Sequence[ErrorList]) -> None:
|
|
if PYDANTIC_1:
|
|
super().__init__(errors, RequestErrorModel)
|
|
else:
|
|
super().__init__(errors, Request) # type: ignore # pragma: nocover
|
|
|
|
|
|
class WebSocketRequestValidationError(ValidationError):
|
|
def __init__(self, errors: Sequence[ErrorList]) -> None:
|
|
if PYDANTIC_1:
|
|
super().__init__(errors, WebSocketErrorModel)
|
|
else:
|
|
super().__init__(errors, WebSocket) # type: ignore # pragma: nocover
|