
    ci                         d dl Z d dlZd dlZd dlmZmZmZmZmZ  ed      Z	 ed      Z
 ed      Z ed      Zerd dlmZ  ed      Z G d	 d
ej                   e
   ee
         Z	 	 	 	 ddZy)    N)TYPE_CHECKINGCallableGenericIteratorTypeVarVRPargsPkwargs)	ParamSpecPc                   8     e Zd ZdZdefdZdedef fdZ xZS )_AgnosticContextManagera  Context manager that can decorate both async and sync functions.

    This is an overridden version of the contextlib._GeneratorContextManager
    class that will decorate async functions with an async context manager
    to end the span AFTER the entire async function coroutine finishes.

    Else it will report near zero spans durations for async functions.

    We are overriding the contextlib._GeneratorContextManager class as
    reimplementing it is a lot of code to maintain and this class (even if it's
    marked as protected) doesn't seems like to be evolving a lot.

    For more information, see:
    https://github.com/open-telemetry/opentelemetry-python/pull/3633
    returnc                 l    | ` | `| `	 t        | j                        S # t
        $ r t        d      dw xY w)zReimplementing __enter__ to avoid the type error.

        The original __enter__ method returns Any type, but we want to return R.
        zgenerator didn't yieldN)argskwdsfuncnextgenStopIterationRuntimeError)selfs    D/tmp/pip-target-z3e9_cxr/lib/python/opentelemetry/util/_decorator.py	__enter__z!_AgnosticContextManager.__enter__6   sD    
 Ity$)	C>! 	C78dB	Cs    3r   c                      t        j                        r3t        j                        dt        dt
        dt        f fd       }|S t         !        S )Nr   kwargsr   c                  |   K   j                         5   | i | d {   cd d d        S 7 # 1 sw Y   y xY wwN)_recreate_cm)r   r   r   r   s     r   async_wrapperz7_AgnosticContextManager.__call__.<locals>.async_wrapperD   s@     &&( 7!%t!6v!667 767 7s    <0.0
<09<)	asyncioiscoroutinefunction	functoolswrapsr
   r   r	   super__call__)r   r   r!   	__class__s   `` r   r'   z _AgnosticContextManager.__call__A   sY    &&t,__T"75 7G 7 7 #7 ! w%%    )	__name__
__module____qualname____doc__r	   r   r   r'   __classcell__)r(   s   @r   r   r   "   s.     	C1 	C	&Q 	&1 	& 	&r)   r   c                 v     t        j                         dt        dt        dt        t
           f fd       }|S )Nr   r   r   c                      t        | |      S r   )r   )r   r   r   s     r   helperz'_agnosticcontextmanager.<locals>.helperP   s    &tT6::r)   )r$   r%   r
   r   r   r	   )r   r1   s   ` r   _agnosticcontextmanagerr2   M   sB     __T;e ;w ;3J13M ; ; Mr)   )r   zCallable[P, Iterator[R]]r   z'Callable[P, _AgnosticContextManager[R]])r"   
contextlibr$   typingr   r   r   r   r   r   r	   r
   r   typing_extensionsr   r   _GeneratorContextManagerr   r2    r)   r   <module>r8      s       F FCLCL
)

 +#A(&''*AJ(&V
$.r)   