|
28 | 28 |
|
29 | 29 | from . import _typings as t |
30 | 30 |
|
| 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 | + |
31 | 69 | __all__ = ("LIFOQueue", "PriorityQueue", "Queue", "QueueEmpty", "QueueFull") |
32 | 70 |
|
33 | 71 |
|
@@ -567,7 +605,7 @@ def _get(self, /) -> T: |
567 | 605 | return self._data.pop() |
568 | 606 |
|
569 | 607 |
|
570 | | -class PriorityQueue[T](BaseQueue[T]): |
| 608 | +class PriorityQueue[T: HeapqOrderable](BaseQueue[T]): |
571 | 609 | """A thread-safe queue with both sync and async access methods.""" |
572 | 610 |
|
573 | 611 | __slots__ = ("_data", "_lock") |
|
0 commit comments