4141from datetime import datetime
4242from functools import total_ordering
4343from typing import TypedDict
44+ import typing
4445
4546from gevent .event import Event
4647
4748from cmscommon .datetime import make_datetime , make_timestamp
4849
4950
50- # TODO: make PriorityQueue generic over the exact type of the QueueItem.
51- # Allows more exact typing in other classes (i.e. TriggeredService and its children).
5251class QueueItem :
5352
5453 """Payload of an item in the queue.
@@ -61,15 +60,16 @@ def to_dict(self) -> dict:
6160 """Return a dict() representation of the object."""
6261 return self .__dict__
6362
63+ QueueItemT = typing .TypeVar ('QueueItemT' , bound = QueueItem )
6464
6565@total_ordering
66- class QueueEntry :
66+ class QueueEntry ( typing . Generic [ QueueItemT ]) :
6767
6868 """Type of the actual objects in the queue.
6969
7070 """
7171
72- def __init__ (self , item : QueueItem , priority : int , timestamp : datetime , index : int ):
72+ def __init__ (self , item : QueueItemT , priority : int , timestamp : datetime , index : int ):
7373 """Create a QueueEntry object.
7474
7575 item: the payload.
@@ -102,7 +102,7 @@ class QueueEntryDict(TypedDict):
102102 priority : int
103103 timestamp : float
104104
105- class PriorityQueue :
105+ class PriorityQueue ( typing . Generic [ QueueItemT ]) :
106106
107107 """A priority queue.
108108
@@ -125,11 +125,11 @@ def __init__(self):
125125 """Create a priority queue."""
126126 # The queue: a min-heap whose elements are of the form
127127 # (priority, timestamp, item), where item is the actual data.
128- self ._queue : list [QueueEntry ] = []
128+ self ._queue : list [QueueEntry [ QueueItemT ] ] = []
129129
130130 # Reverse lookup for the items in the queue: a dictionary
131131 # associating the index in the queue to each item.
132- self ._reverse = {}
132+ self ._reverse : dict [ QueueItemT , int ] = {}
133133
134134 # Event to signal that there are items in the queue.
135135 self ._event = Event ()
@@ -159,7 +159,7 @@ def _verify(self) -> bool:
159159 return False
160160 return True
161161
162- def __contains__ (self , item : QueueItem ) -> bool :
162+ def __contains__ (self , item : QueueItemT ) -> bool :
163163 """Implement the 'in' operator for an item in the queue.
164164
165165 item: an item to search.
@@ -234,7 +234,7 @@ def _updown_heap(self, idx: int) -> int:
234234 idx = self ._up_heap (idx )
235235 return self ._down_heap (idx )
236236
237- def push (self , item : QueueItem , priority : int | None = None ,
237+ def push (self , item : QueueItemT , priority : int | None = None ,
238238 timestamp : datetime | None = None ) -> bool :
239239 """Push an item in the queue. If timestamp is not specified,
240240 uses the current time.
@@ -270,7 +270,7 @@ def push(self, item: QueueItem, priority: int | None = None,
270270
271271 return True
272272
273- def top (self , wait : bool = False ) -> QueueEntry :
273+ def top (self , wait : bool = False ) -> QueueEntry [ QueueItemT ] :
274274 """Return the first element in the queue without extracting it.
275275
276276 wait: if True, block until an element is present.
@@ -292,7 +292,7 @@ def top(self, wait: bool = False) -> QueueEntry:
292292 continue
293293 return self ._queue [0 ]
294294
295- def pop (self , wait : bool = False ) -> QueueEntry :
295+ def pop (self , wait : bool = False ) -> QueueEntry [ QueueItemT ] :
296296 """Extract (and return) the first element in the queue.
297297
298298 wait: if True, block until an element is present.
@@ -317,7 +317,7 @@ def pop(self, wait: bool = False) -> QueueEntry:
317317 self ._event .clear ()
318318 return top
319319
320- def remove (self , item : QueueItem ) -> QueueEntry :
320+ def remove (self , item : QueueItemT ) -> QueueEntry [ QueueItemT ] :
321321 """Remove an item from the queue. Raise a KeyError if not present.
322322
323323 item: the item to remove.
@@ -343,7 +343,7 @@ def remove(self, item: QueueItem) -> QueueEntry:
343343
344344 return entry
345345
346- def set_priority (self , item : QueueItem , priority : int ):
346+ def set_priority (self , item : QueueItemT , priority : int ):
347347 """Change the priority of an item inside the queue. Raises an
348348 exception if the item is not in the queue.
349349
0 commit comments