a
    �˜ëj[$  ã                   @   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 ddl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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ÚFunctionWrapper)Ú_BOUND_DOC_PROPERTYÚ_DOC_PROPERTYÚ_NO_DOC_OVERRIDEc                    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© úN/mnt/bigstor/www/usecases/.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>1   s   þz'_derive_annotations.<locals>.<dictcomp>Úreturn)Ú
parametersÚvaluesÚreturn_annotationr   r   )ÚsigÚannr   r   r   Ú_derive_annotations0   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    þ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$   F   s   þz&_derive_kwdefaults.<locals>.<dictcomp>)r&   r'   )r)   Ú
kwdefaultsr   r   r   Ú_derive_kwdefaultsE   s    þr6   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_POSITIONALr4   ÚVAR_KEYWORDr1   )r)   Zpos_onlyZ	pos_or_kwZkw_onlyZvar_posZvar_kwr#   Únamesr   r   r   Ú_derive_varnamesN   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 )Ú_SignatureCodezÔCode-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?   k   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r†t|ƒS |d	krÜd
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   r0   s   ó    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   r0   u   s   Úco_kwonlyargcountc                 s   s   | ]}|j tju V  qd S r   )r,   r   r4   r!   r   r   r   r0   y   s   Úco_varnamesÚco_flagsc                 S   s   h | ]
}|j ’qS r   rF   r!   r   r   r   Ú	<setcomp>   rI   z)_SignatureCode._derive.<locals>.<setcomp>)r@   r   r-   r.   Úsumr&   r'   r;   Ú__wrapped__rM   r   r   r8   r9   ÚAttributeError)r   r   r)   Úflagsr   rG   r   r   o   s.    ÿÿ

z_SignatureCode._deriverE   rJ   rK   rL   rM   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r?   r   r   rE   rJ   rK   rL   rM   Ú__classcell__r   r   rC   r   r<   d   s   r<   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+   r3   r6   r<   rP   r\   rQ   )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__rY   rZ   r[   r\   N)rS   rT   rU   rV   r   r   r^   r   rY   rZ   r[   r\   r   r   r   r   rX   �   s   
rX   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=   rA   rC   r   r   r?   °   s    z$_SignatureFunctionSurrogate.__init__)rS   rT   rU   rV   r?   rW   r   r   rC   r   r_   ¨   s   r_   c                   @   s(   e Zd ZeZedd„ ƒZedd„ ƒZdS )Ú_BoundSignatureFunctionWrapperc                 C   s   t | jj| jjƒS r   )r_   rP   Ú__func__Ú_self_parentr@   r]   r   r   r   ra   ¸   s    ÿz'_BoundSignatureFunctionWrapper.__func__c                 C   s   | j jS r   )rb   r@   r]   r   r   r   r^   ¾   s    z,_BoundSignatureFunctionWrapper.__signature__N)rS   rT   rU   r   rV   r   ra   r^   r   r   r   r   r`   µ   s
   
r`   c                       s4   e Zd ZeZeZef‡ fdd„	Ze	dd„ ƒZ
‡  ZS )Ú_SignatureFunctionWrapperc                    s   t ƒ  ||¡ || _|| _d S r   )r>   r?   r@   Z	_self_doc)r   rB   Úwrapperr   ÚdocrC   r   r   r?   È   s    z"_SignatureFunctionWrapper.__init__c                 C   s   t | j| jƒS r   )r_   rP   r@   r]   r   r   r   ra   Í   s    z"_SignatureFunctionWrapper.__func__)rS   rT   rU   r   rV   r`   Z__bound_function_wrapper__r   r?   r   ra   rW   r   r   rC   r   rc   Ã   s
   rc   N)Ú	prototyper   Úfactoryre   c                  s^   t dd„ ˆˆˆfD ƒƒ}|dkr(tdƒ‚|dkr8tdƒ‚‡ ‡‡‡fdd„}| d	u rV|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, or a
      tuple of `(signature_or_prototype, docstring)` to override the
      docstring at the same time.

    The docstring may also be overridden by supplying `doc`, in which case
    a docstring from the factory is ignored. When neither is supplied the
    wrapper reports the docstring of the wrapped function as normal.

    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   ê   rI   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                    s‚   dd„ }ˆ d u rt nˆ }ˆd ur&ˆ}nNˆd ur8tˆƒ}n<ˆ| ƒ}t|tƒr^|\}}ˆ d u r^|}t|tƒrl|nt|ƒ}t| |||ƒS )Nc                 S   s   | |i |¤ŽS r   r   )rB   ÚinstanceÚargsÚkwargsr   r   r   Ú_wrapperõ   s    z4with_signature.<locals>._decorator.<locals>._wrapper)r   Ú_inspect_signatureÚ
isinstancer1   r   rc   )rB   rl   Zresolved_docZresolvedZproducedZproduced_doc©re   rg   rf   r   r   r   Ú
_decoratorô   s     

ÿýz"with_signature.<locals>._decoratorN)rO   Ú	TypeError)rB   rf   r   rg   re   Z	specifiedrp   r   ro   r   Úwith_signatureÒ   s    ÿÿrr   )N)rV   Úinspectr   r   r   r   r   rm   Z	__wrapt__r   r	   r
   r   re   r   r   r   Úobjectr   r   r+   r3   r6   r;   r<   rX   r_   r`   rc   rr   r   r   r   r   Ú<module>   s&   

	+ ÿÿ