Format: application/x-python-code
⬇ Download File (17.0 KB)
   1  o
   2  
   3  �hC�@sZddlmZddlmZddlmZddlmZddlm	Z	ddl
   4  mZddlm
   5  Z
   6  mZmZddlmZmZdd	lmZmZdd
   7  lmZddlmZddlmZGd
   8  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
   9  �Zdd�Zd
  10  d�Z	dd�Z
  11  dd�Zdd�Zdd�Z
  12  dd�Zdd�Zdd�Zedd��Zdd �Zed!d"��Zd#d$�Zd%d&�Zed'd(��ZdS)+�AbstractContextcCs||_i|_dS�N)�inference_state�predefined_names)�selfr�r�Lc:\Users\shubh\Videos\agent\nova\Lib\site-packages\jedi/inference/context.py�__init__s
  13  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�
  14  isinstancerZfilter_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|}	|	|}
  15  Wn	t	yGYq"w|
  16  }n	|durTt�|�r+|durt|rtddl
  17  m}|j||j
  18  |d�}||jurqt}
  19  n
  20  |}
  21  n
  22  t�dd�|D��}
  23  |s�|
  24  s�|r�t|t�r�ddl
  25  m}d	|}|�|d
  26  ||�t�d||
  27  �|
  28  r�|
  29  S|�|||�S)zZ
  30          :param position: Position of the last statement -> tuple of line, column
  31          N)Zif_stmtZfor_stmt�comp_for�
  32  sync_comp_forr)�
  33  flow_analysis)�contextZvalue_scope�nodecss�|]}|��VqdSr)Zinfer)�.0�namerrr�	<genexpr>Ms�z5AbstractContext.py__getattribute__.<locals>.<genexpr>)�analysisz$NameError: name '%s' is not defined.z
  34  name-errorz context.names_to_types: %s -> %s)r+r%r�valuerr�is_scope�parent�type�KeyErrorr#r.Zreachability_check�	tree_nodeZUNREACHABLEr
  35  rZ	from_setsr4�addrr&�_check_for_additional_knowledge)rr'�name_contextr(Zanalysis_errorsr*Zstring_nameZfound_predefined_typesr0Z	name_dict�typesr.�check�valuesr4�messagerrr�py__getattribute__&sN
  36  
  37  ���
  38  
  39  z"AbstractContext.py__getattribute__cCs�|p|}t|t�r@|��s@|}|jg}tdd�|D��rtSddlm}	t|dd�}|||||�}|dur8|S||vr?	tSq%tS)Ncss�|]}|jdvVqdS)�r,r-N)r8)r1�brrrr3as�zBAbstractContext._check_for_additional_knowledge.<locals>.<genexpr>r)�check_flow_informationT)Z
  40  include_flows)	r%r�is_instancer:�anyr
  41  Zjedi.inference.finderrEr)rr'r=r(�
  42  flow_scopeZ
  43  base_nodesrE�nrrrr<Zs&��z/AbstractContext._check_for_additional_knowledgecCs|j}|dur	|S|��Sr)�parent_context�get_root_context)rrJrrrrKnsz AbstractContext.get_root_contextcC�dS�NFr�rrrr�	is_modulet�zAbstractContext.is_modulecCrLrMrrNrrr�is_builtins_modulewrPz"AbstractContext.is_builtins_modulecCrLrMrrNrrr�is_classzrPzAbstractContext.is_classcCrLrMrrNrrr�is_stub}rPzAbstractContext.is_stubcCrLrMrrNrrrrF�rPzAbstractContext.is_instancecCrLrMrrNrrr�is_compiled�rPzAbstractContext.is_compiledcCrLrMrrNrrr�is_bound_method�rPzAbstractContext.is_bound_methodcCrrrrNrrr�
  44  py__name__�r!zAbstractContext.py__name__cCrrrrNrrr�	get_value�rPzAbstractContext.get_valuecC�dSrrrNrrrr2�r!zAbstractContext.namecCrL)NrrrNrrr�get_qualified_names�rPz#AbstractContext.get_qualified_namescCrL)N�rrNrrr�	py__doc__�rPzAbstractContext.py__doc__ccs,�|j}|||<z	dVW||=dS||=wr)r)rrH�dctZ
  45  predefinedrrr�predefine_names�s�zAbstractContext.predefine_names�NN)NNT)�__name__�
  46  __module__�__qualname__rrr r+rBr<rKrOrQrRrSrFrTrUrVrW�propertyr2rYr[rr]rrrrrs4	
  47  �4
  48  
  49  rcs�eZdZdZ�fdd�Zedd��Zedd��Zdd	�Zd
  50  d�Z	dd
  51  �Z
  52  dd�Zdd�Zdd�Z
  53  dd�Zdd�Zedd��Zdd�Zdd�Zdd�Zd d!�Z�ZS)"�ValueContextzC
  54      Should be defined, otherwise the API returns empty types.
  55      cst��|j�||_dSr)�superrr�_value)rr5��	__class__rrr�s
  56  zValueContext.__init__cC�|jjSr)rer:rNrrrr:��zValueContext.tree_nodecCrhr)rerJrNrrrrJ�rizValueContext.parent_contextcC�
  57  |j��Sr)rerOrNrrrrO��
  58  zValueContext.is_modulecCs|j|jjkSr)rer�builtins_modulerNrrrrQ�szValueContext.is_builtins_modulecCrjr)rerRrNrrrrR�rkzValueContext.is_classcCrjr)rerSrNrrrrS�rkzValueContext.is_stubcCrjr)rerFrNrrrrF�rkzValueContext.is_instancecCrjr)rerTrNrrrrT�rkzValueContext.is_compiledcCrjr)rerUrNrrrrU�rkzValueContext.is_bound_methodcCrjr)rerVrNrrrrV�rkzValueContext.py__name__cCrhr)rer2rNrrrr2�rizValueContext.namecCrjr)rerYrNrrrrY�rkz ValueContext.get_qualified_namescCrjr)rer[rNrrrr[�rkzValueContext.py__doc__cC�|jSr�rerNrrrrW��zValueContext.get_valuecC�d|jj|jfS�Nz%s(%s))rgr_rerNrrr�__repr__��zValueContext.__repr__)r_r`ra�__doc__rrbr:rJrOrQrRrSrFrTrUrVr2rYr[rWrr�
  59  __classcell__rrrfrrc�s*
  60  
  61  
  62  rcc@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
  63  �TreeContextMixincCsddlm}|||�S)Nr)�
  64  infer_node)Zjedi.inference.syntax_treerw)rr0rwrrrrw�s
  65  zTreeContextMixin.infer_nodecCs�ddlm}||jkr|��sJ�|��S|�|�}|jdvrF|j�||�}|�	�rD|j
  66  �|j�}|�|j
  67  |j
  68  |�}|j||��|d�}|S|jdkrS|�|j
  69  ||�Std|��)Nr)r5)�funcdef�lambdef)�instanceZ
  70  class_context�function�classdefzProbably shouldn't happen: %s)r#r5r:rOrW�create_contextr8Z
  71  FunctionValueZfrom_contextrRrJ�create_valueZAnonymousInstancerZBoundMethod�
  72  as_contextZ
  73  ClassValuer)rr0r5rJ�funcZclass_valuerzrrrr~�s*
  74  
  75  
  76  
  77  ��
  78  zTreeContextMixin.create_valuecszd
  79  ����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)rxryr|rC�����z(There's a scope that was not managed: %s)	r:r8r~rr7�	start_pos�children�CompForContext�	Exception)�
  80  scope_node�	is_nestedrJ��from_scope_noder0�parent_scoperrrr��s
  81  
  82  
  83  
  84  z8TreeContextMixin.create_context.<locals>.from_scope_nodecSsn	|j}t�|�r|S|jdvr|jdjdvr|jdSn|jdkr6|jdd�D]}|jdvr5|Sq*q)NT)ZargumentZ
  85  testlist_comp�rCZdictorsetmaker�)r7rr6r8r�)r0rIrrrr�
  86  s
  87  
  88  
  89  �
  90  
  91  ��z5TreeContextMixin.create_context.<locals>.parent_scope)rxr|�:�param)r�)T)r8r��indexr�r7r2)rr0r��colonr7rr�rr}�s
  92  
  93  zTreeContextMixin.create_contextcCsT|��}|r |jdkr |j|kr t|dd�}|�|�}t||�S|�|�}t||�S)Nr�rxry)Zget_definitionr8r2rr~rr}r)rZ	tree_nameZ
  94  definitionrxr�r/rrr�create_name"s
  95  
  96  
  97  
  98  zTreeContextMixin.create_nameN)r_r`rarwr~r}r�rrrrrv�s
  99  &rvc@�eZdZddd�ZdS)�FunctionContextNccs�t|j|||d�VdS�N)rJrr)rrrrrrr .s��zFunctionContext.get_filtersr^�r_r`rar rrrrr�-�r�c@sPeZdZdeefdd�Zddd�Zdd�Zed	d
 100  ��Z	edd��Z
 101  d
 102  d�ZdS)�
 103  ModuleContext�returncCrjr�re�
 104  py__file__rNrrrr�8rkzModuleContext.py__file__Nccs@�|j�|�}t|d�tt|||d�|���V|EdHdSr�)rer �nextr	r�get_global_filter)rrrr)rrrr ;s�
 105  ��zModuleContext.get_filterscCst|�Sr)r
 106  rNrrrr�IszModuleContext.get_global_filtercCrhr�re�string_namesrNrrrr�LrizModuleContext.string_namescCrhr)re�
 107  code_linesrNrrrr�PrizModuleContext.code_linescCrm)z�
 108          This is the only function that converts a context back to a value.
 109          This is necessary for stub -> python conversion and vice versa. However
 110          this method shouldn't be moved to AbstractContext.
 111          rnrNrrrrWTszModuleContext.get_valuer^)r_r`rarrr�r r�rbr�r�rWrrrrr�7s
 112  
 113  
 114  r�c@s<eZdZddd�Zdd�Zedd��Zdeefd	d
 115  �Z	dS)�NamespaceContextNcCrjr�rer rrrrr ^rkzNamespaceContext.get_filterscCrmrrnrNrrrrWarozNamespaceContext.get_valuecCrhrr�rNrrrr�drizNamespaceContext.string_namesr�cCrjrr�rNrrrr�hrkzNamespaceContext.py__file__r^)
 116  r_r`rar rWrbr�rrr�rrrrr�]s
 117  
 118  r�c@s eZdZddd�Zddd�ZdS)�ClassContextNccs�|�||�VdSr)r�rrrrr ms�zClassContext.get_filterscCst|||d�Sr��rrrrrr�ps
 119  �zClassContext.get_global_filterr^)r_r`rar r�rrrrr�ls
 120  r�cs>eZdZ�fdd�Zddd�Zdd�Zdd	�Zd
 121  d�Z�ZS)
 122  r�cst��|j�||_||_dSr)rdrrr:rJ)rrJr,rfrrrys
 123  zCompForContext.__init__Nccs�t|�VdSrr�rrrrr ~s�zCompForContext.get_filterscCrXrrrNrrrrW�rPzCompForContext.get_valuecCrL)Nz<comprehension context>rrNrrrrV�rPzCompForContext.py__name__cCrprq)rgr_r:rNrrrrr�rszCompForContext.__repr__r^)	r_r`rarr rWrVrrrurrrfrr�xs
 124  r�c@r�)�CompiledContextNcCrjrr�rrrrr �rkzCompiledContext.get_filtersr^r�rrrrr��r�r�c@s6eZdZdZdd�Zedd��Zdeefdd�Z	dS)	�CompiledModuleContextNcCrmrrnrNrrrrW�rozCompiledModuleContext.get_valuecCrhrr�rNrrrr��riz"CompiledModuleContext.string_namesr�cCrjrr�rNrrrr��rkz CompiledModuleContext.py__file__)
 125  r_r`rar�rWrbr�rrr�rrrrr��s
 126  r�cCs�|dur:t|ddd�}d}|dkr|}t|dd�}|dur:|jd}|dur:||jkr:|dus7||jdjkr:|j}t|||�S)Nrxr|ry�����)rr�r��get_global_filters)r/Zname_or_noner(Zancestorryr�rrrr$�s
 127  r$ccsv�|}ddlm}|dur'|j||d�EdHt||tf�r d}|j}|dus
 128  t|jj��d�}|dus6J�|VdS)a�
 129      Returns all filters in order of priority for name resolution.
 130  
 131      For global name lookups. The filters will handle name resolution
 132      themselves, but here we gather possible filters downwards.
 133  
 134      >>> from jedi import Script
 135      >>> script = Script('''
 136      ... x = ['a', 'b', 'c']
 137      ... def func():
 138      ...     y = None
 139      ... ''')
 140      >>> module_node = script._module_node
 141      >>> scope = next(module_node.iter_funcdefs())
 142      >>> scope
 143      <Function: func@3-5>
 144      >>> context = script._get_module_context().create_context(scope)
 145      >>> filters = list(get_global_filters(context, (4, 0), None))
 146  
 147      First we get the names from the function scope.
 148  
 149      >>> print(filters[0])  # doctest: +ELLIPSIS
 150      MergedFilter(<ParserTreeFilter: ...>, <GlobalNameFilter: ...>)
 151      >>> sorted(str(n) for n in filters[0].values())  # doctest: +NORMALIZE_WHITESPACE
 152      ['<TreeNameDefinition: string_name=func start_pos=(3, 4)>',
 153       '<TreeNameDefinition: string_name=x start_pos=(2, 0)>']
 154      >>> filters[0]._filters[0]._until_position
 155      (4, 0)
 156      >>> filters[0]._filters[1]._until_position
 157  
 158      Then it yields the names from one level "lower". In this example, this is
 159      the module scope (including globals).
 160      As a side note, you can see, that the position in the filter is None on the
 161      globals filter, because there the whole module is searched.
 162  
 163      >>> list(filters[1].values())  # package modules -> Also empty.
 164      []
 165      >>> sorted(name.string_name for name in filters[2].values())  # Module attributes
 166      ['__doc__', '__name__', '__package__']
 167  
 168      Finally, it yields the builtin filter, if `include_builtin` is
 169      true (default).
 170  
 171      >>> list(filters[3].values())  # doctest: +ELLIPSIS
 172      [...]
 173      r)�BaseFunctionExecutionContextN)rr)	Zjedi.inference.value.functionr�r r%r�rJr�rrl)r/rrZbase_contextr�rDrrrr��s�/��
 174  r�N)'�abcr�
 175  contextlibr�pathlibr�typingrZ
 176  parso.treerZparso.python.treerZjedi.inference.filtersrr	r
 177  Zjedi.inference.namesrrZjedi.inference.base_valuer
 178  rZjedi.parser_utilsrZjedirrrrcrvr�r�r�r�r�r�r�r$r�rrrr�<module>s29P
 179  &