21
21
Type ,
22
22
TypeVar ,
23
23
Union ,
24
- no_type_check ,
25
24
)
25
+ from typing import get_args as typing_get_args
26
+ from typing import no_type_check
26
27
27
28
from more_itertools import ichunked
28
29
from redis .commands .json .path import Path
@@ -82,8 +83,6 @@ def get_outer_type(field):
82
83
field .annotation
83
84
):
84
85
return field .annotation
85
- # elif not isinstance(field.annotation.__args__[0], type):
86
- # return field.annotation.__args__[0].__origin__
87
86
else :
88
87
return field .annotation .__args__ [0 ]
89
88
@@ -156,7 +155,7 @@ def validate_model_fields(model: Type["RedisModel"], field_values: Dict[str, Any
156
155
obj = getattr (obj , sub_field )
157
156
return
158
157
159
- if field_name not in model .__fields__ :
158
+ if field_name not in model .__fields__ : # type: ignore
160
159
raise QuerySyntaxError (
161
160
f"The field { field_name } does not exist on the model { model .__name__ } "
162
161
)
@@ -495,7 +494,7 @@ def validate_sort_fields(self, sort_fields: List[str]):
495
494
field_name = sort_field .lstrip ("-" )
496
495
if self .knn and field_name == self .knn .score_field :
497
496
continue
498
- if field_name not in self .model .__fields__ :
497
+ if field_name not in self .model .__fields__ : # type: ignore
499
498
raise QueryNotSupportedError (
500
499
f"You tried sort by { field_name } , but that field "
501
500
f"does not exist on the model { self .model } "
@@ -516,7 +515,11 @@ def validate_sort_fields(self, sort_fields: List[str]):
516
515
return sort_fields
517
516
518
517
@staticmethod
519
- def resolve_field_type (field : ModelField , op : Operators ) -> RediSearchFieldTypes :
518
+ def resolve_field_type (
519
+ field : Union [ModelField , PydanticFieldInfo ], op : Operators
520
+ ) -> RediSearchFieldTypes :
521
+ field_info : Union [FieldInfo , ModelField , PydanticFieldInfo ]
522
+
520
523
if not hasattr (field , "field_info" ):
521
524
field_info = field
522
525
else :
@@ -527,7 +530,7 @@ def resolve_field_type(field: ModelField, op: Operators) -> RediSearchFieldTypes
527
530
fts = getattr (field_info , "full_text_search" , None )
528
531
if fts is not True : # Could be PydanticUndefined
529
532
raise QuerySyntaxError (
530
- f"You tried to do a full-text search on the field '{ field .name } ', "
533
+ f"You tried to do a full-text search on the field '{ field .alias } ', "
531
534
f"but the field is not indexed for full-text search. Use the "
532
535
f"full_text_search=True option. Docs: { ERRORS_URL } #E3"
533
536
)
@@ -1144,27 +1147,27 @@ def Field(
1144
1147
default : Any = Undefined ,
1145
1148
* ,
1146
1149
default_factory : Optional [NoArgAnyCallable ] = None ,
1147
- alias : str = None ,
1148
- title : str = None ,
1149
- description : str = None ,
1150
+ alias : Optional [ str ] = None ,
1151
+ title : Optional [ str ] = None ,
1152
+ description : Optional [ str ] = None ,
1150
1153
exclude : Union [
1151
1154
AbstractSet [Union [int , str ]], Mapping [Union [int , str ], Any ], Any
1152
1155
] = None ,
1153
1156
include : Union [
1154
1157
AbstractSet [Union [int , str ]], Mapping [Union [int , str ], Any ], Any
1155
1158
] = None ,
1156
- const : bool = None ,
1157
- gt : float = None ,
1158
- ge : float = None ,
1159
- lt : float = None ,
1160
- le : float = None ,
1161
- multiple_of : float = None ,
1162
- min_items : int = None ,
1163
- max_items : int = None ,
1164
- min_length : int = None ,
1165
- max_length : int = None ,
1159
+ const : Optional [ bool ] = None ,
1160
+ gt : Optional [ float ] = None ,
1161
+ ge : Optional [ float ] = None ,
1162
+ lt : Optional [ float ] = None ,
1163
+ le : Optional [ float ] = None ,
1164
+ multiple_of : Optional [ float ] = None ,
1165
+ min_items : Optional [ int ] = None ,
1166
+ max_items : Optional [ int ] = None ,
1167
+ min_length : Optional [ int ] = None ,
1168
+ max_length : Optional [ int ] = None ,
1166
1169
allow_mutation : bool = True ,
1167
- regex : str = None ,
1170
+ regex : Optional [ str ] = None ,
1168
1171
primary_key : bool = False ,
1169
1172
sortable : Union [bool , UndefinedType ] = Undefined ,
1170
1173
index : Union [bool , UndefinedType ] = Undefined ,
@@ -2060,8 +2063,11 @@ def schema_for_type(
2060
2063
"See docs: TODO"
2061
2064
)
2062
2065
2066
+ # For more complicated compound validators (e.g. PositiveInt), we might get a _GenericAlias rather than
2067
+ # a proper type, we can pull the type information from the origin of the first argument.
2063
2068
if not isinstance (typ , type ):
2064
- typ = field_info .annotation .__args__ [0 ].__origin__
2069
+ type_args = typing_get_args (field_info .annotation )
2070
+ typ = type_args [0 ].__origin__
2065
2071
2066
2072
# TODO: GEO field
2067
2073
if is_vector and vector_options :
0 commit comments