o
    jN!                     @   s   d Z ddlmZmZmZmZ ddlmZ ddlm	Z	m
Z
mZmZ e Zdd Zdd	 Zd
d Zdd Zdd ZG dd de	ZG dd dZG dd deeZG dd de
ZG dd deeZdddddddZdS )a  The `with_signature` decorator: override the signature (and related
introspection attributes) of a wrapped callable without mutating the wrapped
function itself. Accepts a prototype callable, a prebuilt `inspect.Signature`,
or a factory that derives a signature from the wrapped function at decoration
time.

This replaces the need for the `adapter=` argument of `wrapt.decorator`,
which is planned for deprecation.
    )
CO_VARARGSCO_VARKEYWORDS	Parameter	Signature)	signature   )BaseObjectProxyBoundFunctionWrapperCallableObjectProxyFunctionWrapperc                    s&   d  d  fdd}t|S )aQ  Read-only property that memoizes its derivation in a `_self_*` slot.

    `functools.cached_property` is unusable on ObjectProxy subclasses: wrapt
    overrides `__dict__` with a property returning the wrapped object's dict,
    so cached_property's `instance.__dict__[name] = value` would mutate the
    wrapped function. Storing under a `_self_*` name routes through wrapt's
    own setattr handling and lands in the proxy's real instance dict. Using
    a `@property` (data descriptor) also ensures we win over any value
    copied into the instance dict by `FunctionWrapper.__init__`.
    _self_cached__c                    s.   t | t}|tu r|  }| | |S N)getattr_MISSING_derive__self_setattr__)selfvalueattrslot D/var/www/ai-app/venv/lib/python3.10/site-packages/wrapt/signature.pygetter%   s
   
z_cached.<locals>.getter)lstripproperty)r   r   r   r   r   _cached   s   r   c                 C   s.   dd | j  D }| jtjur| j|d< |S )Nc                 S   s"   i | ]}|j tjur|j|j qS r   )
annotationr   emptyname.0pr   r   r   
<dictcomp>0   s
    z'_derive_annotations.<locals>.<dictcomp>return)
parametersvaluesreturn_annotationr   r   )sigannr   r   r   _derive_annotations/   s   
r+   c                 C   s    t dd | j D }|pd S )Nc                 s   s4    | ]}|j tjtjfv r|jtjur|jV  qd S r   )kindr   POSITIONAL_ONLYPOSITIONAL_OR_KEYWORDdefaultr   r!   r   r   r   	<genexpr>;   s    z#_derive_defaults.<locals>.<genexpr>)tupler&   r'   )r)   defaultsr   r   r   _derive_defaults:   s   r3   c                 C   s   dd | j  D }|pd S )Nc                 S   s.   i | ]}|j tju r|jtjur|j|jqS r   )r,   r   KEYWORD_ONLYr/   r   r    r!   r   r   r   r$   E   s
    z&_derive_kwdefaults.<locals>.<dictcomp>)r&   r'   )r)   
kwdefaultsr   r   r   _derive_kwdefaultsD   s   r6   c                 C   s   g g g }}}d  }}| j  D ]<}|jtju r ||j q|jtju r-||j q|jtju r7|j}q|jtj	u rD||j q|jtj
u rM|j}q|| | }|r[|| |rb|| t|S r   )r&   r'   r,   r   r-   appendr    r.   VAR_POSITIONALr4   VAR_KEYWORDr1   )r)   pos_only	pos_or_kwkw_onlyvar_posvar_kwr#   namesr   r   r   _derive_varnamesM   s(   

r@   c                       sP   e Zd ZdZ fddZdd ZedZedZedZ	ed	Z
ed
Z  ZS )_SignatureCodezCode-object proxy deriving argument-related co_* attrs from a Signature.

    Non-argument bits (co_flags for coroutine/generator, co_filename, etc.)
    fall through to the wrapped function's real __code__.
    c                       t  | || _d S r   super__init___self_signaturer   wrappedr   	__class__r   r   rE   j      
z_SignatureCode.__init__c                    s   | j }|dkrtjtjf t fdd|j D S |dkr+tdd |j D S |dkr;tdd |j D S |dkrCt|S |d	krnd
d |j D  | jj	t
tB  @ }tj v rc|t
O }tj v rl|tO }|S t|)Nco_argcountc                 3   s    | ]}|j  v V  qd S r   r,   r!   kindsr   r   r0   r   s    z)_SignatureCode._derive.<locals>.<genexpr>co_posonlyargcountc                 s       | ]	}|j tju V  qd S r   )r,   r   r-   r!   r   r   r   r0   t       
co_kwonlyargcountc                 s   rQ   r   )r,   r   r4   r!   r   r   r   r0   x   rR   co_varnamesco_flagsc                 S   s   h | ]}|j qS r   rM   r!   r   r   r   	<setcomp>~   s    z)_SignatureCode._derive.<locals>.<setcomp>)rF   r   r-   r.   sumr&   r'   r@   __wrapped__rU   r   r   r8   r9   AttributeError)r   r   r)   flagsr   rN   r   r   n   s.   

z_SignatureCode._deriverL   rP   rS   rT   rU   )__name__
__module____qualname____doc__rE   r   r   rL   rP   rS   rT   rU   __classcell__r   r   rI   r   rA   c   s    rA   c                   @   sD   e Zd ZdZdd Zedd ZedZedZ	edZ
ed	Zd
S )_SignatureMixinzEShared Signature-derived introspection attrs for wrapper + surrogate.c                 C   sT   | j }|dkrt|S |dkrt|S |dkrt|S |dkr&t| jj|S t|)N__annotations____defaults____kwdefaults____code__)rF   r+   r3   r6   rA   rX   rd   rY   )r   r   r)   r   r   r   r      s   z_SignatureMixin._derivec                 C   s   | j S r   )rF   r   r   r   r   __signature__   s   z_SignatureMixin.__signature__ra   rb   rc   rd   N)r[   r\   r]   r^   r   r   rf   r   ra   rb   rc   rd   r   r   r   r   r`      s    
r`   c                       s    e Zd ZdZ fddZ  ZS )_SignatureFunctionSurrogatea  Function surrogate exposing Signature-derived introspection attrs.

    Used as __func__ of a bound wrapper so that inspect.signature -- which
    treats the bound wrapper as a MethodType and consults __func__ -- sees
    our override and strips self/cls correctly.
    c                    rB   r   rC   rG   rI   r   r   rE      rK   z$_SignatureFunctionSurrogate.__init__)r[   r\   r]   r^   rE   r_   r   r   rI   r   rg      s    rg   c                   @   s$   e Zd Zedd Zedd ZdS )_BoundSignatureFunctionWrapperc                 C   s   t | jj| jjS r   )rg   rX   __func___self_parentrF   re   r   r   r   ri      s   z'_BoundSignatureFunctionWrapper.__func__c                 C   s   | j jS r   )rj   rF   re   r   r   r   rf      s   z,_BoundSignatureFunctionWrapper.__signature__N)r[   r\   r]   r   ri   rf   r   r   r   r   rh      s
    
rh   c                       s,   e Zd ZeZ fddZedd Z  ZS )_SignatureFunctionWrapperc                    s   t  || || _d S r   rC   )r   rH   wrapperr   rI   r   r   rE      s   
z"_SignatureFunctionWrapper.__init__c                 C   s   t | j| jS r   )rg   rX   rF   re   r   r   r   ri      s   z"_SignatureFunctionWrapper.__func__)	r[   r\   r]   rh   __bound_function_wrapper__rE   r   ri   r_   r   r   rI   r   rk      s
    rk   N)	prototyper   factoryc                  s\   t dd  fD }|dkrtd|dkrtd fdd}| d	u r*|S || S )
a  Override the signature of a wrapped callable.

    Exactly one of `prototype`, `signature`, or `factory` must be supplied:

    - `prototype`: a callable whose signature will be used.
    - `signature`: a prebuilt `inspect.Signature` object.
    - `factory`: a callable `factory(wrapped)` invoked at decoration time
      that returns either a `Signature` or a prototype callable.

    The resulting wrapper exposes the override via `__signature__`, and
    derives `__annotations__`, `__defaults__`, `__kwdefaults__`, and the
    argument-related attributes of `__code__` from the same source. The
    wrapped function is not mutated. Calling behaviour is unchanged.
    c                 s   s    | ]}|d uV  qd S r   r   )r"   xr   r   r   r0      s    z!with_signature.<locals>.<genexpr>r   zBwith_signature requires one of prototype=, signature=, or factory=r   zFwith_signature accepts only one of prototype=, signature=, or factory=c                    sR   dd }d ur}nd urt }n | }t|tr|nt |}t| ||S )Nc                 S   s   | |i |S r   r   )rH   instanceargskwargsr   r   r   _wrapper   s   z4with_signature.<locals>._decorator.<locals>._wrapper)_inspect_signature
isinstancer   rk   )rH   rt   resolvedproducedro   rn   r   r   r   
_decorator   s   
z"with_signature.<locals>._decoratorN)rW   	TypeError)rH   rn   r   ro   	specifiedrz   r   ry   r   with_signature   s   r}   r   )r^   inspectr   r   r   r   r   ru   	__wrapt__r   r	   r
   r   objectr   r   r+   r3   r6   r@   rA   r`   rg   rh   rk   r}   r   r   r   r   <module>   s     

	+