Skip to content

Commit 758d9c7

Browse files
authoredJan 14, 2025··
fixing issue with inhereted defaults (#673)
* fixing issue with inhereted defaults
1 parent d0e3a68 commit 758d9c7

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed
 

‎.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ jobs:
7676
strategy:
7777
matrix:
7878
os: [ ubuntu-latest ]
79-
pyver: [ "3.8", "3.9", "3.10", "3.11", "3.12", "pypy-3.8", "pypy-3.9", "pypy-3.10" ]
79+
pyver: [ "3.9", "3.10", "3.11", "3.12", "pypy-3.9", "pypy-3.10" ]
8080
redisstack: [ "latest" ]
8181
fail-fast: false
8282
services:

‎aredis_om/model/model.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -1320,6 +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
1328+
13231329
if meta and meta != DefaultMeta and meta != base_meta:
13241330
new_class.Meta = meta
13251331
new_class._meta = meta
@@ -1455,7 +1461,16 @@ class Config:
14551461

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

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

‎tests/test_hash_model.py

+25
Original file line numberDiff line numberDiff line change
@@ -951,3 +951,28 @@ 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+
bio: Optional[str] = Field(default=None)
964+
965+
class Child(Model):
966+
other_name: str
967+
# is_new: bool = Field(default=True)
968+
969+
await Migrator().run()
970+
m = Child(first_name="Steve", last_name="Lorello", other_name="foo")
971+
await m.save()
972+
print(m.age)
973+
assert m.age == 18
974+
975+
rematerialized = await Child.find(Child.pk == m.pk).first()
976+
977+
assert rematerialized.age == 18
978+
assert rematerialized.bio is None

‎tests/test_json_model.py

+25-1
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,
@@ -1159,7 +1160,29 @@ class TestLiterals(JsonModel):
11591160
assert rematerialized.pk == item.pk
11601161

11611162

1162-
@py_test_mark_asyncio
1163+
async def test_child_class_expression_proxy():
1164+
# https://github.com/redis/redis-om-python/issues/669 seeing weird issue with child classes initalizing all their undefined members as ExpressionProxies
1165+
class Model(JsonModel):
1166+
first_name: str
1167+
last_name: str
1168+
age: int = Field(default=18)
1169+
bio: Optional[str] = Field(default=None)
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
1184+
assert rematerialized.bio is None
1185+
11631186
async def test_merged_model_error():
11641187
class Player(EmbeddedJsonModel):
11651188
username: str = Field(index=True)
@@ -1173,3 +1196,4 @@ class Game(JsonModel):
11731196
)
11741197
print(q.query)
11751198
assert q.query == "(@player1_username:{username})| (@player2_username:{username})"
1199+

0 commit comments

Comments
 (0)
Please sign in to comment.