Skip to content

Commit 61bcd30

Browse files
committed
Make RedisModel compatible with Pydantic 2.x
1 parent 44cbeaf commit 61bcd30

File tree

6 files changed

+40
-20
lines changed

6 files changed

+40
-20
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ sync: $(INSTALL_STAMP)
5353
lint: $(INSTALL_STAMP) dist
5454
$(POETRY) run isort --profile=black --lines-after-imports=2 ./tests/ $(NAME) $(SYNC_NAME)
5555
$(POETRY) run black ./tests/ $(NAME)
56-
$(POETRY) run flake8 --ignore=W503,E501,F401,E731,E712 ./tests/ $(NAME) $(SYNC_NAME)
56+
$(POETRY) run flake8 --ignore=E231,E501,E712,E731,F401,W503 ./tests/ $(NAME) $(SYNC_NAME)
5757
$(POETRY) run mypy ./tests/ $(NAME) $(SYNC_NAME) --ignore-missing-imports --exclude migrate.py --exclude _compat\.py$
5858
$(POETRY) run bandit -r $(NAME) $(SYNC_NAME) -s B608
5959

aredis_om/_compat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
def use_pydantic_2_plus():
2626
return True
2727

28-
from pydantic import BaseModel, TypeAdapter
28+
from pydantic import BaseModel, ConfigDict, TypeAdapter
2929
from pydantic import ValidationError as ValidationError
3030
from pydantic import validator
3131
from pydantic._internal._model_construction import ModelMetaclass

aredis_om/model/model.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from ulid import ULID
3333

3434
from .. import redis
35-
from .._compat import BaseModel
35+
from .._compat import PYDANTIC_V2, BaseModel, ConfigDict
3636
from .._compat import FieldInfo as PydanticFieldInfo
3737
from .._compat import (
3838
ModelField,
@@ -1422,10 +1422,16 @@ class RedisModel(BaseModel, abc.ABC, metaclass=ModelMeta):
14221422

14231423
Meta = DefaultMeta
14241424

1425-
class Config:
1426-
orm_mode = True
1427-
arbitrary_types_allowed = True
1428-
extra = "allow"
1425+
if PYDANTIC_V2:
1426+
model_config = ConfigDict(
1427+
from_attributes=True, arbitrary_types_allowed=True, extra="allow"
1428+
)
1429+
else:
1430+
1431+
class Config:
1432+
orm_mode = True
1433+
arbitrary_types_allowed = True
1434+
extra = "allow"
14291435

14301436
def __init__(__pydantic_self__, **data: Any) -> None:
14311437
__pydantic_self__.validate_primary_key()
@@ -1657,8 +1663,8 @@ def __init_subclass__(cls, **kwargs):
16571663
for typ in (Set, Mapping, List):
16581664
if isinstance(origin, type) and issubclass(origin, typ):
16591665
raise RedisModelError(
1660-
f"HashModels cannot index set, list,"
1661-
f" or mapping fields. Field: {name}"
1666+
f"HashModels cannot index set, list, "
1667+
f"or mapping fields. Field: {name}"
16621668
)
16631669
if isinstance(field_type, type) and issubclass(field_type, RedisModel):
16641670
raise RedisModelError(
@@ -1678,8 +1684,8 @@ def __init_subclass__(cls, **kwargs):
16781684
for typ in (Set, Mapping, List):
16791685
if issubclass(origin, typ):
16801686
raise RedisModelError(
1681-
f"HashModels cannot index set, list,"
1682-
f" or mapping fields. Field: {name}"
1687+
f"HashModels cannot index set, list, "
1688+
f"or mapping fields. Field: {name}"
16831689
)
16841690

16851691
if issubclass(outer_type, RedisModel):

docker-compose.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
version: "3.8"
2-
1+
---
32
services:
43
redis:
54
image: "redis/redis-stack:latest"

docs/models.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,11 @@ from redis_om import HashModel
142142
class Customer(HashModel):
143143
# ... Fields ...
144144

145-
class Config:
146-
orm_mode = True
147-
arbitrary_types_allowed = True
148-
extra = "allow"
145+
model_config = ConfigDict(
146+
from_attributes=True,
147+
arbitrary_types_allowed=True,
148+
extra="allow",
149+
)
149150
```
150151

151152
Some features may not work correctly if you change these settings.

tests/test_json_model.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -882,9 +882,23 @@ async def test_schema(m, key_prefix):
882882
# We need to build the key prefix because it will differ based on whether
883883
# these tests were copied into the tests_sync folder and unasynce'd.
884884
key_prefix = m.Member.make_key(m.Member._meta.primary_key_pattern.format(pk=""))
885-
assert (
886-
m.Member.redisearch_schema()
887-
== f"ON JSON PREFIX 1 {key_prefix} SCHEMA $.pk AS pk TAG SEPARATOR | $.first_name AS first_name TAG SEPARATOR | CASESENSITIVE $.last_name AS last_name TAG SEPARATOR | $.email AS email TAG SEPARATOR | $.age AS age NUMERIC $.bio AS bio TAG SEPARATOR | $.bio AS bio_fts TEXT $.address.pk AS address_pk TAG SEPARATOR | $.address.city AS address_city TAG SEPARATOR | $.address.postal_code AS address_postal_code TAG SEPARATOR | $.address.note.pk AS address_note_pk TAG SEPARATOR | $.address.note.description AS address_note_description TAG SEPARATOR | $.orders[*].pk AS orders_pk TAG SEPARATOR | $.orders[*].items[*].pk AS orders_items_pk TAG SEPARATOR | $.orders[*].items[*].name AS orders_items_name TAG SEPARATOR |"
885+
assert m.Member.redisearch_schema() == (
886+
f"ON JSON PREFIX 1 {key_prefix} SCHEMA "
887+
"$.pk AS pk TAG SEPARATOR | "
888+
"$.first_name AS first_name TAG SEPARATOR | CASESENSITIVE "
889+
"$.last_name AS last_name TAG SEPARATOR | "
890+
"$.email AS email TAG SEPARATOR | "
891+
"$.age AS age NUMERIC "
892+
"$.bio AS bio TAG SEPARATOR | "
893+
"$.bio AS bio_fts TEXT "
894+
"$.address.pk AS address_pk TAG SEPARATOR | "
895+
"$.address.city AS address_city TAG SEPARATOR | "
896+
"$.address.postal_code AS address_postal_code TAG SEPARATOR | "
897+
"$.address.note.pk AS address_note_pk TAG SEPARATOR | "
898+
"$.address.note.description AS address_note_description TAG SEPARATOR | "
899+
"$.orders[*].pk AS orders_pk TAG SEPARATOR | "
900+
"$.orders[*].items[*].pk AS orders_items_pk TAG SEPARATOR | "
901+
"$.orders[*].items[*].name AS orders_items_name TAG SEPARATOR |"
888902
)
889903

890904

0 commit comments

Comments
 (0)