B
    /dC                 @   sZ  d dl mZ d dlmZ d dlmZ d dlm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mZ d d	lmZmZ d d
lmZ d dlmZ d dlmZ G dd dZG dd deZG dd dZG dd deeZG dd deeZG dd deeZ G dd deeZ!G dd deeZ"G dd deZ#G dd  d e#Z$d!d" Z%d#d$ Z&d%S )&    )abstractmethod)contextmanager)Path)Optional)search_ancestor)Name)ParserTreeFilterMergedFilterGlobalNameFilter)AnonymousParamNameTreeNameDefinition)	NO_VALUESValueSet)get_parent_scope)debug)parser_utilsc               @   s   e Zd Zdd Zed)ddZdd Zd*d	d
Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zedd Zdd  Zed!d" Zd#d$ Zd%d& Zed'd( ZdS )+AbstractContextc             C   s   || _ i | _d S )N)inference_statepredefined_names)selfr    r   c/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/jedi/inference/context.py__init__   s    zAbstractContext.__init__Nc             C   s   t d S )N)NotImplementedError)r   until_positionorigin_scoper   r   r   get_filters   s    zAbstractContext.get_filtersc             C   sF   ddl m} t| t|tr|nd |}|||}td|| | |S )Nr   )finderzcontext.goto %s in (%s): %s)jedi.inferencer   _get_global_filters_for_name
isinstancer   Zfilter_namer   dbg)r   name_or_strpositionr   filtersnamesr   r   r   goto   s    zAbstractContext.gotoTc             C   s\  |dkr| }|  ||}t|tr(|jn|}d}| jrt|tr|}xZ|dk	rt|s|j}|jdkrFy| j| }	|	| }
W n t	k
r   wFY qFX |
}P qFW |dk	r|rddl
m} |j| | j|d}||jkrt}q|}ntdd |D }|s6|s6|r6t|tr6ddl
m} d	| }||d
|| td|| |rN|S | |||S )zZ
        :param position: Position of the last statement -> tuple of line, column
        N)Zif_stmtZfor_stmtcomp_forsync_comp_forr   )flow_analysis)contextZvalue_scopenodec             s   s   | ]}|  V  qd S )N)Zinfer).0namer   r   r   	<genexpr>M   s    z5AbstractContext.py__getattribute__.<locals>.<genexpr>)analysisz$NameError: name '%s' is not defined.z
name-errorz context.names_to_types: %s -> %s)r&   r    r   valuer   r   is_scopeparenttypeKeyErrorr   r)   Zreachability_check	tree_nodeZUNREACHABLEr   r   Z	from_setsr/   addr   r!   _check_for_additional_knowledge)r   r"   name_contextr#   Zanalysis_errorsr%   Zstring_nameZfound_predefined_typesr+   Z	name_dicttypesr)   checkvaluesr/   messager   r   r   py__getattribute__&   sH    


z"AbstractContext.py__getattribute__c             C   s   |p| }t |tr~| s~|}|jg}tdd |D r<tS ddlm} x4t|dd}|||||}|d k	rp|S ||krJP qJW tS )Nc             s   s   | ]}|j d kV  qdS ))r'   r(   N)r3   )r,   br   r   r   r.   a   s    zBAbstractContext._check_for_additional_knowledge.<locals>.<genexpr>r   )check_flow_informationT)Zinclude_flows)	r    r   is_instancer5   anyr   Zjedi.inference.finderr?   r   )r   r"   r8   r#   
flow_scopeZ
base_nodesr?   nr   r   r   r7   Z   s     z/AbstractContext._check_for_additional_knowledgec             C   s   | j }|d kr| S | S )N)parent_contextget_root_context)r   rD   r   r   r   rE   n   s    z AbstractContext.get_root_contextc             C   s   dS )NFr   )r   r   r   r   	is_modulet   s    zAbstractContext.is_modulec             C   s   dS )NFr   )r   r   r   r   is_builtins_modulew   s    z"AbstractContext.is_builtins_modulec             C   s   dS )NFr   )r   r   r   r   is_classz   s    zAbstractContext.is_classc             C   s   dS )NFr   )r   r   r   r   is_stub}   s    zAbstractContext.is_stubc             C   s   dS )NFr   )r   r   r   r   r@      s    zAbstractContext.is_instancec             C   s   dS )NFr   )r   r   r   r   is_compiled   s    zAbstractContext.is_compiledc             C   s   dS )NFr   )r   r   r   r   is_bound_method   s    zAbstractContext.is_bound_methodc             C   s   t d S )N)r   )r   r   r   r   
py__name__   s    zAbstractContext.py__name__c             C   s   t d S )N)r   )r   r   r   r   	get_value   s    zAbstractContext.get_valuec             C   s   d S )Nr   )r   r   r   r   r-      s    zAbstractContext.namec             C   s   dS )Nr   r   )r   r   r   r   get_qualified_names   s    z#AbstractContext.get_qualified_namesc             C   s   dS )N r   )r   r   r   r   	py__doc__   s    zAbstractContext.py__doc__c             c   s&   | j }|||< z
d V  W d ||= X d S )N)r   )r   rB   dctZ
predefinedr   r   r   predefine_names   s
    
zAbstractContext.predefine_names)NN)NNT)__name__
__module____qualname__r   r   r   r&   r=   r7   rE   rF   rG   rH   rI   r@   rJ   rK   rL   rM   propertyr-   rN   rP   r   rR   r   r   r   r   r      s*   	 
3r   c                   s   e Zd ZdZ fddZedd Zedd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd Zdd Zedd Zdd Zdd Zdd Zd d! Z  ZS )"ValueContextzC
    Should be defined, otherwise the API returns empty types.
    c                s   t  |j || _d S )N)superr   r   _value)r   r0   )	__class__r   r   r      s    zValueContext.__init__c             C   s   | j jS )N)rY   r5   )r   r   r   r   r5      s    zValueContext.tree_nodec             C   s   | j jS )N)rY   rD   )r   r   r   r   rD      s    zValueContext.parent_contextc             C   s
   | j  S )N)rY   rF   )r   r   r   r   rF      s    zValueContext.is_modulec             C   s   | j | jjkS )N)rY   r   builtins_module)r   r   r   r   rG      s    zValueContext.is_builtins_modulec             C   s
   | j  S )N)rY   rH   )r   r   r   r   rH      s    zValueContext.is_classc             C   s
   | j  S )N)rY   rI   )r   r   r   r   rI      s    zValueContext.is_stubc             C   s
   | j  S )N)rY   r@   )r   r   r   r   r@      s    zValueContext.is_instancec             C   s
   | j  S )N)rY   rJ   )r   r   r   r   rJ      s    zValueContext.is_compiledc             C   s
   | j  S )N)rY   rK   )r   r   r   r   rK      s    zValueContext.is_bound_methodc             C   s
   | j  S )N)rY   rL   )r   r   r   r   rL      s    zValueContext.py__name__c             C   s   | j jS )N)rY   r-   )r   r   r   r   r-      s    zValueContext.namec             C   s
   | j  S )N)rY   rN   )r   r   r   r   rN      s    z ValueContext.get_qualified_namesc             C   s
   | j  S )N)rY   rP   )r   r   r   r   rP      s    zValueContext.py__doc__c             C   s   | j S )N)rY   )r   r   r   r   rM      s    zValueContext.get_valuec             C   s   d| j j| jf S )Nz%s(%s))rZ   rS   rY   )r   r   r   r   __repr__   s    zValueContext.__repr__)rS   rT   rU   __doc__r   rV   r5   rD   rF   rG   rH   rI   r@   rJ   rK   rL   r-   rN   rP   rM   r\   __classcell__r   r   )rZ   r   rW      s"   rW   c               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TreeContextMixinc             C   s   ddl m} || |S )Nr   )
infer_node)Zjedi.inference.syntax_treer`   )r   r+   r`   r   r   r   r`      s    zTreeContextMixin.infer_nodec             C   s   ddl m} || jkr*|  s"t|  S | |}|jdkr|j	||}|
 r|j|j}|| j|j|}|j|| |d}|S |jdkr|| j||S td| d S )Nr   )r0   )funcdeflambdef)instanceZclass_contextfunctionclassdefzProbably shouldn't happen: %s)r   r0   r5   rF   AssertionErrorrM   create_contextr3   ZFunctionValueZfrom_contextrH   rD   create_valueZAnonymousInstancer   ZBoundMethod
as_contextZ
ClassValuer   )r   r+   r0   rD   funcZclass_valuerc   r   r   r   rh      s&    



zTreeContextMixin.create_valuec                sz   d
 fdd	 dd }|j dkrn|j|jd }j|jk rnj}|j dkrf|jksn|} |dd	S )NTc                sn   | j krS | jdkr&|  S | jdkr^ | j}j| jd jkrT|S t|| S td|  d S )N)ra   rb   re   )r'   r(   z(There's a scope that was not managed: %s)	r5   r3   rh   ri   r2   	start_poschildrenCompForContext	Exception)
scope_node	is_nestedrD   )from_scope_noder+   parent_scoper   r   r   rr      s    



z8TreeContextMixin.create_context.<locals>.from_scope_nodec             S   st   xn| j } t| r| S | jdkr<| jd jdkrl| jd S q| jdkrx$| jdd D ]}|jdkrV|S qVW qW d S )N)argumentZtestlist_comp   )r'   r(   Zdictorsetmaker   )r2   r   r1   r3   rm   )r+   rC   r   r   r   rs   
  s    



z5TreeContextMixin.create_context.<locals>.parent_scope)ra   re   :param)rq   )T)r3   rm   indexrl   r2   r-   )r   r+   rp   colonr2   r   )rr   r+   rs   r   r   rg      s    
zTreeContextMixin.create_contextc             C   sX   |  }|r@|jdkr@|j|kr@t|dd}| |}t||S | |}t||S d S )Nrx   ra   rb   )Zget_definitionr3   r-   r   rh   r   rg   r   )r   Z	tree_nameZ
definitionra   rj   r*   r   r   r   create_name"  s    


zTreeContextMixin.create_nameN)rS   rT   rU   r`   rh   rg   r{   r   r   r   r   r_      s   &r_   c               @   s   e Zd ZdddZdS )FunctionContextNc             c   s   t | j| ||dV  d S )N)rD   r   r   )r   r   )r   r   r   r   r   r   r   .  s
    zFunctionContext.get_filters)NN)rS   rT   rU   r   r   r   r   r   r|   -  s   r|   c               @   sP   e Zd Zee dddZdddZdd Zed	d
 Z	edd Z
dd ZdS )ModuleContext)returnc             C   s
   | j  S )N)rY   
py__file__)r   r   r   r   r   8  s    zModuleContext.py__file__Nc             c   s>   | j |}t|d  tt| ||d|  V  |E d H  d S )N)rD   r   r   )rY   r   nextr	   r   get_global_filter)r   r   r   r$   r   r   r   r   ;  s    
zModuleContext.get_filtersc             C   s   t | S )N)r
   )r   r   r   r   r   I  s    zModuleContext.get_global_filterc             C   s   | j jS )N)rY   string_names)r   r   r   r   r   L  s    zModuleContext.string_namesc             C   s   | j jS )N)rY   
code_lines)r   r   r   r   r   P  s    zModuleContext.code_linesc             C   s   | j S )z
        This is the only function that converts a context back to a value.
        This is necessary for stub -> python conversion and vice versa. However
        this method shouldn't be moved to AbstractContext.
        )rY   )r   r   r   r   rM   T  s    zModuleContext.get_value)NN)rS   rT   rU   r   r   r   r   r   rV   r   r   rM   r   r   r   r   r}   7  s   
r}   c               @   s<   e Zd ZdddZdd Zedd Zee dd	d
Z	dS )NamespaceContextNc             C   s
   | j  S )N)rY   r   )r   r   r   r   r   r   r   ^  s    zNamespaceContext.get_filtersc             C   s   | j S )N)rY   )r   r   r   r   rM   a  s    zNamespaceContext.get_valuec             C   s   | j jS )N)rY   r   )r   r   r   r   r   d  s    zNamespaceContext.string_names)r~   c             C   s
   | j  S )N)rY   r   )r   r   r   r   r   h  s    zNamespaceContext.py__file__)NN)
rS   rT   rU   r   rM   rV   r   r   r   r   r   r   r   r   r   ]  s   
r   c               @   s    e Zd ZdddZdddZdS )ClassContextNc             c   s   |  ||V  d S )N)r   )r   r   r   r   r   r   r   m  s    zClassContext.get_filtersc             C   s   t | ||dS )N)rD   r   r   )r   )r   r   r   r   r   r   r   p  s    zClassContext.get_global_filter)NN)NN)rS   rT   rU   r   r   r   r   r   r   r   l  s   
r   c                   s>   e Zd Z fddZdddZdd Zdd	 Zd
d Z  ZS )rn   c                s   t  |j || _|| _d S )N)rX   r   r   r5   rD   )r   rD   r'   )rZ   r   r   r   y  s    zCompForContext.__init__Nc             c   s   t | V  d S )N)r   )r   r   r   r   r   r   r   ~  s    zCompForContext.get_filtersc             C   s   d S )Nr   )r   r   r   r   rM     s    zCompForContext.get_valuec             C   s   dS )Nz<comprehension context>r   )r   r   r   r   rL     s    zCompForContext.py__name__c             C   s   d| j j| jf S )Nz%s(%s))rZ   rS   r5   )r   r   r   r   r\     s    zCompForContext.__repr__)NN)	rS   rT   rU   r   r   rM   rL   r\   r^   r   r   )rZ   r   rn   x  s
   
rn   c               @   s   e Zd ZdddZdS )CompiledContextNc             C   s
   | j  S )N)rY   r   )r   r   r   r   r   r   r     s    zCompiledContext.get_filters)NN)rS   rT   rU   r   r   r   r   r   r     s   r   c               @   s6   e Zd ZdZdd Zedd Zee dddZ	dS )	CompiledModuleContextNc             C   s   | j S )N)rY   )r   r   r   r   rM     s    zCompiledModuleContext.get_valuec             C   s   | j jS )N)rY   r   )r   r   r   r   r     s    z"CompiledModuleContext.string_names)r~   c             C   s
   | j  S )N)rY   r   )r   r   r   r   r     s    z CompiledModuleContext.py__file__)
rS   rT   rU   r   rM   rV   r   r   r   r   r   r   r   r   r     s   r   c             C   s   |d k	rtt |ddd}d }|dkr2|}t |dd}|d k	rt|jd }|d k	rt||jk rt|d ksn||jd jk rt|j}t| ||S )Nra   re   rb   )r   rm   rl   get_global_filters)r*   Zname_or_noner#   Zancestorrb   rz   r   r   r   r     s    
r   c             c   sr   | }ddl m} x8| dk	rH| j||dE dH  t| |tfr@d}| j} qW t|jj d}|dk	sht	|V  dS )a  
    Returns all filters in order of priority for name resolution.

    For global name lookups. The filters will handle name resolution
    themselves, but here we gather possible filters downwards.

    >>> from jedi import Script
    >>> script = Script('''
    ... x = ['a', 'b', 'c']
    ... def func():
    ...     y = None
    ... ''')
    >>> module_node = script._module_node
    >>> scope = next(module_node.iter_funcdefs())
    >>> scope
    <Function: func@3-5>
    >>> context = script._get_module_context().create_context(scope)
    >>> filters = list(get_global_filters(context, (4, 0), None))

    First we get the names from the function scope.

    >>> print(filters[0])  # doctest: +ELLIPSIS
    MergedFilter(<ParserTreeFilter: ...>, <GlobalNameFilter: ...>)
    >>> sorted(str(n) for n in filters[0].values())  # doctest: +NORMALIZE_WHITESPACE
    ['<TreeNameDefinition: string_name=func start_pos=(3, 4)>',
     '<TreeNameDefinition: string_name=x start_pos=(2, 0)>']
    >>> filters[0]._filters[0]._until_position
    (4, 0)
    >>> filters[0]._filters[1]._until_position

    Then it yields the names from one level "lower". In this example, this is
    the module scope (including globals).
    As a side note, you can see, that the position in the filter is None on the
    globals filter, because there the whole module is searched.

    >>> list(filters[1].values())  # package modules -> Also empty.
    []
    >>> sorted(name.string_name for name in filters[2].values())  # Module attributes
    ['__doc__', '__name__', '__package__']

    Finally, it yields the builtin filter, if `include_builtin` is
    true (default).

    >>> list(filters[3].values())  # doctest: +ELLIPSIS
    [...]
    r   )BaseFunctionExecutionContextN)r   r   )
Zjedi.inference.value.functionr   r   r    r}   rD   r   r   r[   rf   )r*   r   r   Zbase_contextr   r>   r   r   r   r     s    /

r   N)'abcr   
contextlibr   pathlibr   typingr   Z
parso.treer   Zparso.python.treer   Zjedi.inference.filtersr   r	   r
   Zjedi.inference.namesr   r   Zjedi.inference.base_valuer   r   Zjedi.parser_utilsr   Zjedir   r   r   rW   r_   r|   r}   r   r   rn   r   r   r   r   r   r   r   r   <module>   s0    9P
&