Skip to content

Commit 4fb221f

Browse files
authored
Merge branch 'main' into primarykeycheck
2 parents 2d4998d + 758d9c7 commit 4fb221f

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
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

Lines changed: 16 additions & 1 deletion
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

Lines changed: 25 additions & 0 deletions
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

Lines changed: 27 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,
@@ -1159,6 +1160,7 @@ class TestLiterals(JsonModel):
11591160
assert rematerialized.pk == item.pk
11601161

11611162

1163+
11621164
@py_test_mark_asyncio
11631165
async def test_two_false_pks():
11641166
from pydantic_core import PydanticUndefined as Undefined
@@ -1169,6 +1171,30 @@ class SomeModel(JsonModel):
11691171

11701172
SomeModel(field1="foo", field2="bar")
11711173

1174+
@py_test_mark_asyncio
1175+
async def test_child_class_expression_proxy():
1176+
# https://github.com/redis/redis-om-python/issues/669 seeing weird issue with child classes initalizing all their undefined members as ExpressionProxies
1177+
class Model(JsonModel):
1178+
first_name: str
1179+
last_name: str
1180+
age: int = Field(default=18)
1181+
bio: Optional[str] = Field(default=None)
1182+
1183+
class Child(Model):
1184+
is_new: bool = Field(default=True)
1185+
1186+
await Migrator().run()
1187+
m = Child(first_name="Steve", last_name="Lorello")
1188+
await m.save()
1189+
print(m.age)
1190+
assert m.age == 18
1191+
1192+
rematerialized = await Child.find(Child.pk == m.pk).first()
1193+
1194+
assert rematerialized.age == 18
1195+
assert rematerialized.age != 19
1196+
assert rematerialized.bio is None
1197+
11721198
@py_test_mark_asyncio
11731199
async def test_merged_model_error():
11741200
class Player(EmbeddedJsonModel):
@@ -1183,3 +1209,4 @@ class Game(JsonModel):
11831209
)
11841210
print(q.query)
11851211
assert q.query == "(@player1_username:{username})| (@player2_username:{username})"
1212+

0 commit comments

Comments
 (0)