Attempt run-time change of type annotations on model fields
This commit is contained in:
parent
15b7d360eb
commit
0f9f7aa868
2 changed files with 18 additions and 2 deletions
|
@ -976,6 +976,13 @@ class ModelMeta(ModelMetaclass):
|
||||||
# in queries, like Model.get(Model.field_name == 1)
|
# in queries, like Model.get(Model.field_name == 1)
|
||||||
for field_name, field in new_class.__fields__.items():
|
for field_name, field in new_class.__fields__.items():
|
||||||
setattr(new_class, field_name, ExpressionProxy(field, []))
|
setattr(new_class, field_name, ExpressionProxy(field, []))
|
||||||
|
annotation = new_class.get_annotations().get(field_name)
|
||||||
|
if annotation:
|
||||||
|
new_class.__annotations__[field_name] = Union[
|
||||||
|
annotation, ExpressionProxy
|
||||||
|
]
|
||||||
|
else:
|
||||||
|
new_class.__annotations__[field_name] = ExpressionProxy
|
||||||
# Check if this is our FieldInfo version with extended ORM metadata.
|
# Check if this is our FieldInfo version with extended ORM metadata.
|
||||||
if isinstance(field.field_info, FieldInfo):
|
if isinstance(field.field_info, FieldInfo):
|
||||||
if field.field_info.primary_key:
|
if field.field_info.primary_key:
|
||||||
|
@ -1139,6 +1146,17 @@ class RedisModel(BaseModel, abc.ABC, metaclass=ModelMeta):
|
||||||
docs.append(doc)
|
docs.append(doc)
|
||||||
return docs
|
return docs
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_annotations(cls):
|
||||||
|
d = {}
|
||||||
|
for c in cls.mro():
|
||||||
|
try:
|
||||||
|
d.update(**c.__annotations__)
|
||||||
|
except AttributeError:
|
||||||
|
# object, at least, has no __annotations__ attribute.
|
||||||
|
pass
|
||||||
|
return d
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def add(cls, models: Sequence["RedisModel"]) -> Sequence["RedisModel"]:
|
def add(cls, models: Sequence["RedisModel"]) -> Sequence["RedisModel"]:
|
||||||
# TODO: Add transaction support
|
# TODO: Add transaction support
|
||||||
|
|
|
@ -6,7 +6,6 @@ from typing import List, Optional
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import redis
|
|
||||||
from pydantic import ValidationError
|
from pydantic import ValidationError
|
||||||
|
|
||||||
from redis_developer.model import EmbeddedJsonModel, Field, JsonModel
|
from redis_developer.model import EmbeddedJsonModel, Field, JsonModel
|
||||||
|
@ -528,7 +527,6 @@ def test_not_found(m):
|
||||||
m.Member.get(1000)
|
m.Member.get(1000)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skip("Does not clean up after itself properly")
|
|
||||||
def test_list_field_limitations(m):
|
def test_list_field_limitations(m):
|
||||||
with pytest.raises(RedisModelError):
|
with pytest.raises(RedisModelError):
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue