|
7 | 7 | from copy import copy
|
8 | 8 | from enum import Enum
|
9 | 9 | from functools import reduce
|
| 10 | +from typing_extensions import Unpack |
10 | 11 | from typing import (
|
11 |
| - AbstractSet, |
12 | 12 | Any,
|
13 | 13 | Callable,
|
14 | 14 | Dict,
|
|
30 | 30 | from pydantic import BaseModel, ConfigDict, TypeAdapter, field_validator
|
31 | 31 | from pydantic._internal._model_construction import ModelMetaclass
|
32 | 32 | from pydantic._internal._repr import Representation
|
33 |
| -from pydantic.fields import FieldInfo as PydanticFieldInfo |
| 33 | +from pydantic.fields import FieldInfo as PydanticFieldInfo, _FieldInfoInputs |
34 | 34 | from pydantic_core import PydanticUndefined as Undefined
|
35 | 35 | from pydantic_core import PydanticUndefinedType as UndefinedType
|
36 | 36 | from redis.commands.json.path import Path
|
@@ -155,7 +155,7 @@ def validate_model_fields(model: Type["RedisModel"], field_values: Dict[str, Any
|
155 | 155 | obj = getattr(obj, sub_field)
|
156 | 156 | return
|
157 | 157 |
|
158 |
| - if field_name not in model.__fields__: # type: ignore |
| 158 | + if field_name not in model.model_fields: # type: ignore |
159 | 159 | raise QuerySyntaxError(
|
160 | 160 | f"The field {field_name} does not exist on the model {model.__name__}"
|
161 | 161 | )
|
@@ -1170,66 +1170,22 @@ def schema(self):
|
1170 | 1170 |
|
1171 | 1171 |
|
1172 | 1172 | def Field(
|
1173 |
| - default: Any = Undefined, |
1174 |
| - *, |
1175 |
| - default_factory: Callable[[], Any] | Callable[[dict[str, Any]], Any] | None = None, |
1176 |
| - alias: Optional[str] = None, |
1177 |
| - title: Optional[str] = None, |
1178 |
| - description: Optional[str] = None, |
1179 |
| - exclude: Union[ |
1180 |
| - AbstractSet[Union[int, str]], Mapping[Union[int, str], Any], Any |
1181 |
| - ] = None, |
1182 |
| - include: Union[ |
1183 |
| - AbstractSet[Union[int, str]], Mapping[Union[int, str], Any], Any |
1184 |
| - ] = None, |
1185 |
| - const: Optional[bool] = None, |
1186 |
| - gt: Optional[float] = None, |
1187 |
| - ge: Optional[float] = None, |
1188 |
| - lt: Optional[float] = None, |
1189 |
| - le: Optional[float] = None, |
1190 |
| - multiple_of: Optional[float] = None, |
1191 |
| - min_items: Optional[int] = None, |
1192 |
| - max_items: Optional[int] = None, |
1193 |
| - min_length: Optional[int] = None, |
1194 |
| - max_length: Optional[int] = None, |
1195 |
| - allow_mutation: bool = True, |
1196 |
| - regex: Optional[str] = None, |
1197 | 1173 | primary_key: bool = False,
|
1198 | 1174 | sortable: Union[bool, UndefinedType] = Undefined,
|
1199 | 1175 | case_sensitive: Union[bool, UndefinedType] = Undefined,
|
1200 | 1176 | index: Union[bool, UndefinedType] = Undefined,
|
1201 | 1177 | full_text_search: Union[bool, UndefinedType] = Undefined,
|
1202 | 1178 | vector_options: Optional[VectorFieldOptions] = None,
|
1203 |
| - schema_extra: Optional[Dict[str, Any]] = None, |
| 1179 | + **kwargs: Unpack[_FieldInfoInputs], |
1204 | 1180 | ) -> Any:
|
1205 |
| - current_schema_extra = schema_extra or {} |
1206 | 1181 | field_info = FieldInfo(
|
1207 |
| - default, |
1208 |
| - default_factory=default_factory, |
1209 |
| - alias=alias, |
1210 |
| - title=title, |
1211 |
| - description=description, |
1212 |
| - exclude=exclude, |
1213 |
| - include=include, |
1214 |
| - const=const, |
1215 |
| - gt=gt, |
1216 |
| - ge=ge, |
1217 |
| - lt=lt, |
1218 |
| - le=le, |
1219 |
| - multiple_of=multiple_of, |
1220 |
| - min_items=min_items, |
1221 |
| - max_items=max_items, |
1222 |
| - min_length=min_length, |
1223 |
| - max_length=max_length, |
1224 |
| - allow_mutation=allow_mutation, |
1225 |
| - regex=regex, |
| 1182 | + **kwargs, |
1226 | 1183 | primary_key=primary_key,
|
1227 | 1184 | sortable=sortable,
|
1228 | 1185 | case_sensitive=case_sensitive,
|
1229 | 1186 | index=index,
|
1230 | 1187 | full_text_search=full_text_search,
|
1231 | 1188 | vector_options=vector_options,
|
1232 |
| - **current_schema_extra, |
1233 | 1189 | )
|
1234 | 1190 | return field_info
|
1235 | 1191 |
|
@@ -1410,7 +1366,7 @@ class RedisModel(BaseModel, abc.ABC, metaclass=ModelMeta):
|
1410 | 1366 | Meta = DefaultMeta
|
1411 | 1367 |
|
1412 | 1368 | model_config = ConfigDict(
|
1413 |
| - from_attributes=True, arbitrary_types_allowed=True, extra="allow" |
| 1369 | + from_attributes=True, arbitrary_types_allowed=True, extra="allow", validate_default=True |
1414 | 1370 | )
|
1415 | 1371 |
|
1416 | 1372 | def __init__(__pydantic_self__, **data: Any) -> None:
|
@@ -1677,7 +1633,7 @@ async def save(
|
1677 | 1633 | ) -> "Model":
|
1678 | 1634 | self.check()
|
1679 | 1635 | db = self._get_db(pipeline)
|
1680 |
| - document = jsonable_encoder(self.dict()) |
| 1636 | + document = jsonable_encoder(self.model_dump()) |
1681 | 1637 |
|
1682 | 1638 | # filter out values which are `None` because they are not valid in a HSET
|
1683 | 1639 | document = {k: v for k, v in document.items() if v is not None}
|
@@ -1915,7 +1871,7 @@ async def get(cls: Type["Model"], pk: Any) -> "Model":
|
1915 | 1871 | document = json.dumps(await cls.db().json().get(cls.make_key(pk)))
|
1916 | 1872 | if document == "null":
|
1917 | 1873 | raise NotFoundError
|
1918 |
| - return cls.parse_raw(document) |
| 1874 | + return cls.model_validate_json(document) |
1919 | 1875 |
|
1920 | 1876 | @classmethod
|
1921 | 1877 | def redisearch_schema(cls):
|
|
0 commit comments