B
    7r\%                 @   s   d Z ddlmZmZ ddlmZ ddlmZmZm	Z	 ddl
mZmZ ddlmZmZ ddlmZmZmZ G dd	 d	eZdddZG dd deZG dd deZG dd deZG dd deZe Zdd Zd
S )a<  
Contexts are the "values" that Python would return. However Contexts are at the
same time also the "contexts" that a user is currently sitting in.

A ContextSet is typically used to specify the return of a function or any other
static analysis operation. In jedi there are always multiple returns and not
just one.
    )ExprStmtCompFor)debug)Python3Methodzip_longestunicode)clean_scope_docstringget_doc_with_call_signature)BaseContextSetBaseContext)EvaluatorIndexErrorEvaluatorTypeErrorEvaluatorKeyErrorc               @   s   e Zd ZdZi ZdZedd Zej	dd Z
dd Zdd
dZdd Zdd ZedddZdddZdd Zdd ZdddZdS )ContextzC
    Should be defined, otherwise the API returns empty types.
    Nc             C   s   | j j S )N)	__class____name__lower)self r   9lib/python3.7/site-packages/jedi/evaluate/base_context.pyapi_type   s    zContext.api_typec             C   s   | j jr|  td| | ddlm} y|| j | |S  |jk
rP   Y nX y
| j	}W n  t
k
r|   td|  tS X ||}td||  |S | j | |S )z
        In contrast to py__call__ this function is always available.

        `hasattr(x, py__call__)` can also be checked to see if a context is
        executable.
        zexecute: %s %sr   )stdlibzno execution possible %szexecute result: %s in %s)	evaluatorZis_analysisZeval_allr   dbgjedi.evaluater   executeZNotInStdLibZ
py__call__AttributeErrorZwarningNO_CONTEXTS)r   	argumentsr   funcZcontext_setr   r   r   r   $   s"    
zContext.executec             G   s(   ddl m} |dd |D }| |S )zE
        Execute a function with already executed arguments.
        r   )ValuesArgumentsc             S   s   g | ]}t |qS r   )
ContextSet).0valuer   r   r   
<listcomp>I   s    z-Context.execute_evaluated.<locals>.<listcomp>)Zjedi.evaluate.argumentsr    r   )r   Z
value_listr    r   r   r   r   execute_evaluatedD   s    zContext.execute_evaluatedFc          	   C   st   t d|  y|r| j}n| j}W nF tk
rh   |d k	r`ddlm} |j|jd|j	d|  d t
g S X | S d S )Nz
iterate %sr   )analysisztype-error-not-iterablez&TypeError: '%s' object is not iterable)message)r   r   Zpy__aiter__Z
py__iter__r   r   r&   addcontextnodeiter)r   contextualized_nodeis_asyncZiter_methodr&   r   r   r   iterateL   s    

zContext.iteratec       
   
   C   sv  ddl m} ddlm}m} t }xL|D ]B}t||r@|j}t||rly| }W n t	k
rj   Y nX t
|ttttttfkrt| |r| jdkr||  O }q*|tt| O }q*y
| j}W n: tk
r   ddlm}	 |	j|jd|jd|  d Y q*X y|||O }W q* tk
r4   |tt| O }Y q* tk
rT   ||  O }Y q* tk
rl   t}Y q*X q*W |S )	Nr   )CompiledObject)SliceSequencedict)r&   ztype-error-not-subscriptablez+TypeError: '%s' object is not subscriptable)r'   )Zjedi.evaluate.compiledr/   Zjedi.evaluate.context.iterabler0   r1   r!   
isinstanceobjZget_safe_value
ValueErrortypefloatintstrr   slicebytesZ
array_typedict_valuesiterate_contextsZpy__getitem__r   r   r&   r(   r)   r*   r   r   r   r   )
r   Zindex_contextsr,   r/   r0   r1   resultindexgetitemr&   r   r   r   get_item_   sD    


zContext.get_itemc             C   s   | j | |S )N)r   Zeval_element)r   r*   r   r   r   	eval_node   s    zContext.eval_nodeTc       
      C   sX   |dkr| }ddl m} |j| j| ||||d}||}	|rH||	S |j|	| dS )zZ
        :param position: Position of the last statement -> tuple of line, column
        Nr   )finder)analysis_errors)Zattribute_lookup)r   rC   Z
NameFinderr   Zget_filtersZfilter_namefind)
r   Zname_or_strZname_contextZpositionZsearch_globalZis_gotorD   rC   ffiltersr   r   r   py__getattribute__   s    


zContext.py__getattribute__c             C   s   | j | |||S )N)r   create_context)r   r*   Znode_is_contextZnode_is_objectr   r   r   rI      s    zContext.create_contextc             C   s   dS )NFr   )r   r   r   r   is_class   s    zContext.is_classc             C   s   dS )z
        Since Wrapper is a super class for classes, functions and modules,
        the return value will always be true.
        Tr   )r   r   r   r   
py__bool__   s    zContext.py__bool__c             C   s>   y| j j W n tk
r    dS X |r0t| j S t| j S d S )N )	tree_nodeZget_doc_noder   r	   r   )r   Zinclude_call_signaturer   r   r   	py__doc__   s    

zContext.py__doc__)NF)NNFFT)FF)F)r   
__module____qualname____doc__predefined_namesrM   propertyr   r   Zincrease_indentr   r%   r.   rA   rB   r   rH   rI   rJ   rK   rN   r   r   r   r   r      s"    
1  
r   NFc             C   s   t dd | j||dD S )z
    Calls `iterate`, on all contexts but ignores the ordering and just returns
    all contexts that the iterate functions yield.
    c             s   s   | ]}|  V  qd S )N)infer)r"   Zlazy_contextr   r   r   	<genexpr>   s   z#iterate_contexts.<locals>.<genexpr>)r-   )r!   Z	from_setsr.   )Zcontextsr,   r-   r   r   r   r=      s    r=   c                   s$   e Zd Z fddZdd Z  ZS )TreeContextc                s"   t t| || i | _|| _d S )N)superrV   __init__rR   rM   )r   r   Zparent_contextrM   )r   r   r   rX      s    zTreeContext.__init__c             C   s   d| j j| jf S )Nz<%s: %s>)r   r   rM   )r   r   r   r   __repr__   s    zTreeContext.__repr__)r   rO   rP   rX   rY   __classcell__r   r   )r   r   rV      s   rV   c               @   s$   e Zd Zdd Zdd Zdd ZdS )ContextualizedNodec             C   s   || _ || _d S )N)r)   r*   )r   r)   r*   r   r   r   rX      s    zContextualizedNode.__init__c             C   s
   | j  S )N)r)   get_root_context)r   r   r   r   r\      s    z#ContextualizedNode.get_root_contextc             C   s   | j | jS )N)r)   rB   r*   )r   r   r   r   rT      s    zContextualizedNode.inferN)r   rO   rP   rX   r\   rT   r   r   r   r   r[      s   r[   c               @   s    e Zd Zedd Zdd ZdS )ContextualizedNamec             C   s   | j S )N)r*   )r   r   r   r   name   s    zContextualizedName.namec             C   s   g }| j j}| j }xt|dk	r|jdkrjxRt|jD ]*\}}||kr2|dt|d |f P q2W tdnt|t	t
frzP |}|j}qW |S )a  
        Returns an array of tuple(int, node) of the indexes that are used in
        tuple assignments.

        For example if the name is ``y`` in the following code::

            x, (y, z) = 2, ''

        would result in ``[(1, xyz_node), (0, yz_node)]``.
        N)ZtestlistZtestlist_compZtestlist_star_exprZexprlistr      zCouldn't find the assignment.)r*   parentr6   	enumerateZchildreninsertr8   LookupErrorr3   r   r   )r   Zindexesr*   ZcompareiZchildr   r   r   assignment_indexes   s    



z%ContextualizedName.assignment_indexesN)r   rO   rP   rS   r^   re   r   r   r   r   r]      s   r]   c               @   s   e Zd Zdd ZdddZdS )r!   c             C   s   t dd | jD S )Nc             s   s   | ]}|  V  qd S )N)py__class__)r"   cr   r   r   rU     s    z)ContextSet.py__class__.<locals>.<genexpr>)r!   from_iterable_set)r   r   r   r   rf     s    zContextSet.py__class__NFc             #   sL   ddl m}  fdd| jD }x$t| D ]}|dd |D V  q,W d S )Nr   )get_merged_lazy_contextc                s   g | ]}|j  d qS ))r-   )r.   )r"   rg   )r,   r-   r   r   r$   	  s    z&ContextSet.iterate.<locals>.<listcomp>c             S   s   g | ]}|d k	r|qS )Nr   )r"   lr   r   r   r$     s    )Zjedi.evaluate.lazy_contextrj   ri   r   )r   r,   r-   rj   Z
type_itersZlazy_contextsr   )r,   r-   r   r.     s
    zContextSet.iterate)NF)r   rO   rP   rf   r.   r   r   r   r   r!     s   r!   c                s    fdd}|S )Nc                 s   t  | |S )N)r!   rh   )argskwargs)r   r   r   wrapper  s    z(iterator_to_context_set.<locals>.wrapperr   )r   rn   r   )r   r   iterator_to_context_set  s    ro   )NF)rQ   Zparso.python.treer   r   Zjedir   Zjedi._compatibilityr   r   r   Zjedi.parser_utilsr   r	   Zjedi.commonr
   r   Zjedi.evaluate.helpersr   r   r   r   r=   rV   objectr[   r]   r!   r   ro   r   r   r   r   <module>   s    ,

$