B
    S\,                 @   s\   d Z ddlZddlZddlm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)Optionalc               @   sL   e Zd ZdZdZdddZ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 infinite stop infinite recursion
    )path
lookupnamecallcontext	boundnodeinferredextra_contextNc             C   s2   |pt  | _d | _d | _d | _|p$i | _i | _d S )N)setr   r   r   r   r   r	   )selfr   r    r   .lib/python3.7/site-packages/astroid/context.py__init__   s    

zInferenceContext.__init__c             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   pushP   s
    zInferenceContext.pushc             C   s,   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   r   r   r   r	   )r   cloner   r   r   r   _   s
    zInferenceContext.clonec             c   s4   g }x|D ]}| | |V  q
W t|| j|< dS )zRCache result of generator into dictionary

        Used to cache inference resultsN)appendtupler   )r   key	generatorZresultsresultr   r   r   cache_generatorl   s
    


z InferenceContext.cache_generatorc             c   s   t | j}d V  || _d S )N)r
   r   )r   r   r   r   r   restore_pathw   s    
zInferenceContext.restore_pathc                s,    fdd j D }dt jd|f S )Nc             3   s2   | ]*}d |t jt |dt| df V  qdS )z%s=%sP   )widthN)pprintZpformatgetattrlen).0Zfield)r   r   r   	<genexpr>   s   z+InferenceContext.__str__.<locals>.<genexpr>z%s(%s)z,
    )	__slots__type__name__join)r   stater   )r   r   __str__}   s    

zInferenceContext.__str__)NN)r$   
__module____qualname____doc__r"   r   r   r   r   
contextlibcontextmanagerr   r'   r   r   r   r   r      s   
1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   )argvalue)r    r0   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$   r(   r)   r*   r"   r   r   r   r   r   r-      s   r-   )contextreturnc             C   s   | dk	r|   S t S )z4Clone a context if given, or return a fresh contexxtN)r   r   )r3   r   r   r   copy_context   s    r5   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
    )r5   r   )r3   r   r   r   r   bind_context_to_node   s    r6   )	r*   r+   r   typingr   r   r-   r5   r6   r   r   r   r   <module>	   s   w