defpatch_threading()->None:"""Patch threading.Thread to automatically wrap the target with context."""ifnothasattr(threading.Thread,APPL_PATCHED_NAME):# print("patching threading.Thread")classThreadWithContext(threading.Thread):def__init__(self,*args,**kwargs):if"target"inkwargsandkwargs["target"]isnotNone:kwargs["target"]=_get_new_target(kwargs["target"])eliflen(args)>0andargs[0]isnotNone:args=(_get_new_target(args[0]),)+args[1:]super().__init__(*args,**kwargs)setattr(ThreadWithContext,APPL_PATCHED_NAME,True)threading.Thread=ThreadWithContext# type: ignore