o
    ˷e-A                     @   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/ideatree/venv/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!   )jedi.inference.syntax_treer)   parent_contextinference_stater"   r   	py__get__r   )r    r)   inferredresult_valuer'   r'   r(   infer?   s   zClassName.inferc                    s*   t  j}|dkr| j }t|rdS |S )Nfunctionproperty)r   api_typer"   get_definitionr   )r    type_
definitionr%   r'   r(   r4   L   s   
zClassName.api_type)	__name__
__module____qualname__r   r   r1   r3   r4   __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_contextr=   r>   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   _node_contextr@   .0namer    r'   r(   
<listcomp>g   s    z.ClassFilter._convert_names.<locals>.<listcomp>r'   r    namesr'   rG   r(   _convert_namesf   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)_origin_scope_parser_scoper,   r   _parso_cache_node)r    noder'   r'   r(   _equals_origin_scopep   s   z ClassFilter._equals_origin_scopec                 C   sv   | j s*| }|d ur*|jdkr*|jd }|jdkr*d|jd  vr*d|jvr*dS |jd p:|jdp:|  S )N	expr_stmt   	annassignClassVar=F__)	r@   r5   typechildrenget_codevalue
startswithendswithrP   )r    rF   rQ   rS   r'   r'   r(   _access_possiblex   s   


zClassFilter._access_possiblec                    s   t  |} fdd|D S )Nc                    s   g | ]	}  |r|qS r'   )r]   rD   rG   r'   r(   rH      s    z'ClassFilter._filter.<locals>.<listcomp>)r   _filterrI   r%   rG   r(   r^      s   zClassFilter._filter)NNNF)	r8   r9   r:   r   rK   rP   r]   r^   r;   r'   r'   r%   r(   r<   [   s    

r<   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'   rG   r'   r'   r(   is_class      zClassMixin.is_classc                 C   r`   ra   r'   rG   r'   r'   r(   is_class_mixin   rc   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)jedi.inference.valuere   jedi.inference.gradual.typingrf   is_typeddictr   r-   r,   )r    	argumentsre   rf   r'   r'   r(   
py__call__   s
   zClassMixin.py__call__c                 C   s   t | jdS )NrW   )r	   builtin_from_namer-   rG   r'   r'   r(   py__class__   s   zClassMixin.py__class__c                 C   s   t | | jjS r   )r   	tree_noderF   rG   r'   r'   r(   rF      s   zClassMixin.namec                 C   s   | j jS r   )rF   string_namerG   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__r1   	py__mro__AttributeErrorr   warningappend)r    mrolazy_clscls
mro_methodcls_newr'   r'   r(   rs      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)rB   )rA   r>   rB   r   )rl   rW   )get_metaclassesget_metaclass_filtersrs   is_compiledget_filtersr<   r?   jedi.inference.compiledrl   r-   
isinstance
ClassValuer   rk   next)r    r>   rB   include_metaclassesinclude_type_when_classmetaclassesry   rl   r6   argsr*   instance_filtersxr'   r'   r(   r      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)rE   sigrG   r'   r(   rH      s    z-ClassMixin.get_signatures.<locals>.<listcomp>)r|   get_metaclass_signaturesr   rk   py__getattribute__get_signatures)r    r   sigsr   
init_funcsr'   rG   r(   r      s   
zClassMixin.get_signaturesc                 C   s   t | S r   r   rG   r'   r'   r(   _as_context   rq   zClassMixin._as_contextc                 C   s   |rd|    S |   S )NzType[%s])rp   )r    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)
rh   r   rr   r   r   datar   r1   ri   rt   )r    r   rx   rn   ry   methodr'   r'   r(   ri     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 ))context_of_indexindex_valueN)r   context)rE   r   r   contextualized_noder    r'   r(   	<genexpr>%  s    
z+ClassMixin.py__getitem__.<locals>.<genexpr>)jedi.inference.gradual.baser   r   ru   r   )r    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    generics_tupler   r'   r'   r(   with_generics0  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getrp   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_generics7  s   
zClassMixin.define_generics)NFTT)T)r8   r9   r:   rb   rd   rk   rm   r3   rF   rp   r   rs   r   r   r   r   r   ri   r   r   r   r'   r'   r'   r(   r_      s*    

 
"

r_   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 )r   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)rn   get_super_arglistr   !jedi.inference.gradual.annotationr   r,   rv   )r    foundargliststarsrO   r   r   r'   r'   r(   r   R  s   

zClassValue.list_type_varsc                 C   s0   | j  }|rddlm} || j| j|S d S )Nr   )rj   )rn   r   jedi.inferencerj   TreeArgumentsr-   r,   )r    r   rj   r'   r'   r(   _get_bases_argumentsd  s
   
zClassValue._get_bases_argumentsr'   r   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'   rE   keyrZ   r'   r'   r(   rH   o      z*ClassValue.py__bases__.<locals>.<listcomp>object)	r   unpackrp   r,   is_builtins_moduler
   r-   builtins_moduler   )r    r   lstr'   r'   r(   rr   k  s   zClassValue.py__bases__c                 C   s   t d| g S )NzUnprocessed metaclass %s)r   ru   )r    r   rB   r'   r'   r(   r}   z  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(   rH     r   z.ClassValue.get_metaclasses.<locals>.<listcomp>c                 s   s    | ]}|  V  qd S r   )r1   )rE   
lazy_valuer'   r'   r(   r     s    z-ClassValue.get_metaclasses.<locals>.<genexpr>c                 s   s    | ]	}|  r|V  qd S r   )rb   )rE   mr'   r'   r(   r     s    )	r   r   r   	from_setsrr   r1   rb   r|   r   )r    r   r   r   	lazy_baserZ   valuesr'   r'   r(   r|     s    zClassValue.get_metaclassesc                 C   s   g S r   r'   )r    r   r'   r'   r(   r     s   z#ClassValue.get_metaclass_signaturesN)r8   r9   r:   r4   r   r   r   rr   r   decorater}   r   r|   r   r'   r'   r'   r(   r   O  s    



r   r   N))__doc__jedir   jedi.parser_utilsr   r   r   jedi.inference.cacher   r   r   r   r	   jedi.inference.lazy_valuer
   r   jedi.inference.filtersr   jedi.inference.namesr   r   jedi.inference.argumentsr   r   jedi.inference.base_valuer   r   r   jedi.inference.contextr   jedi.inference.value.functionr   jedi.inference.gradual.genericsr   r   jedi.pluginsr   r   r<   r_   r   r'   r'   r'   r(   <module>   s&    &"6 ?