B
    7r\"                 @   s   d Z ddlmZ ddlmZ ddlmZmZ ddlm	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
 ZG dd deZG dd deZG dd deee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__(params: 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__mro__()                            Returns a list of classes (the mro).
py__iter__()                           Returns a generator of a set of types.
py__class__()                          Returns the class of an instance.
py__getitem__(index: int/str)          Returns a a set of types of the index.
                                       Can raise an IndexError/KeyError.
py__file__()                           Only on modules. Returns None if does
                                       not exist.
py__package__()                        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__(include_call_signature:      Returns the docstring for a context.
          bool)
====================================== ========================================

    )use_metaclass)get_parent_scope)evaluator_method_cacheCachedMetaClass)compiled)LazyKnownContext)ParserTreeFilterTreeNameDefinitionContextName)
ContextSetiterator_to_context_setTreeContextc             c   sB   y
| j }W n tk
r$   | V  Y nX x||D ]
}|V  q0W d S )N)Z	py__get__AttributeError)contextZbase_contextmethodZdescriptor_context r   :lib/python3.7/site-packages/jedi/evaluate/context/klass.pyapply_py__get__3   s    
r   c                   s(   e Zd Z fddZedd Z  ZS )	ClassNamec                s"   t t| || || _|| _d S )N)superr   __init___name_context_apply_decorators)selfparent_context	tree_namename_contextapply_decorators)	__class__r   r   r   >   s    zClassName.__init__c             c   s\   ddl m} || jj| j| j}x6|D ].}| jrNx"t|| jD ]
}|V  q>W q&|V  q&W d S )Nr   )tree_name_to_contexts)Zjedi.evaluate.syntax_treer   r   	evaluatorr   r   r   r   )r   r   ZinferredZresult_contextcr   r   r   inferC   s    
zClassName.infer)__name__
__module____qualname__r   r   r"   __classcell__r   r   )r   r   r   =   s   r   c                   sD   e Zd ZeZ fddZdd Zdd Zdd Z fd	d
Z	  Z
S )ClassFilterc                s"   | d| _tt| j|| d S )Nis_instance)pop_is_instancer   r'   r   )r   argskwargs)r   r   r   r   W   s    zClassFilter.__init__c                s    fdd|D S )Nc                s&   g | ]} j  j| j j d qS ))r   r   r   r   )
name_classr   Z_node_contextr*   ).0name)r   r   r   
<listcomp>]   s   z.ClassFilter._convert_names.<locals>.<listcomp>r   )r   namesr   )r   r   _convert_names[   s    
zClassFilter._convert_namesc             C   s8   | j }x,|d k	r2|| jks$|| jkr(dS t|}qW dS )NTF)Z_origin_scopeZ_parser_scoper   r   )r   Znoder   r   r   _equals_origin_scopee   s    
z ClassFilter._equals_origin_scopec             C   s"   |j d p |j dp |  S )N__)value
startswithendswithr3   )r   r/   r   r   r   _access_possiblem   s    zClassFilter._access_possiblec                s"   t t |} fdd|D S )Nc                s   g | ]}  |r|qS r   )r8   )r.   r/   )r   r   r   r0   s   s    z'ClassFilter._filter.<locals>.<listcomp>)r   r'   _filter)r   r1   )r   )r   r   r9   q   s    zClassFilter._filter)r#   r$   r%   r   r-   r   r2   r3   r8   r9   r&   r   r   )r   r   r'   T   s   
r'   c               @   sv   e Zd ZdZdZedddd Zedddd Zd	d
 Zdd Z	dddZ
dd Zdd Zdd Zedd ZdS )ClassContextz
    This class is not only important to extend `tree.Class`, it is also a
    important for descriptors (if the descriptor methods are evaluated or not).
    classr   )defaultc          
      s~    fdd}| g xb|   D ]V}xP| D ]D}y
|j}W n tk
rL   Y q*X || x| D ]}|| q^W q*W qW t S )Nc                s   |  kr  |  d S )N)append)cls)mror   r   add   s    z#ClassContext.py__mro__.<locals>.add)py__bases__r"   	py__mro__r   tuple)r   r@   Zlazy_clsr>   Z
mro_methodZcls_newr   )r?   r   rB   }   s    
zClassContext.py__mro__c             C   sV   | j  }|r>ddlm} || j| j|}dd | D S tt	
| jdgS d S )Nr   )	argumentsc             S   s   g | ]\}}|d kr|qS )Nr   )r.   keyr5   r   r   r   r0      s    z,ClassContext.py__bases__.<locals>.<listcomp>object)	tree_nodeZget_super_arglistjedi.evaluaterD   ZTreeArgumentsr    r   Zunpackr   r   builtin_from_name)r   ZarglistrD   r+   r   r   r   rA      s    
zClassContext.py__bases__c             C   s"   ddl m} t|| j| j| |S )Nr   )TreeInstance)Zjedi.evaluate.contextrJ   r   r    r   )r   ZparamsrJ   r   r   r   
py__call__   s    zClassContext.py__call__c             C   s   t | jdS )Ntype)r   rI   r    )r   r   r   r   py__class__   s    zClassContext.py__class__NFc             c   sp   |rt | j| ||dV  nRxP|  D ]D}t|tjrRx2|j|dD ]
}|V  qBW q$t| j| |||dV  q$W d S )N)r   until_positionorigin_scope)r(   )Znode_contextrO   r(   )r   r    rB   
isinstancer   ZCompiledObjectget_filtersr'   )r   search_globalrN   rO   r(   r>   filterr   r   r   rQ      s    zClassContext.get_filtersc             C   s   dS )NTr   )r   r   r   r   is_class   s    zClassContext.is_classc             C   s,   x&| j ddD ]}||}|r|S qW g S )NF)rR   )rQ   get)r   r/   rS   r1   r   r   r   get_function_slot_names   s
    
z$ClassContext.get_function_slot_namesc          
   C   s\   xV|  dD ]H}xB| D ]6}y
|j}W n tk
r<   Y qX t| dd  S qW qW g S )Nr      )rV   r"   get_param_namesr   list)r   r/   Zcontext_r   r   r   r   rX      s    
zClassContext.get_param_namesc             C   s   t | | jjS )N)r
   rG   r/   )r   r   r   r   r/      s    zClassContext.name)NNF)r#   r$   r%   __doc__Zapi_typer   rB   rA   rK   rM   rQ   rT   rV   rX   propertyr/   r   r   r   r   r:   v   s   $

r:   N)rZ   Zjedi._compatibilityr   Zjedi.parser_utilsr   Zjedi.evaluate.cacher   r   rH   r   Zjedi.evaluate.lazy_contextr   Zjedi.evaluate.filtersr   r	   r
   Zjedi.evaluate.base_contextr   r   r   r   r   r'   r:   r   r   r   r   <module>'   s   
"