11import enum
22
33from types import TracebackType
4- from typing import final , Optional , Type
54
65from . import events
76from . import exceptions
@@ -23,14 +22,13 @@ class _State(enum.Enum):
2322 EXITED = "finished"
2423
2524
26- @final
2725class Timeout :
2826 """Asynchronous context manager for cancelling overdue coroutines.
2927
3028 Use `timeout()` or `timeout_at()` rather than instantiating this class directly.
3129 """
3230
33- def __init__ (self , when : Optional [ float ] ) -> None :
31+ def __init__ (self , when : float | None ) -> None :
3432 """Schedule a timeout that will trigger at a given loop time.
3533
3634 - If `when` is `None`, the timeout will never trigger.
@@ -39,15 +37,15 @@ def __init__(self, when: Optional[float]) -> None:
3937 """
4038 self ._state = _State .CREATED
4139
42- self ._timeout_handler : Optional [ events .TimerHandle ] = None
43- self ._task : Optional [ tasks .Task ] = None
40+ self ._timeout_handler : events .TimerHandle | None = None
41+ self ._task : tasks .Task | None = None
4442 self ._when = when
4543
46- def when (self ) -> Optional [ float ] :
44+ def when (self ) -> float | None :
4745 """Return the current deadline."""
4846 return self ._when
4947
50- def reschedule (self , when : Optional [ float ] ) -> None :
48+ def reschedule (self , when : float | None ) -> None :
5149 """Reschedule the timeout."""
5250 if self ._state is not _State .ENTERED :
5351 if self ._state is _State .CREATED :
@@ -96,10 +94,10 @@ async def __aenter__(self) -> "Timeout":
9694
9795 async def __aexit__ (
9896 self ,
99- exc_type : Optional [ Type [ BaseException ]] ,
100- exc_val : Optional [ BaseException ] ,
101- exc_tb : Optional [ TracebackType ] ,
102- ) -> Optional [ bool ] :
97+ exc_type : type [ BaseException ] | None ,
98+ exc_val : BaseException | None ,
99+ exc_tb : TracebackType | None ,
100+ ) -> bool | None :
103101 assert self ._state in (_State .ENTERED , _State .EXPIRING )
104102
105103 if self ._timeout_handler is not None :
@@ -142,7 +140,7 @@ def _insert_timeout_error(exc_val: BaseException) -> None:
142140 exc_val = exc_val .__context__
143141
144142
145- def timeout (delay : Optional [ float ] ) -> Timeout :
143+ def timeout (delay : float | None ) -> Timeout :
146144 """Timeout async context manager.
147145
148146 Useful in cases when you want to apply timeout logic around block
@@ -162,7 +160,7 @@ def timeout(delay: Optional[float]) -> Timeout:
162160 return Timeout (loop .time () + delay if delay is not None else None )
163161
164162
165- def timeout_at (when : Optional [ float ] ) -> Timeout :
163+ def timeout_at (when : float | None ) -> Timeout :
166164 """Schedule the timeout at absolute time.
167165
168166 Like timeout() but argument gives absolute time in the same clock system
0 commit comments