Skip to content

Commit b2ab476

Browse files
committed
fix omit_exception to handle generators properly
1 parent 15dde95 commit b2ab476

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

django_valkey/base.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,18 @@ def decorate(cls):
4848
)
4949
elif attr == "get_many":
5050
setattr(cls, attr, decorator(attribute, return_value={}))
51+
elif attr in {"iter_keys", "sscan_iter"}:
52+
setattr(cls, attr, decorator(attribute, gen=True))
5153
else:
5254
setattr(cls, attr, decorator(attribute))
5355
return cls
5456

5557
return decorate
5658

5759

58-
def omit_exception(method: Callable | None = None, return_value: Any | None = None):
60+
def omit_exception(
61+
method: Callable | None = None, return_value: Any | None = None, gen: bool = False
62+
):
5963
"""
6064
Simple decorator that intercepts connection
6165
errors and ignores these if settings specify this.
@@ -102,10 +106,10 @@ async def _async_generator_decorator(self, *args, **kwargs):
102106
except ConnectionInterrupted as e:
103107
yield __handle_error(self, e)
104108

105-
# inspect.isfunction returns true for generators, so can't use that to check this
106-
if not inspect.isasyncgenfunction(method) and not inspect.isgeneratorfunction(
107-
method
108-
):
109+
# sync generators (iter_keys, sscan_iter) are only generator on client class
110+
# in the backend they are just a function (that returns a generator)
111+
# so inspect.isgeneratorfunction does not work
112+
if not inspect.isasyncgenfunction(method) and not gen:
109113
wrapper = _async_decorator if iscoroutinefunction(method) else _decorator
110114

111115
# if method is a generator or async generator, it should be iterated over by this decorator

0 commit comments

Comments
 (0)