/ lib / jedi / inference / context.pyc
context.pyc
  1  o

  2  )��cC�@sZddlmZddlmZddlmZddlmZddlm	Z	ddl
  3  mZddlm
Z
mZmZddlmZmZdd	lmZmZdd
  4  lmZddlmZddlmZGd
d�d�ZGdd�de�ZGdd�d�ZGdd�dee�ZGdd�dee�ZGdd�dee�Z Gdd�dee�Z!Gdd�dee�Z"Gdd�de�Z#Gdd �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�eZdZdd�Zed)dd��Zdd�Z		d*d	d
  5  �Zdd�Zd
d�Z	dd�Z
  6  dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zedd��Zdd �Zed!d"��Zd#d$�Zd%d&�Zed'd(��ZdS)+�AbstractContextcCs||_i|_dS�N)�inference_state�predefined_names)�selfr�r��C:\Users\Jacks.GUTTSPC\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\jedi\inference\context.py�__init__s
  7  zAbstractContext.__init__NcC�t�r��NotImplementedError�r�until_position�origin_scoperrr�get_filters�zAbstractContext.get_filterscCsFddlm}t|t|t�r|nd|�}|�||�}t�d|||�|S)Nr)�finderzcontext.goto %s in (%s): %s)�jedi.inferencer"�_get_global_filters_for_name�
  8  isinstancer�filter_namer�dbg)r�name_or_str�positionr"�filters�namesrrr�gotos�zAbstractContext.gotoTcCsZ|dur|}|�||�}t|t�r|jn|}d}|jrTt|t�rT|}|durTt�|�sT|j}|jdvrKz|j|}	|	|}
  9  Wn	t	yGYq"w|
 10  }n	|durTt�|�r+|durt|rtddl
 11  m}|j||j
|d�}||jurqt}
n
|}
n
 12  t�dd�|D��}
|s�|
s�|r�t|t�r�ddl
 13  m}d	|}|�|d
 14  ||�t�d||
�|
r�|
S|�|||�S)zZ
 15          :param position: Position of the last statement -> tuple of line, column
 16          N)�if_stmt�for_stmt�comp_for�
sync_comp_forr)�
flow_analysis)�context�value_scope�nodecss�|]}|��VqdSr)�infer)�.0�namerrr�	<genexpr>Ms�z5AbstractContext.py__getattribute__.<locals>.<genexpr>)�analysisz$NameError: name '%s' is not defined.z
 17  name-errorz context.names_to_types: %s -> %s)r,r%r�valuerr�is_scope�parent�type�KeyErrorr#r1�reachability_check�	tree_node�UNREACHABLEr
r�	from_setsr9�addrr'�_check_for_additional_knowledge)rr(�name_contextr)�analysis_errorsr+�string_nameZfound_predefined_typesr4�	name_dict�typesr1�check�valuesr9�messagerrr�py__getattribute__&sN
 18  
 19  ���
 20  
 21  z"AbstractContext.py__getattribute__cCs�|p|}t|t�r@|��s@|}|jg}tdd�|D��rtSddlm}	t|dd�}|||||�}|dur8|S||vr?	tSq%tS)Ncss�|]}|jdvVqdS)�r/r0N)r=)r6�brrrr8as�zBAbstractContext._check_for_additional_knowledge.<locals>.<genexpr>r)�check_flow_informationT)�
include_flows)	r%r�is_instancer@�anyr
�jedi.inference.finderrPr)rr(rEr)�
 22  flow_scopeZ
 23  base_nodesrP�nrrrrDZs&��z/AbstractContext._check_for_additional_knowledgecCs|j}|dur	|S|��Sr)�parent_context�get_root_context)rrWrrrrXnsz AbstractContext.get_root_contextcC�dS�NFr�rrrr�	is_modulet�zAbstractContext.is_modulecCrYrZrr[rrr�is_builtins_modulewr]z"AbstractContext.is_builtins_modulecCrYrZrr[rrr�is_classzr]zAbstractContext.is_classcCrYrZrr[rrr�is_stub}r]zAbstractContext.is_stubcCrYrZrr[rrrrR�r]zAbstractContext.is_instancecCrYrZrr[rrr�is_compiled�r]zAbstractContext.is_compiledcCrYrZrr[rrr�is_bound_method�r]zAbstractContext.is_bound_methodcCrrrr[rrr�
 24  py__name__�r!zAbstractContext.py__name__cCrrrr[rrr�	get_value�r]zAbstractContext.get_valuecC�dSrrr[rrrr7�r!zAbstractContext.namecCrY)Nrrr[rrr�get_qualified_names�r]z#AbstractContext.get_qualified_namescCrY)N�rr[rrr�	py__doc__�r]zAbstractContext.py__doc__ccs,�|j}|||<z	dVW||=dS||=wr)r)rrU�dctZ
 25  predefinedrrr�predefine_names�s�zAbstractContext.predefine_names�NN)NNT)�__name__�
 26  __module__�__qualname__rrr r,rMrDrXr\r^r_r`rRrarbrcrd�propertyr7rfrhrrjrrrrrs4	
 27  �4
 28  
 29  rcs�eZdZdZ�fdd�Zedd��Zedd��Zdd	�Zd
 30  d�Z	dd
�Z
 31  dd�Zdd�Zdd�Z
dd�Zdd�Zedd��Zdd�Zdd�Zdd�Zd d!�Z�ZS)"�ValueContextzC
 32      Should be defined, otherwise the API returns empty types.
 33      cst��|j�||_dSr)�superrr�_value)rr:��	__class__rrr�s
 34  zValueContext.__init__cC�|jjSr)rrr@r[rrrr@��zValueContext.tree_nodecCrur)rrrWr[rrrrW�rvzValueContext.parent_contextcC�
 35  |j��Sr)rrr\r[rrrr\��
 36  zValueContext.is_modulecCs|j|jjkSr)rrr�builtins_moduler[rrrr^�szValueContext.is_builtins_modulecCrwr)rrr_r[rrrr_�rxzValueContext.is_classcCrwr)rrr`r[rrrr`�rxzValueContext.is_stubcCrwr)rrrRr[rrrrR�rxzValueContext.is_instancecCrwr)rrrar[rrrra�rxzValueContext.is_compiledcCrwr)rrrbr[rrrrb�rxzValueContext.is_bound_methodcCrwr)rrrcr[rrrrc�rxzValueContext.py__name__cCrur)rrr7r[rrrr7�rvzValueContext.namecCrwr)rrrfr[rrrrf�rxz ValueContext.get_qualified_namescCrwr)rrrhr[rrrrh�rxzValueContext.py__doc__cC�|jSr�rrr[rrrrd��zValueContext.get_valuecC�d|jj|jfS�Nz%s(%s))rtrlrrr[rrr�__repr__��zValueContext.__repr__)rlrmrn�__doc__rror@rWr\r^r_r`rRrarbrcr7rfrhrdr�
__classcell__rrrsrrp�s*
 37  
 38  
 39  rpc@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
 40  �TreeContextMixincCsddlm}|||�S)Nr)�
 41  infer_node)�jedi.inference.syntax_treer�)rr4r�rrrr��s
 42  zTreeContextMixin.infer_nodecCs�ddlm}||jkr|��sJ�|��S|�|�}|jdvrF|j�||�}|�	�rD|j
 43  �|j�}|�|j
|j
 44  |�}|j||��|d�}|S|jdkrS|�|j
||�Std|��)Nr)r:)�funcdef�lambdef)�instance�
class_context�function�classdefzProbably shouldn't happen: %s)r#r:r@r\rd�create_contextr=�
FunctionValue�from_contextr_rW�create_value�AnonymousInstancer�BoundMethod�
 45  as_context�
 46  ClassValuer)rr4r:rW�func�class_valuer�rrrr��s*
 47  
 48  
 49  
 50  ��
 51  zTreeContextMixin.create_valuecszd
 52  ����fdd�	�dd�����}|jdvr7|j|j�d�}�j|jkr7�j}|jdkr3|j�ks7�|�}�|dd	�S)NTcsj|�jkr�S|jdvr��|���S|jdvr/��|j��}�j|jdjkr*|St||�Std|��)N)r�r�r�rN�����z(There's a scope that was not managed: %s)	r@r=r�r�r<�	start_pos�children�CompForContext�	Exception)�
 53  scope_node�	is_nestedrW��from_scope_noder4�parent_scoperrrr��s
 54  
 55  
 56  
 57  z8TreeContextMixin.create_context.<locals>.from_scope_nodecSsn	|j}t�|�r|S|jdvr|jdjdvr|jdSn|jdkr6|jdd�D]}|jdvr5|Sq*q)NT)�argument�
testlist_comp�rN�dictorsetmaker�)r<rr;r=r�)r4rVrrrr�
 58  s
 59  
 60  
 61  �
 62  
 63  ��z5TreeContextMixin.create_context.<locals>.parent_scope)r�r��:�param)r�)T)r=r��indexr�r<r7)rr4r��colonr<rr�rr��s

 64  zTreeContextMixin.create_contextcCsT|��}|r |jdkr |j|kr t|dd�}|�|�}t||�S|�|�}t||�S)Nr�r�r�)�get_definitionr=r7rr�rr�r)r�	tree_name�
 65  definitionr�r�r2rrr�create_name"s
 66  
 67  
 68  
 69  zTreeContextMixin.create_nameN)rlrmrnr�r�r�r�rrrrr��s
 70  &r�c@�eZdZddd�ZdS)�FunctionContextNccs�t|j|||d�VdS�N)rWrr)rrrrrrr .s��zFunctionContext.get_filtersrk�rlrmrnr rrrrr�-�r�c@sPeZdZdeefdd�Zddd�Zdd�Zed	d
 71  ��Z	edd��Z
 72  d
d�ZdS)�
ModuleContext�returncCrwr�rr�
 73  py__file__r[rrrr�8rxzModuleContext.py__file__Nccs@�|j�|�}t|d�tt|||d�|���V|EdHdSr�)rrr �nextr	r�get_global_filter)rrrr*rrrr ;s�
 74  ��zModuleContext.get_filterscCst|�Sr)r
 75  r[rrrr�IszModuleContext.get_global_filtercCrur�rr�string_namesr[rrrr�LrvzModuleContext.string_namescCrur)rr�
 76  code_linesr[rrrr�PrvzModuleContext.code_linescCrz)z�
 77          This is the only function that converts a context back to a value.
 78          This is necessary for stub -> python conversion and vice versa. However
 79          this method shouldn't be moved to AbstractContext.
 80          r{r[rrrrdTszModuleContext.get_valuerk)rlrmrnrrr�r r�ror�r�rdrrrrr�7s
 81  
 82  
 83  r�c@s<eZdZddd�Zdd�Zedd��Zdeefd	d
 84  �Z	dS)�NamespaceContextNcCrwr�rrr rrrrr ^rxzNamespaceContext.get_filterscCrzrr{r[rrrrdar|zNamespaceContext.get_valuecCrurr�r[rrrr�drvzNamespaceContext.string_namesr�cCrwrr�r[rrrr�hrxzNamespaceContext.py__file__rk)
 85  rlrmrnr rdror�rrr�rrrrr�]s
 86  
 87  r�c@s eZdZddd�Zddd�ZdS)�ClassContextNccs�|�||�VdSr)r�rrrrr ms�zClassContext.get_filterscCst|||d�Sr��rrrrrr�ps
 88  �zClassContext.get_global_filterrk)rlrmrnr r�rrrrr�ls
 89  r�cs>eZdZ�fdd�Zddd�Zdd�Zdd	�Zd
 90  d�Z�ZS)
r�cst��|j�||_||_dSr)rqrrr@rW)rrWr/rsrrrys
 91  zCompForContext.__init__Nccs�t|�VdSrr�rrrrr ~s�zCompForContext.get_filterscCrerrr[rrrrd�r]zCompForContext.get_valuecCrY)Nz<comprehension context>rr[rrrrc�r]zCompForContext.py__name__cCr}r~)rtrlr@r[rrrr�r�zCompForContext.__repr__rk)	rlrmrnrr rdrcrr�rrrsrr�xs
 92  r�c@r�)�CompiledContextNcCrwrr�rrrrr �rxzCompiledContext.get_filtersrkr�rrrrr��r�r�c@s6eZdZdZdd�Zedd��Zdeefdd�Z	dS)	�CompiledModuleContextNcCrzrr{r[rrrrd�r|zCompiledModuleContext.get_valuecCrurr�r[rrrr��rvz"CompiledModuleContext.string_namesr�cCrwrr�r[rrrr��rxz CompiledModuleContext.py__file__)
 93  rlrmrnr�rdror�rrr�rrrrr��s
 94  r�cCs�|dur:t|ddd�}d}|dkr|}t|dd�}|dur:|jd}|dur:||jkr:|dus7||jdjkr:|j}t|||�S)Nr�r�r������)rr�r��get_global_filters)r2Zname_or_noner)�ancestorr�r�rrrr$�s
 95  r$ccsv�|}ddlm}|dur'|j||d�EdHt||tf�r d}|j}|dus
t|jj��d�}|dus6J�|VdS)a�
 96      Returns all filters in order of priority for name resolution.
 97  
 98      For global name lookups. The filters will handle name resolution
 99      themselves, but here we gather possible filters downwards.
100  
101      >>> from jedi import Script
102      >>> script = Script('''
103      ... x = ['a', 'b', 'c']
104      ... def func():
105      ...     y = None
106      ... ''')
107      >>> module_node = script._module_node
108      >>> scope = next(module_node.iter_funcdefs())
109      >>> scope
110      <Function: func@3-5>
111      >>> context = script._get_module_context().create_context(scope)
112      >>> filters = list(get_global_filters(context, (4, 0), None))
113  
114      First we get the names from the function scope.
115  
116      >>> print(filters[0])  # doctest: +ELLIPSIS
117      MergedFilter(<ParserTreeFilter: ...>, <GlobalNameFilter: ...>)
118      >>> sorted(str(n) for n in filters[0].values())  # doctest: +NORMALIZE_WHITESPACE
119      ['<TreeNameDefinition: string_name=func start_pos=(3, 4)>',
120       '<TreeNameDefinition: string_name=x start_pos=(2, 0)>']
121      >>> filters[0]._filters[0]._until_position
122      (4, 0)
123      >>> filters[0]._filters[1]._until_position
124  
125      Then it yields the names from one level "lower". In this example, this is
126      the module scope (including globals).
127      As a side note, you can see, that the position in the filter is None on the
128      globals filter, because there the whole module is searched.
129  
130      >>> list(filters[1].values())  # package modules -> Also empty.
131      []
132      >>> sorted(name.string_name for name in filters[2].values())  # Module attributes
133      ['__doc__', '__name__', '__package__']
134  
135      Finally, it yields the builtin filter, if `include_builtin` is
136      true (default).
137  
138      >>> list(filters[3].values())  # doctest: +ELLIPSIS
139      [...]
140      r)�BaseFunctionExecutionContextN)rr)	�jedi.inference.value.functionr�r r%r�rWr�rry)r2rrZbase_contextr�rOrrrr��s�/��
141  r�N)'�abcr�
142  contextlibr�pathlibr�typingr�
143  parso.treer�parso.python.treer�jedi.inference.filtersrr	r
144  �jedi.inference.namesrr�jedi.inference.base_valuer
r�jedi.parser_utilsr�jedirrrrpr�r�r�r�r�r�r�r�r$r�rrrr�<module>s29P
145  &