o
    bw#                     @   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 | }||_|S N)r   Zbuild_classparent)cls_namebuiltinsproxy r   N/var/www/secure340b-portal/env/lib/python3.10/site-packages/astroid/helpers.py_build_proxy_class   s   
r   c                 C   sR   t | tjr|  jtkrd}nd}nt | tjrd}nt | tjr$d}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}|pt }| j|dD ]?}t|tjr2|j	r)|
 }|r)|V  q|dd V  qt|tjtjfrBt||V  qt|tjrOtd|V  q|jV  qd S )Nr   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   z	t t| |}W n tjy   tj Y S w t|dks|s"t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   

r1   c                 C   sx   t |ttfs|f}n|}| tju rtjS dd |D }|D ]}|tju r*td|  D ]
}||kr8  dS q.qdS )Nc                 S   s"   g | ]}t |tjs|ntjqS r   )r   r   Instancer
   r-   ).0itemr   r   r   
<listcomp>a   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"   


r<   c                 C   s(   t | |}|tju rtjS t|||dS )zCheck 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   )r1   r
   r-   r<   )r'   r:   r   r9   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formatr<   )r'   r:   r   r   r   r   object_issubclass   s   rA   c                 C   sj   z| j |d}t|}W n tjy   Y dS w zt| W dS  tjy*   Y dS  ty4   | Y S w )zReturn 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   rE   c                 C   sj   z| j W S  ty   Y nw | jD ]}t||d}t|tjr)|| u s)t||ds/d| _  dS qd| _ dS )z=Return true if all base classes of a class could be inferred.r   FT)Z_all_bases_knownAttributeErrorr   rE   r   r	   r"   has_known_bases)r;   r   baseresultr   r   r   rG      s$   


rG   c                 C   sZ   t tt| |fstjt | j|jgsdS z| | d d v W S  tjy,   tjw )NF)allmaprG   r   Z_NonDeducibleTypeHierarchyr#   r8   ZMroErrorZtype1Ztype2r   r   r   _type_check   s   rN   c                 C   s
   t || S )z(Check if *type1* is a subtype of *typ2*.rN   rM   r   r   r   
is_subtype      
rP   c                 C   s
   t | |S )z+Check if *type2* is a supertype of *type1*.rO   rM   r   r   r   is_supertype   rQ   rR   c                 C   s   t  }t j| gd|_z1| jd|dD ]&}t|tjsq|j| |dD ]}t|t	j
r9t|jtr9|    W S q$qW dS  tjyH   Y dS w )zGet 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   ConstrD   intr   r,   )r'   r   r(   rI   r   r   r   class_instance_as_index   s"   	rY   c              	   C   s  ddl m} t| |d}|du s|tju rt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rJt|jS zt||d}t|jd|d}W n tjym   td| w t|| |}t|tj	r| dkr|j
S td	|)
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   )	FrozenSetr   Nr>   __len__z object of type '{}' has no len()zbuiltins.intz/'{}' object cannot be interpreted as an integer)Zastroid.objectsrZ   rE   r
   r-   r   r,   r   r   rW   rD   bytesstrr.   ListSetTupleeltsDictitemsr1   rB   rU   ZAttributeInferenceErrorr7   r@   ZpytyperV   )r'   r   rZ   Zinferred_nodeZ	node_typeZlen_callZresult_of_lenr   r   r   
object_len   s:   





rd   r   )__doc__r   Zbuiltins_modZastroidr   r   r   r   r   r   r   r	   r
   __name__r   r   r   r)   r1   r<   r=   rA   rE   rG   rN   rP   rR   rY   rd   r   r   r   r   <module>   s2   






