o
    ZhD>                     @   s  d Z ddlmZ ddlmZmZmZ ddlmZm	Z	m
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mZmZ ddlmZ ddlmZ ddl m!Z!m"Z" ddl#m$Z$ G dd deZ%G dd deZ&G dd dZ'G dd de'ee	dZ(dS )a  
Like described in the :mod:`parso.python.tree` module,
there's a need for an ast like module to represent the states of parsed
modules.

But now there are also structures in Python that need a little bit more than
that. An ``Instance`` for example is only a ``Class`` before it is
instantiated. This class represents these cases.

So, why is there also a ``Class`` class here? Well, there are decorators and
they change classes in Python 3.

Representation modules also define "magic methods". Those methods look like
``py__foo__`` and are typically mappable to the Python equivalents ``__call__``
and others. Here's a list:

====================================== ========================================
**Method**                             **Description**
-------------------------------------- ----------------------------------------
py__call__(arguments: Array)           On callable objects, returns types.
py__bool__()                           Returns True/False/None; None means that
                                       there's no certainty.
py__bases__()                          Returns a list of base classes.
py__iter__()                           Returns a generator of a set of types.
py__class__()                          Returns the class of an instance.
py__simple_getitem__(index: int/str)   Returns a a set of types of the index.
                                       Can raise an IndexError/KeyError.
py__getitem__(indexes: ValueSet)       Returns a a set of types of the index.
py__file__()                           Only on modules. Returns None if does
                                       not exist.
py__package__() -> List[str]           Only on modules. For the import system.
py__path__()                           Only on modules. For the import system.
py__get__(call_object)                 Only on instances. Simulates
                                       descriptors.
py__doc__()                            Returns the docstring for a value.
====================================== ========================================

    )debug)get_cached_parent_scopeexpr_is_dottedfunction_is_property)inference_state_method_cacheCachedMetaClass&inference_state_method_generator_cache)compiled)LazyKnownValuesLazyTreeValue)ParserTreeFilter)TreeNameDefinition	ValueName)unpack_arglistValuesArguments)ValueSetiterator_to_value_set	NO_VALUESClassContext)FunctionAndClassBase)LazyGenericManagerTupleGenericManager)plugin_managerc                       s8   e Zd Z fddZedd Ze fddZ  ZS )	ClassNamec                    s   t  || || _|| _d S N)super__init___apply_decorators_class_value)selfclass_value	tree_namename_contextapply_decorators	__class__ Q/var/www/html/lang_env/lib/python3.10/site-packages/jedi/inference/value/klass.pyr   :   s   
zClassName.__init__c                 c   sT    ddl m} || jj| j| j}|D ]}| jr$|jd | jdE d H  q|V  qd S )Nr   )tree_name_to_values)instancer!   )Zjedi.inference.syntax_treer)   parent_contextinference_stater"   r   Z	py__get__r   )r    r)   inferredZresult_valuer'   r'   r(   infer?   s   zClassName.inferc                    s6   t  j}|dkr| j }|d u r|S t|rdS |S )Nfunctionproperty)r   api_typer"   Zget_definitionr   )r    type_Z
definitionr%   r'   r(   r1   L   s   
zClassName.api_type)	__name__
__module____qualname__r   r   r.   r0   r1   __classcell__r'   r'   r%   r(   r   9   s    
r   c                       sF   e Zd Z		d fdd	Zdd Zdd Zd	d
 Z fddZ  ZS )ClassFilterNFc                    s(   t  j| |||d || _|| _d S )N)until_positionorigin_scope)r   r   
as_contextr   _is_instance)r    r!   node_contextr8   r9   is_instancer%   r'   r(   r   ^   s   
zClassFilter.__init__c                    s    fdd|D S )Nc                    s$   g | ]}t  j| j j d qS ))r!   r"   r#   r$   )r   r   Z_node_contextr;   .0namer    r'   r(   
<listcomp>i   s    z.ClassFilter._convert_names.<locals>.<listcomp>r'   r    namesr'   rA   r(   _convert_namesh   s   
zClassFilter._convert_namesc                 C   s>   | j }|d ur|| jks|| jkrdS t| j|}|d usdS )NTF)Z_origin_scopeZ_parser_scoper+   r   Z_parso_cache_node)r    noder'   r'   r(   _equals_origin_scoper   s   z ClassFilter._equals_origin_scopec                 C   s"   |j d p|j dp|  S )N__)value
startswithendswithrG   )r    r@   r'   r'   r(   _access_possiblez   s   zClassFilter._access_possiblec                    s   t  |} fdd|D S )Nc                    s   g | ]	}  |r|qS r'   )rL   r>   rA   r'   r(   rB      s    z'ClassFilter._filter.<locals>.<listcomp>)r   _filterrC   r%   rA   r(   rM      s   zClassFilter._filter)NNNF)	r3   r4   r5   r   rE   rG   rL   rM   r6   r'   r'   r%   r(   r7   ]   s    

r7   c                   @   s   e Zd Zdd Zdd Zdd Zdd Zed	d
 Zdd Z	e
 dd Z		d#ddZdd Zdd Zd$ddZedddd Zdd Zdd  Zd!d" ZdS )%
ClassMixinc                 C      dS NTr'   rA   r'   r'   r(   is_class      zClassMixin.is_classc                 C   rO   rP   r'   rA   r'   r'   r(   is_class_mixin   rR   zClassMixin.is_class_mixinc                 C   sF   ddl m} ddlm} |  rt|| gS t|| j| j| |gS )Nr   )TreeInstance)	TypedDict)Zjedi.inference.valuerT   jedi.inference.gradual.typingrU   is_typeddictr   r,   r+   )r    	argumentsrT   rU   r'   r'   r(   
py__call__   s
   zClassMixin.py__call__c                 C   s   t | jdS )Ntype)r	   builtin_from_namer,   rA   r'   r'   r(   py__class__   s   zClassMixin.py__class__c                 C   s   t | | jjS r   )r   	tree_noder@   rA   r'   r'   r(   r@      s   zClassMixin.namec                 C   s   | j jS r   )r@   Zstring_namerA   r'   r'   r(   
py__name__      zClassMixin.py__name__c              
   c   s    | g}| V  |   D ]2}| D ]+}z|j}W n ty)   	 td| | Y qw | D ]}||vr;|| |V  q-qqd S )Nz$Super class of %s is not a class: %s)py__bases__r.   	py__mro__AttributeErrorr   warningappend)r    mrolazy_clsclsZ
mro_methodZcls_newr'   r'   r(   ra      s&   



zClassMixin.py__mro__NFTc                 c   s    |r|   }|r| ||E d H  |  D ]}| r&|j|dE d H  qt| | ||dV  q|su|rwddlm} || j	d}t
|tsIJ || krytg }	||	D ]$}
|
 }t|d  t|d  t|d }|d usqJ |V  qVd S d S d S d S )N)r=   )r<   r9   r=   r   )r[   rZ   )get_metaclassesget_metaclass_filtersra   Zis_compiledget_filtersr7   r:   Zjedi.inference.compiledr[   r,   
isinstance
ClassValuer   rY   next)r    r9   r=   Zinclude_metaclassesZinclude_type_when_classmetaclassesrg   r[   r2   argsr*   Zinstance_filtersxr'   r'   r(   rj      s:   



zClassMixin.get_filtersc                    sL      }|r |}|r|S tg } |d} fdd| D S )Nr   c                    s   g | ]}|  qS r'   )bind)r?   sigrA   r'   r(   rB      s    z-ClassMixin.get_signatures.<locals>.<listcomp>)rh   get_metaclass_signaturesr   rY   py__getattribute__get_signatures)r    rn   Zsigsro   Z
init_funcsr'   rA   r(   ru      s   
zClassMixin.get_signaturesc                 C   s   t | S r   r   rA   r'   r'   r(   _as_context   r_   zClassMixin._as_contextc                 C   s   |rd|    S |   S )NzType[%s])r^   )r    Zadd_class_infor'   r'   r(   get_type_hint   s   zClassMixin.get_type_hintdefaultc              
   C   s   ddl m} |  D ]=}t|ts dS |j}t|s dS | D ]$}t||r-  dS z|j}W n t	y>   Y   dS w | rF  dS q"q
dS )Nr   )TypedDictClassFT)
rV   rz   r`   rk   r   datar   r.   rW   rb   )r    rz   rf   r]   rg   methodr'   r'   r(   rW      s(   



zClassMixin.is_typeddictc                    s>   ddl m  |std tgS t fdd|D S )Nr   GenericClassz:Class indexes inferred to nothing. Returning class insteadc                 3   s$    | ]} t j|d V  qdS ))Zcontext_of_indexindex_valueN)r   context)r?   r   r~   contextualized_noder    r'   r(   	<genexpr>  s    
z+ClassMixin.py__getitem__.<locals>.<genexpr>)jedi.inference.gradual.baser~   r   rc   r   )r    Zindex_value_setr   r'   r   r(   py__getitem__  s   

zClassMixin.py__getitem__c                 C   s   ddl m} || t|S )Nr   r}   )r   r~   r   )r    Zgenerics_tupler~   r'   r'   r(   with_generics#  s
   zClassMixin.with_genericsc                    sB   ddl m}  fdd}rt| tt| gS t hS )Nr   r}   c                  3   s&       D ]} |  tV  qdS )aa  
            The TypeVars in the resulting classes have sometimes different names
            and we need to check for that, e.g. a signature can be:

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

            However, the iterator is defined as Iterator[_T_co], which means it has
            a different type var name.
            N)list_type_varsgetr^   r   )type_varr    type_var_dictr'   r(   remap_type_vars-  s   
z3ClassMixin.define_generics.<locals>.remap_type_vars)r   r~   r   r   tuple)r    r   r~   r   r'   r   r(   define_generics*  s   
zClassMixin.define_generics)NFTT)T)r3   r4   r5   rQ   rS   rY   r\   r0   r@   r^   r   ra   rj   ru   rv   rw   r   rW   r   r   r   r'   r'   r'   r(   rN      s*    

 
"

rN   c                   @   sj   e Zd ZdZe dd Zdd Zedddd	 Ze	 d
d Z
eeddd Ze	 dd ZdS )rl   classc                 C   sf   g }| j  }|d u rg S t|D ]\}}|rqddlm} || j|D ]}||vr/|| q$q|S )Nr   )find_unknown_type_vars)r]   get_super_arglistr   Z!jedi.inference.gradual.annotationr   r+   rd   )r    foundargliststarsrF   r   r   r'   r'   r(   r   E  s   

zClassValue.list_type_varsc                 C   s0   | j  }|rddlm} || j| j|S d S )Nr   )rX   )r]   r   jedi.inferencerX   ZTreeArgumentsr,   r+   )r    r   rX   r'   r'   r(   _get_bases_argumentsW  s
   
zClassValue._get_bases_argumentsr'   rx   c                 C   sX   |   }|d urdd | D }|r|S |  dkr"| j r"g S t| jjdgS )Nc                 S   s   g | ]
\}}|d u r|qS r   r'   r?   keyrI   r'   r'   r(   rB   b      z*ClassValue.py__bases__.<locals>.<listcomp>object)	r   unpackr^   r+   Zis_builtins_moduler
   r,   Zbuiltins_modulert   )r    ro   lstr'   r'   r(   r`   ^  s   zClassValue.py__bases__c                 C   s   t d| g S )NzUnprocessed metaclass %s)r   rc   )r    rn   r=   r'   r'   r(   ri   m  s   z ClassValue.get_metaclass_filtersc                 C   s   |   }|d ur(dd | D }tdd |D }tdd |D }|r(|S |  D ]}| D ]}| rD| }|rD|    S q2q,tS )Nc                 S   s   g | ]
\}}|d kr|qS 	metaclassr'   r   r'   r'   r(   rB   v  r   z.ClassValue.get_metaclasses.<locals>.<listcomp>c                 s   s    | ]}|  V  qd S r   )r.   )r?   Z
lazy_valuer'   r'   r(   r   w  s    z-ClassValue.get_metaclasses.<locals>.<genexpr>c                 s   s    | ]	}|  r|V  qd S r   )rQ   )r?   mr'   r'   r(   r   x  s    )	r   r   r   Z	from_setsr`   r.   rQ   rh   r   )r    ro   r   rn   Z	lazy_baserI   valuesr'   r'   r(   rh   r  s    zClassValue.get_metaclassesc                 C   s   g S r   r'   )r    rn   r'   r'   r(   rs     s   z#ClassValue.get_metaclass_signaturesN)r3   r4   r5   r1   r   r   r   r`   r   Zdecorateri   r   rh   rs   r'   r'   r'   r(   rl   B  s    



rl   r   N))__doc__Zjedir   Zjedi.parser_utilsr   r   r   Zjedi.inference.cacher   r   r   r   r	   Zjedi.inference.lazy_valuer
   r   Zjedi.inference.filtersr   Zjedi.inference.namesr   r   Zjedi.inference.argumentsr   r   Zjedi.inference.base_valuer   r   r   Zjedi.inference.contextr   Zjedi.inference.value.functionr   Zjedi.inference.gradual.genericsr   r   Zjedi.pluginsr   r   r7   rN   rl   r'   r'   r'   r(   <module>   s&    &$' ?