Skip to content

Commit

Permalink
feat: EmptySequenceError (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobTheBuidler authored Apr 26, 2024
1 parent 6fffa4d commit 2b5a3b2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
3 changes: 3 additions & 0 deletions a_sync/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
...
23 changes: 11 additions & 12 deletions a_sync/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]:
Expand All @@ -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
Expand Down

0 comments on commit 2b5a3b2

Please sign in to comment.