o
    Zh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   M/var/www/html/lang_env/lib/python3.10/site-packages/jedi/inference/context.py__init__   s   
zAbstractContext.__init__Nc                 C      t r   NotImplementedErrorr   until_positionorigin_scoper   r   r   get_filters      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   sZ  |du r| }|  ||}t|tr|jn|}d}| jrTt|trT|}|durTt|sT|j}|jdv rKz| j| }	|	| }
W n	 t	yG   Y q"w |
}n	|durTt|r+|durt|rtddl
m} |j| | j|d}||ju rqt}n|}n
tdd |D }|s|s|rt|trddl
m} d	| }||d
|| td|| |r|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 r   )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_setsr4   addr   r&   _check_for_additional_knowledge)r   r'   name_contextr(   Zanalysis_errorsr*   Zstring_nameZfound_predefined_typesr0   Z	name_dicttypesr.   checkvaluesr4   messager   r   r   py__getattribute__&   sN   



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} 	 t|dd}|||||}|d ur8|S ||v r?	 tS q%tS )Nc                 s   s    | ]}|j d v V  qdS )r,   r-   N)r8   )r1   br   r   r   r3   a   s    zBAbstractContext._check_for_additional_knowledge.<locals>.<genexpr>r   )check_flow_informationT)Zinclude_flows)	r%   r   is_instancer:   anyr   Zjedi.inference.finderrE   r   )r   r'   r=   r(   
flow_scopeZ
base_nodesrE   nr   r   r   r<   Z   s&   z/AbstractContext._check_for_additional_knowledgec                 C   s   | j }|d u r	| S | S r   )parent_contextget_root_context)r   rJ   r   r   r   rK   n   s   z AbstractContext.get_root_contextc                 C      dS NFr   r   r   r   r   	is_modulet      zAbstractContext.is_modulec                 C   rL   rM   r   rN   r   r   r   is_builtins_modulew   rP   z"AbstractContext.is_builtins_modulec                 C   rL   rM   r   rN   r   r   r   is_classz   rP   zAbstractContext.is_classc                 C   rL   rM   r   rN   r   r   r   is_stub}   rP   zAbstractContext.is_stubc                 C   rL   rM   r   rN   r   r   r   rF      rP   zAbstractContext.is_instancec                 C   rL   rM   r   rN   r   r   r   is_compiled   rP   zAbstractContext.is_compiledc                 C   rL   rM   r   rN   r   r   r   is_bound_method   rP   zAbstractContext.is_bound_methodc                 C   r   r   r   rN   r   r   r   
py__name__   r!   zAbstractContext.py__name__c                 C   r   r   r   rN   r   r   r   	get_value   rP   zAbstractContext.get_valuec                 C      d S r   r   rN   r   r   r   r2      r!   zAbstractContext.namec                 C   rL   )Nr   r   rN   r   r   r   get_qualified_names   rP   z#AbstractContext.get_qualified_namesc                 C   rL   )N r   rN   r   r   r   	py__doc__   rP   zAbstractContext.py__doc__c                 c   s,    | j }|||< z	d V  W ||= d S ||= w r   )r   )r   rH   dctZ
predefinedr   r   r   predefine_names   s   zAbstractContext.predefine_namesNN)NNT)__name__
__module____qualname__r   r   r    r+   rB   r<   rK   rO   rQ   rR   rS   rF   rT   rU   rV   rW   propertyr2   rY   r[   r   r]   r   r   r   r   r      s4    	
4

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 r   )superr   r   _value)r   r5   	__class__r   r   r      s   
zValueContext.__init__c                 C      | j jS r   )re   r:   rN   r   r   r   r:         zValueContext.tree_nodec                 C   rh   r   )re   rJ   rN   r   r   r   rJ      ri   zValueContext.parent_contextc                 C   
   | j  S r   )re   rO   rN   r   r   r   rO         
zValueContext.is_modulec                 C   s   | j | jjkS r   )re   r   builtins_modulerN   r   r   r   rQ      s   zValueContext.is_builtins_modulec                 C   rj   r   )re   rR   rN   r   r   r   rR      rk   zValueContext.is_classc                 C   rj   r   )re   rS   rN   r   r   r   rS      rk   zValueContext.is_stubc                 C   rj   r   )re   rF   rN   r   r   r   rF      rk   zValueContext.is_instancec                 C   rj   r   )re   rT   rN   r   r   r   rT      rk   zValueContext.is_compiledc                 C   rj   r   )re   rU   rN   r   r   r   rU      rk   zValueContext.is_bound_methodc                 C   rj   r   )re   rV   rN   r   r   r   rV      rk   zValueContext.py__name__c                 C   rh   r   )re   r2   rN   r   r   r   r2      ri   zValueContext.namec                 C   rj   r   )re   rY   rN   r   r   r   rY      rk   z ValueContext.get_qualified_namesc                 C   rj   r   )re   r[   rN   r   r   r   r[      rk   zValueContext.py__doc__c                 C      | j S r   re   rN   r   r   r   rW         zValueContext.get_valuec                 C      d| j j| jf S Nz%s(%s))rg   r_   re   rN   r   r   r   __repr__      zValueContext.__repr__)r_   r`   ra   __doc__r   rb   r:   rJ   rO   rQ   rR   rS   rF   rT   rU   rV   r2   rY   r[   rW   rr   __classcell__r   r   rf   r   rc      s*    


rc   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_treerw   )r   r0   rw   r   r   r   rw      s   
zTreeContextMixin.infer_nodec                 C   s   ddl m} || jkr|  sJ |  S | |}|jdv rF|j||}|	 rD|j
|j}|| j|j
|}|j|| |d}|S |jdkrS|| j||S td| )Nr   )r5   )funcdeflambdef)instanceZclass_contextfunctionclassdefzProbably shouldn't happen: %s)r#   r5   r:   rO   rW   create_contextr8   ZFunctionValueZfrom_contextrR   rJ   create_valueZAnonymousInstancer   ZBoundMethod
as_contextZ
ClassValuer   )r   r0   r5   rJ   funcZclass_valuerz   r   r   r   r~      s*   




zTreeContextMixin.create_valuec                    sz   d
 fdd	 dd }|j dv r7|j|jd }j|jk r7j}|j dkr3|jks7|} |dd	S )NTc                    sj   | j krS | jdv r|  S | jdv r/ | j}j| jd jkr*|S t|| S td|  )N)rx   ry   r|   rC   z(There's a scope that was not managed: %s)	r:   r8   r~   r   r7   	start_poschildrenCompForContext	Exception)
scope_node	is_nestedrJ   from_scope_noder0   parent_scoper   r   r   r      s   



z8TreeContextMixin.create_context.<locals>.from_scope_nodec                 S   sn   	 | j } t| r| S | jdv r| jd jdv r| jd S n| jdkr6| jdd D ]}|jdv r5|  S q*q)NT)argumentZtestlist_comp   rC   Zdictorsetmaker   )r7   r   r6   r8   r   )r0   rI   r   r   r   r   
  s   




z5TreeContextMixin.create_context.<locals>.parent_scope)rx   r|   :param)r   )T)r8   r   indexr   r7   r2   )r   r0   r   colonr7   r   r   r   r}      s   
zTreeContextMixin.create_contextc                 C   sT   |  }|r |jdkr |j|kr t|dd}| |}t||S | |}t||S )Nr   rx   ry   )Zget_definitionr8   r2   r   r~   r   r}   r   )r   Z	tree_nameZ
definitionrx   r   r/   r   r   r   create_name"  s   



zTreeContextMixin.create_nameN)r_   r`   ra   rw   r~   r}   r   r   r   r   r   rv      s
    &rv   c                   @      e Zd ZdddZdS )FunctionContextNc                 c   s    t | j| ||dV  d S N)rJ   r   r   )r   r   r   r   r   r   r    .  s   zFunctionContext.get_filtersr^   r_   r`   ra   r    r   r   r   r   r   -      r   c                   @   sP   e Zd Zdee f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   rj   r   re   
py__file__rN   r   r   r   r   8  rk   zModuleContext.py__file__Nc                 c   s@    | j |}t|d  tt| ||d|  V  |E d H  d S r   )re   r    nextr	   r   get_global_filter)r   r   r   r)   r   r   r   r    ;  s   
zModuleContext.get_filtersc                 C   s   t | S r   )r
   rN   r   r   r   r   I  s   zModuleContext.get_global_filterc                 C   rh   r   re   string_namesrN   r   r   r   r   L  ri   zModuleContext.string_namesc                 C   rh   r   )re   
code_linesrN   r   r   r   r   P  ri   zModuleContext.code_linesc                 C   rm   )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.
        rn   rN   r   r   r   rW   T  s   zModuleContext.get_valuer^   )r_   r`   ra   r   r   r   r    r   rb   r   r   rW   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dee fd	d
Z	dS )NamespaceContextNc                 C   rj   r   re   r    r   r   r   r   r    ^  rk   zNamespaceContext.get_filtersc                 C   rm   r   rn   rN   r   r   r   rW   a  ro   zNamespaceContext.get_valuec                 C   rh   r   r   rN   r   r   r   r   d  ri   zNamespaceContext.string_namesr   c                 C   rj   r   r   rN   r   r   r   r   h  rk   zNamespaceContext.py__file__r^   )
r_   r`   ra   r    rW   rb   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 r   )r   r   r   r   r   r    m  s   zClassContext.get_filtersc                 C   s   t | ||dS r   r   r   r   r   r   r   p  s
   zClassContext.get_global_filterr^   )r_   r`   ra   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 )r   c                    s   t  |j || _|| _d S r   )rd   r   r   r:   rJ   )r   rJ   r,   rf   r   r   r   y  s   
zCompForContext.__init__Nc                 c   s    t | V  d S r   r   r   r   r   r   r    ~  s   zCompForContext.get_filtersc                 C   rX   r   r   rN   r   r   r   rW     rP   zCompForContext.get_valuec                 C   rL   )Nz<comprehension context>r   rN   r   r   r   rV     rP   zCompForContext.py__name__c                 C   rp   rq   )rg   r_   r:   rN   r   r   r   rr     rs   zCompForContext.__repr__r^   )	r_   r`   ra   r   r    rW   rV   rr   ru   r   r   rf   r   r   x  s    
r   c                   @   r   )CompiledContextNc                 C   rj   r   r   r   r   r   r   r      rk   zCompiledContext.get_filtersr^   r   r   r   r   r   r     r   r   c                   @   s6   e Zd ZdZdd Zedd Zdee fddZ	dS )	CompiledModuleContextNc                 C   rm   r   rn   rN   r   r   r   rW     ro   zCompiledModuleContext.get_valuec                 C   rh   r   r   rN   r   r   r   r     ri   z"CompiledModuleContext.string_namesr   c                 C   rj   r   r   rN   r   r   r   r     rk   z CompiledModuleContext.py__file__)
r_   r`   ra   r   rW   rb   r   r   r   r   r   r   r   r   r     s    
r   c                 C   s   |d ur:t |ddd}d }|dkr|}t |dd}|d ur:|jd }|d ur:||jk r:|d u s7||jd jk r:|j}t| ||S )Nrx   r|   ry   )r   r   r   get_global_filters)r/   Zname_or_noner(   Zancestorry   r   r   r   r   r$     s   
r$   c                 c   sv    | }ddl m} | dur'| j||dE dH  t| |tfr d}| j} | dust|jj d}|dus6J |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   rJ   r   r   rl   )r/   r   r   Zbase_contextr   rD   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   rc   rv   r   r   r   r   r   r   r   r$   r   r   r   r   r   <module>   s2     9P
&