
    fC                        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      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)search_ancestor)Name)ParserTreeFilterMergedFilterGlobalNameFilter)AnonymousParamNameTreeNameDefinition)	NO_VALUESValueSet)get_parent_scope)debug)parser_utilsc                       e Zd Z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)AbstractContextc                      || _         i | _        y N)inference_statepredefined_names)selfr   s     Q/var/www/cvtools/html/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>M   s     'G

'Gs   )analysisz$NameError: name '%s' is not defined.z
name-errorz context.names_to_types: %s -> %s)r0   r)   r   valuer   r   is_scopeparenttypeKeyErrorr'   r6   reachability_check	tree_nodeUNREACHABLEr   r   	from_setsr?   addr   r+   _check_for_additional_knowledge)r   r,   name_contextr-   analysis_errorsr/   string_namefound_predefined_typesr9   	name_dicttypesr6   checkvaluesr?   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)r4   r5   N)rC   )r<   bs     r   r>   zBAbstractContext._check_for_additional_knowledge.<locals>.<genexpr>a   s     Oq166::Os   r   )check_flow_informationT)include_flows)	r)   r   is_instancerF   anyr   jedi.inference.finderrY   r   )r   r,   rK   r-   
flow_scope
base_nodesrY   ns           r   rJ   z/AbstractContext._check_for_additional_knowledgeZ   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   )parent_contextget_root_context)r   rb   s     r   rc   z AbstractContext.get_root_contextn   s'    ,,!K..00r   c                      yNF r   s    r   	is_modulezAbstractContext.is_modulet       r   c                      yre   rf   rg   s    r   is_builtins_modulez"AbstractContext.is_builtins_modulew   ri   r   c                      yre   rf   rg   s    r   is_classzAbstractContext.is_classz   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   r   r#   r0   rT   rJ   rc   rh   rk   rm   ro   r[   rr   rt   rv   rx   propertyr=   r{   r   r   r   rf   r   r   r   r      s    # " " 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   r@   	__class__s     r   r   zValueContext.__init__   s    ../r   c                 .    | j                   j                  S r   )r   rF   rg   s    r   rF   zValueContext.tree_node   s    {{$$$r   c                 .    | j                   j                  S r   )r   rb   rg   s    r   rb   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   rF   rb   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                   $    e Zd Zd Zd Zd Zd Zy)TreeContextMixinc                      ddl m}  || |      S )Nr   )
infer_node)jedi.inference.syntax_treer   )r   r9   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   )r@   )funcdeflambdef)instanceclass_contextfunctionclassdefzProbably shouldn't happen: %s)r'   r@   rF   rh   rx   create_contextrC   FunctionValuefrom_contextrm   rb   create_valueAnonymousInstancer   BoundMethod
as_context
ClassValuer   )r   r9   r@   rb   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   rW   z(There's a scope that was not managed: %s)	rF   rC   r   r   rB   	start_poschildrenCompForContext	Exception)
scope_node	is_nestedrb   from_scope_noder9   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   rW   dictorsetmaker   )rB   r   rA   rC   r   )r9   r`   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)rC   r   indexr   rB   r=   )r   r9   r   colonrB   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                         }|rH|j                  dk(  r9|j                  |k(  r*t        |dd      }| j	                  |      }t        ||      S | j                  |      }t        ||      S )Nr   r   r   )get_definitionrC   r=   r   r   r   r   r   )r   	tree_name
definitionr   r   r7   s         r   create_namezTreeContextMixin.create_name"  sq    --/
*//W4I9U%j)YGG$$W-D%dI66)))4G%gy99r   N)r   r   r   r   r   r   r   rf   r   r   r   r      s    &N4$;L:r   r   c                       e Zd ZddZy)FunctionContextNc              #   B   K   t        | j                  | ||       y wN)rb   r!   r"   )r	   r   r    s      r   r#   zFunctionContext.get_filters.  s%       )%	
 	
s   r   r   r   r   r#   rf   r   r   r   r   -  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__8  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"   r.   s       r   r#   zModuleContext.get_filters;  s[     ++)),7Wd#-)
 ""$
 	
 s   AAAAc                     t        |       S r   )r   rg   s    r   r   zModuleContext.get_global_filterI  s    %%r   c                 .    | j                   j                  S r   r   string_namesrg   s    r   r   zModuleContext.string_namesL      {{'''r   c                 .    | j                   j                  S r   )r   
code_linesrg   s    r   r   zModuleContext.code_linesP  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_valueT  s     {{r   r   )r   r   r   r   r   r   r#   r   r   r   r   rx   rf   r   r   r   r   7  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_filters^  r   r   c                     | j                   S r   r   rg   s    r   rx   zNamespaceContext.get_valuea  r   r   c                 .    | j                   j                  S r   r   rg   s    r   r   zNamespaceContext.string_namesd  r   r   r   c                 6    | j                   j                         S r   r   rg   s    r   r   zNamespaceContext.py__file__h  r   r   r   )
r   r   r   r#   rx   r   r   r   r   r   rf   r   r   r   r   ]  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_filtersm  s     $$^\BBs   c                     t        | ||      S r   r	   r    s      r   r   zClassContext.get_global_filterp  s    )%
 	
r   r   )r   r   r   r#   r   rf   r   r   r   r   l  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   rF   rb   )r   rb   r4   r   s      r   r   zCompForContext.__init__y  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   rF   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   x  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                     |pt        |ddd      }d }|dk(  r|}t        |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)r7   name_or_noner-   ancestorr   r   s         r   r(   r(     s     "<J	Ry  G&|Y
KH%%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   rb   r   r   r   )r7   r!   r"   base_contextr  rX   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   
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   r   r   r   r   r   r   r   r  r  r(   r  rf   r   r   <module>r     s     %   & "  G 9 .  O' O'd6A? 6ArM: M:`
& 
#$l #L(' (	
#\ 	
D% D&)l )
(O (?*@r   