o
    ˷e-G                     @   s2  d 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	lmZ dd
lmZ ddlmZ e ZG dd deZG dd dZG dd deZd&ddZG dd deZG dd deZG dd deZG dd deZG dd dZ d d! Z!G d"d# d#Z"e"g Z#d$d% Z$dS )'a4  
Values are the "values" that Python would return. However Values are at the
same time also the "values" that a user is currently sitting in.

A ValueSet is typically used to specify the return of a function or any other
static analysis operation. In jedi there are always multiple returns and not
just one.
    )reduce)add)zip_longest)Name)debug)clean_scope_docstringSimpleGetItemNotFound)safe_property)%inference_state_as_method_param_cache)memoize_methodc                   @   s   e Zd ZdS )HasNoContextN)__name__
__module____qualname__ r   r   P/var/www/ideatree/venv/lib/python3.10/site-packages/jedi/inference/base_value.pyr      s    r   c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zd"ddZdd Z	d#ddZ
		d$ddZdd Zdd Zd"ddZdd Zdd Zed d! ZdS )%HelperValueMixinc                 C   s.   | }|j d u r| S 	 |j d u r|S |j }qN)parent_context
as_context)selfvaluer   r   r   get_root_context   s   

z!HelperValueMixin.get_root_contextc                 C   s   | j j| |dS )N	argumentsinference_stateexecuter   r   r   r   r   r   (      zHelperValueMixin.executec                 G   s,   ddl m} |dd |D }| j| |S )Nr   )ValuesArgumentsc                 S   s   g | ]}t |gqS r   ValueSet.0r   r   r   r   
<listcomp>-   s    z8HelperValueMixin.execute_with_values.<locals>.<listcomp>)jedi.inference.argumentsr!   r   r   )r   
value_listr!   r   r   r   r   execute_with_values+   s   z$HelperValueMixin.execute_with_valuesc                 C   s   |   S r   r)   r   r   r   r   execute_annotation0      z#HelperValueMixin.execute_annotationc                 C   s
   t | gS r   r"   r+   r   r   r   gather_annotation_classes3      
z*HelperValueMixin.gather_annotation_classesNFc                 C   s   t dd | ||D S )Nc                 s       | ]}|  V  qd S r   inferr%   
lazy_valuer   r   r   	<genexpr>7   
    
z:HelperValueMixin.merge_types_of_iterate.<locals>.<genexpr>r#   	from_setsiterate)r   contextualized_nodeis_asyncr   r   r   merge_types_of_iterate6   s   

z'HelperValueMixin.merge_types_of_iteratec                 c   sd    t |tr|nd }| j|dE d H  |  r.ddlm} |t| hD ]}| E d H  q$d S d S )N)origin_scoper   )convert_values)
isinstancer   get_filtersis_stub!jedi.inference.gradual.conversionr>   r#   )r   name_or_strr=   r>   cr   r   r   _get_value_filters<   s   z#HelperValueMixin._get_value_filtersTc                 C   s6   ddl m} | |}|||}td|| | |S )Nr   )finderzcontext.goto %s in (%s): %s)jedi.inferencerF   rE   filter_namer   dbg)r   rC   name_contextanalysis_errorsrF   filtersnamesr   r   r   gotoE   s
   
zHelperValueMixin.gotoc           	      C   s   |du r| }|  |||}tdd |D }|s(t|tr!|jn|}| |}|s@|s@|r@t|tr@ddlm} |	|| | t
d|| |S )zZ
        :param position: Position of the last statement -> tuple of line, column
        Nc                 s   r0   r   r1   )r%   namer   r   r   r5   T       z6HelperValueMixin.py__getattribute__.<locals>.<genexpr>r   analysisz context.names_to_types: %s -> %s)rN   r#   r8   r?   r   r   py__getattribute__alternativesrG   rR   add_attribute_errorr   rI   )	r   rC   rJ   positionrK   rM   valuesnrR   r   r   r   py__getattribute__L   s   

z#HelperValueMixin.py__getattribute__c                 C   s"   |  d}|std|  | S )N	__await__z"Tried to run __await__ on value %s)rX   r   warningr)   )r   await_value_setr   r   r   py__await__a   s   
zHelperValueMixin.py__await__c                 C   s   | j jS r   )rO   string_namer+   r   r   r   
py__name__g   r-   zHelperValueMixin.py__name__c                 C   sV   t d|  |r&ddlm} t|| d d d  gS | |S )Nz
iterate %sr   )LazyKnownValues	__aiter__	__anext__rY   )	r   rI   jedi.inference.lazy_valuer_   iterrX   r)   py__stop_iteration_returns
py__iter__)r   r:   r;   r_   r   r   r   r9   j   s   


zHelperValueMixin.iteratec                 C   s   t jd| |f dd- |  D ]}||r't jddd  W d    dS qt jddd 	 W d    dS 1 s;w   Y  d S )Nzsubclass matching of %s <=> %sBLUE)colorzmatched subclass TrueTzmatched subclass FalseF)r   increase_indent_cm	py__mro__is_same_classrI   )r   class_valueclsr   r   r   is_sub_class_ofz   s   
$z HelperValueMixin.is_sub_class_ofc                 C   s"   t |jtjkr|| S | |kS r   )typerj   r   )r   class2r   r   r   rj      s   
zHelperValueMixin.is_same_classc                 O   s   | j |i |S r   )_as_contextr   argskwargsr   r   r   r      s   zHelperValueMixin.as_contextNF)NT)NNT)r   r   r   r   r   r)   r,   r.   r<   rE   rN   rX   r\   r^   r9   rm   rj   r   r   r   r   r   r   r      s&    


	


r   c                   @   s  e Zd ZdZdZdZdZdAddZdd Zdd	 Z	dAd
dZ
dAddZdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zefd&d'Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Z d6d7 Z!d8d9 Z"e#d:d; Z$dBd=d>Z%d?d@ Z&dS )CValuez*
    To be implemented by subclasses.
    Nnot_defined_please_report_bugc                 C      || _ || _d S r   )r   r   )r   r   r   r   r   r   __init__      
zValue.__init__c                 C   s*   ddl m} |j|jd|jd|  d tS )Nr   rQ   ztype-error-not-subscriptablez+TypeError: '%s' object is not subscriptablemessage)rG   rR   r   contextnode	NO_VALUES)r   index_value_setr:   rR   r   r   r   py__getitem__   s   zValue.py__getitem__c                 C      t r   r   )r   indexr   r   r   py__simple_getitem__      zValue.py__simple_getitem__c                 C   s6   |d urddl m} |j|jd|jd|  d tg S )Nr   rQ   ztype-error-not-iterablez&TypeError: '%s' object is not iterablerz   )rG   rR   r   r|   r}   rc   )r   r:   rR   r   r   r   re      s   zValue.py__iter__c                 C   s
   |  |S r   )re   )r   r:   r   r   r   
py__next__   r/   zValue.py__next__c                 C   s   g S r   r   r+   r   r   r   get_signatures   r   zValue.get_signaturesc                 C      dS rt   r   r+   r   r   r   is_class   r   zValue.is_classc                 C   r   rt   r   r+   r   r   r   is_class_mixin   r   zValue.is_class_mixinc                 C   r   rt   r   r+   r   r   r   is_instance   r   zValue.is_instancec                 C   r   rt   r   r+   r   r   r   is_function   r   zValue.is_functionc                 C   r   rt   r   r+   r   r   r   	is_module   r   zValue.is_modulec                 C   r   rt   r   r+   r   r   r   is_namespace   r   zValue.is_namespacec                 C   r   rt   r   r+   r   r   r   is_compiled   r   zValue.is_compiledc                 C   r   rt   r   r+   r   r   r   is_bound_method   r   zValue.is_bound_methodc                 C   r   rt   r   r+   r   r   r   is_builtins_module   r   zValue.is_builtins_modulec                 C   r   )z
        Since Wrapper is a super class for classes, functions and modules,
        the return value will always be true.
        Tr   r+   r   r   r   
py__bool__   s   zValue.py__bool__c                 C   s,   z| j j W n
 ty   Y dS w t| j S )N )	tree_nodeget_doc_nodeAttributeErrorr   r+   r   r   r   	py__doc__   s   
zValue.py__doc__c                 C   s   |t u r
td|  |S )Nz'There exists no safe value for value %s)sentinel
ValueError)r   defaultr   r   r   get_safe_value   s   zValue.get_safe_valuec                 C   s   t d|| | tS )Nz!%s not possible between %s and %sr   rZ   r~   )r   otheroperatorr   r   r   execute_operation   s   zValue.execute_operationc                 C      t d|  tS )Nzno execution possible %sr   r   r   r   r   
py__call__      zValue.py__call__c                 C   r   )Nz0Not possible to return the stop iterations of %sr   r+   r   r   r   rd      r   z Value.py__stop_iteration_returnsc                 C      t S )zH
        For now a way to add values in cases like __getattr__.
        )r~   )r   rC   r   r   r   rS      s   z$Value.py__getattribute__alternativesc                 C   s   t d|  t| gS )NzNo __get__ defined on %s)r   rZ   r#   )r   instancerk   r   r   r   	py__get__   s   
zValue.py__get__c                 C   r   r   )NotImplemented)r   calling_instancer   rk   r   r   r   py__get__on_class   r   zValue.py__get__on_classc                 C      d S r   r   r+   r   r   r   get_qualified_names     zValue.get_qualified_namesc                 C   
   | j  S r   )r   rA   r+   r   r   r   rA     s   
zValue.is_stubc                 C   r   r   )r   r+   r   r   r   rp     r   zValue._as_contextc                 C   r   r   NotImplementedErrorr+   r   r   r   rO     r   z
Value.nameTc                 C   r   r   r   )r   add_class_infor   r   r   get_type_hint  r   zValue.get_type_hintc                 C   s   i S )ap  
        When the current instance represents a type annotation, this method
        tries to find information about undefined type vars and returns a dict
        from type var name to value set.

        This is for example important to understand what `iter([1])` returns.
        According to typeshed, `iter` returns an `Iterator[_T]`:

            def iter(iterable: Iterable[_T]) -> Iterator[_T]: ...

        This functions would generate `int` for `_T` in this case, because it
        unpacks the `Iterable`.

        Parameters
        ----------

        `self`: represents the annotation of the current parameter to infer the
            value for. In the above example, this would initially be the
            `Iterable[_T]` of the `iterable` parameter and then, when recursing,
            just the `_T` generic parameter.

        `value_set`: represents the actual argument passed to the parameter
            we're inferrined for, or (for recursive calls) their types. In the
            above example this would first be the representation of the list
            `[1]` and then, when recursing, just of `1`.
        r   )r   	value_setr   r   r   infer_type_vars  s   zValue.infer_type_varsr   T)'r   r   r   __doc__r   
array_typeapi_typerx   r   r   re   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rd   rS   r   r   r   rA   rp   propertyrO   r   r   r   r   r   r   ru      sH    





ru   NFc                 C   s   t dd | j||dD S )z
    Calls `iterate`, on all values but ignores the ordering and just returns
    all values that the iterate functions yield.
    c                 s   r0   r   r1   r3   r   r   r   r5   8  r6   z!iterate_values.<locals>.<genexpr>r;   r7   )rV   r:   r;   r   r   r   iterate_values3  s   
r   c                   @   s2   e Zd Zedd Zee dd Zdd ZdS )_ValueWrapperBasec                 C   sB   ddl m} | jj}|jd ur|| |jS ddlm} || |jS )Nr   )	ValueName)CompiledValueName)jedi.inference.namesr   _wrapped_valuerO   	tree_namejedi.inference.compiledr   r]   )r   r   wrapped_namer   r   r   r   rO   ?  s   
z_ValueWrapperBase.namec                 O   s   | |i |S r   r   )rl   r   rr   rs   r   r   r   create_cachedI  s   z_ValueWrapperBase.create_cachedc                 C   s   |dksJ dt | j|S )Nr   zProblem with _get_wrapped_value)getattrr   )r   rO   r   r   r   __getattr__N  s   z_ValueWrapperBase.__getattr__N)	r   r   r   r
   rO   classmethodr   r   r   r   r   r   r   r   >  s    
	r   c                   @   s,   e Zd Zeedd Zdd Zdd ZdS )LazyValueWrapperc                 C   s6   t d |  W  d    S 1 sw   Y  d S )NzResolve lazy value wrapper)r   rh   _get_wrapped_valuer+   r   r   r   r   T  s   $zLazyValueWrapper._wrapped_valuec                 C   s   d| j j S )Nz<%s>)	__class__r   r+   r   r   r   __repr__Z     zLazyValueWrapper.__repr__c                 C   r   r   r   r+   r   r   r   r   ]  r   z#LazyValueWrapper._get_wrapped_valueN)r   r   r   r
   r   r   r   r   r   r   r   r   r   S  s    r   c                   @   s   e Zd Zdd Zdd ZdS )ValueWrapperc                 C   s
   || _ d S r   )r   )r   wrapped_valuer   r   r   rx   b  r/   zValueWrapper.__init__c                 C      d| j j| jf S )Nz%s(%s))r   r   r   r+   r   r   r   r   e     zValueWrapper.__repr__N)r   r   r   rx   r   r   r   r   r   r   a  s    r   c                       s$   e Zd Z fddZdd Z  ZS )	TreeValuec                    s   t  || || _d S r   )superrx   r   )r   r   r   r   r   r   r   rx   j  s   
zTreeValue.__init__c                 C   r   )Nz<%s: %s>)r   r   r   r+   r   r   r   r   n  r   zTreeValue.__repr__)r   r   r   rx   r   __classcell__r   r   r   r   r   i  s    r   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
ContextualizedNodec                 C   rw   r   )r|   r}   )r   r|   r}   r   r   r   rx   s  ry   zContextualizedNode.__init__c                 C   r   r   )r|   r   r+   r   r   r   r   w  r/   z#ContextualizedNode.get_root_contextc                 C   s   | j | jS r   )r|   
infer_noder}   r+   r   r   r   r2   z  s   zContextualizedNode.inferc                 C   s   d| j j| j| jf S )Nz<%s: %s in %s>)r   r   r}   r|   r+   r   r   r   r   }     zContextualizedNode.__repr__N)r   r   r   rx   r   r2   r   r   r   r   r   r   r  s
    r   c              	   C   s   t }t }|D ]+}|jd d}t|tttttfv r-z	|| 	|O }W q t
y,   Y nw || q|s7|sA|| t||O }td| |S )N)r   zpy__getitem__ result: %s)r~   setr   rn   floatintstrslicebytesr   r	   r   r   r#   r   rI   )r   index_valuesr:   resultunused_valuesindex_valuer   r   r   r   _getitem  s&   r   c                   @   s   e Zd Zdd Ze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dd Zdd Zdd Zdd Zdd Zd8d!d"Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd9d4d5Zd6d7 ZdS ):r#   c                 C   s&   t || _|D ]	}t|trJ qd S r   )	frozenset_setr?   r#   )r   iterabler   r   r   r   rx     s   
zValueSet.__init__c                 C   s   |  | }||_|S r   )__new__r   )rl   
frozenset_r   r   r   r   _from_frozen_set  s   
zValueSet._from_frozen_setc                 C   s@   t  }|D ]}t|tr||jO }q|t|O }q| t|S )z7
        Used to work with an iterable of set.
        )r   r?   r#   r   r   r   )rl   sets
aggregatedset_r   r   r   r8     s   
zValueSet.from_setsc                 C   s   |  | j|jB S r   r   r   r   r   r   r   r   __or__  r   zValueSet.__or__c                 C   s   |  | j|j@ S r   r   r   r   r   r   __and__  r   zValueSet.__and__c                 C   
   t | jS r   )rc   r   r+   r   r   r   __iter__  r/   zValueSet.__iter__c                 C   r   r   )boolr   r+   r   r   r   __bool__  r/   zValueSet.__bool__c                 C   r   r   )lenr   r+   r   r   r   __len__  r/   zValueSet.__len__c                 C   s   dd dd | jD  S )NzS{%s}, c                 s   s    | ]}t |V  qd S r   )r   )r%   sr   r   r   r5     rP   z$ValueSet.__repr__.<locals>.<genexpr>)joinr   r+   r   r   r   r        zValueSet.__repr__c                 C   s   |  t|| jS r   )r   filterr   )r   filter_funcr   r   r   r     r   zValueSet.filterc                    s    fdd}|S )Nc                     s     fddjD S )Nc                 3   s$    | ]}t | i V  qd S r   )r   r$   )rr   rs   rO   r   r   r5     s
    
z7ValueSet.__getattr__.<locals>.mapper.<locals>.<genexpr>)r8   r   rr   rs   rO   r   r   r   mapper  s   z$ValueSet.__getattr__.<locals>.mapperr   )r   rO   r   r   r   r   r     s   zValueSet.__getattr__c                 C   s   | j |j kS r   r   r   r   r   r   __eq__  r   zValueSet.__eq__c                 C   s   |  | S r   )r   r   r   r   r   __ne__  r   zValueSet.__ne__c                 C   r   r   )hashr   r+   r   r   r   __hash__  r/   zValueSet.__hash__c                 C   s   t dd | jD S )Nc                 s   r0   r   )py__class__r%   rD   r   r   r   r5     rP   z'ValueSet.py__class__.<locals>.<genexpr>)r#   r   r+   r   r   r   r    s   zValueSet.py__class__NFc                 #   sJ    ddl m}  fdd| jD }t| D ]}|dd |D V  qd S )Nr   )get_merged_lazy_valuec                    s   g | ]	}|j  d qS )r   )r9   r  r:   r;   r   r   r&     s    z$ValueSet.iterate.<locals>.<listcomp>c                 S   s   g | ]}|d ur|qS r   r   )r%   lr   r   r   r&         )rb   r  r   r   )r   r:   r;   r  
type_iterslazy_valuesr   r  r   r9     s   zValueSet.iteratec                    s   t  fdd| jD S )Nc                 3   s    | ]
}|j | V  qd S r   r   r  r   r   r   r5     s    z#ValueSet.execute.<locals>.<genexpr>r#   r8   r   r   r   r   r   r     r   zValueSet.executec                       t  fdd| jD S )Nc                 3        | ]}|j  i V  qd S r   r*   r  r   r   r   r5         z/ValueSet.execute_with_values.<locals>.<genexpr>r	  rq   r   r   r   r)        zValueSet.execute_with_valuesc                    s   t t fdd| jD g S )Nc                    s   g | ]
}|j  i qS r   )rN   r  r   r   r   r&     s    z!ValueSet.goto.<locals>.<listcomp>)r   r   r   rq   r   r   r   rN     s   zValueSet.gotoc                    r
  )Nc                 3   r  r   )rX   r  r   r   r   r5     r  z.ValueSet.py__getattribute__.<locals>.<genexpr>r	  rq   r   r   r   rX     r  zValueSet.py__getattribute__c                    r
  )Nc                 3   s&    | ]}t |g R i V  qd S r   )r   r  r   r   r   r5     s   $ z$ValueSet.get_item.<locals>.<genexpr>r	  rq   r   r   r   get_item  r  zValueSet.get_itemc              	   C   sF   |  g }| jD ]}zt||}W n	 ty   Y qw || O }q|S r   )r   r   r   r   )r   function_namer   rD   methodr   r   r   	try_merge  s   

zValueSet.try_mergec                 C   s   t dd | jD S )Nc                 S   s   g | ]}|  qS r   )r.   r  r   r   r   r&     s    z6ValueSet.gather_annotation_classes.<locals>.<listcomp>r	  r+   r   r   r   r.      r   z"ValueSet.gather_annotation_classesc                 C   s   dd | j D S )Nc                 S   s   g | ]}|  D ]}|qqS r   )r   )r%   rD   sigr   r   r   r&     s    z+ValueSet.get_signatures.<locals>.<listcomp>r   r+   r   r   r   r     r    zValueSet.get_signaturesTc                    s    fdd| j D }ttd |}t|dkr|d S d|v }|r&|d t|dkr.d S t|dkr9|d }ndd| }|rFd| }|S )	Nc                    s   g | ]}|j  d qS )r   )r   )r%   vr  r   r   r&     r  z*ValueSet.get_type_hint.<locals>.<listcomp>   r   Nonez	Union[%s]r   zOptional[%s])r   sortedr   r   remover   )r   r   t
type_hintsoptionalr   r   r  r   r     s   

zValueSet.get_type_hintc                 C   s0   ddl m} i }| jD ]
}|||| q|S )Nr   )merge_type_var_dicts)!jedi.inference.gradual.annotationr  r   r   )r   r   r  type_var_dictr   r   r   r   r     s   
zValueSet.infer_type_varsrt   r   )r   r   r   rx   r   r   r8   r   r   r   r   r   r   r   r   r   r   r   r  r9   r   r)   rN   rX   r  r  r.   r   r   r   r   r   r   r   r#     s:    



r#   c                    s    fdd}|S )Nc                     s   t  | i |S r   r"   r   funcr   r   wrapper+  r   z&iterator_to_value_set.<locals>.wrapperr   )r   r!  r   r  r   iterator_to_value_set*  s   r"  rt   )%r   	functoolsr   r   r   	itertoolsr   parso.python.treer   jedir   jedi.parser_utilsr   jedi.inference.helpersr	   jedi.inference.utilsr
   jedi.inference.cacher   
jedi.cacher   objectr   	Exceptionr   r   ru   r   r   r   r   r   r   r   r#   r~   r"  r   r   r   r   <module>   s6    r 
%	 