Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions aredis_om/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,21 @@
from copy import copy
from enum import Enum
from functools import reduce
from typing import (Any, Callable, Dict, List, Literal, Mapping, Optional,
Sequence, Set, Tuple, Type, TypeVar, Union)
from typing import (
Any,
Callable,
Dict,
List,
Literal,
Mapping,
Optional,
Sequence,
Set,
Tuple,
Type,
TypeVar,
Union,
)
from typing import get_args as typing_get_args
from typing import no_type_check

Expand Down Expand Up @@ -43,6 +56,7 @@
_FromFieldInfoInputs = dict
Undefined = ...
UndefinedType = type(...)
from redis.asyncio.client import Pipeline
from redis.commands.json.path import Path
from redis.exceptions import ResponseError
from typing_extensions import Protocol, Unpack, get_args, get_origin
Expand Down Expand Up @@ -2719,9 +2733,7 @@ async def _delete(cls, db, *pks):
return await db.delete(*pks)

@classmethod
async def delete(
cls, pk: Any, pipeline: Optional[redis.client.Pipeline] = None
) -> int:
async def delete(cls, pk: Any, pipeline: Optional[Pipeline] = None) -> int:
"""Delete data at this key."""
db = cls._get_db(pipeline)

Expand All @@ -2737,7 +2749,7 @@ async def update(self, **field_values):

async def save(
self: "Model",
pipeline: Optional[redis.client.Pipeline] = None,
pipeline: Optional[Pipeline] = None,
nx: bool = False,
xx: bool = False,
) -> Optional["Model"]:
Expand All @@ -2757,9 +2769,7 @@ async def save(
"""
raise NotImplementedError

async def expire(
self, num_seconds: int, pipeline: Optional[redis.client.Pipeline] = None
):
async def expire(self, num_seconds: int, pipeline: Optional[Pipeline] = None):
db = self._get_db(pipeline)

# TODO: Wrap any Redis response errors in a custom exception?
Expand Down Expand Up @@ -2905,7 +2915,7 @@ def get_annotations(cls):
async def add(
cls: Type["Model"],
models: Sequence["Model"],
pipeline: Optional[redis.client.Pipeline] = None,
pipeline: Optional[Pipeline] = None,
pipeline_verifier: Callable[..., Any] = verify_pipeline_response,
) -> Sequence["Model"]:
db = cls._get_db(pipeline, bulk=True)
Expand All @@ -2923,9 +2933,7 @@ async def add(
return models

@classmethod
def _get_db(
self, pipeline: Optional[redis.client.Pipeline] = None, bulk: bool = False
):
def _get_db(self, pipeline: Optional[Pipeline] = None, bulk: bool = False):
if pipeline is not None:
return pipeline
elif bulk:
Expand All @@ -2937,7 +2945,7 @@ def _get_db(
async def delete_many(
cls,
models: Sequence["RedisModel"],
pipeline: Optional[redis.client.Pipeline] = None,
pipeline: Optional[Pipeline] = None,
) -> int:
db = cls._get_db(pipeline)

Expand Down Expand Up @@ -3069,7 +3077,7 @@ def _get_field_expirations(

async def save(
self: "Model",
pipeline: Optional[redis.client.Pipeline] = None,
pipeline: Optional[Pipeline] = None,
nx: bool = False,
xx: bool = False,
field_expirations: Optional[Dict[str, int]] = None,
Expand Down Expand Up @@ -3479,7 +3487,7 @@ def __init__(self, *args, **kwargs):

async def save(
self: "Model",
pipeline: Optional[redis.client.Pipeline] = None,
pipeline: Optional[Pipeline] = None,
nx: bool = False,
xx: bool = False,
) -> Optional["Model"]:
Expand Down
13 changes: 13 additions & 0 deletions make_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,20 @@ def remove_run_async_call(match):
with open(file_path, 'w') as f:
f.write(content)

# Post-process model.py to fix async imports for sync version
model_file = Path(__file__).absolute().parent / "redis_om/model/model.py"
if model_file.exists():
with open(model_file, 'r') as f:
content = f.read()

# Fix Pipeline import: redis.asyncio.client -> redis.client
content = content.replace(
'from redis.asyncio.client import Pipeline',
'from redis.client import Pipeline'
)

with open(model_file, 'w') as f:
f.write(content)


if __name__ == "__main__":
Expand Down
Loading