Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Queue.get_nowait with i == 1 #85

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions a_sync/primitives/queue.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncio
import sys
from typing import Generic, TypeVar
from typing import Generic, List, TypeVar, Union, overload

T = TypeVar('T')

Expand All @@ -11,11 +11,19 @@

class Queue(*bases):
"""The only difference between an a_sync.Queue and an asyncio.Queue is that `get_nowait` can retrn multiple responses."""
def get_nowait(self, i: int = 1, can_return_less: bool = False) -> T:
@overload
def get_nowait(self, i = 1, can_return_less: bool = False) -> T:
...
@overload
def get_nowait(self, i: int, can_return_less: bool = False) -> List[T]:
...
def get_nowait(self, i: int = 1, can_return_less: bool = False) -> Union[T, List[T]]:
"""
Just like `asyncio.Queue.get_nowait`, but will return `i` items instead of 1.
Set `can_return_less` to True if you want to receive up to `i` items.
"""
if can_return_less and i == 1:
raise ValueError("you cant set i == 1 with can_return_less == True")
values = []
if i == -1:
while True:
Expand All @@ -30,4 +38,4 @@ def get_nowait(self, i: int = 1, can_return_less: bool = False) -> T:
if can_return_less:
break
raise
return values
return values[0] if i == 1 else values