a
    ÷£BeD>  ã                   @   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__© ú]/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/jedi/inference/value/klass.pyr   :   s    zClassName.__init__c                 c   sR   ddl m} || jj| j| jƒ}|D ](}| jrF|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r2| j ¡ }|d u r&|S t|ƒr2d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                       sB   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   s8   | j }|d ur4|| jks"|| jkr&dS t| j|ƒ}q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   �   ó    z'ClassFilter._filter.<locals>.<listcomp>)r   Ú_filterrC   r%   rA   r(   rN      s    zClassFilter._filter)NNNF)	r3   r4   r5   r   rE   rG   rL   rN   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   s   dS ©NTr'   rA   r'   r'   r(   Úis_class…   s    zClassMixin.is_classc                 C   s   dS rP   r'   rA   r'   r'   r(   Úis_class_mixinˆ   s    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.valuerS   Újedi.inference.gradual.typingrT   Úis_typeddictr   r,   r+   )r    Ú	argumentsrS   rT   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__š   s    zClassMixin.py__name__c              
   c   s|   | g}| V  |   ¡ D ]b}| ¡ D ]T}z
|j}W n  tyN   t d| |¡ Y q 0 |ƒ D ]}||vrV| |¡ |V  qVq 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(   r_   �   s    


zClassMixin.py__mro__NFTc                 c   sì   |r"|   ¡ }|r"|  ||¡E d H  |  ¡ D ]6}| ¡ rJ|j|d�E d H  q*t| | ¡ ||d�V  q*|sè|rèddlm} || j	dƒ}t
|tƒs�J ‚|| krètg ƒ}	| |	¡D ]<}
|
 ¡ }t|d ƒ t|d ƒ t|d ƒ}|d usàJ ‚|V  qªd S )N)r=   )r<   r9   r=   r   )rZ   rY   )Úget_metaclassesÚget_metaclass_filtersr_   Zis_compiledÚget_filtersr7   r:   Zjedi.inference.compiledrZ   r,   Ú
isinstanceÚ
ClassValuer   rX   Únext)r    r9   r=   Zinclude_metaclassesZinclude_type_when_classÚmetaclassesre   rZ   r2   Úargsr*   Zinstance_filtersÚxr'   r'   r(   rh   ¾   s2    ý



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   ë   rM   z-ClassMixin.get_signatures.<locals>.<listcomp>)rf   Úget_metaclass_signaturesr   rX   Úpy__getattribute__Úget_signatures)r    rl   Zsigsrm   Z
init_funcsr'   rA   r(   rs   à   s    
zClassMixin.get_signaturesc                 C   s   t | ƒS r   r   rA   r'   r'   r(   Ú_as_contextí   s    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 ]z}t|tƒs( dS |j}t|ƒs< dS | ¡ D ]H}t||ƒrZ  dS z
|j}W n t	y|   Y   dS 0 |ƒ rD  dS qDqdS )Nr   )ÚTypedDictClassFT)
rU   rx   r^   ri   r   Údatar   r.   rV   r`   )r    rx   rd   r\   re   Úmethodr'   r'   r(   rV   õ   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   ra   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„}ˆr8t|ˆ 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   rR   rX   r[   r0   r@   r]   r   r_   rh   rs   rt   ru   r   rV   rƒ   r„   rŒ   r'   r'   r'   r(   rO   „   s(   

   ÿ
"

rO   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 )rj   Úclassc                 C   sf   g }| j  ¡ }|d u rg S t|ƒD ]>\}}|r0q"ddlm} || j|ƒD ]}||vrH| |¡ qHq"|S )Nr   )Úfind_unknown_type_vars)r\   Úget_super_arglistr   Z!jedi.inference.gradual.annotationrŽ   r+   rb   )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   )rW   )r\   r�   Újedi.inferencerW   ZTreeArgumentsr,   r+   )r    r‘   rW   r'   r'   r(   Ú_get_bases_argumentsW  s
    
zClassValue._get_bases_argumentsr'   rv   c                 C   sX   |   ¡ }|d ur*dd„ | ¡ D ƒ}|r*|S |  ¡ dkrD| j ¡ rDg 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  rM   z*ClassValue.py__bases__.<locals>.<listcomp>Úobject)	r”   Úunpackr]   r+   Zis_builtins_moduler
   r,   Zbuiltins_modulerr   )r    rm   Úlstr'   r'   r(   r^   ^  s    ÿÿzClassValue.py__bases__c                 C   s   t  d|¡ g S )NzUnprocessed metaclass %s)r   ra   )r    rl   r=   r'   r'   r(   rg   m  s    z ClassValue.get_metaclass_filtersc                 C   s�   |   ¡ }|d urPdd„ | ¡ D ƒ}t dd„ |D ƒ¡}tdd„ |D ƒƒ}|rP|S |  ¡ D ]2}| ¡ D ]$}| ¡ rd| ¡ }|rd|    S qdqXtS )Nc                 S   s   g | ]\}}|d kr|‘qS ©Ú	metaclassr'   r•   r'   r'   r(   rB   v  rM   z.ClassValue.get_metaclasses.<locals>.<listcomp>c                 s   s   | ]}|  ¡ V  qd S r   )r.   )r?   Z
lazy_valuer'   r'   r(   r�   w  rM   z-ClassValue.get_metaclasses.<locals>.<genexpr>c                 s   s   | ]}|  ¡ r|V  qd S r   )rQ   )r?   Úmr'   r'   r(   r�   x  rM   )	r”   r˜   r   Z	from_setsr^   r.   rQ   rf   r   )r    rm   rœ   rl   Z	lazy_baserI   Úvaluesr'   r'   r(   rf   r  s    zClassValue.get_metaclassesc                 C   s   g S r   r'   )r    rl   r'   r'   r(   rq   „  s    z#ClassValue.get_metaclass_signaturesN)r3   r4   r5   r1   r   r…   r”   r^   r   Zdecoraterg   r   rf   rq   r'   r'   r'   r(   rj   B  s   



rj   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   rO   rj   r'   r'   r'   r(   Ú<module>   s$   &$' ?