Browse Source

Prioritise async iteration before sync iteration in utils.find/get

pull/10109/head
James Hilton-Balfe 3 years ago
committed by dolfies
parent
commit
0cd06789e4
  1. 20
      discord/utils.py

20
discord/utils.py

@ -453,12 +453,12 @@ async def _afind(predicate: Callable[[T], Any], iterable: AsyncIterable[T], /) -
@overload
def find(predicate: Callable[[T], Any], iterable: Iterable[T], /) -> Optional[T]:
def find(predicate: Callable[[T], Any], iterable: AsyncIterable[T], /) -> Coro[Optional[T]]:
...
@overload
def find(predicate: Callable[[T], Any], iterable: AsyncIterable[T], /) -> Coro[Optional[T]]:
def find(predicate: Callable[[T], Any], iterable: Iterable[T], /) -> Optional[T]:
...
@ -492,9 +492,9 @@ def find(predicate: Callable[[T], Any], iterable: _Iter[T], /) -> Union[Optional
"""
return (
_find(predicate, iterable) # type: ignore
if hasattr(iterable, '__iter__') # isinstance(iterable, collections.abc.Iterable) is too slow
else _afind(predicate, iterable) # type: ignore
_afind(predicate, iterable) # type: ignore
if hasattr(iterable, '__aiter__') # isinstance(iterable, collections.abc.AsyncIterable) is too slow
else _find(predicate, iterable) # type: ignore
)
@ -539,12 +539,12 @@ async def _aget(iterable: AsyncIterable[T], /, **attrs: Any) -> Optional[T]:
@overload
def get(iterable: Iterable[T], /, **attrs: Any) -> Optional[T]:
def get(iterable: AsyncIterable[T], /, **attrs: Any) -> Coro[Optional[T]]:
...
@overload
def get(iterable: AsyncIterable[T], /, **attrs: Any) -> Coro[Optional[T]]:
def get(iterable: Iterable[T], /, **attrs: Any) -> Optional[T]:
...
@ -608,9 +608,9 @@ def get(iterable: _Iter[T], /, **attrs: Any) -> Union[Optional[T], Coro[Optional
"""
return (
_get(iterable, **attrs) # type: ignore
if hasattr(iterable, '__iter__') # isinstance(iterable, collections.abc.Iterable) is too slow
else _aget(iterable, **attrs) # type: ignore
_aget(iterable, **attrs) # type: ignore
if hasattr(iterable, '__aiter__') # isinstance(iterable, collections.abc.AsyncIterable) is too slow
else _get(iterable, **attrs) # type: ignore
)

Loading…
Cancel
Save