Skip to content

Commit 1571c4a

Browse files
committed
more linting
1 parent dc3107c commit 1571c4a

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

aredis_om/model/encoders.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def jsonable_encoder(
6868
if exclude is not None and not isinstance(exclude, (set, dict)):
6969
exclude = set(exclude)
7070

71-
if isinstance(obj, BaseModel):
71+
if isinstance(obj, BaseModel) and hasattr(obj, "__config__"):
7272
encoder = getattr(obj.__config__, "json_encoders", {})
7373
if custom_encoder:
7474
encoder.update(custom_encoder)

aredis_om/model/model.py

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
Type,
2222
TypeVar,
2323
Union,
24-
no_type_check,
2524
)
25+
from typing import get_args as typing_get_args
26+
from typing import no_type_check
2627

2728
from more_itertools import ichunked
2829
from redis.commands.json.path import Path
@@ -82,8 +83,6 @@ def get_outer_type(field):
8283
field.annotation
8384
):
8485
return field.annotation
85-
# elif not isinstance(field.annotation.__args__[0], type):
86-
# return field.annotation.__args__[0].__origin__
8786
else:
8887
return field.annotation.__args__[0]
8988

@@ -156,7 +155,7 @@ def validate_model_fields(model: Type["RedisModel"], field_values: Dict[str, Any
156155
obj = getattr(obj, sub_field)
157156
return
158157

159-
if field_name not in model.__fields__:
158+
if field_name not in model.__fields__: # type: ignore
160159
raise QuerySyntaxError(
161160
f"The field {field_name} does not exist on the model {model.__name__}"
162161
)
@@ -495,7 +494,7 @@ def validate_sort_fields(self, sort_fields: List[str]):
495494
field_name = sort_field.lstrip("-")
496495
if self.knn and field_name == self.knn.score_field:
497496
continue
498-
if field_name not in self.model.__fields__:
497+
if field_name not in self.model.__fields__: # type: ignore
499498
raise QueryNotSupportedError(
500499
f"You tried sort by {field_name}, but that field "
501500
f"does not exist on the model {self.model}"
@@ -516,7 +515,11 @@ def validate_sort_fields(self, sort_fields: List[str]):
516515
return sort_fields
517516

518517
@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+
520523
if not hasattr(field, "field_info"):
521524
field_info = field
522525
else:
@@ -527,7 +530,7 @@ def resolve_field_type(field: ModelField, op: Operators) -> RediSearchFieldTypes
527530
fts = getattr(field_info, "full_text_search", None)
528531
if fts is not True: # Could be PydanticUndefined
529532
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}', "
531534
f"but the field is not indexed for full-text search. Use the "
532535
f"full_text_search=True option. Docs: {ERRORS_URL}#E3"
533536
)
@@ -1144,27 +1147,27 @@ def Field(
11441147
default: Any = Undefined,
11451148
*,
11461149
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,
11501153
exclude: Union[
11511154
AbstractSet[Union[int, str]], Mapping[Union[int, str], Any], Any
11521155
] = None,
11531156
include: Union[
11541157
AbstractSet[Union[int, str]], Mapping[Union[int, str], Any], Any
11551158
] = 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,
11661169
allow_mutation: bool = True,
1167-
regex: str = None,
1170+
regex: Optional[str] = None,
11681171
primary_key: bool = False,
11691172
sortable: Union[bool, UndefinedType] = Undefined,
11701173
index: Union[bool, UndefinedType] = Undefined,
@@ -2060,8 +2063,11 @@ def schema_for_type(
20602063
"See docs: TODO"
20612064
)
20622065

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.
20632068
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__
20652071

20662072
# TODO: GEO field
20672073
if is_vector and vector_options:

0 commit comments

Comments
 (0)