Format: application/x-python-code
⬇ Download File (12.1 KB)
   1  o
   2  
   3  �h�>�@sfdZddlZddlmZddlmZmZddlmZddl	m
   4  Z
   5  mZddlm
   6  Z
   7  mZddlmZdd	lmZdd
   8  lmZddlmZddlmZdd
   9  lmZddlmZdd�Zd;dd�Zdd�Zdd�Ze�d<dd��Z dd�Z!dd�Z"dd�Z#e�d d!��Z$d"d#�Z%d$d%�Z&d&d'�Z'd(d)�Z(d*d+�Z)d,d-�Z*d.d/�Z+d0d1�Z,d2d3�Z-d4d5�Z.d=d7d8�Z/d9d:�Z0dS)>a
  10  PEP 0484 ( https://www.python.org/dev/peps/pep-0484/ ) describes type hints
  11  through function annotations. There is a strong suggestion in this document
  12  that only the type of type hinting defined in PEP0484 should be allowed
  13  as annotations in future python versions.
  14  �N)�	Parameter)�ParserSyntaxError�parse)�inference_state_method_cache)�ValueSet�	NO_VALUES)�DefineGenericBaseClass�GenericClass)�TupleGenericManager)�TypeVar)�	is_string)�builtin_from_name)�get_executed_param_names)�debug)�parser_utilscCsd|�|�}t|�dkrt�d||f�|St|�d}t|�r0t||���}|dur0|�|�S|S)z�
  15      Inferes an annotation node. This means that it inferes the part of
  16      `int` here:
  17  
  18          foo: int = 3
  19  
  20      Also checks for forward references (strings)
  21      �z9Inferred typing index %s should lead to 1 object,  not %srN)�
  22  infer_node�lenr�warning�listr�_get_forward_reference_nodeZget_safe_value)�context�
  23  annotation�	value_setZinferred_value�result�r�Wc:\Users\shubh\Videos\agent\nova\Lib\site-packages\jedi/inference/gradual/annotation.py�infer_annotations
  24  	�
  25  rcsDt||�}|durtS|�|�}�dur |��fdd�����}|S)Ncs|jdkott|�����kS)N�tuple)�
  26  array_typerr�
  27  py__iter__)�value��indexrr�<lambda>8s
  28  z*_infer_annotation_string.<locals>.<lambda>)rrr�filterZpy__simple_getitem__)r�stringr#�noderrr"r�_infer_annotation_string0s
  29  
  30  
  31  ��r(cCsdz|jjj|ddd�}Wntyt�d|�YdSw|j��}t�	||j
  32  d�|j|_|S)NZ
  33  eval_inputF)Zstart_symbol�error_recoveryzAnnotation not parsed: %sr)�inference_stateZgrammarrrrr�	tree_nodeZ
  34  get_root_noder�move�end_pos�parent)rr&�new_node�modulerrrr@s
  35  ��
  36  rcCs�zt|dd�jd}Wntyt�d|�gYSw|jdvr*|����gSg}z|j}Wnty<gYSw|D]}|jdvrO|�	|�����q?|S)z�
  37      Split decl_text on commas, but group generic expressions
  38      together.
  39  
  40      For example, given "foo, Bar[baz, biz]" we return
  41      ['foo', 'Bar[baz, biz]'].
  42  
  43      F)r)rz*Comment annotation is not valid Python: %s)�name�	atom_expr�power)
  44  r�childrenrrr�type�get_code�strip�AttributeError�append)Z	decl_textr'�paramsr4�childrrr� _split_comment_param_declarationQs&	�
  45  
  46  �
  47  �r<FcCs�t||�}|s	|s|S|j}|jdkr#t|d�}tt|t|f��g�S|jdkrAt|d�}tt|d�g�|f}tt|t|��g�S|S)Nrr��dict�str)�_infer_paramr*Z
  48  star_countr
  49  rr	r
  50  )�function_value�paramZignore_stars�valuesr*�tuple_�dctZgenericsrrr�infer_paramps(
  51  
  52  
  53  �
  54  
  55  ��rFcCs�|j}|duredd�|jjD�}|jj}t�|�}|durtSt�d|�}|s)tSt|�	d��}|�
  56  |�}t|�t|�krDt�
  57  d||�|��rR|dkrNtS|d8}|t|�krZtS||}	t|��|	�S|��}
  58  t|
  59  |�S)zJ
  60      Infers the type of a function parameter, using type annotations.
  61      NcSsg|]	}|jdkr|�qS)rB)r5)�.0r;rrr�
  62  <listcomp>�s
  63  �z _infer_param.<locals>.<listcomp>z^#\s*type:\s*\(([^#]*)\)\s*->rz&Comments length != Params length %s %sr)rr.r4r�get_following_comment_same_liner�re�matchr<�groupr#rrrZis_bound_methodr(�get_default_param_contextr)rArBr�
  64  all_paramsr'�commentrKZparams_commentsr#Z
  65  param_commentrrrrr@�s<
  66  
  67  ��
  68  r@cCsBi}|��D]}|j}|dur|||jj<q|j}|r||d<|S)N�return)�
  69  get_paramsrr1r!)ZfuncdefrEZfunction_paramZparam_annotation�return_annotationrrr�py__annotations__�s�rScs"�fdd���fdd�|��D�S)NcsF|dus	|jdkr|St��jj�|j��}|durdS|jd}|S)Nr&r)r5rr*Zcompiled_subprocessZsafe_literal_evalr!r4)r')rrr�resolve�s��
  70  z+resolve_forward_references.<locals>.resolvecsi|]	\}}|�|��qSrr)rGr1r')rTrr�
  71  <dictcomp>�sz.resolve_forward_references.<locals>.<dictcomp>)�items)r�all_annotationsr)rrTr�resolve_forward_references�srXc
  72  s�|��}t|t|j��}|�dd�}|dur:|j}t�|�}|dur$tSt�	d|�}|s.tSt
  73  ||�d�����
  74  �St||�}t||�}	|sJ|	�
  75  �St|||��t��fdd�|	D���
  76  �S)zZ
  77      Infers the type of a function's return value,
  78      according to type annotations.
  79      rPNz%^#\s*type:\s*\([^#]*\)\s*->\s*([^#]*)rc3�2�|]}t|ttf�r|���nt|h�VqdS�N��
  80  isinstancerrZdefine_genericsr)rG�ann��
  81  type_var_dictrr�	<genexpr>s���
  82  �z%infer_return_types.<locals>.<genexpr>)rMrXrSr+�getrrIrrJrKr(rLr7�execute_annotation�find_unknown_type_varsr�infer_type_vars_for_executionr�	from_sets)
  83  �function�	argumentsrrWrr'rOrKZunknown_type_varsZannotation_valuesrr^r�infer_return_types�s<�
  84  ��
  85  
  86  ��rhc	Cs�|��}i}t||�}|D]C}z||j}Wn	tyYq
  87  wt||�}|rP|�|�}	|��}
  88  |��}|
  89  tj	ur>|�
  90  �}n
  91  |
  92  tjurH|�d�}t
  93  ||	�|��q
  94  |S)aM
  95      Some functions use type vars that are not defined by the class, but rather
  96      only defined in the function. See for example `iter`. In those cases we
  97      want to:
  98  
  99      1. Search for undefined type vars.
 100      2. Infer type vars with the execution state we have.
 101      3. Return the union of all type vars that have been found.
 102      Z_dict_values)rMrZstring_name�KeyErrorrcrZget_kind�inferr�VAR_POSITIONALZmerge_types_of_iterate�VAR_KEYWORDZ	try_merge�merge_type_var_dicts�infer_type_vars)rfrgZannotation_dictr�annotation_variable_resultsZexecuted_param_namesZexecuted_param_nameZannotation_nodeZannotation_variablesZannotation_value_set�kind�actual_value_setrrrrds0
 103  
 104  �
 105  
 106  
 107  
 108  
 109  
 110  ��rdcsLi�|D]}|jdkrt||���}��|�qt��fdd�|D����S)Nrc3rYrZr[)rG�v�Z
 111  all_type_varsrrr`9s���
 112  �z,infer_return_for_callable.<locals>.<genexpr>)r�_infer_type_vars_for_callabler �updaterrerb)rgZparam_valuesZ
 113  result_values�pvr_rrsr�infer_return_for_callable2s
 114  
 115  ���rwcCsDi}t|��|�D]\\}}}|��}|��}t||�|��q	|S)zn
 116      Infers type vars for the Calllable class:
 117  
 118          def x() -> Callable[[Callable[..., _T]], _T]: ...
 119      )�zip�unpackrjrmrn)rgZlazy_paramsro�_Z
 120  lazy_valueZlazy_callable_paramZcallable_param_valuesrqrrrrtAs�rtc	CsJ|��D]\}}|r"z
 121  |||O<Wqty!|||<YqwqdSrZ)rVri)Z	base_dictZnew_dictZ
 122  type_var_namerCrrrrmSs���rmcCsNi}t|t�s	|S|��}|��}t||�D]\}}t||�|����q|S)aw
 123      Match up the generic parameters from the given argument class to the
 124      target annotation.
 125  
 126      This walks the generic parameters immediately within the annotation and
 127      argument's type, in order to determine the concrete values of the
 128      annotation's parameters for the current case.
 129  
 130      For example, given the following code:
 131  
 132          def values(mapping: Mapping[K, V]) -> List[V]: ...
 133  
 134          for val in values({1: 'a'}):
 135              val
 136  
 137      Then this function should be given representations of `Mapping[K, V]`
 138      and `Mapping[int, str]`, so that it can determine that `K` is `int and
 139      `V` is `str`.
 140  
 141      Note that it is responsibility of the caller to traverse the MRO of the
 142      argument type as needed in order to find the type matching the
 143      annotation (in this case finding `Mapping[int, str]` as a parent of
 144      `Dict[int, str]`).
 145  
 146      Parameters
 147      ----------
 148  
 149      `annotation_value`: represents the annotation to infer the concrete
 150          parameter types of.
 151  
 152      `annotated_argument_class`: represents the annotated class of the
 153          argument being passed to the object annotated by `annotation_value`.
 154      )r\rZget_genericsrxrmrnrb)Zannotation_valueZannotated_argument_classr_Zannotation_genericsZactual_genericsZannotation_generics_setZactual_generic_setrrr�merge_pairwise_generics\s#
 155  �r{cC�t|||jd|�S)Nr��_find_type_from_comment_hintr4�rr'r1rrr�find_type_from_comment_hint_for��r�cCsLt|j�dkr	gSt|jdj�dksJd��|jdjd}t||||�S)N�r�z1Can only be here when children[1] is 'foo() as f'r=)rr4r~)rr'r1�varlistrrr� find_type_from_comment_hint_with�s�r�cCr|)Nrr}rrrr�"find_type_from_comment_hint_assign�r�r�cCs�d}|jdvr!d}|jD]}||krn
 156  |jdkrq|d7}qgSt�|�}|dur,gSt�d|�}|dur8gSt||�d���|��	�S)N)Ztestlist_star_exprZexprlistZtestlistr�operatorrz^#\s*type:\s*([^#]*))
 157  r5r4rrIrJrKr(rLr7rb)rr'r�r1r#r;rOrKrrrr~�s*
 158  
 159  
 160  
 161  
 162  ��r~cs ���fdd��g��|��S)Ncsp|jdvr*|jd}|jdkr$|jddkr&t|jd�D]
 163  }�|�qdSdSdSt��|����dd�<dS)N)r2r3������trailerr�[r)r5r4�_unpack_subscriptlist�_filter_type_varsr)r'r�Zsubscript_node��
 164  check_noder�foundrrr��s
 165  
 166  
 167  �z*find_unknown_type_vars.<locals>.check_noder)rr'rr�rrc�s	rcrcCs2t|�}|D]}t|t�r||vr|�|�q|SrZ)rr\rr9)rr�Z	new_foundZtype_varrrrr��s
 168  �r�ccsN�|jdkr|jddd�D]
 169  }|jdkr|VqdS|jdkr%|VdSdS)N�
 170  subscriptlistr=�	subscript)r5r4)r�r�rrrr��s�
 171  
 172  ��
 173  
 174  �r�rZ)F)r)1�__doc__rJ�inspectrZparsorrZjedi.inference.cacherZjedi.inference.base_valuerrZjedi.inference.gradual.baserr	Zjedi.inference.gradual.genericsr
 175  Zjedi.inference.gradual.type_varrZjedi.inference.helpersrZjedi.inference.compiledr
 176  Zjedi.inference.paramrZjedirrrr(rr<rFr@rSrXrhrdrwrtrmr{r�r�r�r~rcr�r�rrrr�<module>sJ
 177  1
 178  
 179  *&	4
 180