
    ړiN                         d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	m
Z
mZ ddlmZ dd	lmZ dd
lmZ e	rddlZg dZ G d d      Z G d d      Z	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 ddZy)z
Helpers for retrieving the function signature of the function call that we are
editing.

Either with the Jedi library, or using `inspect.signature` if Jedi fails and we
can use `eval()` to evaluate the function object.
    )annotationsN)	Signature)_ParameterKind)TYPE_CHECKINGAnySequence)Document   )DictionaryCompleter)get_jedi_script_from_document)r   get_signatures_using_jediget_signatures_using_evalc                  B    e Zd Z	 	 	 	 	 	 	 	 	 	 ddZddZedd       Zy)	Parameterc                <    || _         || _        || _        || _        y N)namekind
annotationdefault)selfr   r   r   r   s        :/tmp/pip-target-20744g9h/lib/python/ptpython/signatures.py__init__zParameter.__init__   s      		$    c                "    d| j                   dS )NzParameter(name=))r   r   s    r   __repr__zParameter.__repr__)   s     Q//r   c                Z    | j                   }| j                  |d| j                   z  }|S )z$
        Name + annotation.
        z: )r   r   )r   descriptions     r   r    zParameter.description,   s3    
 ii??&R011Kr   N)
r   strr   
str | Noner   r"   r   ParameterKindreturnNoner$   r!   )__name__
__module____qualname__r   r   propertyr     r   r   r   r      sP      	
  
0 	 	r   r   c                  z    e Zd ZdZ	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 ddZe	 	 	 	 	 	 	 	 	 	 d	d       Zed
d       ZddZy)r   a  
    Signature definition used wrap around both Jedi signatures and
    python-inspect signatures.

    :param index: Parameter index of the current cursor position.
    :param bracket_start: (line, column) tuple for the open bracket that starts
        the function call.
    Nc                X    || _         || _        || _        || _        || _        || _        y r   r   	docstring
parametersindexreturnsbracket_start)r   r   r/   r0   r1   r2   r3   s          r   r   zSignature.__init__C   s/     	"$
*r   c           
     `   g }dd}|j                   j                         D ]  }|j                  t        |j                   ||j
                        |j                  t        j                  j                  urt        |j                        nd |j                                 | ||||d      S )Nc                    	 t        | j                        S # t        $ r Y nw xY wt        |       } | j                  d      r| t	        d      d } | S )zJ
            Get annotation as string from inspect signature.
            ztyping.ztyping:N)r!   r'   AttributeError
startswithlen)r   s    r   get_annotation_namez=Signature.from_inspect_signature.<locals>.get_annotation_name]   sZ    :..//!  ZJ$$Y/'I(89
s    	##r   r   r   r    )r   r/   r0   r1   r2   )r   objectr$   r!   )r0   valuesappendr   r   r   r   inspectemptyreprr   )clsr   r/   	signaturer1   r0   r9   ps           r   from_inspect_signaturez Signature.from_inspect_signatureS   s     
	 %%,,. 
	A21<<@yy(9(9(?(?? !O	
	 !
 	
r   c           	     T   g }|j                   D ];  }||j                  t        |j                         d d |j                               = |j                         }t        |t              s|j                  d      } | |j                  |||j                  d|j                        S )Nr:   zutf-8r;   r.   )paramsr>   r   	to_stringr   r/   
isinstancer!   decoder   r1   r3   )rB   rC   r0   rD   r/   s        r   from_jedi_signaturezSignature.from_jedi_signature   s    
!! 	Ay# 		 '')	)S)!((1I!//#11
 	
r   c                <    d| j                   d| j                  dS )Nz
Signature(z, parameters=r   )r   r0   r   s    r   r   zSignature.__repr__   s     DII=doo5HJJr   )Nr;   )r   r   )r   r!   r/   r!   r0   zSequence[Parameter]r1   z
int | Noner2   r!   r3   ztuple[int, int]r$   r%   )
r   r!   r/   r!   rC   InspectSignaturer1   intr$   r   )rC   zjedi.api.classes.Signaturer$   r   r&   )	r'   r(   r)   __doc__r   classmethodrE   rK   r   r+   r   r   r   r   9   s     !)/++ + (	+
 + + '+ 
+  *
*
 *
 $	*

 *
 
*
 *
X 
 
<Kr   r   c                   t        | ||      }|sg S 	 |j                         }	 |r|d   j                   |D cg c]  }t        j                  |       c}S # t        $ r Y .w xY w# t        $ r g }Y ?t
        $ r g }Y Kw xY wc c}w )Nr   )r   get_signaturesrG   r6   
ValueError	Exceptionr   rK   )documentlocalsglobalsscript
signaturessigs         r   r   r      s     +8VWEF 	**,
 	1$$ ;EE3I))#.EE  		%  
  
	$ Fs-   A' A B	A$#A$'B4
B Bc                   | j                   dz
  }dddd}dg}d}d}|dk\  r|| j                  |   }|d	v r|j                  |       nL|d
v r0|snP||d      |k7  rnD|j                          t	        |      dk(  rd}n#|dk(  rt	        |      dk(  r|dz  }|dz  }|dk\  r||sg S t        fdfd      j                  t        | j                  |            }	|	g S 	 |	j                  }
	 t        j                  |	      }	 |	j                  xs d}t         j#                  |
|||      gS # t        $ r |	j                  j                  }
Y bw xY w# t        $ r g cY S t        $ r g cY S w xY w#  d}Y bxY w)z
    Look for the signature of the function before the cursor position without
    use of Jedi. This uses a similar approach as the `DictionaryCompleter` of
    running `eval()` over the detected function name.
    r
   ({[)r   }]r   r   Fz)]}z([{T,c                      S r   r+   )rW   s   r   <lambda>z+get_signatures_using_eval.<locals>.<lambda>   s    g r   c                      S r   r+   )rV   s   r   rd   z+get_signatures_using_eval.<locals>.<lambda>   s    v r   )cursor_positionr;   )r1   )rf   textr>   popr8   r   eval_expressionr	   r'   rT   	__class__r?   rC   	TypeErrorrS   rO   r   rE   )rU   rV   rW   posparen_mappingparen_stackcomma_countfound_startcharobjr   rC   docs    ``          r   r   r      s    
"
"Q
&CCc2MK KK
(}}S!5=t$U] [_-5OO;1$"S[S-21Kq/ (2 	
 o~
>
N
N4fC {	&||%%c*	kkR
 ,,T3	,UVV!  &}}%%&
  	 	
s6    D- -E E/ -EEE,
E,+E,/E5)rU   r	   rV   dict[str, Any]rW   rt   r$   zlist[Signature])rO   
__future__r   r?   r   rM   r   r#   typingr   r   r   prompt_toolkit.documentr	   	completerr   utilsr   jedi.api.classesjedi__all__r   r   r   r+   r   r   <module>r}      s    #  1 3 / / , * 0
Q <gK gKT F F . F9G F FFIWIW .IW9GIWIWr   