@@ -809,23 +809,24 @@ def run_in_background( # type: ignore[misc]
809809
810810 # `res` may be a coroutine, `Deferred`, some other kind of awaitable, or a plain
811811 # value. Convert it to a `Deferred`.
812+ d : "defer.Deferred[R]"
812813 if isinstance (res , typing .Coroutine ):
813814 # Wrap the coroutine in a `Deferred`.
814- res = defer .ensureDeferred (res )
815+ d = defer .ensureDeferred (res )
815816 elif isinstance (res , defer .Deferred ):
816- pass
817+ d = res
817818 elif isinstance (res , Awaitable ):
818819 # `res` is probably some kind of completed awaitable, such as a `DoneAwaitable`
819820 # or `Future` from `make_awaitable`.
820- res = defer .ensureDeferred (_unwrap_awaitable (res ))
821+ d = defer .ensureDeferred (_unwrap_awaitable (res ))
821822 else :
822823 # `res` is a plain value. Wrap it in a `Deferred`.
823- res = defer .succeed (res )
824+ d = defer .succeed (res )
824825
825- if res .called and not res .paused :
826+ if d .called and not d .paused :
826827 # The function should have maintained the logcontext, so we can
827828 # optimise out the messing about
828- return res
829+ return d
829830
830831 # The function may have reset the context before returning, so
831832 # we need to restore it now.
@@ -843,8 +844,8 @@ def run_in_background( # type: ignore[misc]
843844 # which is supposed to have a single entry and exit point. But
844845 # by spawning off another deferred, we are effectively
845846 # adding a new exit point.)
846- res .addBoth (_set_context_cb , ctx )
847- return res
847+ d .addBoth (_set_context_cb , ctx )
848+ return d
848849
849850
850851T = TypeVar ("T" )
@@ -877,7 +878,7 @@ def make_deferred_yieldable(deferred: "defer.Deferred[T]") -> "defer.Deferred[T]
877878ResultT = TypeVar ("ResultT" )
878879
879880
880- def _set_context_cb (result : ResultT , context : LoggingContext ) -> ResultT :
881+ def _set_context_cb (result : ResultT , context : LoggingContextOrSentinel ) -> ResultT :
881882 """A callback function which just sets the logging context"""
882883 set_current_context (context )
883884 return result
0 commit comments