Skip to content

Make RedisModel compatible with Pydantic 2.x #626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ sync: $(INSTALL_STAMP)
lint: $(INSTALL_STAMP) dist
$(POETRY) run isort --profile=black --lines-after-imports=2 ./tests/ $(NAME) $(SYNC_NAME)
$(POETRY) run black ./tests/ $(NAME)
$(POETRY) run flake8 --ignore=W503,E501,F401,E731,E712 ./tests/ $(NAME) $(SYNC_NAME)
$(POETRY) run flake8 --ignore=E231,E501,E712,E731,F401,W503 ./tests/ $(NAME) $(SYNC_NAME)
$(POETRY) run mypy ./tests/ $(NAME) $(SYNC_NAME) --ignore-missing-imports --exclude migrate.py --exclude _compat\.py$
$(POETRY) run bandit -r $(NAME) $(SYNC_NAME) -s B608

Expand Down
2 changes: 1 addition & 1 deletion aredis_om/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
def use_pydantic_2_plus():
return True

from pydantic import BaseModel, TypeAdapter
from pydantic import BaseModel, ConfigDict, TypeAdapter
from pydantic import ValidationError as ValidationError
from pydantic import validator
from pydantic._internal._model_construction import ModelMetaclass
Expand Down
24 changes: 15 additions & 9 deletions aredis_om/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from ulid import ULID

from .. import redis
from .._compat import BaseModel
from .._compat import PYDANTIC_V2, BaseModel, ConfigDict
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to move this import statement, it will break if pydantic v2 isn't present since it's conditionally exporting it in _compat

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@slorello89 Updated. Please have a look again. Thanks!

from .._compat import FieldInfo as PydanticFieldInfo
from .._compat import (
ModelField,
Expand Down Expand Up @@ -1422,10 +1422,16 @@ class RedisModel(BaseModel, abc.ABC, metaclass=ModelMeta):

Meta = DefaultMeta

class Config:
orm_mode = True
arbitrary_types_allowed = True
extra = "allow"
if PYDANTIC_V2:
model_config = ConfigDict(
from_attributes=True, arbitrary_types_allowed=True, extra="allow"
)
else:

class Config:
orm_mode = True
arbitrary_types_allowed = True
extra = "allow"

def __init__(__pydantic_self__, **data: Any) -> None:
__pydantic_self__.validate_primary_key()
Expand Down Expand Up @@ -1657,8 +1663,8 @@ def __init_subclass__(cls, **kwargs):
for typ in (Set, Mapping, List):
if isinstance(origin, type) and issubclass(origin, typ):
raise RedisModelError(
f"HashModels cannot index set, list,"
f" or mapping fields. Field: {name}"
f"HashModels cannot index set, list, "
f"or mapping fields. Field: {name}"
)
if isinstance(field_type, type) and issubclass(field_type, RedisModel):
raise RedisModelError(
Expand All @@ -1678,8 +1684,8 @@ def __init_subclass__(cls, **kwargs):
for typ in (Set, Mapping, List):
if issubclass(origin, typ):
raise RedisModelError(
f"HashModels cannot index set, list,"
f" or mapping fields. Field: {name}"
f"HashModels cannot index set, list, "
f"or mapping fields. Field: {name}"
)

if issubclass(outer_type, RedisModel):
Expand Down
3 changes: 1 addition & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
version: "3.8"

---
services:
redis:
image: "redis/redis-stack:latest"
Expand Down
9 changes: 5 additions & 4 deletions docs/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,11 @@ from redis_om import HashModel
class Customer(HashModel):
# ... Fields ...

class Config:
orm_mode = True
arbitrary_types_allowed = True
extra = "allow"
model_config = ConfigDict(
from_attributes=True,
arbitrary_types_allowed=True,
extra="allow",
)
```

Some features may not work correctly if you change these settings.
Expand Down
20 changes: 17 additions & 3 deletions tests/test_json_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,9 +882,23 @@ async def test_schema(m, key_prefix):
# We need to build the key prefix because it will differ based on whether
# these tests were copied into the tests_sync folder and unasynce'd.
key_prefix = m.Member.make_key(m.Member._meta.primary_key_pattern.format(pk=""))
assert (
m.Member.redisearch_schema()
== 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 |"
assert m.Member.redisearch_schema() == (
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 |"
)


Expand Down