a
    lc<>                     @   sf  d Z ddl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 ddlmZ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 ddlmZ dd Zd;ddZdd Zdd Ze d<ddZ dd Z!dd Z"dd 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: Z0dS )>a  
PEP 0484 ( https://www.python.org/dev/peps/pep-0484/ ) describes type hints
through function annotations. There is a strong suggestion in this document
that only the type of type hinting defined in PEP0484 should be allowed
as annotations in future python versions.
    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_utilsc                 C   sd   |  |}t|dkr,td||f  |S t|d }t|r`t| | }|dur`|  |S |S )z
    Inferes an annotation node. This means that it inferes the part of
    `int` here:

        foo: int = 3

    Also checks for forward references (strings)
       z9Inferred typing index %s should lead to 1 object,  not %sr   N)
infer_nodelenr   warninglistr   _get_forward_reference_nodeZget_safe_value)context
annotation	value_setZinferred_valueresult r   ]/var/www/html/django/DPS/env/lib/python3.9/site-packages/jedi/inference/gradual/annotation.pyinfer_annotation   s    	

r   c                    sD   t | |}|d u rtS | |} d ur@| fdd }|S )Nc                    s   | j dkott|   kS )Ntuple)
array_typer   r   
py__iter__)valueindexr   r   <lambda>8   s    
z*_infer_annotation_string.<locals>.<lambda>)r   r   r   filterZpy__simple_getitem__)r   stringr#   noder   r   r"   r   _infer_annotation_string0   s    


r(   c                 C   sh   z| j jj|ddd}W n" ty:   td|  Y d S 0 | j }t	||j
d  | j|_|S d S )NZ
eval_inputF)Zstart_symbolerror_recoveryzAnnotation not parsed: %sr   )inference_stateZgrammarr   r   r   r   	tree_nodeZget_root_noder   moveend_posparent)r   r&   new_nodemoduler   r   r   r   @   s    

r   c                 C   s   zt | ddjd }W n$ ty:   td|   g  Y S 0 |jdv rT|  gS g }z
|j}W n tyx   g  Y S 0 |D ] }|jdv r~|	|   q~|S )z
    Split decl_text on commas, but group generic expressions
    together.

    For example, given "foo, Bar[baz, biz]" we return
    ['foo', 'Bar[baz, biz]'].

    F)r)   r   z*Comment annotation is not valid Python: %s)name	atom_exprpower)
r   childrenr   r   r   typeget_codestripAttributeErrorappend)Z	decl_textr'   paramsr4   childr   r   r    _split_comment_param_declarationQ   s     	




r<   Fc                 C   s   t | |}|s|s|S | j}|jdkrFt|d}tt|t|fgS |jdkrt|d}tt|dg|f}tt|t|gS |S )Nr   r      dictstr)_infer_paramr*   Z
star_countr   r   r	   r
   )function_valueparamZignore_starsvaluesr*   tuple_dctZgenericsr   r   r   infer_paramp   s(    




rF   c                 C   s   |j }|du rdd |jjD }|jj}t|}|du r>tS td|}|sRtS t|	d}|
|}t|t|krtd|| |  r|dkrtS |d8 }|t|krtS || }	t|  |	S |  }
t|
|S )zJ
    Infers the type of a function parameter, using type annotations.
    Nc                 S   s   g | ]}|j d kr|qS )rB   )r5   ).0r;   r   r   r   
<listcomp>   s   
z _infer_param.<locals>.<listcomp>z^#\s*type:\s*\(([^#]*)\)\s*->r   z&Comments length != Params length %s %sr   )r   r.   r4   r   get_following_comment_same_liner   rematchr<   groupr#   r   r   r   Zis_bound_methodr(   get_default_param_contextr   )rA   rB   r   
all_paramsr'   commentrK   Zparams_commentsr#   Zparam_commentr   r   r   r   r@      s<    

r@   c                 C   sB   i }|   D ]}|j}|d ur|||jj< q| j}|r>||d< |S )Nreturn)
get_paramsr   r1   r!   )ZfuncdefrE   Zfunction_paramZparam_annotationreturn_annotationr   r   r   py__annotations__   s    rS   c                    s"    fddfdd|  D S )Nc                    sF   | d u s| j dkr| S t  jj| j} | d u r8d S | jd } | S )Nr&   r   )r5   r   r*   Zcompiled_subprocessZsafe_literal_evalr!   r4   )r'   )r   r   r   resolve   s    
z+resolve_forward_references.<locals>.resolvec                    s   i | ]\}}| |qS r   r   )rG   r1   r'   )rT   r   r   
<dictcomp>       z.resolve_forward_references.<locals>.<dictcomp>)items)r   all_annotationsr   )r   rT   r   resolve_forward_references   s    rY   c           
         s   |   }t|t| j}|dd}|du rt| j}t|}|du rHtS t	d|}|s\tS t
||d  S t||}t||}	|s|	 S t| || t fdd|	D  S )zZ
    Infers the type of a function's return value,
    according to type annotations.
    rP   Nz%^#\s*type:\s*\([^#]*\)\s*->\s*([^#]*)r   c                 3   s0   | ](}t |ttfr| nt|hV  qd S N
isinstancer   r   Zdefine_genericsr   )rG   anntype_var_dictr   r   	<genexpr>  s   z%infer_return_types.<locals>.<genexpr>)rM   rY   rS   r+   getr   rI   r   rJ   rK   r(   rL   r7   execute_annotationfind_unknown_type_varsr   infer_type_vars_for_executionr   	from_sets)
function	argumentsr   rX   r   r'   rO   rK   Zunknown_type_varsZannotation_valuesr   r^   r   infer_return_types   s4    


rh   c              	   C   s   |   }i }t| |}|D ]}z||j }W n tyB   Y qY n0 t||}|r||}	| }
| }|
tj	u r|
 }n|
tju r|d}t||	| q|S )aM  
    Some functions use type vars that are not defined by the class, but rather
    only defined in the function. See for example `iter`. In those cases we
    want to:

    1. Search for undefined type vars.
    2. Infer type vars with the execution state we have.
    3. Return the union of all type vars that have been found.
    Z_dict_values)rM   r   Zstring_nameKeyErrorrc   r   Zget_kindinferr   VAR_POSITIONALZmerge_types_of_iterateVAR_KEYWORDZ	try_mergemerge_type_var_dictsinfer_type_vars)rf   rg   Zannotation_dictr   annotation_variable_resultsZexecuted_param_namesZexecuted_param_nameZannotation_nodeZannotation_variablesZannotation_value_setkindactual_value_setr   r   r   rd     s,    








rd   c                    sL   i  |D ]&}|j dkrt| | } | qt fdd|D  S )Nr   c                 3   s0   | ](}t |ttfr| nt|hV  qd S rZ   r[   )rG   vZall_type_varsr   r   r`   9  s   z,infer_return_for_callable.<locals>.<genexpr>)r   _infer_type_vars_for_callabler    updater   re   rb   )rg   Zparam_valuesZresult_valuespvr_   r   rs   r   infer_return_for_callable2  s    
rw   c                 C   sD   i }t |  |D ],\\}}}| }| }t||| q|S )zn
    Infers type vars for the Calllable class:

        def x() -> Callable[[Callable[..., _T]], _T]: ...
    )zipunpackrj   rm   rn   )rg   Zlazy_paramsro   _Z
lazy_valueZlazy_callable_paramZcallable_param_valuesrq   r   r   r   rt   A  s    rt   c              	   C   sJ   |  D ]<\}}|rz| |  |O  < W q tyB   || |< Y q0 qd S rZ   )rW   ri   )Z	base_dictZnew_dictZtype_var_namerC   r   r   r   rm   S  s    rm   c                 C   sN   i }t |ts|S |  }| }t||D ]\}}t|||  q,|S )aw  
    Match up the generic parameters from the given argument class to the
    target annotation.

    This walks the generic parameters immediately within the annotation and
    argument's type, in order to determine the concrete values of the
    annotation's parameters for the current case.

    For example, given the following code:

        def values(mapping: Mapping[K, V]) -> List[V]: ...

        for val in values({1: 'a'}):
            val

    Then this function should be given representations of `Mapping[K, V]`
    and `Mapping[int, str]`, so that it can determine that `K` is `int and
    `V` is `str`.

    Note that it is responsibility of the caller to traverse the MRO of the
    argument type as needed in order to find the type matching the
    annotation (in this case finding `Mapping[int, str]` as a parent of
    `Dict[int, str]`).

    Parameters
    ----------

    `annotation_value`: represents the annotation to infer the concrete
        parameter types of.

    `annotated_argument_class`: represents the annotated class of the
        argument being passed to the object annotated by `annotation_value`.
    )r\   r   Zget_genericsrx   rm   rn   rb   )Zannotation_valueZannotated_argument_classr_   Zannotation_genericsZactual_genericsZannotation_generics_setZactual_generic_setr   r   r   merge_pairwise_generics\  s    #
r{   c                 C   s   t | ||jd |S )Nr   _find_type_from_comment_hintr4   r   r'   r1   r   r   r   find_type_from_comment_hint_for  s    r   c                 C   s:   t |jd jdksJ d|jd jd }t| |||S )Nr      z1Can only be here when children[1] is 'foo() as f'r=   )r   r4   r}   )r   r'   r1   varlistr   r   r    find_type_from_comment_hint_with  s
    r   c                 C   s   t | ||jd |S )Nr   r|   r~   r   r   r   "find_type_from_comment_hint_assign  s    r   c                 C   s   d }|j dv rBd}|jD ]$}||kr( qB|j dkr4q|d7 }qg S t|}|d u rXg S td|}|d u rpg S t| |d |	 S )N)Ztestlist_star_exprZexprlistZtestlistr   operatorr   z^#\s*type:\s*([^#]*))
r5   r4   r   rI   rJ   rK   r(   rL   r7   rb   )r   r'   r   r1   r#   r;   rO   rK   r   r   r   r}     s&    




r}   c                    s     fdd g  | S )Nc                    sf   | j dv rJ| jd }|j dkrb|jd dkrbt|jd D ]} | q:nt| d d < d S )N)r2   r3   trailerr   [r   )r5   r4   _unpack_subscriptlist_filter_type_varsr   )r'   r   Zsubscript_node
check_noder   foundr   r   r     s    

z*find_unknown_type_vars.<locals>.check_noder   )r   r'   r   r   r   rc     s    	rc   r   c                 C   s2   t |}| D ] }t|tr||vr|| q|S rZ   )r   r\   r   r9   )r   r   Z	new_foundZtype_varr   r   r   r     s
    r   c                 c   sF   | j dkr2| jd d d D ]}|j dkr|V  qn| j dkrB| V  d S )Nsubscriptlistr=   	subscript)r5   r4   )r   r   r   r   r   r     s    



r   )N)F)r   )1__doc__rJ   inspectr   Zparsor   r   Zjedi.inference.cacher   Zjedi.inference.base_valuer   r   Zjedi.inference.gradual.baser   r	   Zjedi.inference.gradual.genericsr
   Zjedi.inference.gradual.type_varr   Zjedi.inference.helpersr   Zjedi.inference.compiledr   Zjedi.inference.paramr   Zjedir   r   r   r(   r   r<   rF   r@   rS   rY   rh   rd   rw   rt   rm   r{   r   r   r   r}   rc   r   r   r   r   r   r   <module>   sH   
1
*&	4
