Skip to content

Commit 6724f1a

Browse files
authored
Merge branch 'main' into dependabot/pip/setuptools-gte-69.2-and-lt-73.0
2 parents 1daf1a1 + 424b842 commit 6724f1a

File tree

5 files changed

+573
-30
lines changed

5 files changed

+573
-30
lines changed

aredis_om/model/encoders.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def jsonable_encoder(
9090
sqlalchemy_safe=sqlalchemy_safe,
9191
)
9292
if dataclasses.is_dataclass(obj):
93-
return dataclasses.asdict(obj)
93+
return dataclasses.asdict(obj) # type: ignore[call-overload]
9494
if isinstance(obj, Enum):
9595
return obj.value
9696
if isinstance(obj, PurePath):

aredis_om/model/model.py

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
ClassVar,
1515
Dict,
1616
List,
17+
Literal,
1718
Mapping,
1819
Optional,
1920
Sequence,
@@ -141,10 +142,10 @@ def embedded(cls):
141142

142143
def is_supported_container_type(typ: Optional[type]) -> bool:
143144
# TODO: Wait, why don't we support indexing sets?
144-
if typ == list or typ == tuple:
145+
if typ == list or typ == tuple or typ == Literal:
145146
return True
146147
unwrapped = get_origin(typ)
147-
return unwrapped == list or unwrapped == tuple
148+
return unwrapped == list or unwrapped == tuple or unwrapped == Literal
148149

149150

150151
def validate_model_fields(model: Type["RedisModel"], field_values: Dict[str, Any]):
@@ -872,7 +873,9 @@ def resolve_redisearch_query(cls, expression: ExpressionOrNegated) -> str:
872873

873874
return result
874875

875-
async def execute(self, exhaust_results=True, return_raw_result=False):
876+
async def execute(
877+
self, exhaust_results=True, return_raw_result=False, return_query_args=False
878+
):
876879
args: List[Union[str, bytes]] = [
877880
"FT.SEARCH",
878881
self.model.Meta.index_name,
@@ -897,6 +900,9 @@ async def execute(self, exhaust_results=True, return_raw_result=False):
897900
if self.nocontent:
898901
args.append("NOCONTENT")
899902

903+
if return_query_args:
904+
return self.model.Meta.index_name, args
905+
900906
# Reset the cache if we're executing from offset 0.
901907
if self.offset == 0:
902908
self._model_cache.clear()
@@ -930,6 +936,10 @@ async def execute(self, exhaust_results=True, return_raw_result=False):
930936
self._model_cache += _results
931937
return self._model_cache
932938

939+
async def get_query(self):
940+
query = self.copy()
941+
return await query.execute(return_query_args=True)
942+
933943
async def first(self):
934944
query = self.copy(offset=0, limit=1, sort_fields=self.sort_fields)
935945
results = await query.execute(exhaust_results=False)
@@ -1414,6 +1424,8 @@ def outer_type_or_annotation(field):
14141424
if not isinstance(field.annotation, type):
14151425
raise AttributeError(f"could not extract outer type from field {field}")
14161426
return field.annotation
1427+
elif get_origin(field.annotation) == Literal:
1428+
return str
14171429
else:
14181430
return field.annotation.__args__[0]
14191431

@@ -2057,21 +2069,33 @@ def schema_for_type(
20572069
# find any values marked as indexed.
20582070
if is_container_type and not is_vector:
20592071
field_type = get_origin(typ)
2060-
embedded_cls = get_args(typ)
2061-
if not embedded_cls:
2062-
log.warning(
2063-
"Model %s defined an empty list or tuple field: %s", cls, name
2072+
if field_type == Literal:
2073+
path = f"{json_path}.{name}"
2074+
return cls.schema_for_type(
2075+
path,
2076+
name,
2077+
name_prefix,
2078+
str,
2079+
field_info,
2080+
parent_type=field_type,
2081+
)
2082+
else:
2083+
embedded_cls = get_args(typ)
2084+
if not embedded_cls:
2085+
log.warning(
2086+
"Model %s defined an empty list or tuple field: %s", cls, name
2087+
)
2088+
return ""
2089+
path = f"{json_path}.{name}[*]"
2090+
embedded_cls = embedded_cls[0]
2091+
return cls.schema_for_type(
2092+
path,
2093+
name,
2094+
name_prefix,
2095+
embedded_cls,
2096+
field_info,
2097+
parent_type=field_type,
20642098
)
2065-
return ""
2066-
embedded_cls = embedded_cls[0]
2067-
return cls.schema_for_type(
2068-
f"{json_path}.{name}[*]",
2069-
name,
2070-
name_prefix,
2071-
embedded_cls,
2072-
field_info,
2073-
parent_type=field_type,
2074-
)
20752099
elif field_is_model:
20762100
name_prefix = f"{name_prefix}_{name}" if name_prefix else name
20772101
sub_fields = []

0 commit comments

Comments
 (0)