Files
homeassistant/custom_components/yandex_smart_home/schema/base.py
Victor Alexandrovich Tsyrenschikov 373ed28445 python
2026-03-30 20:25:42 +05:00

46 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Base class for API response schemas."""
from __future__ import annotations
from typing import Any, Generic, TypeVar
from pydantic import BaseModel, ConfigDict, model_serializer
T = TypeVar("T")
class APIModel(BaseModel):
"""Base API response model."""
model_config = ConfigDict(
extra="forbid",
populate_by_name=True,
arbitrary_types_allowed=True,
validate_assignment=True,
# strict=False, # можно включить, если нужно более строгое приведение типов
)
@model_serializer(mode="wrap")
def serialize(self, handler, info):
"""Serialize model excluding None values."""
dumped = handler(self)
if info.mode == "json":
# для json() — исключаем None и используем ensure_ascii=False
return {k: v for k, v in dumped.items() if v is not None}
return dumped
def as_dict(self) -> dict[str, Any]:
"""Generate a dictionary representation of the model (exclude None)."""
return self.model_dump(exclude_none=True, by_alias=False)
def as_json(self) -> str:
"""Generate a JSON representation of the model (exclude None, ensure_ascii=False)."""
return self.model_dump_json(exclude_none=True, ensure_ascii=False)
class GenericAPIModel(APIModel, Generic[T]):
"""Base generic API response model."""
# В pydantic v2 GenericModel больше не нужен — достаточно Generic + BaseModel
# Если нужно что-то специфическое — добавляй поля/валидаторы здесь
pass