Skip to content

Commit dbbd2da

Browse files
committed
Add typevar bound for priority queue
see #23
1 parent 40b96a3 commit dbbd2da

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/async_utils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
__author__ = "Michael Hall"
1010
__license__ = "Apache-2.0"
1111
__copyright__ = "Copyright 2020-Present Michael Hall"
12-
__version__ = "2025.07.11b"
12+
__version__ = "2025.07.15b"
1313

1414
import os
1515
import sys

src/async_utils/_qs.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,44 @@
2828

2929
from . import _typings as t
3030

31+
TYPE_CHECKING = False
32+
if TYPE_CHECKING:
33+
import typing
34+
35+
_contraT = typing.TypeVar("_contraT", contravariant=True)
36+
37+
class LT(typing.Protocol[_contraT]):
38+
def __lt__(self, other: _contraT) -> bool:
39+
...
40+
41+
class GT(typing.Protocol[_contraT]):
42+
def __gt__(self, other: _contraT) -> bool:
43+
...
44+
45+
type HeapqOrderable = LT[t.Any] | GT[t.Any]
46+
47+
else:
48+
49+
class ExprWrapper:
50+
"""Future proof against runtime change preventing call expr in type statement."""
51+
52+
def __class_getitem__(cls, key: None) -> t.Any:
53+
import typing
54+
55+
_contraT = typing.TypeVar("_contraT", contravariant=True) # noqa: RUF052
56+
57+
class LT(typing.Protocol[_contraT]):
58+
def __lt__(self, other: _contraT) -> bool:
59+
...
60+
61+
class GT(typing.Protocol[_contraT]):
62+
def __gt__(self, other: _contraT) -> bool:
63+
...
64+
65+
return LT[t.Any] | GT[t.Any]
66+
67+
type HeapqOrderable = ExprWrapper[None]
68+
3169
__all__ = ("LIFOQueue", "PriorityQueue", "Queue", "QueueEmpty", "QueueFull")
3270

3371

@@ -567,7 +605,7 @@ def _get(self, /) -> T:
567605
return self._data.pop()
568606

569607

570-
class PriorityQueue[T](BaseQueue[T]):
608+
class PriorityQueue[T: HeapqOrderable](BaseQueue[T]):
571609
"""A thread-safe queue with both sync and async access methods."""
572610

573611
__slots__ = ("_data", "_lock")

0 commit comments

Comments
 (0)