B
    a                 @   s   d Z ddlZddlZddlmZmZmZmZmZ er@ddl	m
Z
 i Zdd ZG dd dZG d	d
 d
Zee edddZdd ZdS )zIVarious context related utilities, including inference and call contexts.    N)TYPE_CHECKINGMutableMappingOptionalSequenceTuple)NodeNGc               C   s   t   d S )N)_INFERENCE_CACHEclear r
   r
   3/tmp/pip-unpacked-wheel-mm06h1t3/astroid/context.py_invalidate_cache   s    r   c               @   s   e Zd ZdZdZdZdddZedd Zej	d	d Zee
ed
ee ee ee f ed
 f dddZdd Zdd Zejdd Zdd ZdS )InferenceContextzProvide context for inference

    Store already inferred nodes to save time
    Account for already visited nodes to stop infinite recursion
    )path
lookupnamecallcontext	boundnodeextra_context_nodes_inferredd   Nc             C   s@   |d krdg| _ n|| _ |p t | _d | _d | _d | _i | _d S )Nr   )r   setr   r   r   r   r   )selfr   nodes_inferredr
   r
   r   __init__0   s    

zInferenceContext.__init__c             C   s
   | j d S )z
        Number of nodes inferred in this context and all its clones/decendents

        Wrap inner value in a mutable cell to allow for mutating a class
        variable in the presence of __slots__
        r   )r   )r   r
   r
   r   r   ]   s    zInferenceContext.nodes_inferredc             C   s   || j d< d S )Nr   )r   )r   valuer
   r
   r   r   g   s    r   )returnc             C   s   t S )z
        Inferred node contexts to their mapped results

        Currently the key is ``(node, lookupname, callcontext, boundnode)``
        and the value is tuple of the inferred results
        )r   )r   r
   r
   r   inferredk   s    zInferenceContext.inferredc             C   s,   | j }||f| jkrdS | j||f dS )zPush node into inference path

        :return: True if node is already in context path else False
        :rtype: bool

        Allows one to see if the given node has already
        been looked at for this inference contextTF)r   r   add)r   nodenamer
   r
   r   pushy   s
    zInferenceContext.pushc             C   s0   t | j | jd}| j|_| j|_| j|_|S )zClone inference path

        For example, each side of a binary operation (BinOp)
        starts with the same context but diverge as each side is inferred
        so the InferenceContext will need be cloned)r   )r   r   copyr   r   r   r   )r   cloner
   r
   r   r!      s
    zInferenceContext.clonec             c   s   t | j}d V  || _d S )N)r   r   )r   r   r
   r
   r   restore_path   s    
zInferenceContext.restore_pathc                s,    fdd j D }dt jd|S )Nc             3   s2   | ]*}d |t jt |dt| df V  qdS )z%s=%sP   )widthN)pprintpformatgetattrlen).0field)r   r
   r   	<genexpr>   s   z+InferenceContext.__str__.<locals>.<genexpr>z{}({})z,
    )	__slots__formattype__name__join)r   stater
   )r   r   __str__   s    

zInferenceContext.__str__)NN)r/   
__module____qualname____doc__r,   Zmax_inferredr   propertyr   setterr   r   r   strr   r   r   r!   
contextlibcontextmanagerr"   r2   r
   r
   r
   r   r      s   
-
2
r   c               @   s   e Zd ZdZdZdddZdS )CallContextz"Holds information for a call site.)argskeywordsNc             C   s(   || _ |rdd |D }ng }|| _dS )z
        :param List[NodeNG] args: Call positional arguments
        :param Union[List[nodes.Keyword], None] keywords: Call keywords
        c             S   s   g | ]}|j |jfqS r
   )argr   )r)   r>   r
   r
   r   
<listcomp>   s    z(CallContext.__init__.<locals>.<listcomp>N)r<   r=   )r   r<   r=   r
   r
   r   r      s
    zCallContext.__init__)N)r/   r3   r4   r5   r,   r   r
   r
   r
   r   r;      s   r;   )contextr   c             C   s   | dk	r|   S t S )z4Clone a context if given, or return a fresh contexxtN)r!   r   )r@   r
   r
   r   copy_context   s    rA   c             C   s   t | } || _| S )a  Give a context a boundnode
    to retrieve the correct function name or attribute value
    with from further inference.

    Do not use an existing context since the boundnode could then
    be incorrectly propagated higher up in the call stack.

    :param context: Context to use
    :type context: Optional(context)

    :param node: Node to do name lookups from
    :type node NodeNG:

    :returns: A new context
    :rtype: InferenceContext
    )rA   r   )r@   r   r
   r
   r   bind_context_to_node   s    rB   )r5   r9   r%   typingr   r   r   r   r   Zastroid.node_classesr   r   r   r   r;   rA   rB   r
   r
   r
   r   <module>   s    