diff --git a/aredis_om/model/model.py b/aredis_om/model/model.py index 8018ec8..67769cd 100644 --- a/aredis_om/model/model.py +++ b/aredis_om/model/model.py @@ -1320,11 +1320,12 @@ def __new__(cls, name, bases, attrs, **kwargs): # noqa C901 meta = meta or getattr(new_class, "Meta", None) base_meta = getattr(new_class, "_meta", None) - if len(bases) == 1: - for f_name in bases[0].model_fields: - field = bases[0].model_fields[f_name] - print(field) - new_class.model_fields[f_name] = field + if len(bases) >= 1: + for base_index in range(len(bases)): + model_fields = getattr(bases[base_index], "model_fields", []) + for f_name in model_fields: + field = model_fields[f_name] + new_class.model_fields[f_name] = field if meta and meta != DefaultMeta and meta != base_meta: new_class.Meta = meta diff --git a/tests/test_hash_model.py b/tests/test_hash_model.py index 875bfdb..12df8cd 100644 --- a/tests/test_hash_model.py +++ b/tests/test_hash_model.py @@ -18,6 +18,7 @@ Migrator, NotFoundError, QueryNotSupportedError, + RedisModel, RedisModelError, ) @@ -976,3 +977,28 @@ class Child(Model): assert rematerialized.age == 18 assert rematerialized.bio is None + + +@py_test_mark_asyncio +async def test_child_class_expression_proxy_with_mixin(): + # https://github.com/redis/redis-om-python/issues/669 seeing weird issue with child classes initalizing all their undefined members as ExpressionProxies + class Model(RedisModel, abc.ABC): + first_name: str + last_name: str + age: int = Field(default=18) + bio: Optional[str] = Field(default=None) + + class Child(Model, HashModel): + other_name: str + # is_new: bool = Field(default=True) + + await Migrator().run() + m = Child(first_name="Steve", last_name="Lorello", other_name="foo") + await m.save() + print(m.age) + assert m.age == 18 + + rematerialized = await Child.find(Child.pk == m.pk).first() + + assert rematerialized.age == 18 + assert rematerialized.bio is None diff --git a/tests/test_json_model.py b/tests/test_json_model.py index 5677d9e..a3e4d40 100644 --- a/tests/test_json_model.py +++ b/tests/test_json_model.py @@ -21,6 +21,7 @@ Migrator, NotFoundError, QueryNotSupportedError, + RedisModel, RedisModelError, ) @@ -1160,7 +1161,6 @@ class TestLiterals(JsonModel): assert rematerialized.pk == item.pk - @py_test_mark_asyncio async def test_two_false_pks(): from pydantic_core import PydanticUndefined as Undefined @@ -1171,6 +1171,7 @@ class SomeModel(JsonModel): SomeModel(field1="foo", field2="bar") + @py_test_mark_asyncio async def test_child_class_expression_proxy(): # 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): assert rematerialized.age != 19 assert rematerialized.bio is None + +@py_test_mark_asyncio +async def test_child_class_expression_proxy_with_mixin(): + class Model(RedisModel, abc.ABC): + first_name: str + last_name: str + age: int = Field(default=18) + bio: Optional[str] = Field(default=None) + + class Child(Model, JsonModel): + is_new: bool = Field(default=True) + + await Migrator().run() + m = Child(first_name="Steve", last_name="Lorello") + await m.save() + print(m.age) + assert m.age == 18 + + rematerialized = await Child.find(Child.pk == m.pk).first() + + assert rematerialized.age == 18 + assert rematerialized.age != 19 + assert rematerialized.bio is None + + @py_test_mark_asyncio async def test_merged_model_error(): class Player(EmbeddedJsonModel): @@ -1209,4 +1235,3 @@ class Game(JsonModel): ) print(q.query) assert q.query == "(@player1_username:{username})| (@player2_username:{username})" -