a
    lc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   R/var/www/html/django/DPS/env/lib/python3.9/site-packages/jedi/inference/context.py__init__   s    zAbstractContext.__init__Nc                 C   s   t d S r   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   sX  |du r| }|  ||}t|tr(|jn|}d}| jrt|tr|}|durt|s|j}|jdv rDz| j| }	|	| }
W n t	y   Y qDY qD0 |
}qqD|dur|rddl
m} |j| | j|d}||ju rt}q|}ntdd |D }|s2|s2|r2t|tr2ddl
m} d	| }||d
|| td|| |rJ|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       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_setsr3   addr   r$   _check_for_additional_knowledge)r   r%   name_contextr&   Zanalysis_errorsr(   Zstring_nameZfound_predefined_typesr.   Z	name_dicttypesr,   checkvaluesr3   messager   r   r   py__getattribute__&   sJ    



z"AbstractContext.py__getattribute__c                 C   s~   |p| }t |trz| sz|}|jg}tdd |D r<tS ddlm} t|dd}|||||}|d urn|S ||v rHqzqHtS )Nc                 s   s   | ]}|j d v V  qdS )r*   r+   N)r7   )r/   br   r   r   r1   a   r2   zBAbstractContext._check_for_additional_knowledge.<locals>.<genexpr>r   )check_flow_informationT)Zinclude_flows)	r#   r   is_instancer9   anyr   Zjedi.inference.finderrD   r   )r   r%   r<   r&   
flow_scopeZ
base_nodesrD   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   rI   r   r   r   rJ   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 rK   r   rL   r   r   r   is_builtins_modulew   s    z"AbstractContext.is_builtins_modulec                 C   s   dS rK   r   rL   r   r   r   is_classz   s    zAbstractContext.is_classc                 C   s   dS rK   r   rL   r   r   r   is_stub}   s    zAbstractContext.is_stubc                 C   s   dS rK   r   rL   r   r   r   rE      s    zAbstractContext.is_instancec                 C   s   dS rK   r   rL   r   r   r   is_compiled   s    zAbstractContext.is_compiledc                 C   s   dS rK   r   rL   r   r   r   is_bound_method   s    zAbstractContext.is_bound_methodc                 C   s   t d S r   r   rL   r   r   r   
py__name__   s    zAbstractContext.py__name__c                 C   s   t d S r   r   rL   r   r   r   	get_value   s    zAbstractContext.get_valuec                 C   s   d S r   r   rL   r   r   r   r0      s    zAbstractContext.namec                 C   s   dS )Nr   r   rL   r   r   r   get_qualified_names   s    z#AbstractContext.get_qualified_namesc                 C   s   dS )N r   rL   r   r   r   	py__doc__   s    zAbstractContext.py__doc__c                 c   s,   | j }|||< zd V  W ||= n||= 0 d S r   )r   )r   rG   dctZ
predefinedr   r   r   predefine_names   s
    zAbstractContext.predefine_names)NN)NNT)__name__
__module____qualname__r   r   r   r)   rA   r;   rJ   rM   rN   rO   rP   rE   rQ   rR   rS   rT   propertyr0   rU   rW   r   rY   r   r   r   r   r      s2   	  
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   r4   	__class__r   r   r      s    zValueContext.__init__c                 C   s   | j jS r   )r`   r9   rL   r   r   r   r9      s    zValueContext.tree_nodec                 C   s   | j jS r   )r`   rI   rL   r   r   r   rI      s    zValueContext.parent_contextc                 C   s
   | j  S r   )r`   rM   rL   r   r   r   rM      s    zValueContext.is_modulec                 C   s   | j | jjkS r   )r`   r   builtins_modulerL   r   r   r   rN      s    zValueContext.is_builtins_modulec                 C   s
   | j  S r   )r`   rO   rL   r   r   r   rO      s    zValueContext.is_classc                 C   s
   | j  S r   )r`   rP   rL   r   r   r   rP      s    zValueContext.is_stubc                 C   s
   | j  S r   )r`   rE   rL   r   r   r   rE      s    zValueContext.is_instancec                 C   s
   | j  S r   )r`   rQ   rL   r   r   r   rQ      s    zValueContext.is_compiledc                 C   s
   | j  S r   )r`   rR   rL   r   r   r   rR      s    zValueContext.is_bound_methodc                 C   s
   | j  S r   )r`   rS   rL   r   r   r   rS      s    zValueContext.py__name__c                 C   s   | j jS r   )r`   r0   rL   r   r   r   r0      s    zValueContext.namec                 C   s
   | j  S r   )r`   rU   rL   r   r   r   rU      s    z ValueContext.get_qualified_namesc                 C   s
   | j  S r   )r`   rW   rL   r   r   r   rW      s    zValueContext.py__doc__c                 C   s   | j S r   r`   rL   r   r   r   rT      s    zValueContext.get_valuec                 C   s   d| j j| jf S Nz%s(%s))rb   rZ   r`   rL   r   r   r   __repr__   s    zValueContext.__repr__)rZ   r[   r\   __doc__r   r]   r9   rI   rM   rN   rO   rP   rE   rQ   rR   rS   r0   rU   rW   rT   rf   __classcell__r   r   ra   r   r^      s(   


r^   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_treerj   )r   r.   rj   r   r   r   rj      s    zTreeContextMixin.infer_nodec                 C   s   ddl m} || jkr*|  s"J |  S | |}|jdv r|j||}|	 r|j
|j}|| j|j
|}|j|| |d}|S |jdkr|| j||S td| d S )Nr   )r4   )funcdeflambdef)instanceZclass_contextfunctionclassdefzProbably shouldn't happen: %s)r!   r4   r9   rM   rT   create_contextr7   ZFunctionValueZfrom_contextrO   rI   create_valueZAnonymousInstancer   ZBoundMethod
as_contextZ
ClassValuer   )r   r.   r4   rI   funcZclass_valuerm   r   r   r   rq      s*    




zTreeContextMixin.create_valuec                    sz   d
 fdd	 dd }|j dv 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v r&|  S | jdv r^ | j}j| jd jkrT|S t|| S td|  d S )N)rk   rl   ro   rB   z(There's a scope that was not managed: %s)	r9   r7   rq   rr   r6   	start_poschildrenCompForContext	Exception)
scope_node	is_nestedrI   from_scope_noder.   parent_scoper   r   r   r|      s    



z8TreeContextMixin.create_context.<locals>.from_scope_nodec                 S   sp   | j } t| r| S | jdv r:| jd jdv rj| jd S q | jdkr | jdd D ]}|jdv rR|  S qRq d S )N)argumentZtestlist_comp   rB   Zdictorsetmaker   )r6   r   r5   r7   rv   )r.   rH   r   r   r   r}   
  s    



z5TreeContextMixin.create_context.<locals>.parent_scope)rk   ro   :param)rz   )T)r7   rv   indexru   r6   r0   )r   r.   ry   colonr6   r   r{   r   rp      s    
zTreeContextMixin.create_contextc                 C   sX   |  }|r@|jdkr@|j|kr@t|dd}| |}t||S | |}t||S d S )Nr   rk   rl   )Zget_definitionr7   r0   r   rq   r   rp   r   )r   Z	tree_nameZ
definitionrk   rs   r-   r   r   r   create_name"  s    


zTreeContextMixin.create_nameN)rZ   r[   r\   rj   rq   rp   r   r   r   r   r   ri      s   &ri   c                   @   s   e Zd ZdddZdS )FunctionContextNc                 c   s   t | j| ||dV  d S N)rI   r   r   )r   r   r   r   r   r   r   .  s    zFunctionContext.get_filters)NNrZ   r[   r\   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 r   r`   
py__file__rL   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 r   )r`   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
   rL   r   r   r   r   I  s    zModuleContext.get_global_filterc                 C   s   | j jS r   r`   string_namesrL   r   r   r   r   L  s    zModuleContext.string_namesc                 C   s   | j jS r   )r`   
code_linesrL   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.
        rd   rL   r   r   r   rT   T  s    zModuleContext.get_value)NN)rZ   r[   r\   r   r   r   r   r   r]   r   r   rT   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 r   r`   r   r   r   r   r   r   ^  s    zNamespaceContext.get_filtersc                 C   s   | j S r   rd   rL   r   r   r   rT   a  s    zNamespaceContext.get_valuec                 C   s   | j jS r   r   rL   r   r   r   r   d  s    zNamespaceContext.string_namesr   c                 C   s
   | j  S r   r   rL   r   r   r   r   h  s    zNamespaceContext.py__file__)NN)
rZ   r[   r\   r   rT   r]   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_filter)NN)NN)rZ   r[   r\   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 )rw   c                    s   t  |j || _|| _d S r   )r_   r   r   r9   rI   )r   rI   r*   ra   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   s   d S r   r   rL   r   r   r   rT     s    zCompForContext.get_valuec                 C   s   dS )Nz<comprehension context>r   rL   r   r   r   rS     s    zCompForContext.py__name__c                 C   s   d| j j| jf S re   )rb   rZ   r9   rL   r   r   r   rf     s    zCompForContext.__repr__)NN)	rZ   r[   r\   r   r   rT   rS   rf   rh   r   r   ra   r   rw   x  s
   
rw   c                   @   s   e Zd ZdddZdS )CompiledContextNc                 C   s
   | j  S r   r   r   r   r   r   r     s    zCompiledContext.get_filters)NNr   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 r   rd   rL   r   r   r   rT     s    zCompiledModuleContext.get_valuec                 C   s   | j jS r   r   rL   r   r   r   r     s    z"CompiledModuleContext.string_namesr   c                 C   s
   | j  S r   r   rL   r   r   r   r     s    z CompiledModuleContext.py__file__)
rZ   r[   r\   r   rT   r]   r   r   r   r   r   r   r   r   r     s
   
r   c                 C   s   |d urtt |ddd}d }|dkr2|}t |dd}|d urt|jd }|d urt||jk rt|d u sn||jd jk rt|j}t| ||S )Nrk   ro   rl   )r   rv   ru   get_global_filters)r-   Zname_or_noner&   Zancestorrl   r   r   r   r   r"     s    
r"   c                 c   sn   | }ddl m} | durF| j||dE dH  t| |tfr>d}| j} qt|jj d}|dusdJ |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   rI   r   r   rc   )r-   r   r   Zbase_contextr   rC   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   r^   ri   r   r   r   r   rw   r   r   r"   r   r   r   r   r   <module>   s0    9P
&