a
    `*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__`.
    Z_self_cached__c                    s.   t | t}|tu r*|  }| | |S N)getattr_MISSING_deriveZ__self_setattr__)selfvalueattrZslot K/var/www/html/assistant/venv/lib/python3.9/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   s2   | ]*}|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    r0   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    r3   c                 C   s   g g g   }}}d  }}| j  D ]x}|jtju r@||j q"|jtju rZ||j q"|jtju rn|j}q"|jtj	u r||j q"|jtj
u r"|j}q"|| | }|r|| |r|| t|S r   )r#   r$   r)   r   r*   appendr   r+   VAR_POSITIONALr1   VAR_KEYWORDr.   )r&   Zpos_onlyZ	pos_or_kwZkw_onlyZvar_posZvar_kwr    namesr   r   r   _derive_varnamesM   s&    

r8   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                    s   t  | || _d S r   super__init___self_signaturer   wrappedr   	__class__r   r   r<   j   s    z_SignatureCode.__init__c                    s   | j }|dkr6tjtjf t fdd|j D S |dkrVtdd |j D S |dkrvtdd |j D S |dkrt|S |d	krd
d |j D  | jj	t
tB  @ }tj v r|t
O }tj v r|tO }|S t|d S )Nco_argcountc                 3   s   | ]}|j  v V  qd S r   r)   r   kindsr   r   r-   r       z)_SignatureCode._derive.<locals>.<genexpr>co_posonlyargcountc                 s   s   | ]}|j tju V  qd S r   )r)   r   r*   r   r   r   r   r-   t   s   co_kwonlyargcountc                 s   s   | ]}|j tju V  qd S r   )r)   r   r1   r   r   r   r   r-   x   s   co_varnamesco_flagsc                 S   s   h | ]
}|j qS r   rC   r   r   r   r   	<setcomp>~   rF   z)_SignatureCode._derive.<locals>.<setcomp>)r=   r   r*   r+   sumr#   r$   r8   __wrapped__rJ   r   r   r5   r6   AttributeError)r   r   r&   flagsr   rD   r   r   n   s.    

z_SignatureCode._deriverB   rG   rH   rI   rJ   )__name__
__module____qualname____doc__r<   r   r   rB   rG   rH   rI   rJ   __classcell__r   r   r@   r   r9   c   s   r9   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   sX   | j }|dkrt|S |dkr&t|S |dkr6t|S |dkrLt| jj|S t|d S )N__annotations____defaults____kwdefaults____code__)r=   r(   r0   r3   r9   rM   rY   rN   )r   r   r&   r   r   r   r      s    z_SignatureMixin._derivec                 C   s   | j S r   )r=   r   r   r   r   __signature__   s    z_SignatureMixin.__signature__rV   rW   rX   rY   N)rP   rQ   rR   rS   r   r   r[   r   rV   rW   rX   rY   r   r   r   r   rU      s   
rU   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                    s   t  | || _d S r   r:   r>   r@   r   r   r<      s    z$_SignatureFunctionSurrogate.__init__)rP   rQ   rR   rS   r<   rT   r   r   r@   r   r\      s   r\   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   )r\   rM   __func___self_parentr=   rZ   r   r   r   r^      s    z'_BoundSignatureFunctionWrapper.__func__c                 C   s   | j jS r   )r_   r=   rZ   r   r   r   r[      s    z,_BoundSignatureFunctionWrapper.__signature__N)rP   rQ   rR   r   r^   r[   r   r   r   r   r]      s   
r]   c                       s,   e Zd ZeZ fddZedd Z  ZS )_SignatureFunctionWrapperc                    s   t  || || _d S r   r:   )r   r?   wrapperr   r@   r   r   r<      s    z"_SignatureFunctionWrapper.__init__c                 C   s   t | j| jS r   )r\   rM   r=   rZ   r   r   r   r^      s    z"_SignatureFunctionWrapper.__func__)	rP   rQ   rR   r]   Z__bound_function_wrapper__r<   r   r^   rT   r   r   r@   r   r`      s   r`   N)	prototyper   factoryc                  s\   t dd  fD }|dkr(td|dkr8td fdd}| d	u rT|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   r-      rF   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}n0d ur(t }n | }t|tr>|nt |}t| ||S )Nc                 S   s   | |i |S r   r   )r?   instanceargskwargsr   r   r   _wrapper   s    z4with_signature.<locals>._decorator.<locals>._wrapper)_inspect_signature
isinstancer   r`   )r?   rh   ZresolvedZproducedrc   rb   r   r   r   
_decorator   s    
z"with_signature.<locals>._decoratorN)rL   	TypeError)r?   rb   r   rc   Z	specifiedrl   r   rk   r   with_signature   s    rn   )N)rS   inspectr   r   r   r   r   ri   Z	__wrapt__r   r	   r
   r   objectr   r   r(   r0   r3   r8   r9   rU   r\   r]   r`   rn   r   r   r   r   <module>   s   

	+