Skip to content

Commit

Permalink
[result] raise TypeError if bool(Result) is checked without itera…
Browse files Browse the repository at this point in the history
…ting
  • Loading branch information
david-lev committed Jan 25, 2025
1 parent dfd869f commit 71ee3a7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
7 changes: 4 additions & 3 deletions pywa/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1488,6 +1488,7 @@ def unblock_users(
def get_blocked_users(
self,
phone_id: str | int | None = None,
*,
limit: int | None = None,
batch_size: int | None = None,
) -> Result[User]:
Expand All @@ -1502,10 +1503,10 @@ def get_blocked_users(
Args:
phone_id: The phone ID to get the list of blocked users from (optional, if not provided, the client's phone ID will be used).
limit: The maximum number of users to return (optional, if not provided, all users will be returned).
batch_size: The number of users to return per request (optional, default: 50).
batch_size: The number of users to return per request (optional).
Returns:
A list of blocked users.
A Result object with the list of blocked users. You can iterate over the result to get the users.
"""
return Result(
fetcher=functools.partial(
Expand Down Expand Up @@ -2394,7 +2395,7 @@ def get_flows(
batch_size: The number of flows to return in each batch (optional).
Returns:
The details of all flows.
A Result object with the list of flows. You can iterate over the result to get the flows.
"""
return Result(
fetcher=functools.partial(
Expand Down
11 changes: 10 additions & 1 deletion pywa/types/others.py
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,7 @@ def __init__(
self._all_items: list[_T] = []
self._after = None
self._has_next = True
self._iterating = False

def _process_data(self, data: dict) -> list[_T]:
self._after = data.get("paging", {}).get("cursors", {}).get("after")
Expand All @@ -1026,11 +1027,13 @@ async def _get_items_async(self) -> list[_T]:
def __iter__(self) -> Result[_T]:
if self._is_async:
raise TypeError("Use `async for` for async results.")
self._iterating = True
return self

def __aiter__(self) -> Result[_T]:
if not self._is_async:
raise TypeError("Use `for` for sync results.")
self._iterating = True
return self

def _is_fetch_next_needed(self) -> bool:
Expand Down Expand Up @@ -1061,10 +1064,16 @@ async def __anext__(self) -> _T:
return item

def __bool__(self) -> bool:
if not self._iterating:
raise TypeError("Result is not being iterated over.")
return self._has_next or bool(self._all_items)

def __repr__(self) -> str:
return f"Result({self._all_items!r}, has_next={self._has_next})"
return (
f"Result({self._all_items!r}, has_next={self._has_next})"
if self._iterating
else "Result(<start iterating to see items>)"
)

def __str__(self) -> str:
return self.__repr__()
7 changes: 4 additions & 3 deletions pywa_async/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,7 @@ async def unblock_users(
async def get_blocked_users(
self,
phone_id: str | int | None = None,
*,
limit: int | None = None,
batch_size: int | None = None,
) -> Result[User]:
Expand All @@ -1267,10 +1268,10 @@ async def get_blocked_users(
Args:
phone_id: The phone ID to get the list of blocked users from (optional, if not provided, the client's phone ID will be used).
limit: The maximum number of users to return (optional, if not provided, all users will be returned).
batch_size: The number of users to return per request (optional, default: 50).
batch_size: The number of users to return per request (optional).
Returns:
A list of blocked users.
A Result object with the list of blocked users. You can iterate over the result to get the users.
"""
return Result(
fetcher=functools.partial(
Expand Down Expand Up @@ -2180,7 +2181,7 @@ async def get_flows(
batch_size: The number of flows to return in each batch (optional).
Returns:
The details of all flows.
A Result object with the list of flows. You can iterate over the result to get the flows.
"""
return Result(
fetcher=functools.partial(
Expand Down

0 comments on commit 71ee3a7

Please sign in to comment.