Skip to content

Commit

Permalink
fix(Pydantic): For type-checker purposes, have a pydantic_v2_mandator…
Browse files Browse the repository at this point in the history
…y that cannot be None
  • Loading branch information
charles-dyfis-net committed Oct 29, 2024
1 parent cfa38a0 commit 17c0944
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions litestar/contrib/pydantic/pydantic_init_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@


if TYPE_CHECKING:
import pydantic as pydantic_v2_mandatory

from litestar.config.app import AppConfig
from litestar.types.serialization import PydanticV1FieldsListType, PydanticV2FieldsListType
else:
pydantic_v2_mandatory = pydantic_v2


T = TypeVar("T")
Expand All @@ -47,13 +51,13 @@ def _dec_pydantic_v1(model_type: type[pydantic_v1.BaseModel], value: Any) -> pyd
raise ExtendedMsgSpecValidationError(errors=cast("list[dict[str, Any]]", e.errors())) from e


def _dec_pydantic_v2(model_type: type[pydantic_v2.BaseModel], value: Any, strict: bool) -> pydantic_v2.BaseModel: # pyright: ignore[reportInvalidTypeForm]
def _dec_pydantic_v2(
model_type: type[pydantic_v2_mandatory.BaseModel], value: Any, strict: bool
) -> pydantic_v2.BaseModel: # pyright: ignore[reportInvalidTypeForm]
try:
return model_type.model_validate(value, strict=strict)
except pydantic_v2.ValidationError as e:
hide_input = False
if isinstance(model_config := getattr(model_type, "model_config", None), dict):
hide_input = bool(model_config.get("hide_input_in_errors"))
except pydantic_v2_mandatory.ValidationError as e:
hide_input = model_type.model_config.get("hide_input_in_errors", False)
raise ExtendedMsgSpecValidationError(
errors=cast("list[dict[str, Any]]", e.errors(include_input=not hide_input))
) from e
Expand Down

0 comments on commit 17c0944

Please sign in to comment.