Skip to content

Commit 6fb2a73

Browse files
authored
fixing issue with breaking typing.Dict in JsonModel (#614)
1 parent 5ccec37 commit 6fb2a73

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

aredis_om/model/model.py

+5
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ def get_outer_type(field):
8383
field.annotation
8484
):
8585
return field.annotation
86+
elif not hasattr(field.annotation, "__args__"):
87+
return None
8688
else:
8789
return field.annotation.__args__[0]
8890

@@ -1953,6 +1955,9 @@ def schema_for_fields(cls):
19531955

19541956
for name, field in fields.items():
19551957
_type = get_outer_type(field)
1958+
if _type is None:
1959+
continue
1960+
19561961
if (
19571962
not isinstance(field, FieldInfo)
19581963
and hasattr(field, "metadata")

tests/test_json_model.py

+24
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,30 @@ async def test_xfix_queries(m):
973973
assert result.first_name == "Steve"
974974

975975

976+
@py_test_mark_asyncio
977+
async def test_model_with_dict():
978+
class EmbeddedJsonModelWithDict(EmbeddedJsonModel):
979+
dict: Dict
980+
981+
class ModelWithDict(JsonModel):
982+
embedded_model: EmbeddedJsonModelWithDict
983+
info: Dict
984+
985+
await Migrator().run()
986+
d = dict()
987+
inner_dict = dict()
988+
d["foo"] = "bar"
989+
inner_dict["bar"] = "foo"
990+
embedded_model = EmbeddedJsonModelWithDict(dict=inner_dict)
991+
item = ModelWithDict(info=d, embedded_model=embedded_model)
992+
await item.save()
993+
994+
rematerialized = await ModelWithDict.find(ModelWithDict.pk == item.pk).first()
995+
assert rematerialized.pk == item.pk
996+
assert rematerialized.info["foo"] == "bar"
997+
assert rematerialized.embedded_model.dict["bar"] == "foo"
998+
999+
9761000
@py_test_mark_asyncio
9771001
async def test_boolean():
9781002
class Example(JsonModel):

0 commit comments

Comments
 (0)