Skip to content

Commit 49077bb

Browse files
committed
set validate_default=True
1 parent ddf3354 commit 49077bb

File tree

3 files changed

+11
-55
lines changed

3 files changed

+11
-55
lines changed

aredis_om/model/encoders.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def jsonable_encoder(
7373
encoder = getattr(obj.__config__, "json_encoders", {})
7474
if custom_encoder:
7575
encoder.update(custom_encoder)
76-
obj_dict = obj.dict(
76+
obj_dict = obj.model_dump(
7777
include=include, # type: ignore # in Pydantic
7878
exclude=exclude, # type: ignore # in Pydantic
7979
by_alias=by_alias,

aredis_om/model/model.py

Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
from copy import copy
88
from enum import Enum
99
from functools import reduce
10+
from typing_extensions import Unpack
1011
from typing import (
11-
AbstractSet,
1212
Any,
1313
Callable,
1414
Dict,
@@ -30,7 +30,7 @@
3030
from pydantic import BaseModel, ConfigDict, TypeAdapter, field_validator
3131
from pydantic._internal._model_construction import ModelMetaclass
3232
from pydantic._internal._repr import Representation
33-
from pydantic.fields import FieldInfo as PydanticFieldInfo
33+
from pydantic.fields import FieldInfo as PydanticFieldInfo, _FieldInfoInputs
3434
from pydantic_core import PydanticUndefined as Undefined
3535
from pydantic_core import PydanticUndefinedType as UndefinedType
3636
from redis.commands.json.path import Path
@@ -155,7 +155,7 @@ def validate_model_fields(model: Type["RedisModel"], field_values: Dict[str, Any
155155
obj = getattr(obj, sub_field)
156156
return
157157

158-
if field_name not in model.__fields__: # type: ignore
158+
if field_name not in model.model_fields: # type: ignore
159159
raise QuerySyntaxError(
160160
f"The field {field_name} does not exist on the model {model.__name__}"
161161
)
@@ -1170,66 +1170,22 @@ def schema(self):
11701170

11711171

11721172
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,
11971173
primary_key: bool = False,
11981174
sortable: Union[bool, UndefinedType] = Undefined,
11991175
case_sensitive: Union[bool, UndefinedType] = Undefined,
12001176
index: Union[bool, UndefinedType] = Undefined,
12011177
full_text_search: Union[bool, UndefinedType] = Undefined,
12021178
vector_options: Optional[VectorFieldOptions] = None,
1203-
schema_extra: Optional[Dict[str, Any]] = None,
1179+
**kwargs: Unpack[_FieldInfoInputs],
12041180
) -> Any:
1205-
current_schema_extra = schema_extra or {}
12061181
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,
12261183
primary_key=primary_key,
12271184
sortable=sortable,
12281185
case_sensitive=case_sensitive,
12291186
index=index,
12301187
full_text_search=full_text_search,
12311188
vector_options=vector_options,
1232-
**current_schema_extra,
12331189
)
12341190
return field_info
12351191

@@ -1410,7 +1366,7 @@ class RedisModel(BaseModel, abc.ABC, metaclass=ModelMeta):
14101366
Meta = DefaultMeta
14111367

14121368
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
14141370
)
14151371

14161372
def __init__(__pydantic_self__, **data: Any) -> None:
@@ -1677,7 +1633,7 @@ async def save(
16771633
) -> "Model":
16781634
self.check()
16791635
db = self._get_db(pipeline)
1680-
document = jsonable_encoder(self.dict())
1636+
document = jsonable_encoder(self.model_dump())
16811637

16821638
# filter out values which are `None` because they are not valid in a HSET
16831639
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":
19151871
document = json.dumps(await cls.db().json().get(cls.make_key(pk)))
19161872
if document == "null":
19171873
raise NotFoundError
1918-
return cls.parse_raw(document)
1874+
return cls.model_validate_json(document)
19191875

19201876
@classmethod
19211877
def redisearch_schema(cls):

tests/test_hash_model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ class Customer2(m.BaseHashModel):
797797
bio="Python developer, wanna work at Redis, Inc.",
798798
)
799799

800-
assert "pk" in customer.__fields__
800+
assert "pk" in customer.model_fields
801801

802802
customer = Customer2(
803803
id=1,
@@ -806,7 +806,7 @@ class Customer2(m.BaseHashModel):
806806
bio="This is member 2 who can be quite anxious until you get to know them.",
807807
)
808808

809-
assert "pk" not in customer.__fields__
809+
assert "pk" not in customer.model_fields
810810

811811

812812
@py_test_mark_asyncio

0 commit comments

Comments
 (0)