Skip to content

Commit 79e6140

Browse files
author
Savannah Norem
authored
Merge branch 'main' into limit_return_fields
2 parents 2df1beb + b20e887 commit 79e6140

File tree

6 files changed

+61
-24
lines changed

6 files changed

+61
-24
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/model/model.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
AbstractSet,
1212
Any,
1313
Callable,
14+
ClassVar,
1415
Dict,
1516
List,
1617
Mapping,
@@ -32,7 +33,7 @@
3233
from ulid import ULID
3334

3435
from .. import redis
35-
from .._compat import BaseModel
36+
from .._compat import PYDANTIC_V2, BaseModel
3637
from .._compat import FieldInfo as PydanticFieldInfo
3738
from .._compat import (
3839
ModelField,
@@ -1441,13 +1442,22 @@ def outer_type_or_annotation(field):
14411442

14421443
class RedisModel(BaseModel, abc.ABC, metaclass=ModelMeta):
14431444
pk: Optional[str] = Field(default=None, primary_key=True)
1445+
ConfigDict: ClassVar
14441446

14451447
Meta = DefaultMeta
14461448

1447-
class Config:
1448-
orm_mode = True
1449-
arbitrary_types_allowed = True
1450-
extra = "allow"
1449+
if PYDANTIC_V2:
1450+
from pydantic import ConfigDict
1451+
1452+
model_config = ConfigDict(
1453+
from_attributes=True, arbitrary_types_allowed=True, extra="allow"
1454+
)
1455+
else:
1456+
1457+
class Config:
1458+
orm_mode = True
1459+
arbitrary_types_allowed = True
1460+
extra = "allow"
14511461

14521462
def __init__(__pydantic_self__, **data: Any) -> None:
14531463
__pydantic_self__.validate_primary_key()
@@ -1657,9 +1667,6 @@ def redisearch_schema(cls):
16571667

16581668
def check(self):
16591669
"""Run all validations."""
1660-
from pydantic.version import VERSION as PYDANTIC_VERSION
1661-
1662-
PYDANTIC_V2 = PYDANTIC_VERSION.startswith("2.")
16631670
if not PYDANTIC_V2:
16641671
*_, validation_error = validate_model(self.__class__, self.__dict__)
16651672
if validation_error:
@@ -1681,8 +1688,8 @@ def __init_subclass__(cls, **kwargs):
16811688
for typ in (Set, Mapping, List):
16821689
if isinstance(origin, type) and issubclass(origin, typ):
16831690
raise RedisModelError(
1684-
f"HashModels cannot index set, list,"
1685-
f" or mapping fields. Field: {name}"
1691+
f"HashModels cannot index set, list, "
1692+
f"or mapping fields. Field: {name}"
16861693
)
16871694
if isinstance(field_type, type) and issubclass(field_type, RedisModel):
16881695
raise RedisModelError(
@@ -1702,8 +1709,8 @@ def __init_subclass__(cls, **kwargs):
17021709
for typ in (Set, Mapping, List):
17031710
if issubclass(origin, typ):
17041711
raise RedisModelError(
1705-
f"HashModels cannot index set, list,"
1706-
f" or mapping fields. Field: {name}"
1712+
f"HashModels cannot index set, list, "
1713+
f"or mapping fields. Field: {name}"
17071714
)
17081715

17091716
if issubclass(outer_type, RedisModel):
@@ -2008,7 +2015,9 @@ def schema_for_fields(cls):
20082015
if issubclass(_type, str):
20092016
redisearch_field = f"$.{name} AS {name} TAG SEPARATOR {SINGLE_VALUE_TAG_FIELD_SEPARATOR}"
20102017
else:
2011-
redisearch_field = cls.schema_for_type(name, _type, field_info)
2018+
redisearch_field = cls.schema_for_type(
2019+
json_path, name, "", _type, field_info
2020+
)
20122021
schema_parts.append(redisearch_field)
20132022
continue
20142023
schema_parts.append(

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_hash_model.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,7 @@ class ModelWithStringDefault(HashModel):
900900
assert res.test == "None"
901901

902902

903+
@py_test_mark_asyncio
903904
async def test_update_validation():
904905
class TestUpdate(HashModel):
905906
name: str

tests/test_json_model.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# type: ignore
22

33
import abc
4+
import asyncio
45
import dataclasses
56
import datetime
67
import decimal
@@ -882,9 +883,23 @@ async def test_schema(m, key_prefix):
882883
# We need to build the key prefix because it will differ based on whether
883884
# these tests were copied into the tests_sync folder and unasynce'd.
884885
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 |"
886+
assert m.Member.redisearch_schema() == (
887+
f"ON JSON PREFIX 1 {key_prefix} SCHEMA "
888+
"$.pk AS pk TAG SEPARATOR | "
889+
"$.first_name AS first_name TAG SEPARATOR | CASESENSITIVE "
890+
"$.last_name AS last_name TAG SEPARATOR | "
891+
"$.email AS email TAG SEPARATOR | "
892+
"$.age AS age NUMERIC "
893+
"$.bio AS bio TAG SEPARATOR | "
894+
"$.bio AS bio_fts TEXT "
895+
"$.address.pk AS address_pk TAG SEPARATOR | "
896+
"$.address.city AS address_city TAG SEPARATOR | "
897+
"$.address.postal_code AS address_postal_code TAG SEPARATOR | "
898+
"$.address.note.pk AS address_note_pk TAG SEPARATOR | "
899+
"$.address.note.description AS address_note_description TAG SEPARATOR | "
900+
"$.orders[*].pk AS orders_pk TAG SEPARATOR | "
901+
"$.orders[*].items[*].pk AS orders_items_pk TAG SEPARATOR | "
902+
"$.orders[*].items[*].name AS orders_items_name TAG SEPARATOR |"
888903
)
889904

890905

@@ -1006,8 +1021,8 @@ class ModelWithStringDefault(JsonModel):
10061021
assert res.test == "None"
10071022

10081023

1024+
@py_test_mark_asyncio
10091025
async def test_update_validation():
1010-
10111026
class Embedded(EmbeddedJsonModel):
10121027
price: float
10131028
name: str = Field(index=True)
@@ -1037,6 +1052,7 @@ class TestUpdatesClass(JsonModel):
10371052
assert rematerialized.age == 42
10381053

10391054

1055+
@py_test_mark_asyncio
10401056
async def test_model_with_dict():
10411057
class EmbeddedJsonModelWithDict(EmbeddedJsonModel):
10421058
dict: Dict
@@ -1083,6 +1099,17 @@ class Example(JsonModel):
10831099
assert res.name == ex.name
10841100

10851101

1102+
@py_test_mark_asyncio
1103+
async def test_int_pk():
1104+
class ModelWithIntPk(JsonModel):
1105+
my_id: int = Field(index=True, primary_key=True)
1106+
1107+
await Migrator().run()
1108+
await ModelWithIntPk(my_id=42).save()
1109+
1110+
m = await ModelWithIntPk.find(ModelWithIntPk.my_id == 42).first()
1111+
assert m.my_id == 42
1112+
10861113
@py_test_mark_asyncio
10871114
async def test_pagination():
10881115
class Test(JsonModel):

0 commit comments

Comments
 (0)