
    7g.                       d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	m
Z
mZmZ ddlmZmZ ddlmZmZmZmZmZ ddlmZ dd	lmZ dd
lmZ 	 d	 	 	 	 	 dd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Z%d$dZ&d$dZ'd%dZ(dd&dZ)d'dZ*y)(zVarious helper utilities.    )annotationsN)	Generator)basesmanagernodesobjectsraw_buildingutil)CallContextInferenceContext)AstroidTypeErrorAttributeInferenceErrorInferenceErrorMroError_NonDeducibleTypeHierarchy)scoped_nodes)InferenceResult)
safe_inferc                T    t        j                  dt        d       t        | |      S )NzRImport safe_infer from astroid.util; this shim in astroid.helpers will be removed.   )
stacklevelcontext)warningswarnDeprecationWarningreal_safe_infernoder   s     /lib/python3.12/site-packages/astroid/helpers.pyr   r      s(    
 MM\
 411    c                >    t        j                  |       }||_        |S N)r	   build_classparent)cls_namebuiltinsproxys      r    _build_proxy_classr)   '   s    $$X.EELLr!   c                    t        | t        j                  t        j                  f      r#| j	                         j
                  dk(  rd}n"d}nt        | t        j                        rd}nd}t        ||      S )Nr'   builtin_function_or_methodfunctionmethod)	
isinstancer   LambdaFunctionDefrootnamer   BoundMethodr)   )r,   r'   r&   s      r    _function_typer4   -   sd     (\00,2J2JKL==?:-3H!H	He//	0h11r!   c              #  f  K   t        j                         }|j                  }|xs
 t               }| j	                  |      D ]i  }t        |t        j                        r<|j                  r|j                  |      }|r| C|j                  d      d    Zt        |t        j                  t        j                  t        j                  f      rt        ||       t        |t        j                         rt#        d|       t        |t$        j&                        rt(        t        |t*        j,                        r| t        |t        j.                  t$        j0                  t2        j4                  f      r|j6                   Tt9        dt;        |       d       y w)Nr   typer   modulezWe don't handle z
 currently)r   AstroidManagerbuiltins_moduler   inferr.   r   ClassDefnewstyle	metaclassgetattrr/   r   UnboundMethodr0   r4   Moduler)   r   Unknownr   r
   UninferableBaseProxySlicer   Super_proxiedAssertionErrorr6   )r   r   astroid_managerr'   inferredr=   s         r    _object_typerJ   =   sP     ,,.O..H+)+GJJwJ/h 5 56  $..w.?	#O""6*1--  %"5"5|7O7OP
 !844,"5"56$Xx88%--0  $"6"67N5;;W]]"KL### #3DN3C:!NOO- 0s   F/F1c                    	 t        t        | |            }t        |      dkD  s|st        j                  S t        t        |            S # t        $ r t        j                  cY S w xY w)aV  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.
       )setrJ   r   r
   Uninferablelennextiter)r   r   typess      r    object_typerS   ]   s_     Lw/0 5zA~UU	    s   A A'&A'c                   t        | t        j                        st        | t        j                        st        j
                  S |D cg c].  }t        |t        j                        s|nt        j
                  0 }}|D ]D  }t        |t        j                        rt        d      | j                         D ]
  }||k(  s	  y F yc c}w )Nz&arg 2 must be a type or tuple of typesTF)
r.   r
   rB   r   r;   rN   r   Instancer   mro)obj_typeclass_or_seqr   item	class_seqklassobj_subclasss          r    _object_type_is_subclassr]   r   s    
 (D001%..: 
 ! D tU^^4$:J:JJ    eT112"#KLL$LLNLu$ +	  s   	3Cc                    t        | |      }t        |t        j                        rt        j                  S t        |||      S )zCheck if a node 'isinstance' any node in class_or_seq.

    :raises AstroidTypeError: if the given ``classes_or_seq`` are not types
    r   )rS   r.   r
   rB   rN   r]   )r   rX   r   rW   s       r    object_isinstancer_      s;     4)H(D001#HlGLLr!   c                n    t        | t        j                        st        |  d      t	        | ||      S )zCheck if a type is a subclass of any node in class_or_seq.

    :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 needs to be a ClassDef noder   )r.   r   r;   	TypeErrorr]   )r   rX   r   s      r    object_issubclassrb      s4     dENN+4& <=>>#D,HHr!   c                    	 | j                   S # t        $ r Y nw xY w| j                  D ]C  }t        ||      }t	        |t
        j                        r|| u st        ||      r<d| _          y d| _         y)z=Return whether all base classes of a class could be inferred.r   FT)_all_bases_knownAttributeErrorr   r   r.   r   r;   has_known_bases)r[   r   baseresults       r    rf   rf      sz    %%%  w7 6<#8#89"67;%*E"  "Es    	c                    t        t        t        | |f            st        t        | j                  |j                  g      sy	 | |j                         d d v S # t        $ r}t        |d }~ww xY w)NF)allmaprf   r   r<   rV   r   )type1type2es      r    _type_checkrp      sh    s?UEN34((/00		CR((( 0(a/0s   A 	A/#A**A/c                    t        ||       S )z)Check if *type1* is a subtype of *type2*.rm   rn   rp   rr   s     r    
is_subtypert      s    U%00r!   c                    t        | |      S )z+Check if *type2* is a supertype of *type1*.rs   rr   s     r    is_supertyperv      s    ue$$r!   c                x   t               }	 | j                  d|      D ]  }t        |t        j                        s| |_        t        g |      |_        |j                  | |      D ]=  }t        |t        j                        st        |j                  t              s9|c c S   	 y# t        $ r Y yw xY 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.
    	__index__r   )argscalleeN)r   igetattrr.   r   r3   	boundnoder   callcontextinfer_call_resultr   Constvalueintr   )r   r   rI   rh   s       r    class_instance_as_indexr      s      Gk7CHh(9(9: $G"-2h"GG"44T74Kfekk2z&,,PS7T!M L D   s$   A:B- B- "B- (B- -	B98B9c                   ddl m} t        | |      }| j                         }t	        |t
        j                        r|j                  dk(  rrt	        |t        j                        rX|j                  |j                  k(  r?dj                  | j                  | j                         j                        }t!        |      |t	        |t"        j$                        rt!        |       t	        |t&        j(                        r5t	        |j*                  t,        t.        f      rt1        |j*                        S t	        |t&        j2                  t&        j4                  t&        j6                  |f      rt1        |j8                        S t	        |t&        j:                        rt1        |j<                        S t?        ||      }|st!        |       	 tA        |jC                  d|            }|jM                  | |      }	t	        |	t"        j$                        rt!        | |
      tA        |	d      }
t	        |
t&        j(                        r|
jK                         dk(  r|
j*                  S |
+t	        |
t        jN                        r|
jQ                  d      rytG        d|
 d      # tD        $ r}tG        t/        |            |d}~wtH        $ r#}tG        d|jK                          d	      |d}~ww xY w)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 or if the code executed in python
        would result in a infinite recursive check for length
    :rtype int: Integer length of node
    r   )	FrozenSetr   __len__zNSelf referential __len__ function will cause a RecursionError on line {} of {}N)r   zobject of type 'z' has no len()r   zbuiltins.int'z,' object cannot be interpreted as an integer))astroid.objectsr   r   framer.   r   r0   r2   r   rC   rF   r%   formatlinenor1   filer   r
   rB   r   r   r   bytesstrrO   ListSetTupleeltsDictitemsrS   rP   r{   StopIterationr   r   pytyper~   rU   is_subtype_of)r   r   r   inferred_node
node_framemessage	node_typelen_callro   rI   result_of_lens              r    
object_lenr      sn    *#D':M J:|778OOy(}ekk2""j&7&7766<fTYY[--7 	 W%%
=$:N:N O$''--*eS\3 =&&''-%**eiii!PQ=%%&&-,=&&''M7;I$''	**9g*FG ))$8H(D001$884(M=%++.  "n4"""mU^^4''7 

M?FG /  .s1v&A-" y//12.A
	s$   J' '	K40KK4K//K4c                   | }|j                   rat        |j                   t        j                        s=|j                   }|j                   r%t        |j                   t        j                        s=|r|j                   r|j                   S y)a  Search for the first function which encloses the given
    scope.

    This can be used for looking up in that function's
    scope, in case looking up in a lower scope for a particular
    name fails.

    :param node: A scope node.
    :returns:
        ``None``, if no parent function scope was found,
        otherwise an instance of :class:`astroid.nodes.scoped_nodes.Function`,
        which encloses the given node.
    N)r%   r.   r   r0   )r   currents     r    _higher_function_scoper   ?  s^     G
..GNNE<M<M!N.. ..GNNE<M<M!N7>>~~r!   r#   )r   z1nodes.NodeNG | bases.Proxy | util.UninferableBaser   InferenceContext | NonereturnInferenceResult | None)r&   r   r'   nodes.Moduler   nodes.ClassDef)r,   z6nodes.Lambda | nodes.FunctionDef | bases.UnboundMethodr'   r   r   r   )r   r   r   r   r   z!Generator[InferenceResult | None])r   r   r   r   r   r   )rW   r   rX   list[InferenceResult]r   r   r   util.UninferableBase | bool)r   r   rX   r   r   r   r   r   )r   nodes.NodeNGrX   r   r   r   r   r   )r   r   r   bool)r   r   )r   zbases.Instancer   znodes.Const | None)r   r   )r   r   r   znodes.FunctionDef | None)+__doc__
__future__r   r   collections.abcr   astroidr   r   r   r   r	   r
   astroid.contextr   r   astroid.exceptionsr   r   r   r   r   astroid.nodesr   astroid.typingr   astroid.utilr   r   r)   r4   rJ   rS   r]   r_   rb   rf   rp   rt   rv   r   r   r    r!   r    <module>r      s}  
   "  % F F 9  ' * 6
 (,
2
;
2$
2 
22D22 2" ?CP
P$;P&PB ?C
$;0 (,$' % !	> (,M
M'M %M !	M$ (,I
I'I %I !	I (
01
%
.M`r!   