4141from .. import redis
4242from ..checks import has_redis_json , has_redisearch
4343from ..connections import get_redis_connection
44- from ..util import ASYNC_MODE
44+ from ..util import ASYNC_MODE , has_numeric_inner_type , is_numeric_type
4545from .encoders import jsonable_encoder
4646from .render_tree import render_tree
4747from .token_escaper import TokenEscaper
@@ -406,7 +406,6 @@ class RediSearchFieldTypes(Enum):
406406
407407
408408# TODO: How to handle Geo fields?
409- NUMERIC_TYPES = (float , int , decimal .Decimal )
410409DEFAULT_PAGE_SIZE = 1000
411410
412411
@@ -578,7 +577,7 @@ def resolve_field_type(field: "FieldInfo", op: Operators) -> RediSearchFieldType
578577 )
579578 elif field_type is bool :
580579 return RediSearchFieldTypes .TAG
581- elif any ( issubclass ( field_type , t ) for t in NUMERIC_TYPES ):
580+ elif is_numeric_type ( field_type ):
582581 # Index numeric Python types as NUMERIC fields, so we can support
583582 # range queries.
584583 return RediSearchFieldTypes .NUMERIC
@@ -1378,12 +1377,14 @@ def outer_type_or_annotation(field: FieldInfo):
13781377def should_index_field (field_info : Union [FieldInfo , PydanticFieldInfo ]) -> bool :
13791378 # for vector, full text search, and sortable fields, we always have to index
13801379 # We could require the user to set index=True, but that would be a breaking change
1381- index = getattr (field_info , "index" , None ) is True
1380+ _index = getattr (field_info , "index" , None )
1381+
1382+ index = _index is True
13821383 vector_options = getattr (field_info , "vector_options" , None ) is not None
13831384 full_text_search = getattr (field_info , "full_text_search" , None ) is True
13841385 sortable = getattr (field_info , "sortable" , None ) is True
13851386
1386- if index is False and any ([vector_options , full_text_search , sortable ]):
1387+ if _index is False and any ([vector_options , full_text_search , sortable ]):
13871388 log .warning (
13881389 "Field is marked as index=False, but it is a vector, full text search, or sortable field. "
13891390 "This will be ignored and the field will be indexed." ,
@@ -1803,7 +1804,7 @@ def schema_for_type(cls, name, typ: Any, field_info: PydanticFieldInfo):
18031804 schema = cls .schema_for_type (name , embedded_cls , field_info )
18041805 elif typ is bool :
18051806 schema = f"{ name } TAG"
1806- elif any ( issubclass ( typ , t ) for t in NUMERIC_TYPES ):
1807+ elif is_numeric_type ( typ ):
18071808 vector_options : Optional [VectorFieldOptions ] = getattr (
18081809 field_info , "vector_options" , None
18091810 )
@@ -1965,7 +1966,7 @@ def schema_for_type(
19651966 json_path : str ,
19661967 name : str ,
19671968 name_prefix : str ,
1968- typ : Union [type [RedisModel ], Any ],
1969+ typ : Union [Type [RedisModel ], Any ],
19691970 field_info : PydanticFieldInfo ,
19701971 parent_type : Optional [Any ] = None ,
19711972 ) -> str :
@@ -2002,9 +2003,7 @@ def schema_for_type(
20022003 field_info , "vector_options" , None
20032004 )
20042005 try :
2005- is_vector = vector_options and any (
2006- issubclass (get_args (typ )[0 ], t ) for t in NUMERIC_TYPES
2007- )
2006+ is_vector = vector_options and has_numeric_inner_type (typ )
20082007 except IndexError :
20092008 raise RedisModelError (
20102009 f"Vector field '{ name } ' must be annotated as a container type"
@@ -2102,7 +2101,11 @@ def schema_for_type(
21022101 # a proper type, we can pull the type information from the origin of the first argument.
21032102 if not isinstance (typ , type ):
21042103 type_args = typing_get_args (field_info .annotation )
2105- typ = type_args [0 ].__origin__
2104+ typ = (
2105+ getattr (type_args [0 ], "__origin__" , type_args [0 ])
2106+ if type_args
2107+ else typ
2108+ )
21062109
21072110 # TODO: GEO field
21082111 if is_vector and vector_options :
@@ -2125,7 +2128,7 @@ def schema_for_type(
21252128 schema += " CASESENSITIVE"
21262129 elif typ is bool :
21272130 schema = f"{ path } AS { index_field_name } TAG"
2128- elif any ( issubclass ( typ , t ) for t in NUMERIC_TYPES ):
2131+ elif is_numeric_type ( typ ):
21292132 schema = f"{ path } AS { index_field_name } NUMERIC"
21302133 elif issubclass (typ , str ):
21312134 if full_text_search is True :
0 commit comments