
    ^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%y")#    )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y)AbstractContext	tree_nodeparent_contextc                      || _         i | _        y N)inference_statepredefined_names)selfr   s     O/opt/ringagent/.cad-venv/lib/python3.12/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 |      }|j                  ||      }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   sM    ).K!>+D(
 ""7K8		/dEJr   c                 
   || }| j                  ||      }t        |t              r|j                  n|}d}| j                  rst        |t              rc|}|_t        j                  |      sJ|j                  }|j                  dv r	 | j                  |   }	|	|   }
|
}n|t        j                  |      sJ|>|r<ddl
m} |j                  | | j                  |      }||j                  u rt        }n|}nt!        j"                  d |D              }|s3|s1|r/t        |t              rddl
m} d|z  }|j'                  |d	||       t)        j*                  d
||       |r|S | j-                  |||      S # t        $ r Y ,w xY w)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   | ]  }|j                           y wr   )infer).0names     r   	<genexpr>z5AbstractContext.py__getattribute__.<locals>.<genexpr>N   s     'G

'Gs   )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+5k4+Hk''k "&  ZT%BD"<+@+@+F{{99 TT$($9$9$$?	 )+ 6 27. "<+@+@+F "-%4!44 NN  5 E
 111"/'''G'GGFV+t,3AKO\<gN		4eVDM33KxXX9 $ ! !s   E5 5	FFc                    |xs | }t        |t              rf|j                         sV|}|j                  g}t	        d |D              rt
        S ddlm} 	 t        |d      } |||||      }||S ||v r	 t
        S (t
        S )Nc              3   8   K   | ]  }|j                   d v   yw)r6   r7   N)rE   )r>   bs     r   r@   zBAbstractContext._check_for_additional_knowledge.<locals>.<genexpr>b   s     Oq166::Os   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(1I1I1K$J&001JOJOO  D-jM
*<+6B=H+  r   c                 B    | j                   }|| S |j                         S r   )r   get_root_context)r   r   s     r   rc   z AbstractContext.get_root_contexto   s'    ,,!K..00r   c                      yNF r   s    r   	is_modulezAbstractContext.is_moduleu       r   c                      yre   rf   rg   s    r   is_builtins_modulez"AbstractContext.is_builtins_modulex   ri   r   c                      yre   rf   rg   s    r   is_classzAbstractContext.is_class{   ri   r   c                      yre   rf   rg   s    r   is_stubzAbstractContext.is_stub~   ri   r   c                      yre   rf   rg   s    r   r\   zAbstractContext.is_instance   ri   r   c                      yre   rf   rg   s    r   is_compiledzAbstractContext.is_compiled   ri   r   c                      y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                      y r   rf   rg   s    r   r?   zAbstractContext.name   s    r   c                      y)Nrf   rf   rg   s    r   get_qualified_namesz#AbstractContext.get_qualified_names       r   c                      y)N rf   rg   s    r   	py__doc__zAbstractContext.py__doc__   r|   r   c              #   N   K   | j                   }|||<   	 d  ||= y # ||= w xY wwr   )r   )r   r_   dct
predefineds       r   predefine_nameszAbstractContext.predefine_names   s3     **
!$
:	':&
:&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    N# " " KO+/2Yh(1 " ""   ' '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                 F    t         |   |j                         || _        y 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                 6    | j                   j                         S r   )r   rh   rg   s    r   rh   zValueContext.is_module       {{$$&&r   c                 H    | j                   | j                  j                  k(  S r   )r   r   builtins_modulerg   s    r   rk   zValueContext.is_builtins_module   s    {{d22BBBBr   c                 6    | j                   j                         S r   )r   rm   rg   s    r   rm   zValueContext.is_class   s    {{##%%r   c                 6    | j                   j                         S r   )r   ro   rg   s    r   ro   zValueContext.is_stub   s    {{""$$r   c                 6    | j                   j                         S r   )r   r\   rg   s    r   r\   zValueContext.is_instance       {{&&((r   c                 6    | j                   j                         S r   )r   rr   rg   s    r   rr   zValueContext.is_compiled   r   r   c                 6    | j                   j                         S r   )r   rt   rg   s    r   rt   zValueContext.is_bound_method   s    {{**,,r   c                 6    | j                   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                 6    | j                   j                         S r   )r   r{   rg   s    r   r{   z ValueContext.get_qualified_names   s    {{..00r   c                 6    | j                   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                 N    | j                   j                  d| j                  dS N())r   r   r   rg   s    r   __repr__zValueContext.__repr__   s    >>22DKK@@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      s     % % * *'C&%))-(    1'Ar   r   c                   b    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y)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$%%r   c                 Z   ddl m} || j                  k(  r"| j                         sJ | j	                         S | j                  |      }|j                  dv r|j                  j                  ||      }|j                         rn|j                  j                  |j                        }|j                  | j                  |j                  |      }|j                  ||j                         |      }|S |j                  dk(  r|j!                  | 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   s   (4>>!>>###>>##,,T299..&&33NDID&&(,;;HHIaIab 22((.*G*GV((%"-"8"8":! ) 
 KYY*$##D$8$8.$OO%&E&LMMr   c                 H    d fd	d        }|j                   dv rs|j                  |j                  j                  d         }j                  |j                  k  r2j                  }|j                   dk(  r|j
                  k(  s |      } |d      S )	NTc                 P   | j                   k(  rS | j                  dv rj                  |       j                         S | j                  dv rL  | 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((4??AA$AA!0j>O>O1P!Q>>Z%8%8%<%F%FF))%njAAFSTTr   c                 &   	 | j                   } t        j                  |       r| S | j                  dv r*| j                  d   j                  dv rD| j                  d   S | j                  dk(  r&| j                  dd D ]  }|j                  dv s|c S  )N)argumenttestlist_comp   rX   dictorsetmaker   )rD   r   rC   rE   r   )r;   ra   s     r   r   z5TreeContextMixin.create_context.<locals>.parent_scope  s    {{((.KYY"??}}Q',,0MM#}}Q//YY"22!]]1Q/ %66%BB#$H% 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	% "$'
??55''
(;(;(A(A#(FGE~~/w.6;;$3F!-j!9JzT::r   c                     |j                         }|rM|j                  dk(  r>|j                  |k(  r/|j                  dd      }| j	                  |      }t        ||      S | j                  |      }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*  su    --/
*//W4I9U 00IFG$$W-D%dI66)))4G%gy99r   N)	r   r   r   r   r   r   r   r   r   rf   r   r   r   r      s8    NNNM&N4$;L:r   r   c                       e Zd ZddZy)FunctionContextNc              #   ,   K   t        | ||       y wN)r   r#   r$   r	   r"   s      r   r%   zFunctionContext.get_filters6  s     )%
 	
s   r   r   r   r   r%   rf   r   r   r   r   5  s    
r   r   c                   R    e Zd Zdee   fdZd	dZd Zed        Z	ed        Z
d Zy)
ModuleContextreturnc                 6    | j                   j                         S r   r   
py__file__rg   s    r   r   zModuleContext.py__file__?  r   r   Nc              #      K   | j                   j                  |      }t        |d        t        t	        | ||      | j                                |E d {    y 7 wr   )r   r%   nextr
   r	   get_global_filter)r   r#   r$   r0   s       r   r%   zModuleContext.get_filtersB  s[     ++)),7Wd#-)
 ""$
 	
 s   AAAA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   >  sI    (HTN (& ( ( & &r   r   c                   <    e Zd ZddZd Zed        Zdee   fdZ	y)NamespaceContextNc                 6    | j                   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                 6    | j                   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  s0    ) ( ((HTN (r   r   c                       e Zd ZddZddZy)ClassContextNc              #   4   K   | j                  ||       y wr   )r   r"   s      r   r%   zClassContext.get_filterst  s     $$^\BBs   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
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                 T    t         |   |j                         || _        || _        y r   )r   r   r   r   r   )r   r   r6   r   s      r   r   zCompForContext.__init__  s%    778!,r   c              #   &   K   t        |        y wr   r   r"   s      r   r%   zCompForContext.get_filters  s     t$$s   c                      y r   rf   rg   s    r   rx   zCompForContext.get_value  s    r   c                      y)Nz<comprehension context>rf   rg   s    r   rv   zCompForContext.py__name__  s    (r   c                 N    | j                   j                  d| j                  dS r   )r   r   r   rg   s    r   r   zCompForContext.__repr__  s    >>22DNNCCr   r   )	r   r   r   r   r%   rx   rv   r   r   r   s   @r   r   r     s    -
%)Dr   r   c                       e Zd ZddZy)CompiledContextNc                 6    | j                   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                   8    e Zd ZdZd Zed        Zdee   fdZ	y)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                 6    | j                   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	    s0    J ( ((HTN (r   r	  c                    |z|j                  ddd      }d }|dk(  r|}|j                  dd      }|J|j                  d   }|9||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y  G#33IzJH%%b)E#5??(B?h1A1A"1E1O1O&O'11Hgx>>r   c              #     K   | }ddl m} | >| j                  ||      E d{    t        | |t        f      rd}| j
                  } | >t        |j                  j                  j                         d      }|J | y7 ^w)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

&&)% ' 
 	
 	
 g <mLM!N(( 
 	\))99EEGNA==
G	
s   !BB&B8B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 9 .  Q' Q'h6A? 6ArT: T:n
& 
#$l #L(' (	
#\ 	
D% D&)l )
(O (?*@r   