o
    ˷e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/ideatree/venv/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   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)if_stmtfor_stmtcomp_forsync_comp_forr   )flow_analysis)contextvalue_scopenodec                 s   s    | ]}|  V  qd S r   )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#   r1   reachability_check	tree_nodeUNREACHABLEr   r   	from_setsr9   addr   r'   _check_for_additional_knowledge)r   r(   name_contextr)   analysis_errorsr+   string_namefound_predefined_typesr4   	name_dicttypesr1   checkvaluesr9   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/   r0   N)r=   )r6   br   r   r   r8   a   s    zBAbstractContext._check_for_additional_knowledge.<locals>.<genexpr>r   )check_flow_informationT)include_flows)	r%   r   is_instancer@   anyr   jedi.inference.finderrQ   r   )r   r(   rE   r)   
flow_scope
base_nodesrQ   nr   r   r   rD   Z   s&   z/AbstractContext._check_for_additional_knowledgec                 C   s   | j }|d u r	| S | S r   )parent_contextget_root_context)r   rY   r   r   r   rZ   n   s   z AbstractContext.get_root_contextc                 C      dS NFr   r   r   r   r   	is_modulet      zAbstractContext.is_modulec                 C   r[   r\   r   r]   r   r   r   is_builtins_modulew   r_   z"AbstractContext.is_builtins_modulec                 C   r[   r\   r   r]   r   r   r   is_classz   r_   zAbstractContext.is_classc                 C   r[   r\   r   r]   r   r   r   is_stub}   r_   zAbstractContext.is_stubc                 C   r[   r\   r   r]   r   r   r   rS      r_   zAbstractContext.is_instancec                 C   r[   r\   r   r]   r   r   r   is_compiled   r_   zAbstractContext.is_compiledc                 C   r[   r\   r   r]   r   r   r   is_bound_method   r_   zAbstractContext.is_bound_methodc                 C   r   r   r   r]   r   r   r   
py__name__   r!   zAbstractContext.py__name__c                 C   r   r   r   r]   r   r   r   	get_value   r_   zAbstractContext.get_valuec                 C      d S r   r   r]   r   r   r   r7      r!   zAbstractContext.namec                 C   r[   )Nr   r   r]   r   r   r   get_qualified_names   r_   z#AbstractContext.get_qualified_namesc                 C   r[   )N r   r]   r   r   r   	py__doc__   r_   zAbstractContext.py__doc__c                 c   s,    | j }|||< z	d V  W ||= d S ||= w r   )r   )r   rV   dct
predefinedr   r   r   predefine_names   s   zAbstractContext.predefine_namesNN)NNT)__name__
__module____qualname__r   r   r    r,   rN   rD   rZ   r^   r`   ra   rb   rS   rc   rd   re   rf   propertyr7   rh   rj   r   rm   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   r:   	__class__r   r   r      s   
zValueContext.__init__c                 C      | j jS r   )ru   r@   r]   r   r   r   r@         zValueContext.tree_nodec                 C   rx   r   )ru   rY   r]   r   r   r   rY      ry   zValueContext.parent_contextc                 C   
   | j  S r   )ru   r^   r]   r   r   r   r^         
zValueContext.is_modulec                 C   s   | j | jjkS r   )ru   r   builtins_moduler]   r   r   r   r`      s   zValueContext.is_builtins_modulec                 C   rz   r   )ru   ra   r]   r   r   r   ra      r{   zValueContext.is_classc                 C   rz   r   )ru   rb   r]   r   r   r   rb      r{   zValueContext.is_stubc                 C   rz   r   )ru   rS   r]   r   r   r   rS      r{   zValueContext.is_instancec                 C   rz   r   )ru   rc   r]   r   r   r   rc      r{   zValueContext.is_compiledc                 C   rz   r   )ru   rd   r]   r   r   r   rd      r{   zValueContext.is_bound_methodc                 C   rz   r   )ru   re   r]   r   r   r   re      r{   zValueContext.py__name__c                 C   rx   r   )ru   r7   r]   r   r   r   r7      ry   zValueContext.namec                 C   rz   r   )ru   rh   r]   r   r   r   rh      r{   z ValueContext.get_qualified_namesc                 C   rz   r   )ru   rj   r]   r   r   r   rj      r{   zValueContext.py__doc__c                 C      | j S r   ru   r]   r   r   r   rf         zValueContext.get_valuec                 C      d| j j| jf S Nz%s(%s))rw   ro   ru   r]   r   r   r   __repr__      zValueContext.__repr__)ro   rp   rq   __doc__r   rr   r@   rY   r^   r`   ra   rb   rS   rc   rd   re   r7   rh   rj   rf   r   __classcell__r   r   rv   r   rs      s*    


rs   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)jedi.inference.syntax_treer   )r   r4   r   r   r   r   r      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   )r:   )funcdeflambdef)instanceclass_contextfunctionclassdefzProbably shouldn't happen: %s)r#   r:   r@   r^   rf   create_contextr=   FunctionValuefrom_contextra   rY   create_valueAnonymousInstancer   BoundMethod
as_context
ClassValuer   )r   r4   r:   rY   funcclass_valuer   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)r   r   r   rO   z(There's a scope that was not managed: %s)	r@   r=   r   r   r<   	start_poschildrenCompForContext	Exception)
scope_node	is_nestedrY   from_scope_noder4   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)argumenttestlist_comp   rO   dictorsetmaker   )r<   r   r;   r=   r   )r4   rX   r   r   r   r   
  s   




z5TreeContextMixin.create_context.<locals>.parent_scope)r   r   :param)r   )T)r=   r   indexr   r<   r7   )r   r4   r   colonr<   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   r   r   )get_definitionr=   r7   r   r   r   r   r   )r   	tree_name
definitionr   r   r2   r   r   r   create_name"  s   



zTreeContextMixin.create_nameN)ro   rp   rq   r   r   r   r   r   r   r   r   r      s
    &r   c                   @      e Zd ZdddZdS )FunctionContextNc                 c   s    t | j| ||dV  d S N)rY   r   r   )r   r   r   r   r   r   r    .  s   zFunctionContext.get_filtersrn   ro   rp   rq   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   rz   r   ru   
py__file__r]   r   r   r   r   8  r{   zModuleContext.py__file__Nc                 c   s@    | j |}t|d  tt| ||d|  V  |E d H  d S r   )ru   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
   r]   r   r   r   r   I  s   zModuleContext.get_global_filterc                 C   rx   r   ru   string_namesr]   r   r   r   r   L  ry   zModuleContext.string_namesc                 C   rx   r   )ru   
code_linesr]   r   r   r   r   P  ry   zModuleContext.code_linesc                 C   r}   )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.
        r~   r]   r   r   r   rf   T  s   zModuleContext.get_valuern   )ro   rp   rq   r   r   r   r    r   rr   r   r   rf   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   rz   r   ru   r    r   r   r   r   r    ^  r{   zNamespaceContext.get_filtersc                 C   r}   r   r~   r]   r   r   r   rf   a  r   zNamespaceContext.get_valuec                 C   rx   r   r   r]   r   r   r   r   d  ry   zNamespaceContext.string_namesr   c                 C   rz   r   r   r]   r   r   r   r   h  r{   zNamespaceContext.py__file__rn   )
ro   rp   rq   r    rf   rr   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_filterrn   )ro   rp   rq   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   )rt   r   r   r@   rY   )r   rY   r/   rv   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   rg   r   r   r]   r   r   r   rf     r_   zCompForContext.get_valuec                 C   r[   )Nz<comprehension context>r   r]   r   r   r   re     r_   zCompForContext.py__name__c                 C   r   r   )rw   ro   r@   r]   r   r   r   r     r   zCompForContext.__repr__rn   )	ro   rp   rq   r   r    rf   re   r   r   r   r   rv   r   r   x  s    
r   c                   @   r   )CompiledContextNc                 C   rz   r   r   r   r   r   r   r      r{   zCompiledContext.get_filtersrn   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   r}   r   r~   r]   r   r   r   rf     r   zCompiledModuleContext.get_valuec                 C   rx   r   r   r]   r   r   r   r     ry   z"CompiledModuleContext.string_namesr   c                 C   rz   r   r   r]   r   r   r   r     r{   z CompiledModuleContext.py__file__)
ro   rp   rq   r   rf   rr   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 )Nr   r   r   )r   r   r   get_global_filters)r2   name_or_noner)   ancestorr   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   )	jedi.inference.value.functionr   r    r%   r   rY   r   r   r|   )r2   r   r   base_contextr   rP   r   r   r   r     s   /
r   N)'abcr   
contextlibr   pathlibr   typingr   
parso.treer   parso.python.treer   jedi.inference.filtersr   r	   r
   jedi.inference.namesr   r   jedi.inference.base_valuer   r   jedi.parser_utilsr   jedir   r   r   rs   r   r   r   r   r   r   r   r   r$   r   r   r   r   r   <module>   s2     9P
&