Skip to content

Commit 4c9e638

Browse files
committed
Remaining docstrings pre-prose docs
1 parent f509d0c commit 4c9e638

File tree

6 files changed

+55
-15
lines changed

6 files changed

+55
-15
lines changed

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ select = [
3939
typing-modules = ["async_utils._typings"]
4040

4141
ignore = [
42+
"DOC201", # Not docing generic return types
43+
"DOC501", # Not explicitly documenting raised exception types
4244
"ANN202", # implied return fine sometimes
4345
"ANN401", # Any is the correct type in some cases
4446
"ASYNC116", # Long sleeps are fine

src/async_utils/_qs.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828

2929

3030
class QueueEmpty(Exception):
31-
pass
31+
"""Raised when a queue is empty and attempting to get without waiting."""
3232

3333

3434
class QueueFull(Exception):
35-
pass
35+
"""Raised when a queue is full and attempting to put without waiting."""
3636

3737

3838
class AsyncEvent:
@@ -190,7 +190,15 @@ def set(self, /) -> bool:
190190
return success
191191

192192

193-
class _BaseQueue[T]:
193+
class BaseQueue[T]:
194+
"""Base queue implementation.
195+
196+
Not meant for direct public use. You probably should use
197+
one of the provided subclasses.
198+
199+
May make sense as a type annotation.
200+
"""
201+
194202
__slots__ = ("__get_ws", "__put_ws", "__unlocked", "__unlocked", "__ws", "maxsize")
195203

196204
def __init__(self, /, maxsize: int | None = None) -> None:
@@ -302,6 +310,7 @@ def _release(self, /) -> None:
302310
break
303311

304312
async def async_put(self, item: T, /) -> None:
313+
"""Put an item into the queue."""
305314
waiters = self.__ws
306315
put_waiters = self.__put_ws
307316
success = self._acquire_nowait_put()
@@ -339,6 +348,15 @@ async def async_put(self, item: T, /) -> None:
339348
def sync_put(
340349
self, item: T, /, *, blocking: bool = True, timeout: float | None = None
341350
) -> None:
351+
"""Put an item into the queue.
352+
353+
Parameters
354+
----------
355+
blocking: bool
356+
Whether or not to block until space in the queue is available.
357+
timeout: float | None
358+
The maximum time to wait if waiting on room in the queue.
359+
"""
342360
waiters = self.__ws
343361
put_waiters = self.__put_ws
344362
success = self._acquire_nowait_put()
@@ -377,6 +395,7 @@ def sync_put(
377395
self._release()
378396

379397
async def async_get(self, /) -> T:
398+
"""Get an item from the queue."""
380399
waiters = self.__ws
381400
get_waiters = self.__get_ws
382401
success = self._acquire_nowait_get()
@@ -414,6 +433,15 @@ async def async_get(self, /) -> T:
414433
return item
415434

416435
def sync_get(self, /, *, blocking: bool = True, timeout: float | None = None) -> T:
436+
"""Get an item from the queue.
437+
438+
Parameters
439+
----------
440+
blocking: bool
441+
Whether or not to block until an item is available.
442+
timeout: float | None
443+
The maximum time to wait if waiting on an item.
444+
"""
417445
waiters = self.__ws
418446
get_waiters = self.__get_ws
419447

@@ -479,7 +507,7 @@ def getting(self, /) -> int:
479507
return len(self.__get_ws)
480508

481509

482-
class Queue[T](_BaseQueue[T]):
510+
class Queue[T](BaseQueue[T]):
483511
"""A thread-safe queue with both sync and async access methods."""
484512

485513
__slots__ = ("_data",)
@@ -507,7 +535,7 @@ def _get(self, /) -> T:
507535
return self._data.popleft()
508536

509537

510-
class LIFOQueue[T](_BaseQueue[T]):
538+
class LIFOQueue[T](BaseQueue[T]):
511539
"""A thread-safe queue with both sync and async access methods."""
512540

513541
__slots__ = ("_data",)
@@ -535,7 +563,7 @@ def _get(self, /) -> T:
535563
return self._data.pop()
536564

537565

538-
class PriorityQueue[T](_BaseQueue[T]):
566+
class PriorityQueue[T](BaseQueue[T]):
539567
"""A thread-safe queue with both sync and async access methods."""
540568

541569
__slots__ = ("_data",)

src/async_utils/bg_loop.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030

3131

3232
class LoopWrapper:
33+
"""Provides managed access to an event loop created with ``threaded_loop``.
34+
35+
This is not meant to be constructed manually
36+
"""
37+
3338
def __init_subclass__(cls) -> t.Never:
3439
msg = "Don't subclass this"
3540
raise RuntimeError(msg)
@@ -97,7 +102,7 @@ def wait_sync(self, timeout: float | None) -> bool:
97102
return not pending
98103

99104

100-
def run_forever(
105+
def _run_forever(
101106
loop: asyncio.AbstractEventLoop,
102107
/,
103108
*,
@@ -157,7 +162,7 @@ def threaded_loop(
157162
wrapper = None
158163
try:
159164
args, kwargs = (loop,), {"eager": use_eager_task_factory}
160-
thread = threading.Thread(target=run_forever, args=args, kwargs=kwargs)
165+
thread = threading.Thread(target=_run_forever, args=args, kwargs=kwargs)
161166
thread.start()
162167
wrapper = LoopWrapper(loop)
163168
yield wrapper

src/async_utils/dual_color.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414

1515
from __future__ import annotations
1616

17-
from ._qs import LIFOQueue, PriorityQueue, Queue, QueueEmpty, QueueFull
17+
from ._qs import BaseQueue, LIFOQueue, PriorityQueue, Queue, QueueEmpty, QueueFull
1818

19-
__all__ = ("LIFOQueue", "PriorityQueue", "Queue", "QueueEmpty", "QueueFull")
19+
__all__ = ("BaseQueue", "LIFOQueue", "PriorityQueue", "Queue", "QueueEmpty", "QueueFull")

src/async_utils/priority_sem.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
_priority: contextvars.ContextVar[int] = contextvars.ContextVar("_priority", default=0)
3232

3333

34-
class PriorityWaiter:
34+
class _PriorityWaiter:
3535
__slots__ = ("future", "ord")
3636

3737
def __init__(self, priority: int, ts: float, future: asyncio.Future[None], /) -> None:
@@ -53,7 +53,7 @@ def __await__(self) -> Generator[t.Any, t.Any, None]:
5353
return (yield from self.future.__await__())
5454

5555
def __lt__(self: t.Self, other: object) -> bool:
56-
if not isinstance(other, PriorityWaiter):
56+
if not isinstance(other, _PriorityWaiter):
5757
return NotImplemented
5858
return self.ord < other.ord
5959

@@ -119,7 +119,7 @@ def __init__(self, value: int = 1) -> None:
119119
if value < 0:
120120
msg = "Semaphore initial value must be >= 0"
121121
raise ValueError(msg)
122-
self._waiters: list[PriorityWaiter] | None = None
122+
self._waiters: list[_PriorityWaiter] | None = None
123123
self._value: int = value
124124

125125
def _get_loop(self) -> asyncio.AbstractEventLoop:
@@ -168,7 +168,7 @@ async def __acquire(self, priority: int = _default) -> bool:
168168

169169
fut = loop.create_future()
170170
now = loop.time()
171-
waiter = PriorityWaiter(priority, now, fut)
171+
waiter = _PriorityWaiter(priority, now, fut)
172172

173173
heapq.heappush(self._waiters, waiter)
174174

src/async_utils/scheduler.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,17 @@
2020

2121
from . import _typings as t
2222

23-
__all__ = ("Scheduler",)
23+
__all__ = ("CancellationToken", "Scheduler")
2424

2525
MISSING: t.Any = object()
2626

2727

2828
class CancellationToken:
29+
"""An object to use for cancelation of a task.
30+
31+
Not meant for public construction.
32+
"""
33+
2934
__slots__ = ()
3035

3136

0 commit comments

Comments
 (0)