Skip to content

Commit 11b8a53

Browse files
committed
fixing issue with inhereted defaults
1 parent 6b521d5 commit 11b8a53

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

aredis_om/model/model.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,12 @@ def __new__(cls, name, bases, attrs, **kwargs): # noqa C901
13171317
meta = meta or getattr(new_class, "Meta", None)
13181318
base_meta = getattr(new_class, "_meta", None)
13191319

1320+
if len(bases) == 1:
1321+
for f_name in bases[0].model_fields:
1322+
field = bases[0].model_fields[f_name]
1323+
print(field)
1324+
new_class.model_fields[f_name] = field
1325+
13201326
if meta and meta != DefaultMeta and meta != base_meta:
13211327
new_class.Meta = meta
13221328
new_class._meta = meta
@@ -1452,7 +1458,16 @@ class Config:
14521458

14531459
def __init__(__pydantic_self__, **data: Any) -> None:
14541460
__pydantic_self__.validate_primary_key()
1455-
super().__init__(**data)
1461+
missing_fields = __pydantic_self__.model_fields.keys() - data.keys() - {"pk"}
1462+
1463+
kwargs = data.copy()
1464+
1465+
# This is a hack, we need to manually make sure we are setting up defaults correctly when we encounter them
1466+
# because inheritance apparently won't cover that in pydantic 2.0.
1467+
for field in missing_fields:
1468+
default_value = __pydantic_self__.model_fields.get(field).default # type: ignore
1469+
kwargs[field] = default_value
1470+
super().__init__(**kwargs)
14561471

14571472
def __lt__(self, other):
14581473
"""Default sort: compare primary key of models."""

tests/test_hash_model.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,3 +951,26 @@ class TestLiterals(HashModel):
951951
await item.save()
952952
rematerialized = await TestLiterals.find(TestLiterals.flavor == "pumpkin").first()
953953
assert rematerialized.pk == item.pk
954+
955+
956+
@py_test_mark_asyncio
957+
async def test_child_class_expression_proxy():
958+
# https://github.com/redis/redis-om-python/issues/669 seeing weird issue with child classes initalizing all their undefined members as ExpressionProxies
959+
class Model(HashModel):
960+
first_name: str
961+
last_name: str
962+
age: int = Field(default=18)
963+
964+
class Child(Model):
965+
other_name: str
966+
# is_new: bool = Field(default=True)
967+
968+
await Migrator().run()
969+
m = Child(first_name="Steve", last_name="Lorello", other_name="foo")
970+
await m.save()
971+
print(m.age)
972+
assert m.age == 18
973+
974+
rematerialized = await Child.find(Child.pk == m.pk).first()
975+
976+
assert rematerialized.age == 18

tests/test_json_model.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import pytest
1414
import pytest_asyncio
15+
from more_itertools.more import first
1516

1617
from aredis_om import (
1718
EmbeddedJsonModel,
@@ -1157,3 +1158,26 @@ class TestLiterals(JsonModel):
11571158
await item.save()
11581159
rematerialized = await TestLiterals.find(TestLiterals.flavor == "pumpkin").first()
11591160
assert rematerialized.pk == item.pk
1161+
1162+
1163+
@py_test_mark_asyncio
1164+
async def test_child_class_expression_proxy():
1165+
# https://github.com/redis/redis-om-python/issues/669 seeing weird issue with child classes initalizing all their undefined members as ExpressionProxies
1166+
class Model(JsonModel):
1167+
first_name: str
1168+
last_name: str
1169+
age: int = Field(default=18)
1170+
1171+
class Child(Model):
1172+
is_new: bool = Field(default=True)
1173+
1174+
await Migrator().run()
1175+
m = Child(first_name="Steve", last_name="Lorello")
1176+
await m.save()
1177+
print(m.age)
1178+
assert m.age == 18
1179+
1180+
rematerialized = await Child.find(Child.pk == m.pk).first()
1181+
1182+
assert rematerialized.age == 18
1183+
assert rematerialized.age != 19

0 commit comments

Comments
 (0)