
    jmC                        d dl mZ d dlmZ d dlmZ d dlmZ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          Z G d de          Z G d d          Z G d dee          Z G d dee          Z G d dee          Z G d dee          Z  G d dee          Z! G d de          Z" G d de"          Z#d  Z$d! Z%d"S )#    )abstractmethod)contextmanager)Path)OptionalAny)Name)ParserTreeFilterMergedFilterGlobalNameFilter)AnonymousParamNameTreeNameDefinition)	NO_VALUESValueSet)get_parent_scope)debug)parser_utilsc                       e Zd ZU eed<   eed<   d Zedd            Zd Z	 	 ddZ	d	 Z
d
 Zd Zd Zd Zd Zd Zd Zd Zed             Zd Zed             Zd Zd Zed             ZdS )AbstractContext	tree_nodeparent_contextc                 "    || _         i | _        d S N)inference_statepredefined_names)selfr   s     ]/home/ubuntu/.hermes/hermes-agent/venv/lib/python3.11/site-packages/jedi/inference/context.py__init__zAbstractContext.__init__   s    . "    Nc                     t           r   NotImplementedErrorr   until_positionorigin_scopes      r   get_filterszAbstractContext.get_filters       !!r   c                     ddl m} t          | t          |t                    r|nd |          }|                    ||          }t          j        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namess         r   gotozAbstractContext.goto   sp    )))))).K!>!>H++D(
 
 ""7K88	/dEJJJr   Tc                    || }|                      ||          }t          |t                    r|j        n|}d}| j        r|t          |t                    rg|}|ct          j        |          sO|j        }|j        dv r)	 | j        |         }	|	|         }
|
}n&# t          $ r Y Kw xY w|t          j        |          O|9|r7ddl
m} |                    | | j        |          }||j        u rt          }n!|}nt!          j        d |D                       }|s<|s:|r8t          |t                    r#ddl
m} d|z  }|                    |d	||           t)          j        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              3   >   K   | ]}|                                 V  d S r   )infer).0names     r   	<genexpr>z5AbstractContext.py__getattribute__.<locals>.<genexpr>N   s*      'G'G

'G'G'G'G'G'Gr   )analysisz$NameError: name '%s' is not defined.z
name-errorz context.names_to_types: %s -> %s)r2   r+   r   valuer   r   is_scopeparenttypeKeyErrorr)   r8   reachability_checkr   UNREACHABLEr   r   	from_setsrA   addr   r-   _check_for_additional_knowledge)r   r.   name_contextr/   analysis_errorsr1   string_namefound_predefined_typesr;   	name_dicttypesr8   checkvaluesrA   messages                   r   py__getattribute__z"AbstractContext.py__getattribute__'   s   
 L		+x00+5k4+H+HYk''k "&  	ZT%B%B 	D"<+@+F+F"{9 TTT$($9$$?	 )+ 6 27.	 $ ! ! ! ! "<+@+F+F" "-%-444444!44 N  5  E
 111"/''G'G'G'G'GGGF 	OV 	O 	O+t,, O333333AKO\<gNNN	4eVDDD 	M33KxXXXs    B 
B%$B%c                    |p| }t          |t                    rn|                                sZ|}|j        g}t	          d |D                       rt
          S ddlm} 	 t          |d          } |||||          }||S ||v rn)t
          S )Nc              3   (   K   | ]}|j         d v V  dS )r6   r7   N)rE   )r>   bs     r   r@   zBAbstractContext._check_for_additional_knowledge.<locals>.<genexpr>b   s*      OOq16::OOOOOOr   r   )check_flow_informationT)include_flows)	r+   r   is_instancer   anyr   jedi.inference.finderrZ   r   )r   r.   rL   r/   
flow_scope
base_nodesrZ   ns           r   rK   z/AbstractContext._check_for_additional_knowledge[   s    #+tk4(( 	1I1I1K1K 	$J&01JOOJOOOOO !  DDDDDD-jMMM
**<+6B B=H++ r   c                 @    | j         }|| S |                                S r   )r   get_root_context)r   r   s     r   rc   z AbstractContext.get_root_contexto   s'    ,!K..000r   c                     dS NF r   s    r   	is_modulezAbstractContext.is_moduleu       ur   c                     dS re   rf   rg   s    r   is_builtins_modulez"AbstractContext.is_builtins_modulex   ri   r   c                     dS re   rf   rg   s    r   is_classzAbstractContext.is_class{   ri   r   c                     dS re   rf   rg   s    r   is_stubzAbstractContext.is_stub~   ri   r   c                     dS re   rf   rg   s    r   r\   zAbstractContext.is_instance   ri   r   c                     dS re   rf   rg   s    r   is_compiledzAbstractContext.is_compiled   ri   r   c                     dS re   rf   rg   s    r   is_bound_methodzAbstractContext.is_bound_method   ri   r   c                     t           r   r    rg   s    r   
py__name__zAbstractContext.py__name__   r&   r   c                     t           r   r    rg   s    r   	get_valuezAbstractContext.get_value   s    !!r   c                     d S r   rf   rg   s    r   r?   zAbstractContext.name   s    tr   c                     dS )Nrf   rf   rg   s    r   get_qualified_namesz#AbstractContext.get_qualified_names       rr   c                     dS )N rf   rg   s    r   	py__doc__zAbstractContext.py__doc__   r|   r   c              #   B   K   | j         }|||<   	 d V  ||= d S # ||= w xY wr   )r   )r   r_   dct
predefineds       r   predefine_nameszAbstractContext.predefine_names   sH      *
!$
:	'EEE:&&&
:&&&&&s    NN)NNT)__name__
__module____qualname__r   __annotations__r   r   r%   r2   rU   rK   rc   rh   rk   rm   ro   r\   rr   rt   rv   rx   propertyr?   r{   r   r   r   rf   r   r   r   r      s        NNN# # # " " " ^"   KO+/2Y 2Y 2Y 2Yh  (1 1 1               " " ^"" " "   X     ' ' ^' ' 'r   r   c                        e Zd ZdZ fdZed             Zed             Zd Zd Z	d Z
d Zd	 Zd
 Zd Zd Zed             Zd Zd Zd Zd Z xZS )ValueContextzC
    Should be defined, otherwise the API returns empty types.
    c                 b    t                                          |j                   || _        d S r   )superr   r   _value)r   rB   	__class__s     r   r   zValueContext.__init__   s)    .///r   c                     | j         j        S r   )r   r   rg   s    r   r   zValueContext.tree_node   s    {$$r   c                     | j         j        S r   )r   r   rg   s    r   r   zValueContext.parent_context   s    {))r   c                 4    | j                                         S r   )r   rh   rg   s    r   rh   zValueContext.is_module       {$$&&&r   c                 ,    | j         | j        j        k    S r   )r   r   builtins_modulerg   s    r   rk   zValueContext.is_builtins_module   s    {d2BBBr   c                 4    | j                                         S r   )r   rm   rg   s    r   rm   zValueContext.is_class   s    {##%%%r   c                 4    | j                                         S r   )r   ro   rg   s    r   ro   zValueContext.is_stub   s    {""$$$r   c                 4    | j                                         S r   )r   r\   rg   s    r   r\   zValueContext.is_instance       {&&(((r   c                 4    | j                                         S r   )r   rr   rg   s    r   rr   zValueContext.is_compiled   r   r   c                 4    | j                                         S r   )r   rt   rg   s    r   rt   zValueContext.is_bound_method   s    {**,,,r   c                 4    | j                                         S r   )r   rv   rg   s    r   rv   zValueContext.py__name__       {%%'''r   c                     | j         j        S r   )r   r?   rg   s    r   r?   zValueContext.name   s    {r   c                 4    | j                                         S r   )r   r{   rg   s    r   r{   z ValueContext.get_qualified_names   s    {..000r   c                 4    | j                                         S r   )r   r   rg   s    r   r   zValueContext.py__doc__   r   r   c                     | j         S r   r   rg   s    r   rx   zValueContext.get_value   
    {r   c                 0    | j         j        d| j        dS N())r   r   r   rg   s    r   __repr__zValueContext.__repr__   s    >222DKKK@@r   )r   r   r   __doc__r   r   r   r   rh   rk   rm   ro   r\   rr   rt   rv   r?   r{   r   rx   r   __classcell__r   s   @r   r   r      sQ             % % X% * * X*' ' 'C C C& & &% % %) ) )) ) )- - -( ( (     X 1 1 1' ' '  A A A A A A Ar   r   c                   d    e Zd ZU eed<   eed<   eed<   eed<   eed<   eed<   d Zd Zd	 Zd
 ZdS )TreeContextMixinr   rh   rx   r   rm   r   c                 &    ddl m}  || |          S )Nr   )
infer_node)jedi.inference.syntax_treer   )r   r;   r   s      r   r   zTreeContextMixin.infer_node   s&    999999z$%%%r   c                 \   ddl m} || j        k    r*|                                 sJ |                                 S |                     |          }|j        dv r|j                            ||          }|	                                rj|j
                            |j                  }|                    | j        |j
        |          }|                    ||                                |          }|S |j        dk    r|                    | j        ||          S t#          d|z            )Nr   )rB   )funcdeflambdef)instanceclass_contextfunctionclassdefzProbably shouldn't happen: %s)r)   rB   r   rh   rx   create_contextrE   FunctionValuefrom_contextrm   r   create_valueAnonymousInstancer   BoundMethod
as_context
ClassValuer!   )r   r;   rB   r   funcclass_valuer   s          r   r   zTreeContextMixin.create_value   sC   ((((((4>!!>>#####>>###,,T229...&33NDIID&&(( ,;HHIabb 22(.*GV V((%"-"8"8":":! )  
 KY*$$##D$8.$OOO%&E&LMMMr   c                     d fd	d            }|j         dv r]|j        |j                            d                   }j        |j        k     r(j        }|j         dk    r|j        k    s |          } |d          S )	NTc                 @   | j         k    rS | j        dv r'                    |                                           S | j        dv rF  | j                            }j        | j        d         j        k    r|S t          ||           S t          d| z            )N)r   r   r   rX   z(There's a scope that was not managed: %s)	r   rE   r   r   rD   	start_poschildrenCompForContext	Exception)
scope_node	is_nestedr   from_scope_noder;   parent_scoper   s      r   r   z8TreeContextMixin.create_context.<locals>.from_scope_node  s    T^++"DDD((44??AAA$AAA!0j>O1P1P!Q!Q>Z%8%<%FFF))%njAAAFSTTTr   c                     	 | j         } t          j        |           r| S | j        dv r"| j        d         j        dv r| j        d         S n*| j        dk    r| j        dd         D ]}|j        dv r|c S s)NT)argumenttestlist_comp   rX   dictorsetmaker   )rD   r   rC   rE   r   )r;   ra   s     r   r   z5TreeContextMixin.create_context.<locals>.parent_scope  s    %{(.. 	%KY"???}Q',0MMM#}Q// NY"222!]1Q3/ % %6%BBB#$HHH C%r   )r   r   :param)r   )T)rE   r   indexr   rD   r?   )r   r;   r   colonrD   r   r   s   ``   @@r   r   zTreeContextMixin.create_context  s    	U 	U 	U 	U 	U 	U 	U 	U 	U	% 	% 	% "\$''
?555'
(;(A(A#(F(FGE~//w..6;$3F3F!-j!9!9JzT::::r   c                    |                                 }|rQ|j        dk    rF|j        |k    r;|                    dd          }|                     |          }t          ||          S |                     |          }t          ||          S )Nr   r   r   )get_definitionrE   r?   search_ancestorr   r   r   r   )r   	tree_name
definitionr   r   r9   s         r   create_namezTreeContextMixin.create_name*  s    --//
 	:*/W44I9U9U 00IFFG$$W--D%dI666)))44G%gy999r   N)	r   r   r   r   r   r   r   r   r   rf   r   r   r   r      s         NNNNNNNNNMMM& & &N N N4$; $; $;L: : : : :r   r   c                       e Zd ZddZdS )FunctionContextNc              #   2   K   t          | ||          V  d S N)r   r#   r$   r	   r"   s      r   r%   zFunctionContext.get_filters6  s:      )%
 
 
 	
 	
 	
 	
 	
r   r   r   r   r   r%   rf   r   r   r   r   5  s(        
 
 
 
 
 
r   r   c                   f    e Zd Zdee         fdZd	dZd Zed             Z	ed             Z
d ZdS )
ModuleContextreturnc                 4    | j                                         S r   r   
py__file__rg   s    r   r   zModuleContext.py__file__?  r   r   Nc              #      K   | j                             |          }t          |d            t          t	          | ||          |                                           V  |E d {V  d S r   )r   r%   nextr
   r	   get_global_filter)r   r#   r$   r0   s       r   r%   zModuleContext.get_filtersB  s      +)),77Wd#-)  
 ""$$
 
 	
 	
 	
 r   c                      t          |           S r   )r   rg   s    r   r   zModuleContext.get_global_filterP  s    %%%r   c                     | j         j        S r   r   string_namesrg   s    r   r   zModuleContext.string_namesS      {''r   c                     | j         j        S r   )r   
code_linesrg   s    r   r   zModuleContext.code_linesW  s    {%%r   c                     | 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.
        r   rg   s    r   rx   zModuleContext.get_value[  s     {r   r   )r   r   r   r   r   r   r%   r   r   r   r   rx   rf   r   r   r   r   >  s        (HTN ( ( ( (   & & & ( ( X( & & X&    r   r   c                   J    e Zd ZddZd Zed             Zdee         fdZ	dS )NamespaceContextNc                 4    | j                                         S r   r   r%   r"   s      r   r%   zNamespaceContext.get_filterse  r   r   c                     | j         S r   r   rg   s    r   rx   zNamespaceContext.get_valueh  r   r   c                     | j         j        S r   r   rg   s    r   r   zNamespaceContext.string_namesk  r   r   r   c                 4    | j                                         S r   r   rg   s    r   r   zNamespaceContext.py__file__o  r   r   r   )
r   r   r   r%   rx   r   r   r   r   r   rf   r   r   r   r   d  sq        ) ) ) )   ( ( X((HTN ( ( ( ( ( (r   r   c                       e Zd ZddZddZdS )ClassContextNc              #   :   K   |                      ||          V  d S r   )r   r"   s      r   r%   zClassContext.get_filterst  s(      $$^\BBBBBBBr   c                 &    t          | ||          S r   r   r"   s      r   r   zClassContext.get_global_filterw  s"    )%
 
 
 	
r   r   )r   r   r   r%   r   rf   r   r   r   r   s  s@        C C C C
 
 
 
 
 
r   r   c                   8     e Zd Z fdZddZd Zd Zd Z xZS )r   c                 p    t                                          |j                   || _        || _        d S r   )r   r   r   r   r   )r   r   r6   r   s      r   r   zCompForContext.__init__  s3    7888!,r   Nc              #   ,   K   t          |           V  d S r   r   r"   s      r   r%   zCompForContext.get_filters  s       t$$$$$$$r   c                     d S r   rf   rg   s    r   rx   zCompForContext.get_value  s    tr   c                     dS )Nz<comprehension context>rf   rg   s    r   rv   zCompForContext.py__name__  s    ((r   c                 0    | j         j        d| j        dS r   )r   r   r   rg   s    r   r   zCompForContext.__repr__  s    >222DNNNCCr   r   )	r   r   r   r   r%   rx   rv   r   r   r   s   @r   r   r     s        - - - - -
% % % %  ) ) )D D D D D D Dr   r   c                       e Zd ZddZdS )CompiledContextNc                 4    | j                                         S r   r   r"   s      r   r%   zCompiledContext.get_filters  r   r   r   r   rf   r   r   r  r    s(        ) ) ) ) ) )r   r  c                   F    e Zd ZdZd Zed             Zdee         fdZ	dS )CompiledModuleContextNc                     | j         S r   r   rg   s    r   rx   zCompiledModuleContext.get_value  r   r   c                     | j         j        S r   r   rg   s    r   r   z"CompiledModuleContext.string_names  r   r   r   c                 4    | j                                         S r   r   rg   s    r   r   z CompiledModuleContext.py__file__  r   r   )
r   r   r   r   rx   r   r   r   r   r   rf   r   r   r	  r	    sb        J   ( ( X((HTN ( ( ( ( ( (r   r	  c                    |r|                     ddd          }d }|dk    r|}|                     dd          }|9|j        d         }|*||j        k     r|||j        d         j        k     r|j        }t          | ||          S )Nr   r   r   )r   r   r   get_global_filters)r9   name_or_noner/   ancestorr   r   s         r   r*   r*     s     //	:yQQy   G#33IzJJH%b)E#5?(B(B?h1A"1E1O&O&O'1Hgx>>>r   c              #     K   | }ddl m} | ?|                     ||          E d{V  t          | |t          f          rd}| j        } | ?t          |j        j                                        d          }|J |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   r   r   r   r   )r9   r#   r$   base_contextr  rY   s         r   r  r    s      ^ LJJJJJJ

&&)% ' 
 
 	
 	
 	
 	
 	
 	
 	
 g <mLMM 	"!N( 
 	\)9EEGGNNA===
GGGGGr   N)&abcr   
contextlibr   pathlibr   typingr   r   parso.python.treer   jedi.inference.filtersr	   r
   r   jedi.inference.namesr   r   jedi.inference.base_valuer   r   jedi.parser_utilsr   jedir   r   r   r   r   r   r   r   r   r   r  r	  r*   r  rf   r   r   <module>r      s         % % % % % %                       " " " " " "          G G G G G G G G 9 9 9 9 9 9 9 9 . . . . . .            Q' Q' Q' Q' Q' Q' Q' Q'h6A 6A 6A 6A 6A? 6A 6A 6ArT: T: T: T: T: T: T: T:n
 
 
 
 
& 
 
 
# # # # #$l # # #L( ( ( ( (' ( ( (	
 	
 	
 	
 	
#\ 	
 	
 	
D D D D D% D D D&) ) ) ) )l ) ) )
( ( ( ( (O ( ( (? ? ?*@ @ @ @ @r   