1- """Accelerator module for asyncio
2- """
1+ """Accelerator module for asyncio"""
2+
33import sys
44from asyncio .events import AbstractEventLoop
55from collections .abc import Awaitable , Callable , Coroutine , Generator
@@ -26,7 +26,8 @@ class Future(Awaitable[_T]):
2626
2727 - This class is not compatible with the wait() and as_completed()
2828 methods in the concurrent.futures package.
29- """
29+ """
30+
3031 _state : str
3132 @property
3233 def _exception (self ) -> BaseException | None : ...
@@ -38,79 +39,86 @@ class Future(Awaitable[_T]):
3839 _asyncio_future_blocking : bool # is a part of duck-typing contract for `Future`
3940 def __init__ (self , * , loop : AbstractEventLoop | None = None ) -> None : ...
4041 def __del__ (self ) -> None :
41- """Called when the instance is about to be destroyed.
42- """
42+ """Called when the instance is about to be destroyed."""
43+
4344 def get_loop (self ) -> AbstractEventLoop :
44- """Return the event loop the Future is bound to.
45- """
45+ """Return the event loop the Future is bound to."""
46+
4647 @property
4748 def _callbacks (self ) -> list [tuple [Callable [[Self ], Any ], Context ]]: ...
4849 def add_done_callback (self , fn : Callable [[Self ], object ], / , * , context : Context | None = None ) -> None :
4950 """Add a callback to be run when the future becomes done.
5051
51- The callback is called with a single argument - the future object. If
52- the future is already done when this is called, the callback is
53- scheduled with call_soon.
54- """
52+ The callback is called with a single argument - the future object. If
53+ the future is already done when this is called, the callback is
54+ scheduled with call_soon.
55+ """
56+
5557 def cancel (self , msg : Any | None = None ) -> bool :
5658 """Cancel the future and schedule callbacks.
5759
58- If the future is already done or cancelled, return False. Otherwise,
59- change the future's state to cancelled, schedule the callbacks and
60- return True.
61- """
60+ If the future is already done or cancelled, return False. Otherwise,
61+ change the future's state to cancelled, schedule the callbacks and
62+ return True.
63+ """
64+
6265 def cancelled (self ) -> bool :
63- """Return True if the future was cancelled.
64- """
66+ """Return True if the future was cancelled."""
67+
6568 def done (self ) -> bool :
6669 """Return True if the future is done.
6770
68- Done means either that a result / exception are available, or that the
69- future was cancelled.
70- """
71+ Done means either that a result / exception are available, or that the
72+ future was cancelled.
73+ """
74+
7175 def result (self ) -> _T :
7276 """Return the result this future represents.
7377
74- If the future has been cancelled, raises CancelledError. If the
75- future's result isn't yet available, raises InvalidStateError. If
76- the future is done and has an exception set, this exception is raised.
77- """
78+ If the future has been cancelled, raises CancelledError. If the
79+ future's result isn't yet available, raises InvalidStateError. If
80+ the future is done and has an exception set, this exception is raised.
81+ """
82+
7883 def exception (self ) -> BaseException | None :
7984 """Return the exception that was set on this future.
8085
81- The exception (or None if no exception was set) is returned only if
82- the future is done. If the future has been cancelled, raises
83- CancelledError. If the future isn't done yet, raises
84- InvalidStateError.
85- """
86+ The exception (or None if no exception was set) is returned only if
87+ the future is done. If the future has been cancelled, raises
88+ CancelledError. If the future isn't done yet, raises
89+ InvalidStateError.
90+ """
91+
8692 def remove_done_callback (self , fn : Callable [[Self ], object ], / ) -> int :
8793 """Remove all instances of a callback from the "call when done" list.
8894
89- Returns the number of callbacks removed.
90- """
95+ Returns the number of callbacks removed.
96+ """
97+
9198 def set_result (self , result : _T , / ) -> None :
9299 """Mark the future done and set its result.
93100
94- If the future is already done when this method is called, raises
95- InvalidStateError.
96- """
101+ If the future is already done when this method is called, raises
102+ InvalidStateError.
103+ """
104+
97105 def set_exception (self , exception : type | BaseException , / ) -> None :
98106 """Mark the future done and set an exception.
99107
100- If the future is already done when this method is called, raises
101- InvalidStateError.
102- """
108+ If the future is already done when this method is called, raises
109+ InvalidStateError.
110+ """
111+
103112 def __iter__ (self ) -> Generator [Any , None , _T ]:
104- """Implement iter(self).
105- """
113+ """Implement iter(self)."""
114+
106115 def __await__ (self ) -> Generator [Any , None , _T ]:
107- """Return an iterator to be used in await expression.
108- """
116+ """Return an iterator to be used in await expression."""
117+
109118 @property
110119 def _loop (self ) -> AbstractEventLoop : ...
111120 def __class_getitem__ (cls , item : Any , / ) -> GenericAlias :
112- """See PEP 585
113- """
121+ """See PEP 585"""
114122
115123if sys .version_info >= (3 , 12 ):
116124 _TaskCompatibleCoro : TypeAlias = Coroutine [Any , Any , _T_co ]
@@ -123,8 +131,8 @@ else:
123131# and `asyncio.Task.set_result()` always raises.
124132@disjoint_base
125133class Task (Future [_T_co ]): # type: ignore[type-var] # pyright: ignore[reportInvalidTypeArguments]
126- """A coroutine wrapped in a Future.
127- """
134+ """A coroutine wrapped in a Future."""
135+
128136 if sys .version_info >= (3 , 12 ):
129137 def __init__ (
130138 self ,
@@ -162,115 +170,121 @@ class Task(Future[_T_co]): # type: ignore[type-var] # pyright: ignore[reportIn
162170 def get_stack (self , * , limit : int | None = None ) -> list [FrameType ]:
163171 """Return the list of stack frames for this task's coroutine.
164172
165- If the coroutine is not done, this returns the stack where it is
166- suspended. If the coroutine has completed successfully or was
167- cancelled, this returns an empty list. If the coroutine was
168- terminated by an exception, this returns the list of traceback
169- frames.
173+ If the coroutine is not done, this returns the stack where it is
174+ suspended. If the coroutine has completed successfully or was
175+ cancelled, this returns an empty list. If the coroutine was
176+ terminated by an exception, this returns the list of traceback
177+ frames.
178+
179+ The frames are always ordered from oldest to newest.
170180
171- The frames are always ordered from oldest to newest.
181+ The optional limit gives the maximum number of frames to
182+ return; by default all available frames are returned. Its
183+ meaning differs depending on whether a stack or a traceback is
184+ returned: the newest frames of a stack are returned, but the
185+ oldest frames of a traceback are returned. (This matches the
186+ behavior of the traceback module.)
172187
173- The optional limit gives the maximum number of frames to
174- return; by default all available frames are returned. Its
175- meaning differs depending on whether a stack or a traceback is
176- returned: the newest frames of a stack are returned, but the
177- oldest frames of a traceback are returned. (This matches the
178- behavior of the traceback module.)
188+ For reasons beyond our control, only one stack frame is
189+ returned for a suspended coroutine.
190+ """
179191
180- For reasons beyond our control, only one stack frame is
181- returned for a suspended coroutine.
182- """
183192 def print_stack (self , * , limit : int | None = None , file : TextIO | None = None ) -> None :
184193 """Print the stack or traceback for this task's coroutine.
185194
186- This produces output similar to that of the traceback module,
187- for the frames retrieved by get_stack(). The limit argument
188- is passed to get_stack(). The file argument is an I/O stream
189- to which the output is written; by default output is written
190- to sys.stderr.
191- """
195+ This produces output similar to that of the traceback module,
196+ for the frames retrieved by get_stack(). The limit argument
197+ is passed to get_stack(). The file argument is an I/O stream
198+ to which the output is written; by default output is written
199+ to sys.stderr.
200+ """
192201 if sys .version_info >= (3 , 11 ):
193202 def cancelling (self ) -> int :
194203 """Return the count of the task's cancellation requests.
195204
196- This count is incremented when .cancel() is called
197- and may be decremented using .uncancel().
198- """
205+ This count is incremented when .cancel() is called
206+ and may be decremented using .uncancel().
207+ """
208+
199209 def uncancel (self ) -> int :
200210 """Decrement the task's count of cancellation requests.
201211
202- This should be used by tasks that catch CancelledError
203- and wish to continue indefinitely until they are cancelled again.
212+ This should be used by tasks that catch CancelledError
213+ and wish to continue indefinitely until they are cancelled again.
204214
205- Returns the remaining number of cancellation requests.
206- """
215+ Returns the remaining number of cancellation requests.
216+ """
207217
208218 def __class_getitem__ (cls , item : Any , / ) -> GenericAlias :
209- """See PEP 585
210- """
219+ """See PEP 585"""
211220
212221def get_event_loop () -> AbstractEventLoop :
213222 """Return an asyncio event loop.
214223
215- When called from a coroutine or a callback (e.g. scheduled with
216- call_soon or similar API), this function will always return the
217- running event loop.
224+ When called from a coroutine or a callback (e.g. scheduled with
225+ call_soon or similar API), this function will always return the
226+ running event loop.
227+
228+ If there is no running event loop set, the function will return
229+ the result of `get_event_loop_policy().get_event_loop()` call.
230+ """
218231
219- If there is no running event loop set, the function will return
220- the result of `get_event_loop_policy().get_event_loop()` call.
221- """
222232def get_running_loop () -> AbstractEventLoop :
223233 """Return the running event loop. Raise a RuntimeError if there is none.
224234
225- This function is thread-specific.
226- """
235+ This function is thread-specific.
236+ """
237+
227238def _set_running_loop (loop : AbstractEventLoop | None , / ) -> None :
228239 """Set the running event loop.
229240
230- This is a low-level function intended to be used by event loops.
231- This function is thread-specific.
232- """
241+ This is a low-level function intended to be used by event loops.
242+ This function is thread-specific.
243+ """
244+
233245def _get_running_loop () -> AbstractEventLoop :
234246 """Return the running event loop or None.
235247
236- This is a low-level function intended to be used by event loops.
237- This function is thread-specific.
238- """
248+ This is a low-level function intended to be used by event loops.
249+ This function is thread-specific.
250+ """
251+
239252def _register_task (task : Task [Any ]) -> None :
240253 """Register a new task in asyncio as executed by loop.
241254
242- Returns None.
243- """
255+ Returns None.
256+ """
257+
244258def _unregister_task (task : Task [Any ]) -> None :
245259 """Unregister a task.
246260
247- Returns None.
248- """
261+ Returns None.
262+ """
263+
249264def _enter_task (loop : AbstractEventLoop , task : Task [Any ]) -> None :
250265 """Enter into task execution or resume suspended task.
251266
252- Task belongs to loop.
267+ Task belongs to loop.
268+
269+ Returns None.
270+ """
253271
254- Returns None.
255- """
256272def _leave_task (loop : AbstractEventLoop , task : Task [Any ]) -> None :
257273 """Leave task execution or suspend a task.
258274
259- Task belongs to loop.
275+ Task belongs to loop.
260276
261- Returns None.
262- """
277+ Returns None.
278+ """
263279
264280if sys .version_info >= (3 , 12 ):
265281 def current_task (loop : AbstractEventLoop | None = None ) -> Task [Any ] | None :
266- """Return a currently executed task.
267- """
282+ """Return a currently executed task."""
268283
269284if sys .version_info >= (3 , 14 ):
270285 def future_discard_from_awaited_by (future : Future [Any ], waiter : Future [Any ], / ) -> None : ...
271286 def future_add_to_awaited_by (future : Future [Any ], waiter : Future [Any ], / ) -> None :
272- """Record that `fut` is awaited on by `waiter`.
273- """
287+ """Record that `fut` is awaited on by `waiter`."""
288+
274289 def all_tasks (loop : AbstractEventLoop | None = None ) -> set [Task [Any ]]:
275- """Return a set of all tasks for the loop.
276- """
290+ """Return a set of all tasks for the loop."""
0 commit comments