Fix async filter call in combinators (#4435)

This commit is contained in:
Jahongir Qurbonov 2024-08-19 21:24:56 +05:00 committed by GitHub
parent eb7ed5dd31
commit ee3248c3a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,6 +1,7 @@
import abc
import typing
from collections.abc import Callable
from inspect import isawaitable
from typing import Awaitable, TypeAlias
from ..event import Event
@ -41,7 +42,7 @@ class Combinable(abc.ABC):
return self.filter if isinstance(self, Not) else Not(self) # type: ignore [return-value]
@abc.abstractmethod
def __call__(self, event: Event) -> bool:
async def __call__(self, event: Event) -> bool:
pass
@ -81,8 +82,12 @@ class Any(Combinable):
"""
return self._filters
def __call__(self, event: Event) -> bool:
return any(f(event) for f in self._filters)
async def __call__(self, event: Event) -> bool:
for f in self._filters:
if await r if isawaitable(r := f(event)) else r:
return True
return False
class All(Combinable):
@ -121,8 +126,12 @@ class All(Combinable):
"""
return self._filters
def __call__(self, event: Event) -> bool:
return all(f(event) for f in self._filters)
async def __call__(self, event: Event) -> bool:
for f in self._filters:
if not (await r if isawaitable(r := f(event)) else r):
return False
return True
class Not(Combinable):
@ -159,5 +168,5 @@ class Not(Combinable):
"""
return self._filter
def __call__(self, event: Event) -> bool:
return not self._filter(event)
async def __call__(self, event: Event) -> bool:
return not (await r if isawaitable(r := self._filter(event)) else r)