2323 from collections .abc import Callable , Coroutine
2424 from contextvars import Token
2525 from types import TracebackType
26-
2726 from typing_extensions import AsyncContextManager
2827
2928 from duron ._core .options import RunOptions
@@ -60,15 +59,16 @@ def __exit__(
6059 exc_type : type [BaseException ] | None ,
6160 exc_val : BaseException | None ,
6261 exc_tb : TracebackType | None ,
63- ):
62+ ) -> None :
6463 if self ._token :
6564 _context .reset (self ._token )
6665
6766 @staticmethod
6867 def current () -> Context :
6968 ctx = _context .get ()
7069 if ctx is None :
71- raise RuntimeError ("No duron context is active" )
70+ msg = "No duron context is active"
71+ raise RuntimeError (msg )
7272 return ctx
7373
7474 @overload
@@ -100,7 +100,8 @@ async def run(
100100 ** kwargs : _P .kwargs ,
101101 ) -> _T :
102102 if asyncio .get_running_loop () is not self ._loop :
103- raise RuntimeError ("Context time can only be used in the context loop" )
103+ msg = "Context time can only be used in the context loop"
104+ raise RuntimeError (msg )
104105
105106 if isinstance (fn , CheckpointOp ):
106107 async with self .run_stream (fn , options , * args , ** kwargs ) as stream :
@@ -111,7 +112,7 @@ async def run(
111112 fn .return_type
112113 if isinstance (fn , Op ) and fn .return_type
113114 else self ._task .codec .inspect_function (
114- cast ("Callable[..., object]" , fn )
115+ cast ("Callable[..., object]" , fn ),
115116 ).return_type
116117 )
117118
@@ -138,8 +139,9 @@ def run_stream(
138139 ) -> AsyncContextManager [Stream [_S , _T ]]:
139140 _ = options
140141 if asyncio .get_running_loop () is not self ._loop :
141- raise RuntimeError ("Context time can only be used in the context loop" )
142- r = run_stream (
142+ msg = "Context time can only be used in the context loop"
143+ raise RuntimeError (msg )
144+ return run_stream (
143145 self ._loop ,
144146 fn .action_type ,
145147 fn .initial (),
@@ -148,7 +150,6 @@ def run_stream(
148150 * args ,
149151 ** kwargs ,
150152 )
151- return r
152153
153154 async def create_stream (
154155 self ,
@@ -158,9 +159,13 @@ async def create_stream(
158159 metadata : dict [str , JSONValue ] | None = None ,
159160 ) -> tuple [Stream [_T , None ], StreamWriter [_T ]]:
160161 if asyncio .get_running_loop () is not self ._loop :
161- raise RuntimeError ("Context time can only be used in the context loop" )
162+ msg = "Context time can only be used in the context loop"
163+ raise RuntimeError (msg )
162164 return await create_stream (
163- self ._loop , dtype , external = external , metadata = metadata
165+ self ._loop ,
166+ dtype ,
167+ external = external ,
168+ metadata = metadata ,
164169 )
165170
166171 async def create_signal (
@@ -170,7 +175,8 @@ async def create_signal(
170175 metadata : dict [str , JSONValue ] | None = None ,
171176 ) -> tuple [Signal [_T ], SignalWriter [_T ]]:
172177 if asyncio .get_running_loop () is not self ._loop :
173- raise RuntimeError ("Context time can only be used in the context loop" )
178+ msg = "Context time can only be used in the context loop"
179+ raise RuntimeError (msg )
174180 return await create_signal (self ._loop , dtype , metadata = metadata )
175181
176182 async def create_promise (
@@ -180,28 +186,34 @@ async def create_promise(
180186 metadata : dict [str , JSONValue ] | None = None ,
181187 ) -> tuple [str , asyncio .Future [_T ]]:
182188 if asyncio .get_running_loop () is not self ._loop :
183- raise RuntimeError ("Context time can only be used in the context loop" )
189+ msg = "Context time can only be used in the context loop"
190+ raise RuntimeError (msg )
184191 fut = create_op (
185- self ._loop , ExternalPromiseCreate (metadata = metadata , return_type = dtype )
192+ self ._loop ,
193+ ExternalPromiseCreate (metadata = metadata , return_type = dtype ),
186194 )
187195 return (base64 .b64encode (fut .id ).decode (), cast ("asyncio.Future[_T]" , fut ))
188196
189197 async def barrier (self ) -> int :
190198 if asyncio .get_running_loop () is not self ._loop :
191- raise RuntimeError ("Context time can only be used in the context loop" )
199+ msg = "Context time can only be used in the context loop"
200+ raise RuntimeError (msg )
192201 return await create_op (self ._loop , Barrier ())
193202
194203 def time (self ) -> float :
195204 if asyncio .get_running_loop () is not self ._loop :
196- raise RuntimeError ("Context time can only be used in the context loop" )
205+ msg = "Context time can only be used in the context loop"
206+ raise RuntimeError (msg )
197207 return self ._loop .time ()
198208
199209 def time_ns (self ) -> int :
200210 if asyncio .get_running_loop () is not self ._loop :
201- raise RuntimeError ("Context time can only be used in the context loop" )
211+ msg = "Context time can only be used in the context loop"
212+ raise RuntimeError (msg )
202213 return self ._loop .time_us () * 1_000
203214
204215 def random (self ) -> Random :
205216 if asyncio .get_running_loop () is not self ._loop :
206- raise RuntimeError ("Context random can only be used in the context loop" )
217+ msg = "Context random can only be used in the context loop"
218+ raise RuntimeError (msg )
207219 return Random (self ._loop .generate_op_id ())
0 commit comments