Skip to content

Commit 4a4bcd6

Browse files
authored
handle default issue with multiple inheritance (#687)
* handle default issue with multiple inheritance
1 parent dd3509a commit 4a4bcd6

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed

aredis_om/model/model.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -1320,11 +1320,12 @@ def __new__(cls, name, bases, attrs, **kwargs): # noqa C901
13201320
meta = meta or getattr(new_class, "Meta", None)
13211321
base_meta = getattr(new_class, "_meta", None)
13221322

1323-
if len(bases) == 1:
1324-
for f_name in bases[0].model_fields:
1325-
field = bases[0].model_fields[f_name]
1326-
print(field)
1327-
new_class.model_fields[f_name] = field
1323+
if len(bases) >= 1:
1324+
for base_index in range(len(bases)):
1325+
model_fields = getattr(bases[base_index], "model_fields", [])
1326+
for f_name in model_fields:
1327+
field = model_fields[f_name]
1328+
new_class.model_fields[f_name] = field
13281329

13291330
if meta and meta != DefaultMeta and meta != base_meta:
13301331
new_class.Meta = meta

tests/test_hash_model.py

+26
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
Migrator,
1919
NotFoundError,
2020
QueryNotSupportedError,
21+
RedisModel,
2122
RedisModelError,
2223
)
2324

@@ -976,3 +977,28 @@ class Child(Model):
976977

977978
assert rematerialized.age == 18
978979
assert rematerialized.bio is None
980+
981+
982+
@py_test_mark_asyncio
983+
async def test_child_class_expression_proxy_with_mixin():
984+
# https://github.com/redis/redis-om-python/issues/669 seeing weird issue with child classes initalizing all their undefined members as ExpressionProxies
985+
class Model(RedisModel, abc.ABC):
986+
first_name: str
987+
last_name: str
988+
age: int = Field(default=18)
989+
bio: Optional[str] = Field(default=None)
990+
991+
class Child(Model, HashModel):
992+
other_name: str
993+
# is_new: bool = Field(default=True)
994+
995+
await Migrator().run()
996+
m = Child(first_name="Steve", last_name="Lorello", other_name="foo")
997+
await m.save()
998+
print(m.age)
999+
assert m.age == 18
1000+
1001+
rematerialized = await Child.find(Child.pk == m.pk).first()
1002+
1003+
assert rematerialized.age == 18
1004+
assert rematerialized.bio is None

tests/test_json_model.py

+27-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
Migrator,
2222
NotFoundError,
2323
QueryNotSupportedError,
24+
RedisModel,
2425
RedisModelError,
2526
)
2627

@@ -1160,7 +1161,6 @@ class TestLiterals(JsonModel):
11601161
assert rematerialized.pk == item.pk
11611162

11621163

1163-
11641164
@py_test_mark_asyncio
11651165
async def test_two_false_pks():
11661166
from pydantic_core import PydanticUndefined as Undefined
@@ -1171,6 +1171,7 @@ class SomeModel(JsonModel):
11711171

11721172
SomeModel(field1="foo", field2="bar")
11731173

1174+
11741175
@py_test_mark_asyncio
11751176
async def test_child_class_expression_proxy():
11761177
# https://github.com/redis/redis-om-python/issues/669 seeing weird issue with child classes initalizing all their undefined members as ExpressionProxies
@@ -1195,6 +1196,31 @@ class Child(Model):
11951196
assert rematerialized.age != 19
11961197
assert rematerialized.bio is None
11971198

1199+
1200+
@py_test_mark_asyncio
1201+
async def test_child_class_expression_proxy_with_mixin():
1202+
class Model(RedisModel, abc.ABC):
1203+
first_name: str
1204+
last_name: str
1205+
age: int = Field(default=18)
1206+
bio: Optional[str] = Field(default=None)
1207+
1208+
class Child(Model, JsonModel):
1209+
is_new: bool = Field(default=True)
1210+
1211+
await Migrator().run()
1212+
m = Child(first_name="Steve", last_name="Lorello")
1213+
await m.save()
1214+
print(m.age)
1215+
assert m.age == 18
1216+
1217+
rematerialized = await Child.find(Child.pk == m.pk).first()
1218+
1219+
assert rematerialized.age == 18
1220+
assert rematerialized.age != 19
1221+
assert rematerialized.bio is None
1222+
1223+
11981224
@py_test_mark_asyncio
11991225
async def test_merged_model_error():
12001226
class Player(EmbeddedJsonModel):
@@ -1209,4 +1235,3 @@ class Game(JsonModel):
12091235
)
12101236
print(q.query)
12111237
assert q.query == "(@player1_username:{username})| (@player2_username:{username})"
1212-

0 commit comments

Comments
 (0)