o
    ï›h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   úLc:\Users\shubh\Videos\agent\nova\Lib\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›|r›t|tƒr›d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)Z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
&