o
    ȷet                     @   s*  d Z ddlmZmZmZ ddlZddlZddlZddlZddl	m
Z
 dZg dZeejZeejZeeejfZd#ddZd	d
 Zdd ZG dd deZG dd deZG dd deZedddZedddZedddZedddZedddZ G dd deZ!G dd  d eZ"G d!d" d"eZ#dS )$zFunction signature objects for callables

Back port of Python 3.3's function signature tools from the inspect module,
modified to be compatible with Python 2.6, 2.7 and 3.2+.
    )absolute_importdivisionprint_functionN)OrderedDictz0.3)BoundArguments	Parameter	Signature	signaturec                 C   s8   t | tr| jdd|fv r| jS | jd | j S t| S )Nbuiltins__builtin__.)
isinstancetype
__module____name__repr)
annotationbase_module r   K/var/www/ideatree/venv/lib/python3.10/site-packages/backcall/_signatures.pyformatannotation,   s
   
r   c                 G   sZ   z| t u rW d S t| |}|D ]}t|||}qW n
 ty#   Y d S w t|ts+|S d S N)r   getattrAttributeErrorr   _NonUserDefinedCallables)clsmethod_namenestedmethnamer   r   r   _get_user_defined_method4   s   

r    c              
   C   s\  t | std| t| tjr%t| j}|jt	|j
 dd dS z| j}W n	 ty3   Y nw |dur:|S z| j}W t|S  tyK   Y nw t| tjrWt| S t| tjrt| j}t|j
 }| jpmd}| jpri }z
|j|i |}W n ty } z	d| }t|d}~ww |j D ]%\}	}
||	 }|	|v r|j|
dd||	< q|jttfvr|js| |	 q|j| dS d}t| t!rt"t!| d	}|durt|}n3t"| d
}|durt|}n%t"| d}|durt|}nt| t#st"t!| d	d}|durt|}|dur|S t| tj$r'd| }t|td| )z/Get a signature object for the passed callable.z{0!r} is not a callable object   N)
parametersr   z,partial object {0!r} has incorrect argumentsT)default_partial_kwarg__call____new____init__im_funcz-no signature found for builtin function {0!r}z,callable {0!r} is not supported by signature)%callable	TypeErrorformatr   types
MethodTyper	   __func__replacetupler"   values__signature__r   __wrapped__FunctionTyper   from_function	functoolspartialfuncr   itemsargskeywordsbind_partial
ValueError	argumentskind_VAR_KEYWORD_VAR_POSITIONALr$   popr   r    r   BuiltinFunctionType)objsigwrapped
new_paramspartial_argspartial_keywordsbaexmsgarg_name	arg_valueparamcallnewinitr   r   r   r	   D   s   















r	   c                   @   s   e Zd ZdZdS )_voidz0A private marker - used in Parameter & SignatureN)r   r   __qualname____doc__r   r   r   r   rS      s    rS   c                   @   s   e Zd ZdS )_emptyN)r   r   rT   r   r   r   r   rV      s    rV   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )_ParameterKindc                 O   s    t j| g|R  }|d |_|S )Nr   )intr&   _name)selfr:   kwargsrD   r   r   r   r&      s   
z_ParameterKind.__new__c                 C      | j S r   rY   rZ   r   r   r   __str__   s   z_ParameterKind.__str__c                 C   s   d | jS )Nz<_ParameterKind: {0!r}>)r+   rY   r^   r   r   r   __repr__      z_ParameterKind.__repr__N)r   r   rT   r&   r_   r`   r   r   r   r   rW      s    rW   POSITIONAL_ONLYr   r!   POSITIONAL_OR_KEYWORD   VAR_POSITIONAL   KEYWORD_ONLY   VAR_KEYWORDc                   @   s   e Zd ZdZdZeZeZe	Z
eZeZeZeedfddZedd Zedd	 Zed
d Zedd ZeeeeefddZdd Zdd Zdd Zdd Zdd ZdS )r   a  Represents a parameter in a function signature.

    Has the following public attributes:

    * name : str
        The name of the parameter as a string.
    * default : object
        The default value for the parameter if specified.  If the
        parameter has no default value, this attribute is not set.
    * annotation
        The annotation for the parameter if specified.  If the
        parameter has no annotation, this attribute is not set.
    * kind : str
        Describes how argument values are bound to the parameter.
        Possible values: `Parameter.POSITIONAL_ONLY`,
        `Parameter.POSITIONAL_OR_KEYWORD`, `Parameter.VAR_POSITIONAL`,
        `Parameter.KEYWORD_ONLY`, `Parameter.VAR_KEYWORD`.
    )rY   _kind_default_annotationr$   Fc                 C   s   |t ttttfvrtd|| _|tur#|ttfv r#d|}t||| _	|| _
|d u r9|t kr5td|| _nt|}|t krRtd|tjsRd|}t||| _|| _d S )Nz,invalid value for 'Parameter.kind' attributez){0} parameters cannot have default valuesz<None is not a valid name for a non-positional-only parameterz
[a-z_]\w*$z#{0!r} is not a valid parameter name)_POSITIONAL_ONLY_POSITIONAL_OR_KEYWORDrA   _KEYWORD_ONLYr@   r=   rk   rV   r+   rl   rm   rY   strrematchIr$   )rZ   r   r?   r#   r   r$   rL   r   r   r   r'      s*   


zParameter.__init__c                 C   r\   r   r]   r^   r   r   r   r        zParameter.namec                 C   r\   r   )rl   r^   r   r   r   r#     ru   zParameter.defaultc                 C   r\   r   )rm   r^   r   r   r   r     ru   zParameter.annotationc                 C   r\   r   )rk   r^   r   r   r   r?     ru   zParameter.kindc                 C   s\   |t u r| j}|t u r| j}|t u r| j}|t u r| j}|t u r#| j}t| |||||dS )z+Creates a customized copy of the Parameter.)r#   r   r$   )rS   rY   rk   rm   rl   r$   r   )rZ   r   r?   r   r#   r$   r   r   r   r/     s   zParameter.replacec                 C   s   | j }| j}|tkr|d u rd}d|}| jtur#d|t| j}| jtur1d|t| j}|t	kr;d| }|S |t
krCd| }|S )N z<{0}>z{0}:{1}z{0}={1}*z**)r?   rY   rn   r+   rm   rV   r   rl   r   rA   r@   )rZ   r?   	formattedr   r   r   r_   1  s$   


zParameter.__str__c                 C   s   d | jjt| | jS )Nz<{0} at {1:#x} {2!r}>)r+   	__class__r   idr   r^   r   r   r   r`   I  s   

zParameter.__repr__c                 C      d | jj}t|Nzunhashable type: '{0}'r+   ry   r   r*   rZ   rL   r   r   r   __hash__M     zParameter.__hash__c                 C   s<   t |jto| j|jko| j|jko| j|jko| j|jkS r   )
issubclassry   r   rY   rk   rl   rm   rZ   otherr   r   r   __eq__Q  s   



zParameter.__eq__c                 C      |  | S r   r   r   r   r   r   __ne__X  ra   zParameter.__ne__N)r   r   rT   rU   	__slots__rn   rb   ro   rd   rA   rf   rp   rh   r@   rj   rV   emptyr'   propertyr   r#   r   r?   rS   r/   r_   r`   r   r   r   r   r   r   r   r      s8    





r   c                   @   sT   e Zd ZdZdd Zedd Zedd Zedd	 Zd
d Z	dd Z
dd ZdS )r   a  Result of `Signature.bind` call.  Holds the mapping of arguments
    to the function's parameters.

    Has the following public attributes:

    * arguments : OrderedDict
        An ordered mutable mapping of parameters' names to arguments' values.
        Does not contain arguments' default values.
    * signature : Signature
        The Signature object that created this instance.
    * args : tuple
        Tuple of positional arguments values.
    * kwargs : dict
        Dict of keyword arguments values.
    c                 C   s   || _ || _d S r   )r>   
_signature)rZ   r	   r>   r   r   r   r'   m  s   
zBoundArguments.__init__c                 C   r\   r   )r   r^   r   r   r   r	   q  ru   zBoundArguments.signaturec              	   C   s   g }| j j D ]8\}}|jttfv s|jr t|S z| j| }W n ty/   Y  t|S w |jt	kr;|
| q|| qt|S r   )r   r"   r9   r?   r@   rp   r$   r>   KeyErrorrA   extendappendr0   )rZ   r:   
param_namerO   argr   r   r   r:   u  s"   	
zBoundArguments.argsc              	   C   s   i }d}| j j D ]>\}}|s%|jttfv s|jrd}n|| jvr%d}q
|s(q
z| j| }W n	 ty8   Y q
w |jtkrD|	| q
|||< q
|S )NFT)
r   r"   r9   r?   r@   rp   r$   r>   r   update)rZ   r[   kwargs_startedr   rO   r   r   r   r   r[     s,   


zBoundArguments.kwargsc                 C   r{   r|   r}   r~   r   r   r   r     r   zBoundArguments.__hash__c                 C   s$   t |jto| j|jko| j|jkS r   )r   ry   r   r	   r>   r   r   r   r   r     s
   

zBoundArguments.__eq__c                 C   r   r   r   r   r   r   r   r     ra   zBoundArguments.__ne__N)r   r   rT   rU   r'   r   r	   r:   r[   r   r   r   r   r   r   r   r   \  s    


r   c                   @   s   e Zd ZdZdZeZeZe	Z
de	dfddZedd Zed	d
 Zedd ZeefddZdd Zdd Zdd ZdddZdd Zdd Zdd ZdS )r   a  A Signature object represents the overall signature of a function.
    It stores a Parameter object for each parameter accepted by the
    function, as well as information specific to the function itself.

    A Signature object has the following public attributes and methods:

    * parameters : OrderedDict
        An ordered mapping of parameters' names to the corresponding
        Parameter objects (keyword-only arguments are in the same order
        as listed in `code.co_varnames`).
    * return_annotation : object
        The annotation for the return type of the function if specified.
        If the function has no annotation for its return type, this
        attribute is not set.
    * bind(*args, **kwargs) -> BoundArguments
        Creates a mapping from positional and keyword arguments to
        parameters.
    * bind_partial(*args, **kwargs) -> BoundArguments
        Creates a partial mapping from positional and keyword arguments
        to parameters (simulating 'functools.partial' behavior.)
    )_return_annotation_parametersNTc                 C   s   |du rt  }nR|rQt  }t}t|D ]<\}}|j}||k r+d}	|	||j}	t|	|}|j}
|
du r>t|}
|j|
d}|
|v rKd|
}	t|	|||
< qn	t dd |D }|| _	|| _
dS )zConstructs Signature from the given list of Parameter
        objects and 'return_annotation'.  All arguments are optional.
        Nz%wrong parameter order: {0} before {1}rc   zduplicate parameter name: {0!r}c                 s   s    | ]}|j |fV  qd S r   rc   ).0rO   r   r   r   	<genexpr>  s    z%Signature.__init__.<locals>.<genexpr>)r   rn   	enumerater?   r+   r=   r   rq   r/   r   r   )rZ   r"   return_annotation__validate_parameters__paramstop_kindidxrO   r?   rL   r   r   r   r   r'     s4   


zSignature.__init__c              	   C   s  t |tjstd|| j}|j}|j}|j}t	|d| }t
|dd}||||  }t
|di }	|j}
t
|dd}|
rEt|
}nd}g }|| }|d| D ]}|	|t}||||td qSt||d D ]\}}|	|t}||||t|
| d qn|jd	@ r|||  }|	|t}||||td |D ]}t}|dur||t}|	|t}||||t|d q|jd
@ r|| }|jd	@ r|d7 }|| }|	|t}||||td | ||	dtddS )z2Constructs Signature for the given python functionz{0!r} is not a Python functionNco_kwonlyargcountr   __annotations____kwdefaults__)r   r?   )r   r?   r#   ri      r!   returnF)r   r   )r   r,   r4   r*   r+   _parameter_cls__code__co_argcountco_varnamesr0   r   __defaults__lengetrV   r   ro   r   co_flagsrA   rp   r@   )r   r8   r   	func_code	pos_count	arg_names
positionalkeyword_only_countkeyword_onlyannotationsdefaults
kwdefaultspos_default_countr"   non_default_countr   r   offsetr#   indexr   r   r   r5     sp   












zSignature.from_functionc                 C   s0   zt | jW S  ty   t| j  Y S w r   )r,   MappingProxyTyper   r   r   r9   r^   r   r   r   r"   J  s
   zSignature.parametersc                 C   r\   r   )r   r^   r   r   r   r   Q  ru   zSignature.return_annotationc                 C   s0   |t u r	| j }|t u r| j}t| ||dS )zCreates a customized copy of the Signature.
        Pass 'parameters' and/or 'return_annotation' arguments
        to override them in the new copy.
        )r   )rS   r"   r1   r   r   )rZ   r"   r   r   r   r   r/   U  s   
zSignature.replacec                 C   r{   r|   r}   r~   r   r   r   r   d  r   zSignature.__hash__c              	   C   s   t t|tr| j|jkst| jt|jkrdS tdd t|j D }t| j	 D ]F\}\}}|j
tkrTz|j| }W n tyK   Y  dS w ||krS dS q.z|| }W n tye   Y  dS w ||ksq||j| krt dS q.dS )NFc                 s   s    | ]	\}}||fV  qd S r   r   )r   r   rO   r   r   r   r   n  s    z#Signature.__eq__.<locals>.<genexpr>T)r   r   r   r   r   r"   dictr   keysr9   r?   rp   r   )rZ   r   other_positionsr   r   rO   other_param	other_idxr   r   r   r   h  s6   
zSignature.__eq__c                 C   r   r   r   r   r   r   r   r     ra   zSignature.__ne__Fc                 C   sz  t  }t| j }d}t|}|r(| j D ]\}}	|	jr'||vr'|	j||< q	 zt|}
W n^ ty   zt|}	W n
 tyF   Y Y nw |	j	t
krNY n|	j|v rj|	j	tkred}|j|	jd}t||	f}Y nj|	j	tkst|	jtury|	f}Y n[|r|	f}Y nTd}|j|	jd}t|w zt|}	W n ty   tdw |	j	ttfv rtd|	j	t
kr|
g}|| t|||	j< n|	j|v rtdj|	jd|
||	j< q)d}t||D ]H}	|	j	tkrtdj|	jd|	j	tkr|	}q|	j}z||}
W n! ty   |s|	j	t
kr|	jtu rtdj|dY qw |
||< q|r7|dur3|||j< ntd	| | |S )
z$Private method.  Don't use directly.r   TzA{arg!r} parameter is positional only, but was passed as a keyword)r   z'{arg!r} parameter lacking default valueztoo many positional argumentsz$multiple values for argument {arg!r}Nztoo many keyword arguments)r   iterr"   r1   r9   r$   r#   nextStopIterationr?   rA   r   rn   r+   r*   r@   rV   rp   r   r0   	itertoolschainrB   r   _bound_arguments_cls)rZ   r:   r[   r7   r>   r"   parameters_exarg_valsr   rO   arg_valrL   r1   kwargs_paramr   r   r   _bind  s   





&



F



zSignature._bindc                 O   s   |  ||S )zGet a BoundArguments object, that maps the passed `args`
        and `kwargs` to the function's signature.  Raises `TypeError`
        if the passed arguments can not be bound.
        r   rZ   r:   r[   r   r   r   bind	  s   zSignature.bindc                 O   s   | j ||ddS )zGet a BoundArguments object, that partially maps the
        passed `args` and `kwargs` to the function's signature.
        Raises `TypeError` if the passed arguments can not be bound.
        T)r7   r   r   r   r   r   r<     s   zSignature.bind_partialc           	      C   s   g }d}t | j D ]$\}}t|}|j}|tkrd}n|tkr*|r*|d d}|| qdd	|}| j
turIt| j
}|d|7 }|S )NTFrw   z({0})z, z -> {0})r   r"   r1   rq   r?   rA   rp   r   r+   joinr   rV   r   )	rZ   resultrender_kw_only_separatorr   rO   rx   r?   renderedannor   r   r   r_     s    


zSignature.__str__)F)r   r   rT   rU   r   r   r   r   r   rV   r   r'   classmethodr5   r   r"   r   rS   r/   r   r   r   r   r   r<   r_   r   r   r   r   r     s0    
&
H


 r   r   )$rU   
__future__r   r   r   r   r6   rr   r,   collectionsr   __version____all__r   r%   _WrapperDescriptorall_MethodWrapperrC   r   r   r    r	   objectrS   rV   rX   rW   rn   ro   rA   rp   r@   r   r   r   r   r   r   r   <module>   s<    


p a