o
    i                     @   s   d dl Zd dlZd dlZd dlZd dlZd dlmZ d dlZddl	m
Z
mZ ddlmZ defddZd	ejd
efddZd	ejd
efddZedZedZedZdejeejeef f d
ejeeje f fddZdS )    N)asynccontextmanager   )UserCodeException suppress_synchronicity_tb_frames)	Interface	interfacec                    s,   t  r| tjkr fdd}|S t S )a  Like functools.wraps but maintains `inspect.iscoroutinefunction` and allows custom type annotations overrides

    Use this when the wrapper function is non-async but returns the coroutine resulting
    from calling the underlying wrapped `func`. This will make sure that the wrapper
    is still an async function in that case, and can be inspected as such.

    Note: Does not forward async generator information other than explicit annotations
    c                    s   t  fdd}|S )Nc                     sj   t  ' z | i |I d H W W  d    S  ty* } zd|j_|jd }~ww 1 s.w   Y  d S )NT)r   r   exc__suppress_context__)argskwargsuc_excuser_wrapper L/home/ubuntu/.local/lib/python3.10/site-packages/synchronicity/async_wrap.pywrapper   s   z;wraps_by_interface.<locals>.asyncfunc_deco.<locals>.wrapper)	functoolswraps)r   r   funcr   r   asyncfunc_deco   s   z*wraps_by_interface.<locals>.asyncfunc_deco)$is_coroutine_function_follow_wrappedr   _ASYNC_WITH_BLOCKING_TYPESr   r   )r   r   r   r   r   r   wraps_by_interface   s   	
r   r   returnc                 C   <   ddl m} t| drt| |dtjkrt| jS t	| S )zgDetermine if func returns a coroutine, unwrapping decorators, but not the async synchronicity interace.r   TARGET_INTERFACE_ATTR__wrapped__N)
synchronizerr   hasattrgetattrr   BLOCKINGr   r   inspectiscoroutinefunctionr   r   r   r   r   r   *      

r   c                 C   r   )znDetermine if func returns an async generator, unwrapping decorators, but not the async synchronicity interace.r   r   r   N)
r   r   r    r!   r   r"   $is_async_gen_function_follow_wrappedr   r#   isasyncgenfunctionr%   r   r   r   r'   3   r&   r'   
YIELD_TYPE	SEND_TYPEPfc                 C   sp   t | }|jdd}|dur4|jtjjtjjtjjfv r*t	j
|jd  |jd< |S |jtjkr2|S |S td)z{Wrapper around contextlib.asynccontextmanager that sets correct type annotations

    The standard library one doesn't
    r   Nr   zpTo use the fixed @asynccontextmanager, make sure to properly annotate your wrapped function as an AsyncGenerator)_asynccontextmanager__annotations__pop
__origin__collectionsabcAsyncGeneratorAsyncIteratorAsyncIterabletypingAsyncContextManager__args__
contextlibAbstractAsyncContextManager
ValueError)r,   acm_factoryold_retr   r   r   r   C   s    
r   )collections.abcr1   r9   r   r#   r6   r   r-   typing_extensions
exceptionsr   r   r   r   r   Callableboolr   r'   TypeVarr)   r*   	ParamSpecr+   r3   r7   r   r   r   r   <module>   s(    	
	

