11# -*- encoding: utf-8 -*-
2+ from __future__ import annotations
3+
24from functools import partial
35import sys
46from types import ModuleType
2022ASYNCIO_IMPORTED : bool = False
2123
2224
23- def current_task () -> typing .Optional [" asyncio.Task[typing.Any]" ]:
25+ def current_task () -> typing .Optional [asyncio .Task [typing .Any ]]:
2426 return None
2527
2628
27- def get_running_loop () -> typing .Optional [" asyncio.AbstractEventLoop" ]:
29+ def get_running_loop () -> typing .Optional [asyncio .AbstractEventLoop ]:
2830 return None
2931
3032
31- def _task_get_name (task : " asyncio.Task[typing.Any]" ) -> str :
33+ def _task_get_name (task : asyncio .Task [typing .Any ]) -> str :
3234 return "Task-%d" % id (task )
3335
3436
@@ -56,7 +58,7 @@ def link_existing_loop_to_current_thread() -> None:
5658 import asyncio
5759
5860 # Only track if there's actually a running loop
59- running_loop : typing .Optional [" asyncio.AbstractEventLoop" ] = None
61+ running_loop : typing .Optional [asyncio .AbstractEventLoop ] = None
6062 try :
6163 running_loop = asyncio .get_running_loop ()
6264 except RuntimeError :
@@ -79,7 +81,7 @@ def _(asyncio: ModuleType) -> None:
7981 elif hasattr (asyncio .Task , "current_task" ):
8082 globals ()["current_task" ] = asyncio .Task .current_task
8183
82- def _get_running_loop () -> typing .Optional [" aio.AbstractEventLoop" ]:
84+ def _get_running_loop () -> typing .Optional [aio .AbstractEventLoop ]:
8385 try :
8486 return typing .cast ("aio.AbstractEventLoop" , asyncio .get_running_loop ())
8587 except RuntimeError :
@@ -104,11 +106,11 @@ def _get_running_loop() -> typing.Optional["aio.AbstractEventLoop"]:
104106
105107 @partial (wrap , policy_class .set_event_loop ) # pyright: ignore[reportArgumentType]
106108 def _ (
107- f : typing .Callable [[object , typing .Optional [" aio.AbstractEventLoop" ]], None ],
109+ f : typing .Callable [[object , typing .Optional [aio .AbstractEventLoop ]], None ],
108110 args : typing .Any ,
109111 kwargs : typing .Any ,
110112 ) -> None :
111- loop : typing .Optional [" aio.AbstractEventLoop" ] = get_argument_value (args , kwargs , 1 , "loop" )
113+ loop : typing .Optional [aio .AbstractEventLoop ] = get_argument_value (args , kwargs , 1 , "loop" )
112114 if init_stack :
113115 stack .track_asyncio_loop (typing .cast (int , ddtrace_threading .current_thread ().ident ), loop )
114116 return f (* args , ** kwargs )
@@ -120,27 +122,27 @@ def _(f: typing.Callable[..., None], args: tuple[typing.Any, ...], kwargs: dict[
120122 try :
121123 return f (* args , ** kwargs )
122124 finally :
123- children : list [" aio.Future[typing.Any]" ] = typing .cast (
124- list [" aio.Future[typing.Any]" ] , get_argument_value (args , kwargs , 1 , "children" )
125+ children : list [aio .Future [typing .Any ]] = typing .cast (
126+ " list[aio.Future[typing.Any]]" , get_argument_value (args , kwargs , 1 , "children" )
125127 )
126128 assert children is not None # nosec: assert is used for typing
127129
128130 if globals ()["get_running_loop" ]() is not None :
129- parent : typing .Optional [" aio.Task[typing.Any]" ] = globals ()["current_task" ]()
131+ parent : typing .Optional [aio .Task [typing .Any ]] = globals ()["current_task" ]()
130132 if parent is not None :
131133 for child in children :
132134 stack .link_tasks (parent , child )
133135
134136 @partial (wrap , sys .modules ["asyncio" ].tasks ._wait )
135137 def _ (
136- f : typing .Callable [..., tuple [set [" aio.Future[typing.Any]" ], set [" aio.Future[typing.Any]" ]]],
138+ f : typing .Callable [..., tuple [set [aio .Future [typing .Any ]], set [aio .Future [typing .Any ]]]],
137139 args : tuple [typing .Any , ...],
138140 kwargs : dict [str , typing .Any ],
139141 ) -> typing .Any :
140142 try :
141143 return f (* args , ** kwargs )
142144 finally :
143- futures = typing .cast (set [" aio.Future[typing.Any]" ] , get_argument_value (args , kwargs , 0 , "fs" ))
145+ futures = typing .cast (" set[aio.Future[typing.Any]]" , get_argument_value (args , kwargs , 0 , "fs" ))
144146
145147 if globals ()["get_running_loop" ]() is not None :
146148 parent = typing .cast ("aio.Task[typing.Any]" , globals ()["current_task" ]())
@@ -149,16 +151,16 @@ def _(
149151
150152 @partial (wrap , sys .modules ["asyncio" ].tasks .as_completed )
151153 def _ (
152- f : typing .Callable [..., typing .Generator [" aio.Future[typing.Any]" , typing .Any , None ]],
154+ f : typing .Callable [..., typing .Generator [aio .Future [typing .Any ], typing .Any , None ]],
153155 args : tuple [typing .Any , ...],
154156 kwargs : dict [str , typing .Any ],
155157 ) -> typing .Any :
156- loop = typing .cast (typing .Optional [" aio.AbstractEventLoop" ] , kwargs .get ("loop" ))
157- parent : typing .Optional [" aio.Task[typing.Any]" ] = globals ()["current_task" ]()
158+ loop = typing .cast (" typing.Optional[aio.AbstractEventLoop]" , kwargs .get ("loop" ))
159+ parent : typing .Optional [aio .Task [typing .Any ]] = globals ()["current_task" ]()
158160
159161 if parent is not None :
160- fs = typing .cast (typing .Iterable [" aio.Future[typing.Any]" ] , get_argument_value (args , kwargs , 0 , "fs" ))
161- futures : set [" aio.Future[typing.Any]" ] = {asyncio .ensure_future (f , loop = loop ) for f in set (fs )}
162+ fs = typing .cast (" typing.Iterable[aio.Future[typing.Any]]" , get_argument_value (args , kwargs , 0 , "fs" ))
163+ futures : set [aio .Future [typing .Any ]] = {asyncio .ensure_future (f , loop = loop ) for f in set (fs )}
162164 for future in futures :
163165 stack .link_tasks (parent , future )
164166
@@ -177,15 +179,15 @@ def _(
177179 # Wrap asyncio.shield to link parent task to shielded future
178180 @partial (wrap , sys .modules ["asyncio" ].tasks .shield )
179181 def _ (
180- f : typing .Callable [..., " aio.Future[typing.Any]" ],
182+ f : typing .Callable [..., aio .Future [typing .Any ]],
181183 args : tuple [typing .Any , ...],
182184 kwargs : dict [str , typing .Any ],
183185 ) -> typing .Any :
184- loop = typing .cast (typing .Optional [" aio.AbstractEventLoop" ] , kwargs .get ("loop" ))
186+ loop = typing .cast (" typing.Optional[aio.AbstractEventLoop]" , kwargs .get ("loop" ))
185187 awaitable = typing .cast ("aio.Future[typing.Any]" , get_argument_value (args , kwargs , 0 , "arg" ))
186- future : " aio.Future[typing.Any]" = asyncio .ensure_future (awaitable , loop = loop )
188+ future : aio .Future [typing .Any ] = asyncio .ensure_future (awaitable , loop = loop )
187189
188- parent : typing .Optional [" aio.Task[typing.Any]" ] = globals ()["current_task" ]()
190+ parent : typing .Optional [aio .Task [typing .Any ]] = globals ()["current_task" ]()
189191 if parent is not None :
190192 stack .link_tasks (parent , future )
191193
@@ -208,13 +210,13 @@ def _(
208210
209211 @partial (wrap , taskgroup_class .create_task )
210212 def _ (
211- f : typing .Callable [..., " aio.Task[typing.Any]" ],
213+ f : typing .Callable [..., aio .Task [typing .Any ]],
212214 args : tuple [typing .Any , ...],
213215 kwargs : dict [str , typing .Any ],
214- ) -> " aio.Task[typing.Any]" :
215- result : " aio.Task[typing.Any]" = f (* args , ** kwargs )
216+ ) -> aio .Task [typing .Any ]:
217+ result : aio .Task [typing .Any ] = f (* args , ** kwargs )
216218
217- parent : typing .Optional [" aio.Task[typing.Any]" ] = globals ()["current_task" ]()
219+ parent : typing .Optional [aio .Task [typing .Any ]] = globals ()["current_task" ]()
218220 if parent is not None and result is not None :
219221 # Link parent task to the task created by TaskGroup
220222 stack .link_tasks (parent , result )
@@ -228,13 +230,13 @@ def _(
228230 # event loop's timeout handler, not by creating new tasks.
229231 @partial (wrap , sys .modules ["asyncio" ].tasks .create_task )
230232 def _ (
231- f : typing .Callable [..., " aio.Task[typing.Any]" ],
233+ f : typing .Callable [..., aio .Task [typing .Any ]],
232234 args : tuple [typing .Any , ...],
233235 kwargs : dict [str , typing .Any ],
234- ) -> " aio.Task[typing.Any]" :
236+ ) -> aio .Task [typing .Any ]:
235237 # kwargs will typically contain context (Python 3.11+ only) and eager_start (Python 3.14+ only)
236- task : " aio.Task[typing.Any]" = f (* args , ** kwargs )
237- parent : typing .Optional [" aio.Task[typing.Any]" ] = globals ()["current_task" ]()
238+ task : aio .Task [typing .Any ] = f (* args , ** kwargs )
239+ parent : typing .Optional [aio .Task [typing .Any ]] = globals ()["current_task" ]()
238240
239241 if parent is not None :
240242 stack .weak_link_tasks (parent , task )
@@ -264,18 +266,18 @@ def _(uvloop: ModuleType) -> None:
264266 init_stack : bool = config .stack .enabled and stack .is_available
265267
266268 # Wrap uvloop.new_event_loop to track loops when they're created
267- new_event_loop_func : typing .Optional [typing .Callable [[], " asyncio.AbstractEventLoop" ]] = getattr (
269+ new_event_loop_func : typing .Optional [typing .Callable [[], asyncio .AbstractEventLoop ]] = getattr (
268270 uvloop , "new_event_loop" , None
269271 )
270272 if new_event_loop_func is not None :
271273
272274 @partial (wrap , new_event_loop_func ) # type: ignore[arg-type]
273275 def _ (
274- f : typing .Callable [[], " asyncio.AbstractEventLoop" ],
276+ f : typing .Callable [[], asyncio .AbstractEventLoop ],
275277 args : tuple [typing .Any , ...],
276278 kwargs : dict [str , typing .Any ],
277- ) -> " asyncio.AbstractEventLoop" :
278- loop : " asyncio.AbstractEventLoop" = f (* args , ** kwargs )
279+ ) -> asyncio .AbstractEventLoop :
280+ loop : asyncio .AbstractEventLoop = f (* args , ** kwargs )
279281 if init_stack :
280282 thread_id : int = typing .cast (int , ddtrace_threading .current_thread ().ident )
281283 stack .set_uvloop_mode (thread_id , True )
@@ -292,15 +294,15 @@ def _(
292294
293295 @partial (wrap , policy_class .set_event_loop ) # pyright: ignore[reportArgumentType]
294296 def _ (
295- f : typing .Callable [[object , typing .Optional [" asyncio.AbstractEventLoop" ]], None ],
297+ f : typing .Callable [[object , typing .Optional [asyncio .AbstractEventLoop ]], None ],
296298 args : typing .Any ,
297299 kwargs : typing .Any ,
298300 ) -> None :
299301 thread_id : int = typing .cast (int , ddtrace_threading .current_thread ().ident )
300302 if init_stack :
301303 stack .set_uvloop_mode (thread_id , True )
302304
303- loop : typing .Optional [" asyncio.AbstractEventLoop" ] = get_argument_value (args , kwargs , 1 , "loop" )
305+ loop : typing .Optional [asyncio .AbstractEventLoop ] = get_argument_value (args , kwargs , 1 , "loop" )
304306 if init_stack and loop is not None :
305307 stack .track_asyncio_loop (typing .cast (int , ddtrace_threading .current_thread ().ident ), loop )
306308 _call_init_asyncio (asyncio )
0 commit comments