3
tûœ]„#  ã               @   sö   d Z ddl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jZdd„ Zdd„ Zd'dd„Zd(dd„Zd)dd„Zd*dd„Zd+dd„Zd,dd„Zd-dd„Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd.d%d&„ZdS )/z
Various helper utilities.
é    N)Úbases)Úcontext)Ú
exceptions)Úmanager)Únodes)Úraw_building)Úscoped_nodes)Úutilc             C   s   t j| ƒ}||_|S )N)r   Zbuild_classÚparent)Úcls_nameÚbuiltinsÚproxy© r   ú2/tmp/pip-build-fn42xldr/astroid/astroid/helpers.pyÚ_build_proxy_class   s    
r   c             C   sR   t | tjƒr&| jƒ jtkr d}qHd}n"t | tjƒr8d}nt | tjƒrHd}t	||ƒS )NZbuiltin_function_or_methodÚfunctionÚmethod)
Ú
isinstancer   ÚLambdaÚrootÚnameÚBUILTINSr   ÚBoundMethodÚUnboundMethodr   )r   r   r   r   r   r   Ú_function_type"   s    r   c             c   s®   t jƒ }|j}|ptjƒ }xŽ| j|d�D ]~}t|tjƒrd|j	rR|j
ƒ }|rR|V  q(|jdƒd V  q(t|tjtjfƒr„t||ƒV  q(t|tjƒržtd|ƒV  q(|jV  q(W d S )N)r   Útyper   Úmodule)r   ZAstroidManagerZbuiltins_moduleÚ
contextmodÚInferenceContextÚinferr   r   ÚClassDefÚnewstyleÚ	metaclassÚgetattrr   r   r   r   ÚModuler   Z_proxied)Únoder   Zastroid_managerr   Úinferredr"   r   r   r   Ú_object_type/   s     r'   c             C   sP   yt t| |ƒƒ}W n tjk
r*   tjS X t|ƒdks>| rDtjS t|ƒd S )aU  Obtain the type of the given node

    This is used to implement the ``type`` builtin, which means that it's
    used for inferring type calls, as well as used in a couple of other places
    in the inference.
    The node will be inferred first, so this function can support all
    sorts of objects, as long as they support inference.
    é   r   )Úsetr'   r   ÚInferenceErrorr	   ÚUninferableÚlenÚlist)r%   r   Útypesr   r   r   Úobject_typeD   s    
r/   c             C   s|   t |ttfƒs|f}n|}| tjkr*tjS dd„ |D ƒ}x>|D ]6}|tjkrVtjdƒ‚x| jƒ D ]}||kr`dS q`W q>W dS )Nc             S   s"   g | ]}t |tjƒs|ntj‘qS r   )r   r   ZInstancer	   r+   )Ú.0Úitemr   r   r   ú
<listcomp>b   s   z,_object_type_is_subclass.<locals>.<listcomp>z&arg 2 must be a type or tuple of typesTF)r   Útupler-   r	   r+   r   ÚAstroidTypeErrorÚmro)Úobj_typeÚclass_or_seqr   Z	class_seqÚklassZobj_subclassr   r   r   Ú_object_type_is_subclassW   s    



r9   c             C   s(   t | |ƒ}|tjkrtjS t|||d�S )züCheck if a node 'isinstance' any node in class_or_seq

    :param node: A given node
    :param class_or_seq: Union[nodes.NodeNG, Sequence[nodes.NodeNG]]
    :rtype: bool

    :raises AstroidTypeError: if the given ``classes_or_seq`` are not types
    )r   )r/   r	   r+   r9   )r%   r7   r   r6   r   r   r   Úobject_isinstancer   s    	

r:   c             C   s*   t | tjƒstdj| d�ƒ‚t| ||d�S )ar  Check if a type is a subclass of any node in class_or_seq

    :param node: A given node
    :param class_or_seq: Union[Nodes.NodeNG, Sequence[nodes.NodeNG]]
    :rtype: bool

    :raises AstroidTypeError: if the given ``classes_or_seq`` are not types
    :raises AstroidError: if the type of the given node cannot be inferred
        or its type's mro doesn't work
    z"{node} needs to be a ClassDef node)r%   )r   )r   r   r    Ú	TypeErrorÚformatr9   )r%   r7   r   r   r   r   Úobject_issubclass�   s    r=   c             C   sj   y| j |d�}t|ƒ}W n tjk
r.   dS X yt|ƒ dS  tjk
rR   dS  tk
rd   |S X dS )z¡Return the inferred value for the given node.

    Return None if inference failed or if there is some ambiguity (more than
    one node has been inferred).
    )r   N)r   Únextr   r*   ÚStopIteration)r%   r   ZinferitÚvaluer   r   r   Ú
safe_infer‘   s    rA   c             C   sp   y| j S  tk
r   Y nX xH| jD ]>}t||d�}t|tjƒ sX|| ksXt||d� r$d| _ dS q$W d| _ dS )z=Return true if all base classes of a class could be inferred.)r   FT)Z_all_bases_knownÚAttributeErrorr   rA   r   r   r    Úhas_known_bases)r8   r   ÚbaseÚresultr   r   r   rC   ¥   s    rC   c             C   sb   t tt| |fƒƒstj‚t | j|jgƒs,dS y| |jƒ d d… kS  tjk
r\   tj‚Y nX d S )NFr(   éÿÿÿÿ)ÚallÚmaprC   r   Z_NonDeducibleTypeHierarchyr!   r5   ZMroError)Útype1Útype2r   r   r   Ú_type_check¹   s    rK   c             C   s   t || d�S )z)Check if *type1* is a subtype of *type2*.)rI   rJ   )rK   )rI   rJ   r   r   r   Ú
is_subtypeÆ   s    rL   c             C   s
   t | |ƒS )z+Check if *type2* is a supertype of *type1*.)rK   )rI   rJ   r   r   r   Úis_supertypeË   s    rM   c             C   s’   t jƒ }t j| gd�|_y^xX| jd|d�D ]F}t|tjƒs<q*x2|j| |d�D ] }t|t	j
ƒrLt|jtƒrL|S qLW q*W W n tjk
rŒ   Y nX dS )zêGet the value as an index for the given instance.

    If an instance provides an __index__ method, then it can
    be used in some scenarios where an integer is expected,
    for instance when multiplying or subscripting a list.
    )ÚargsÚ	__index__)r   N)r   r   ZCallContextZcallcontextÚigetattrr   r   r   Úinfer_call_resultr   ÚConstr@   Úintr   r*   )r%   r   r&   rE   r   r   r   Úclass_instance_as_indexÐ   s    rT   c             C   s(  ddl m} t| |d�}|dks*|tjkr6tj| d�‚t|tj	ƒr\t|j
ttfƒr\t|j
ƒS t|tjtjtj|fƒr~t|jƒS t|tjƒr”t|jƒS y"t||d�}t|jd|d�ƒ}W n* tjk
rà   tjdj|jƒ ƒƒ‚Y nX t|j| |ƒƒ}t|tj	ƒ�r|jƒ dk�r|j
S tjd	j|ƒƒ‚dS )
a�  Infer length of given node object

    :param Union[nodes.ClassDef, nodes.Instance] node:
    :param node: Node to infer length of

    :raises AstroidTypeError: If an invalid node is returned
        from __len__ method or no __len__ method exists
    :raises InferenceError: If the given node cannot be inferred
        or if multiple nodes are inferred
    :rtype int: Integer length of node
    r   )Ú	FrozenSet)r   N)r%   Ú__len__z object of type '{}' has no len()zbuiltins.intz/'{}' object cannot be interpreted as an integer)Zastroid.objectsrU   rA   r	   r+   r   r*   r   r   rR   r@   ÚbytesÚstrr,   ÚListÚSetÚTupleÚeltsÚDictÚitemsr/   r>   rP   ZAttributeInferenceErrorr4   r<   ZpytyperQ   )r%   r   rU   Zinferred_nodeZ	node_typeZlen_callZresult_of_lenr   r   r   Ú
object_lenç   s.    


r_   )N)N)N)N)N)N)N)N)Ú__doc__r   Zbuiltins_modZastroidr   r   r   r   r   r   r   r   r	   Ú__name__r   r   r   r'   r/   r9   r:   r=   rA   rC   rK   rL   rM   rT   r_   r   r   r   r   Ú<module>   s0   






