Skip to content

Commit

Permalink
feat: ASyncIterable and ASyncIterator repr (#154)
Browse files Browse the repository at this point in the history
* feat: iterable class reprs

* feat: slight optimization
  • Loading branch information
BobTheBuidler authored Mar 1, 2024
1 parent b45dcdf commit d2062bf
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions a_sync/iter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@

class ASyncIterable(AsyncIterable[T], Iterable[T]):
"""A hybrid Iterable/AsyncIterable implementation that can be used in both a `for` loop and an `async for` loop."""
def __iter__(self) -> Iterator[T]:
yield from ASyncIterator(self.__aiter__())
@classmethod
def wrap(cls, wrapped: AsyncIterable[T]) -> "ASyncIterable[T]":
# NOTE: for backward-compatability only. Will be removed soon.
logger.warning("ASyncIterable.wrap will be removed soon. Please replace uses with simple instantiation ie `ASyncIterable(wrapped)`")
return cls(wrapped)
def __init__(self, async_iterable: AsyncIterable[T]):
self.__wrapped__ = async_iterable
def __repr__(self) -> str:
return f"<{type(self).__name__} for {self.__wrapped__} at {hex(id(self))}>"
def __iter__(self) -> Iterator[T]:
yield from ASyncIterator(self.__aiter__())
def __aiter__(self) -> AsyncIterator[T]:
return self.__wrapped__.__aiter__()
__slots__ = "__wrapped__",
Expand Down
2 changes: 1 addition & 1 deletion a_sync/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def _should_await(self, kwargs: dict) -> bool:
except (AttributeError, exceptions.NoFlagsFound):
return False
def __await__(self) -> Generator[Any, None, T]:
return self().__await__()
return self(sync=False).__await__()

class HiddenMethodDescriptor(ASyncMethodDescriptorAsyncDefault[I, Tuple[()], T]):
def __get__(self, instance: I, owner: Any) -> HiddenMethod[I, T]:
Expand Down

0 comments on commit d2062bf

Please sign in to comment.