@@ -182,6 +182,8 @@ def set_fates(clotho, lachesis, atropos, father="Zues", mother="Themis"):
182182 Type ,
183183 TypeVar ,
184184 Union ,
185+ cast ,
186+ overload ,
185187)
186188
187189import attr
@@ -328,6 +330,7 @@ class _Sentinel(enum.Enum):
328330
329331P = ParamSpec ("P" )
330332R = TypeVar ("R" )
333+ T = TypeVar ("T" )
331334
332335
333336def only_if_tracing (func : Callable [P , R ]) -> Callable [P , Optional [R ]]:
@@ -343,22 +346,43 @@ def _only_if_tracing_inner(*args: P.args, **kwargs: P.kwargs) -> Optional[R]:
343346 return _only_if_tracing_inner
344347
345348
346- def ensure_active_span (message : str , ret = None ):
349+ @overload
350+ def ensure_active_span (
351+ message : str ,
352+ ) -> Callable [[Callable [P , R ]], Callable [P , Optional [R ]]]:
353+ ...
354+
355+
356+ @overload
357+ def ensure_active_span (
358+ message : str , ret : T
359+ ) -> Callable [[Callable [P , R ]], Callable [P , Union [T , R ]]]:
360+ ...
361+
362+
363+ def ensure_active_span (
364+ message : str , ret : Optional [T ] = None
365+ ) -> Callable [[Callable [P , R ]], Callable [P , Union [Optional [T ], R ]]]:
347366 """Executes the operation only if opentracing is enabled and there is an active span.
348367 If there is no active span it logs message at the error level.
349368
350369 Args:
351370 message: Message which fills in "There was no active span when trying to %s"
352371 in the error log if there is no active span and opentracing is enabled.
353- ret (object) : return value if opentracing is None or there is no active span.
372+ ret: return value if opentracing is None or there is no active span.
354373
355- Returns (object): The result of the func or ret if opentracing is disabled or there
374+ Returns:
375+ The result of the func, falling back to ret if opentracing is disabled or there
356376 was no active span.
357377 """
358378
359- def ensure_active_span_inner_1 (func ):
379+ def ensure_active_span_inner_1 (
380+ func : Callable [P , R ]
381+ ) -> Callable [P , Union [Optional [T ], R ]]:
360382 @wraps (func )
361- def ensure_active_span_inner_2 (* args , ** kwargs ):
383+ def ensure_active_span_inner_2 (
384+ * args : P .args , ** kwargs : P .kwargs
385+ ) -> Union [Optional [T ], R ]:
362386 if not opentracing :
363387 return ret
364388
@@ -464,7 +488,7 @@ def start_active_span(
464488 finish_on_close : bool = True ,
465489 * ,
466490 tracer : Optional ["opentracing.Tracer" ] = None ,
467- ):
491+ ) -> "opentracing.Scope" :
468492 """Starts an active opentracing span.
469493
470494 Records the start time for the span, and sets it as the "active span" in the
@@ -502,7 +526,7 @@ def start_active_span_follows_from(
502526 * ,
503527 inherit_force_tracing : bool = False ,
504528 tracer : Optional ["opentracing.Tracer" ] = None ,
505- ):
529+ ) -> "opentracing.Scope" :
506530 """Starts an active opentracing span, with additional references to previous spans
507531
508532 Args:
@@ -717,7 +741,9 @@ def inject_response_headers(response_headers: Headers) -> None:
717741 response_headers .addRawHeader ("Synapse-Trace-Id" , f"{ trace_id :x} " )
718742
719743
720- @ensure_active_span ("get the active span context as a dict" , ret = {})
744+ @ensure_active_span (
745+ "get the active span context as a dict" , ret = cast (Dict [str , str ], {})
746+ )
721747def get_active_span_text_map (destination : Optional [str ] = None ) -> Dict [str , str ]:
722748 """
723749 Gets a span context as a dict. This can be used instead of manually
@@ -886,7 +912,7 @@ def _tag_args_inner(*args: P.args, **kwargs: P.kwargs) -> R:
886912 for i , arg in enumerate (argspec .args [1 :]):
887913 set_tag ("ARG_" + arg , args [i ]) # type: ignore[index]
888914 set_tag ("args" , args [len (argspec .args ) :]) # type: ignore[index]
889- set_tag ("kwargs" , kwargs )
915+ set_tag ("kwargs" , str ( kwargs ) )
890916 return func (* args , ** kwargs )
891917
892918 return _tag_args_inner
0 commit comments