Skip to content

Commit

Permalink
Merge branch 'main' into limit_return_fields
Browse files Browse the repository at this point in the history
  • Loading branch information
sav-norem authored Aug 14, 2024
2 parents 79e6140 + c5068e5 commit 3de85c9
Show file tree
Hide file tree
Showing 7 changed files with 579 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/spellcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v4
- name: Check Spelling
uses: rojopolis/spellcheck-github-actions@0.36.0
uses: rojopolis/spellcheck-github-actions@0.40.0
with:
config_path: .github/spellcheck-settings.yml
task_name: Markdown
2 changes: 1 addition & 1 deletion aredis_om/model/encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def jsonable_encoder(
sqlalchemy_safe=sqlalchemy_safe,
)
if dataclasses.is_dataclass(obj):
return dataclasses.asdict(obj)
return dataclasses.asdict(obj) # type: ignore[call-overload]
if isinstance(obj, Enum):
return obj.value
if isinstance(obj, PurePath):
Expand Down
58 changes: 41 additions & 17 deletions aredis_om/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
ClassVar,
Dict,
List,
Literal,
Mapping,
Optional,
Sequence,
Expand Down Expand Up @@ -141,10 +142,10 @@ def embedded(cls):

def is_supported_container_type(typ: Optional[type]) -> bool:
# TODO: Wait, why don't we support indexing sets?
if typ == list or typ == tuple:
if typ == list or typ == tuple or typ == Literal:
return True
unwrapped = get_origin(typ)
return unwrapped == list or unwrapped == tuple
return unwrapped == list or unwrapped == tuple or unwrapped == Literal


def validate_model_fields(model: Type["RedisModel"], field_values: Dict[str, Any]):
Expand Down Expand Up @@ -889,7 +890,9 @@ def resolve_redisearch_query(cls, expression: ExpressionOrNegated) -> str:

return result

async def execute(self, exhaust_results=True, return_raw_result=False):
async def execute(
self, exhaust_results=True, return_raw_result=False, return_query_args=False
):
args: List[Union[str, bytes]] = [
"FT.SEARCH",
self.model.Meta.index_name,
Expand All @@ -914,6 +917,9 @@ async def execute(self, exhaust_results=True, return_raw_result=False):
if self.nocontent:
args.append("NOCONTENT")

if return_query_args:
return self.model.Meta.index_name, args

# Reset the cache if we're executing from offset 0.
if self.offset == 0:
self._model_cache.clear()
Expand Down Expand Up @@ -947,6 +953,10 @@ async def execute(self, exhaust_results=True, return_raw_result=False):
self._model_cache += _results
return self._model_cache

async def get_query(self):
query = self.copy()
return await query.execute(return_query_args=True)

async def first(self):
query = self.copy(offset=0, limit=1, sort_fields=self.sort_fields)
results = await query.execute(exhaust_results=False)
Expand Down Expand Up @@ -1436,6 +1446,8 @@ def outer_type_or_annotation(field):
if not isinstance(field.annotation, type):
raise AttributeError(f"could not extract outer type from field {field}")
return field.annotation
elif get_origin(field.annotation) == Literal:
return str
else:
return field.annotation.__args__[0]

Expand Down Expand Up @@ -2081,21 +2093,33 @@ def schema_for_type(
# find any values marked as indexed.
if is_container_type and not is_vector:
field_type = get_origin(typ)
embedded_cls = get_args(typ)
if not embedded_cls:
log.warning(
"Model %s defined an empty list or tuple field: %s", cls, name
if field_type == Literal:
path = f"{json_path}.{name}"
return cls.schema_for_type(
path,
name,
name_prefix,
str,
field_info,
parent_type=field_type,
)
else:
embedded_cls = get_args(typ)
if not embedded_cls:
log.warning(
"Model %s defined an empty list or tuple field: %s", cls, name
)
return ""
path = f"{json_path}.{name}[*]"
embedded_cls = embedded_cls[0]
return cls.schema_for_type(
path,
name,
name_prefix,
embedded_cls,
field_info,
parent_type=field_type,
)
return ""
embedded_cls = embedded_cls[0]
return cls.schema_for_type(
f"{json_path}.{name}[*]",
name,
name_prefix,
embedded_cls,
field_info,
parent_type=field_type,
)
elif field_is_model:
name_prefix = f"{name_prefix}_{name}" if name_prefix else name
sub_fields = []
Expand Down
10 changes: 5 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "redis-om"
version = "0.3.1"
version = "0.3.2"
description = "Object mappings, and more, for Redis."
authors = ["Redis OSS <oss@redis.com>"]
maintainers = ["Redis OSS <oss@redis.com>"]
Expand Down Expand Up @@ -42,9 +42,9 @@ click = "^8.0.1"
types-redis = ">=3.5.9,<5.0.0"
python-ulid = "^1.0.3"
typing-extensions = "^4.4.0"
hiredis = "^2.2.3"
hiredis = ">=2.2.3,<4.0.0"
more-itertools = ">=8.14,<11.0"
setuptools = {version = "^69.2.0", markers = "python_version >= '3.12'"}
setuptools = {version = ">=70.0,<73.0", markers = "python_version >= '3.12'"}

[tool.poetry.dev-dependencies]
mypy = "^1.9.0"
Expand All @@ -55,9 +55,9 @@ isort = "^5.9.3"
flake8 = "^5.0.4"
bandit = "^1.7.4"
coverage = "^7.1"
pytest-cov = "^4.0.0"
pytest-cov = "^5.0.0"
pytest-xdist = "^3.1.0"
unasync = "^0.5.0"
unasync = "^0.6.0"
pytest-asyncio = "^0.23.5"
email-validator = "^2.0.0"
tox = "^4.14.1"
Expand Down
Loading

0 comments on commit 3de85c9

Please sign in to comment.