Skip to content

Commit 0f9f7aa

Browse files
author
Andrew Brookins
committed
Attempt run-time change of type annotations on model fields
1 parent 15b7d36 commit 0f9f7aa

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

redis_developer/model/model.py

+18
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,13 @@ def __new__(cls, name, bases, attrs, **kwargs): # noqa C901
976976
# in queries, like Model.get(Model.field_name == 1)
977977
for field_name, field in new_class.__fields__.items():
978978
setattr(new_class, field_name, ExpressionProxy(field, []))
979+
annotation = new_class.get_annotations().get(field_name)
980+
if annotation:
981+
new_class.__annotations__[field_name] = Union[
982+
annotation, ExpressionProxy
983+
]
984+
else:
985+
new_class.__annotations__[field_name] = ExpressionProxy
979986
# Check if this is our FieldInfo version with extended ORM metadata.
980987
if isinstance(field.field_info, FieldInfo):
981988
if field.field_info.primary_key:
@@ -1139,6 +1146,17 @@ def to_string(s):
11391146
docs.append(doc)
11401147
return docs
11411148

1149+
@classmethod
1150+
def get_annotations(cls):
1151+
d = {}
1152+
for c in cls.mro():
1153+
try:
1154+
d.update(**c.__annotations__)
1155+
except AttributeError:
1156+
# object, at least, has no __annotations__ attribute.
1157+
pass
1158+
return d
1159+
11421160
@classmethod
11431161
def add(cls, models: Sequence["RedisModel"]) -> Sequence["RedisModel"]:
11441162
# TODO: Add transaction support

tests/test_json_model.py

-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from unittest import mock
77

88
import pytest
9-
import redis
109
from pydantic import ValidationError
1110

1211
from redis_developer.model import EmbeddedJsonModel, Field, JsonModel
@@ -528,7 +527,6 @@ def test_not_found(m):
528527
m.Member.get(1000)
529528

530529

531-
@pytest.mark.skip("Does not clean up after itself properly")
532530
def test_list_field_limitations(m):
533531
with pytest.raises(RedisModelError):
534532

0 commit comments

Comments
 (0)