2121import threading
2222from collections .abc import Callable , Generator
2323from contextlib import contextmanager
24- from typing import Any , NamedTuple
24+
25+ from ._typings import Any , Self
2526
2627__all__ = ["PrioritySemaphore" , "priority_context" ]
2728
3031_priority : contextvars .ContextVar [int ] = contextvars .ContextVar ("_priority" , default = 0 )
3132
3233
33- class PriorityWaiter (NamedTuple ):
34- priority : int
35- ts : float
36- future : asyncio .Future [None ]
34+ class PriorityWaiter (tuple [int , float , asyncio .Future [None ]]):
35+ __slots__ = ()
36+
37+ def __new__ (cls , priority : int , ts : float , future : asyncio .Future [None ]) -> Self :
38+ return super ().__new__ (cls , (priority , ts , future ))
39+
40+ @property
41+ def priority (self ) -> int :
42+ return self [0 ]
43+
44+ @property
45+ def ts (self ) -> float :
46+ return self [1 ]
47+
48+ @property
49+ def future (self ) -> asyncio .Future [None ]:
50+ return self [2 ]
3751
3852 @property
3953 def cancelled (self ) -> Callable [[], bool ]:
@@ -49,7 +63,7 @@ def __await__(self) -> Generator[Any, Any, None]:
4963 def __lt__ (self , other : Any ) -> bool :
5064 if not isinstance (other , PriorityWaiter ):
5165 return NotImplemented
52- return ( self . priority , self . ts ) < ( other . priority , other . ts )
66+ return self [: 2 ] < other [: 2 ]
5367
5468
5569@contextmanager
0 commit comments