From 2b5a3b2a3395b2d0555d42cfa7810c9bc9bf1a31 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Fri, 26 Apr 2024 12:10:59 -0400 Subject: [PATCH] feat: EmptySequenceError (#239) --- a_sync/exceptions.py | 3 +++ a_sync/task.py | 23 +++++++++++------------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/a_sync/exceptions.py b/a_sync/exceptions.py index bf7b8288..a91631aa 100644 --- a/a_sync/exceptions.py +++ b/a_sync/exceptions.py @@ -87,3 +87,6 @@ def __init__(self, exc: E, task: asyncio.Task) -> None: super().__init__(f"{exc.__class__.__name__}: {exc}", task) self.exception = exc self.task = task + +class EmptySequenceError(ValueError): + ... \ No newline at end of file diff --git a/a_sync/task.py b/a_sync/task.py index ef474db2..68806b34 100644 --- a/a_sync/task.py +++ b/a_sync/task.py @@ -5,11 +5,10 @@ import inspect import logging -from a_sync import _kwargs +from a_sync import _kwargs, exceptions from a_sync._bound import ASyncBoundMethod, ASyncMethodDescriptor, ASyncMethodDescriptorSyncDefault from a_sync._task import create_task from a_sync._typing import * -from a_sync import exceptions from a_sync.base import ASyncGenericBase from a_sync.iter import ASyncIterator, ASyncGeneratorFunction from a_sync.modified import ASyncFunction @@ -252,7 +251,7 @@ async def all(self, pop: bool = True) -> bool: await self.__if_pop_clear(pop) return False return True - except IterableIsEmptyError: + except _EmptySequenceError: return True @ASyncMethodDescriptorSyncDefault @@ -263,7 +262,7 @@ async def any(self, pop: bool = True) -> bool: await self.__if_pop_clear(pop) return True return False - except IterableIsEmptyError: + except _EmptySequenceError: return False @ASyncMethodDescriptorSyncDefault @@ -273,8 +272,8 @@ async def max(self, pop: bool = True) -> V: async for key, result in self.__aiter__(pop=pop): if max is None or result > max: max = result - except IterableIsEmptyError: - raise ValueError("max() arg is an empty sequence") from None + except _EmptySequenceError: + raise exceptions.EmptySequenceError("max() arg is an empty sequence") from None return max @ASyncMethodDescriptorSyncDefault @@ -284,8 +283,8 @@ async def min(self, pop: bool = True) -> V: async for key, result in self.__aiter__(pop=pop): if min is None or result < min: min = result - except IterableIsEmptyError: - raise ValueError("min() arg is an empty sequence") from None + except _EmptySequenceError: + raise exceptions.EmptySequenceError("min() arg is an empty sequence") from None return min @ASyncMethodDescriptorSyncDefault @@ -295,7 +294,7 @@ async def sum(self, pop: bool = False) -> V: async for key, result in self.__aiter__(pop=pop): retval += result return result - except IterableIsEmptyError: + except _EmptySequenceError: return 0 @ASyncIterator.wrap @@ -372,7 +371,7 @@ async def _tasks_for_iterables(self, *iterables: AnyIterableOrAwaitableIterable[ try: async for key in as_yielded(*[_yield_keys(iterable) for iterable in iterables]): # type: ignore [attr-defined] yield key, self[key] # ensure task is running - except IterableIsEmptyError: + except _EmptySequenceError: if len(iterables) == 1: raise raise RuntimeError("DEV: figure out how to handle this situation") from None @@ -430,7 +429,7 @@ def _yield(key: K, value: V, yields: Literal['keys', 'both']) -> Union[K, Tuple[ else: raise ValueError(f"`yields` must be 'keys' or 'both'. You passed {yields}") -class IterableIsEmptyError(ValueError): +class _EmptySequenceError(ValueError): ... async def _yield_keys(iterable: AnyIterableOrAwaitableIterable[K]) -> AsyncIterator[K]: @@ -444,7 +443,7 @@ async def _yield_keys(iterable: AnyIterableOrAwaitableIterable[K]) -> AsyncItera Keys extracted from the iterable. """ if not iterable: - raise IterableIsEmptyError(iterable) + raise _EmptySequenceError(iterable) elif isinstance(iterable, AsyncIterable): async for key in iterable: yield key