
    Mh>                     r   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 Zd&dZd Zd Z e            d'd            Z d Z!d Z"d Z# e            d             Z$d Z%d Z&d Z'd Z(d Z)d Z*d Z+d  Z,d! Z-d" Z.d(d$Z/d% 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                 H   |                      |          }t          |          dk    rt          j        d|d|           |S t	          |          d         }t          |          r9t          | |                                          }||                      |          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)
       zInferred typing index z should lead to 1 object,  not r   )
infer_nodelenr   warninglistr   _get_forward_reference_nodeget_safe_value)context
annotation	value_setinferred_valueresults        a/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/jedi/inference/gradual/annotation.pyinfer_annotationr       s     "":..I
9~~#-::yy: 	; 	; 	;)__Q'N   .,Wn6S6S6U6UVV%%f---    c                     t          | |          }|t          S |                     |          }+|                    fd                                        }|S )Nc                 ~    | j         dk    o1t          t          |                                                     k    S )Ntuple)
array_typer   r   
py__iter__)valueindexs    r   <lambda>z*_infer_annotation_string.<locals>.<lambda>8   s<     G+ ;U--//0011U: r!   )r   r   r   filterpy__simple_getitem__)r   stringr(   noder   s     `  r   _infer_annotation_stringr.   0   sz    &w77D|""4((I$$   
 

 
u
%
% 	 r!   c                 &   	 | j         j                            |dd          }| j                                        }t          j        ||j        d                    | j        |_        |S # t          $ r t          j        d|z             Y d S w xY w)N
eval_inputF)start_symbolerror_recoveryr   zAnnotation not parsed: %s)inference_stategrammarr   	tree_nodeget_root_noder   moveend_posparentr   r   r   )r   r,   new_nodemodules       r   r   r   @   s    *288%  9 
 
 "0022(FN1$5666!+    1F:;;;tts   "A+ +!BBc                    	 t          | d          j        d         }n)# t          $ r t          j        d| z             g cY S w xY w|j        dv r'|                                                                gS g }	 |j        }|D ]D}|j        dv r9|                    |                                                                           En# t          $ r g cY S w xY w|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)r2   r   z*Comment annotation is not valid Python: %s)name	atom_exprpower)
r   childrenr   r   r   typeget_codestripappendAttributeError)	decl_textr-   paramsr@   childs        r    _split_comment_param_declarationrI   Q   s   Yu555>qA   BYNOOO			 y222%%''((F8=  	8 	8Ez;;;enn..4466777	8    			 Ms!    #AA;C
 
CCFc                    t          | |          }|s|s|S | j        }|j        dk    r<t          |d          }t	          t          |t          |f                    g          S |j        dk    r[t          |d          }t	          t          |d          g          |f}t	          t          |t          |                    g          S |S )Nr   r$      dictstr)_infer_paramr3   
star_countr   r   r
   r   )function_valueparamignore_starsvaluesr3   tuple_dctgenericss           r   infer_paramrW   p   s    .%00F 6 $4O1"?G<<	**
 
    	 
	Q		88'??@AA
 ))
 
    	 Mr!   c                    |j         }|4d |j        j        D             }|j        j        }t          j        |          }|t
          S t          j        d|          }|st
          S t          |	                    d                    }|
                    |          }t          |          t          |          k    rt          j        d||           |                                 r|dk    rt
          S |dz  }|t          |          k    rt
          S ||         }	t          |                                 |	          S |                                 }
t#          |
|          S )zJ
    Infers the type of a function parameter, using type annotations.
    Nc                 (    g | ]}|j         d k    |S )rQ   )rA   ).0rH   s     r   
<listcomp>z _infer_param.<locals>.<listcomp>   s.     0 0 0w.. ...r!   z^#\s*type:\s*\(([^#]*)\)\s*->r   z&Comments length != Params length %s %sr   )r   r9   r@   r   get_following_comment_same_liner   rematchrI   groupr(   r   r   r   is_bound_methodr.   get_default_param_contextr    )rP   rQ   r   
all_paramsr-   commentr^   params_commentsr(   param_commentr   s              r   rN   rN      sv    !J0 0)> 0 0 0
 |">tDD?97CC 	:5;;q>>JJ   '' 3z??22M8   ))++ 	zz  QJEC(((('.'4466
 
 	

 6688GGZ000r!   c                     i }|                                  D ]}|j        }||||j        j        <   | j        }|r||d<   |S )Nreturn)
get_paramsr   r=   r'   )funcdefrU   function_paramparam_annotationreturn_annotations        r   py__annotations__rm      sa    
C!,,.. > >)4'-=C#)** *)HJr!   c                 P      fdfd|                                 D             S )Nc                     | | j         dk    r| S t          j        j                            | j                            } | d S | j        d         } | S )Nr,   r   )rA   r   r3   compiled_subprocesssafe_literal_evalr'   r@   )r-   r   s    r   resolvez+resolve_forward_references.<locals>.resolve   sh    <4900K*#7II
 
 
 <4
 }Qr!   c                 .    i | ]\  }}| |          S  rt   )rZ   r=   r-   rr   s      r   
<dictcomp>z.resolve_forward_references.<locals>.<dictcomp>   s'    JJJJD$D''$--JJJr!   )items)r   all_annotationsrr   s   ` @r   resolve_forward_referencesrx      sF        , KJJJ/2G2G2I2IJJJJr!   c                   
 |                                  }t          |t          | j                            }|                    dd          }|| j        }t          j        |          }|t          S t          j	        d|          }|st          S t          ||                    d                                                                                    S t          ||          }t          ||          }	|s|	                                S t!          | ||          
t#          j        
fd|	D                                                       S )zZ
    Infers the type of a function's return value,
    according to type annotations.
    rg   Nz%^#\s*type:\s*\([^#]*\)\s*->\s*([^#]*)r   c              3      K   | ]E}t          |t          t          f          r|                              nt	          |h          V  Fd S N
isinstancer	   r   define_genericsr   )rZ   anntype_var_dicts     r   	<genexpr>z%infer_return_types.<locals>.<genexpr>  sm          c2G<==	SM***CKSE??     r!   )ra   rx   rm   r5   getr   r\   r   r]   r^   r.   r_   rC   execute_annotationfind_unknown_type_varsr    infer_type_vars_for_executionr   	from_sets)function	argumentsr   rw   r   r-   rc   r^   unknown_type_varsannotation_valuesr   s             @r   infer_return_typesr      sl    0022G0(,-- O !$$Xt44J !>tDD?A7KK 	'KKNN  ""
 
 


	
 /w
CC(*== 6 335551(IWWM     %     	r!   c                 
   |                                  }i }t          | |          }|D ]}	 ||j                 }n# t          $ r Y w xY wt	          ||          }|r|                    |          }	|                                }
|                                }|
t          j	        u r|
                                }n#|
t          j        u r|                    d          }t          ||	                    |                     |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.
    _dict_values)ra   r   string_nameKeyErrorr   r   get_kindinferr   VAR_POSITIONALmerge_types_of_iterateVAR_KEYWORD	try_mergemerge_type_var_dictsinfer_type_vars)r   r   annotation_dictr   annotation_variable_resultsexecuted_param_namesexecuted_param_nameannotation_nodeannotation_variablesannotation_value_setkindactual_value_sets               r   r   r     s6    0022G"$3HiHH3  	-.A.MNOO 	 	 	H	  6gOO 	#*#5#5o#F#F &//11D288::y///#3#J#J#L#L  ...#3#=#=n#M#M  +$445EFF   '&s   :
AAc                     i |D ]D}|j         dk    r7t          | |                                          }                    |           Et	          j        fd|D                                                       S )Nr   c              3      K   | ]E}t          |t          t          f          r|                              nt	          |h          V  Fd S r{   r|   )rZ   vall_type_varss     r   r   z,infer_return_for_callable.<locals>.<genexpr>9  sl          a0':;;	-(((qc]]     r!   )r%   _infer_type_vars_for_callabler&   updater   r   r   )r   param_valuesresult_valuespvr   r   s        @r   infer_return_for_callabler   2  s    M 0 0=F""9)R]]__UUM  ///     	    
 r!   c                     i }t          |                                 |          D ]S\  \  }}}|                                }|                                }t          ||                    |                     T|S )zn
    Infers type vars for the Calllable class:

        def x() -> Callable[[Callable[..., _T]], _T]: ...
    )zipunpackr   r   r   )r   lazy_paramsr   _
lazy_valuelazy_callable_paramcallable_param_valuesr   s           r   r   r   A  s     #%03I4D4D4F4F0T0T 
 
,J, 3 9 9 ; ;%++--'!112BCC	
 	
 	
 	
 '&r!   c                     |                                 D ].\  }}|r'	 | |xx         |z  cc<   # t          $ r || |<   Y *w xY w/d S r{   )rv   r   )	base_dictnew_dicttype_var_namerS   s       r   r   r   S  s    !)!1!1 2 2v 	22-(((F2(((( 2 2 2+1	-(((2	22 2s   .A A c                    i }t          |t                    s|S |                                 }|                                }t          ||          D ]:\  }}t	          ||                    |                                                     ;|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	   get_genericsr   r   r   r   )annotation_valueannotated_argument_classr   annotation_genericsactual_genericsannotation_generics_setactual_generic_sets          r   merge_pairwise_genericsr   \  s    F M.0FGG *7799.;;==O7:;NP_7`7` 
 
3!3#334F4Y4Y4[4[\\	
 	
 	
 	

 r!   c                 <    t          | ||j        d         |          S )Nr   _find_type_from_comment_hintr@   r   r-   r=   s      r   find_type_from_comment_hint_forr         't}Q7GNNNr!   c                     t          |j                  dk    rg S t          |j        d         j                  dk    s
J d            |j        d         j        d         }t          | |||          S )N   r      z1Can only be here when children[1] is 'foo() as f'rK   )r   r@   r   )r   r-   r=   varlists       r    find_type_from_comment_hint_withr     st    
4=A 	t}Q())Q...; /..mA'*G'wEEEr!   c                 <    t          | ||j        d         |          S )Nr   r   r   s      r   "find_type_from_comment_hint_assignr     r   r!   c                 X   d }|j         dv r'd}|j        D ]}||k    r n|j         dk    r|dz  }g S t          j        |          }|g S t	          j        d|          }|g S t          | |                    d                                          |          	                                S )N)testlist_star_exprexprlisttestlistr   operatorr   z^#\s*type:\s*([^#]*))
rA   r@   r   r\   r]   r^   r.   r_   rC   r   )r   r-   r   r=   r(   rH   rc   r^   s           r   r   r     s    E|EEE% 	 	E}}zZ''QJEEI:4@@G	H,g66E}	#Q%%'' r!   c                 4      fdg  |           S )Nc                    | j         dv rW| j        d         }|j         dk    r9|j        d         dk    r*t          |j        d                   D ]} |           d S d S d S t                              |                     d d <   d S )N)r>   r?   trailerr   [r   )rA   r@   _unpack_subscriptlist_filter_type_varsr   )r-   r   subscript_node
check_noder   founds      r   r   z*find_unknown_type_vars.<locals>.check_node  s    9...mB'G|y((W-=a-@C-G-G&;G<LQ<O&P&P / /NJ~.... )(-G-G/ / )););D)A)A5IIE!!!HHHr!   rt   )r   r-   r   r   s   ` @@r   r   r     sK    J J J J J J J EJtLr!   rt   c                     t          |          }| D ]0}t          |t                    r||vr|                    |           1|S r{   )r   r}   r   rD   )r   r   	new_foundtype_vars       r   r   r     sS    UI ' 'h(( 	'XU-B-BX&&&r!   c              #      K   | j         dk    r$| j        d d d         D ]}|j         dk    r|V  d S | j         dk    r| V  d S d S )NsubscriptlistrK   	subscript)rA   r@   )r   r   s     r   r   r     s|      _,,&/!4 	  	 I~,,	  	  ,, -,r!   r{   )F)rt   )1__doc__r]   inspectr   parsor   r   jedi.inference.cacher   jedi.inference.base_valuer   r   jedi.inference.gradual.baser	   r
   jedi.inference.gradual.genericsr   jedi.inference.gradual.type_varr   jedi.inference.helpersr   jedi.inference.compiledr   jedi.inference.paramr   jedir   r   r    r.   r   rI   rW   rN   rm   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   rt   r!   r   <module>r      s    
			       * * * * * * * * = = = = = = 9 9 9 9 9 9 9 9 L L L L L L L L ? ? ? ? ? ? 3 3 3 3 3 3 , , , , , , 5 5 5 5 5 5 9 9 9 9 9 9              .      "  >     0.1 .1 .1b
 
 
K K K4 ' '  'T#' #' #'L  ' ' '$2 2 21 1 1hO O OF F FO O O  2              r!   