
    %^g+                    (   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mZmZmZmZmZmZ d dlmZmZmZ d d	lmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z: d!d
Z;d!dZ<d"dZ=d#dZ>d$dZ?d$dZ@d%dZA	 	 	 	 d&	 	 	 	 	 	 	 	 	 	 	 	 	 d'dZBdd	 	 	 	 	 	 	 d(dZC	 	 	 	 	 	 	 	 d)dZD	 	 	 	 	 	 	 	 d*dZEd+dZFd,dZGd-dZH G d de2e'         ZId.dZJd/dZKd0dZL	 	 	 	 	 	 	 	 d1d ZMy)2    )annotations)Callable)join)
erase_type)map_instance_to_supertype)state)are_parameters_compatiblefind_memberis_callable_compatibleis_equivalentis_proper_subtypeis_same_type
is_subtype)is_recursive_pairmake_simplified_uniontuple_fallback)"MYPYC_NATIVE_INT_NAMESTUPLE_LIKE_INSTANCE_NAMESAnyTypeCallableTypeDeletedType
ErasedTypeFunctionLikeInstanceLiteralTypeNoneType
Overloaded
ParametersParamSpecTypePartialType
ProperType	TupleTypeTypeTypeAliasTypeTypedDictTypeTypeGuardedType	TypeOfAnyTypeTypeTypeVarLikeTypeTypeVarTupleTypeTypeVarTypeTypeVisitorUnboundTypeUninhabitedType	UnionType
UnpackTypefind_unpack_in_listget_proper_typeget_proper_typessplit_with_prefix_and_suffixc                    t        | |      rt        |       S t        ||       rt        |      S t        j                  r
t	               S t               S )zRReturn one of types (expanded) if it is a subtype of other, otherwise bottom type.)r   r2   r   strict_optionalr.   r   sts     )lib/python3.12/site-packages/mypy/meet.pytrivial_meetr;   ;   sF    !Qq!!	Aq	q!!  "$$:    c                d   t        | |      rt        | |      S t        |       } t        |      }t        | t              rt        |t              r| j
                  |j
                  k(  r| j                  s|j                  rwt        | |      rk| j                  rO|j                  rCt        | j                  j                        t        |j                  j                        kD  r| S |S | j                  r| S |S t        | t              s0t        |t              s t        | |d      r| S t        || d      r|S t        | t              r| S t        | t              r|S t        | t              rt        |t              s|| }} t        j                   | |      \  } }|j#                  t%        |             S )z-Return the greatest lower bound of two types.Tignore_promotions)r   r;   r2   
isinstancer   typeextra_attrsr   lenattrsr-   r   r   r   r/   r   normalize_callablesacceptTypeMeetVisitorr7   s     r:   
meet_typesrH   H   sR   A Aq!!AA!X:a#:qvv?O MMQ]]Q0B}}q}}**+c!--2E2E.FFH}}Ha%jK.HQT:HQT:H!Z !W!Y
1i(@!1 ##Aq)DAq88OA&''r<   c           
        t        |t              r|j                  S | }|}t        |       } t        |      }| |k(  r|S t        | t              rMt        | j                         D cg c]*  }t        ||d      st        ||d      rt        ||      , c}      S t        | |      r|S t        | |d      s$t        j                  r
t               S t               S t        |t              r1t        |j                         D cg c]  }t        | |       c}      S t        |t              r|S t        |t               rt        |j"                  |       r|S t        | t$              rCt        |t$              r3t%        j&                  t        | j(                  |j(                              S t        | t$              r,t        |t*              r|j,                  j/                         r|S t        | t*              rx| j,                  j0                  r|S t        |t*              rD|j,                  j0                  r.|j,                  j0                  j,                  | j,                  u r|S t3        ||      S t        | t4        t$        t6        f      rt3        ||      S t        | t8              r\t        |t*              rL|j,                  j:                  dk(  r't=        d t?        |j@                        D              r|S t3        ||      S |S c c}w c c}w )z7Return the declared type narrowed down to another type.Tr>   F)prohibit_none_typevar_overlapzbuiltins.dictc              3  <   K   | ]  }t        |t                y wN)r@   r   ).0r9   s     r:   	<genexpr>z'narrow_declared_type.<locals>.<genexpr>   s      =
,KqJq'",Ks   )!r@   r&   
type_guardr2   r/   r   relevant_itemsis_overlapping_typesr   narrow_declared_typeis_enum_overlapping_unionr   r6   r.   r   r   r+   upper_boundr(   make_normalizeditemr   rA   is_metaclassalt_promoterH   r"   r   r%   fullnameallr3   args)declarednarrowedoriginal_declaredoriginal_narrowedxs        r:   rR   rR   r   s    (O,"""  x(Hx(H8  (I&$ "0022A )HM!(AG %Q12
 	
 !84  !(HTXY  "$$:	Hi	($8@8O8O8QR8Q1!(A.8QR
 	
 
Hg	&  	Hk	*z(:N:NPX/Y	Hh	'Jx,J''(<X]]HMM(Z[[8X&x*MM&&( ! 	Hh	'==$$$$x*))))..(--? %$+->??	Hy(K@	A+->??	Hm	,Hh1O==!!_4 =
,<X]],K=
 :
 %$+->??y. Ss   %/LLc                T   t        |       } t        | t              r1t        | j                        dkD  r| j                  S | j
                  gS t        | t              rct        | j
                        }t        |t              r$t        |j                  j                  d   g       gS t        t        j                        gS t        | t              r| j
                  gS t        | t              rt        | j                         S t        | t"              rt        | j                         S | gS )a  This function takes any "Union-like" type and returns a list of the available "options".

    Specifically, there are currently exactly three different types that can have
    "variants" or are "union-like":

    - Unions
    - TypeVars with value restrictions
    - Overloads

    This function will return a list of each "option" present in those types.

    If this function receives any other type, we return a list containing just that
    original type. (E.g. pretend the type was contained within a singleton union).

    The only current exceptions are regular TypeVars and ParamSpecs. For these "TypeVarLike"s,
    we return a list containing that TypeVarLike's upper bound.

    This function is useful primarily when checking to see if two types are overlapping:
    the algorithm to check if two unions are overlapping is fundamentally the same as
    the algorithm for checking if two overloads are overlapping.

    Normalizing both kinds of types in the same way lets us reuse the same algorithm
    for both.
    r   )r2   r@   r+   rC   valuesrT   r   r   rA   mror   r'   implementation_artifactr*   r/   listitemsr   )typrT   s     r:   get_possible_variantsri      s    2 #
C#{#szz?Q::OO$$	C	'%coo6k8,[--11"5r:;;	99:;;	C)	*  	C	#CII	C	$ CIIur<   c                     t         t              xrM  j                  j                  xr5 t        |t              xr# t         fd|j                         D              S )zOReturn True if x is an Enum, and y is an Union with at least one Literal from xc              3     K   | ]D  }t        t        |      xt              xr# j                  j                  j                  k(   F y wrL   )r@   r2   r   rA   fallback)rM   zpr`   s     r:   rN   z,is_enum_overlapping_union.<locals>.<genexpr>   sC      
' OA..q<Z1::??AZZ's   A
A)r@   r   rA   is_enumr/   anyrP   )r`   yrn   s   ` @r:   rS   rS      sX     	1h 	
FFNN	
q)$	
  
%%'
 
	r<   c                     t         t              xr0 t        |t              xr t         fd|j                  D              S )z?Return True if x is a Literal and y is an Union that includes xc              3  :   K   | ]  }t        |      k(    y wrL   )r2   )rM   rm   r`   s     r:   rN   z&is_literal_in_union.<locals>.<genexpr>  s     9A_Q''s   )r@   r   r/   rp   rg   )r`   rq   s   ` r:   is_literal_in_unionrt      s:     	1k" 	:q)$	:999r<   c                X    t        | t              xr | j                  j                  dk(  S Nzbuiltins.object)r@   r   rA   rY   )r9   s    r:   	is_objectrw   	  s"    a"Kqvv:K'KKr<   FNc                   t        | t              st        |t              ry
t               | |fv ryt        | t              r#t        |t              rj	                  | |f       t        | |f      \  } }dfdt        | t              st        |t              rJ d       t        t        t        f}t        | |      st        ||      ryt        j                  svt        | t              r#t        j                  | j                               } t        |t              r#t        j                  |j                               }t        | |f      \  } }t        | t              st        |t              r xs t!        |       xs t!        |      S t#        | |      s$t#        ||       st%        | |      st%        ||       rydd}r || |      s	 |||       rydfd} || |      s	 |||       ryt'        |       }	t'        |      }
dd}r || |      s	 |||       ryt)        |	      d	kD  s.t)        |
      d	kD  s t        | t*              st        |t*              r|	D ]  }|
D ]  } ||      s  y  yt        j                  r"t        | t,              t        |t,              k7  ryt        | t.              rt        |t.              rt1        | |      S t3        | |      rt5        | |
      S t        | t.              r| j6                  } nt        |t.              r|j6                  }t9        |       rt9        |      rt;        | |      S t        | t<              rt?        |       } nt        |t<              rt?        |      }t        | t@              r-t        |t@              r | jB                  |jB                        S dfd}t        | t@              st        |t@              r || |      xs	  |||       S t        | tD              r"t        |tD              rtG        | |d d      S t        | tD              st        |tD              ryt        | tH              r"t        |tH              rtK        | |d d      S d}d}t        | tH              r!t        |tL              rtO        d||d      }| }t        |tH              r!t        | tL              rtO        d| | d      }|}t        tQ        |      tR              r||J  ||      S t        | tH              r| j6                  } t        |tH              r|j6                  }t        | tT              rCt        |tT              r3| jV                  |jV                  k(  r| j6                  } |j6                  }n:yt        | tT              r| j6                  } nt        |tT              r|j6                  }t        | tL              rFt        |tL              r5 || |      s	 |||       ry|jX                  jZ                  dk(  r| jX                  jZ                  t\        v ry| jX                  j_                  |jX                  jZ                        rta        | |jX                        } nG|jX                  j_                  | jX                  jZ                        rta        || jX                        }nyt)        | jb                        t)        |jb                        k(  r3te        fdtg        | jb                  |jb                        D              ryytY        |       tY        |      k7  sJ tY        |        dtY        |              y)aE  Can a value of type 'left' also be of type 'right' or vice-versa?

    If 'ignore_promotions' is True, we ignore promotions while checking for overlaps.
    If 'prohibit_none_typevar_overlap' is True, we disallow None from overlapping with
    TypeVars (in both strict-optional and non-strict-optional mode).
    If 'overlap_for_overloads' is True, we check for overlaps more strictly (to avoid false
    positives), for example: None only overlaps with explicitly optional types, Any
    doesn't overlap with anything except object, we don't ignore positional argument names.
    TNc           	     B    t        | |j                               S )zEncode the kind of overlapping check to perform.

        This function mostly exists, so we don't have to repeat keyword arguments everywhere.
        )r?   rJ   overlap_for_overloads
seen_types)rQ   copy)leftrightr?   rz   rJ   r{   s     r:   _is_overlapping_typesz3is_overlapping_types.<locals>._is_overlapping_types-  s+    
 $/*G"7!(
 	
r<   Fz%Unexpectedly encountered partial typec                    t        | |f      \  } }t        | t              xr+ t        |t              xr |j                  j
                  dk(  S rv   )r3   r@   r   r   rA   rY   t1t2s     r:   is_none_object_overlapz4is_overlapping_types.<locals>.is_none_object_overlaph  sJ    !2r(+Br8$ 62x(6  $55	
r<   c                @    rt        | |      S t        | |      S )Nr>   )r   r   )r}   r~   r?   rz   s     r:   _is_subtypez)is_overlapping_types.<locals>._is_subtypet  s&     $T5DUVVdE=NOOr<   c                f    t        | |f      \  } }t        | t              xr t        |t              S rL   )r3   r@   r   r)   r   s     r:   is_none_typevarlike_overlapz9is_overlapping_types.<locals>.is_none_typevarlike_overlap  s-    !2r(+B"h'KJr?,KKr<      )overlappingc                
   t        | |f      \  } }t        | t              r-t        |t              r | j                  |j
                        S t        | t              rt        |t              rt        | j                  t              rF| j                  j                  j                  }|	 ||      S |j                  j                  d      S t        | j                  t              r|j                  j                  d      S y)z-Special cases for type object types overlaps.builtins.typeF)r3   r@   r(   r   rV   ret_typer   rA   metaclass_typehas_baser   )r}   r~   	left_metar   s      r:   _type_object_overlapz2is_overlapping_types.<locals>._type_object_overlap  s     'e}5edH%*UL*I(ENNCCdH%*UH*E$))X. IINN99	(0EBBzz**?;;DIIw/zz**?;;r<   )	is_compatr   ignore_pos_arg_namesallow_partial_overlap__call__)is_operatorzbuiltins.intc              3  6   K   | ]  \  }} ||        y wrL    )rM   left_arg	right_argr   s      r:   rN   z'is_overlapping_types.<locals>.<genexpr>:  s%      +E'Hi &h	:+E   z vs r}   r#   r~   r#   returnbool)r   r#   r   r#   r   r   )4r@   r&   setr$   addr3   r    r-   r   r   r   r6   r/   
make_unionrP   r   rw   rS   rt   ri   rC   r)   r   r%   are_typed_dicts_overlappingtyped_dict_mapping_pairtyped_dict_mapping_overlaprl   is_tupleare_tuples_overlappingr"   r   r(   rV   r   r	   r   r   r   r
   r2   r   r   valuerA   rY   r   r   r   r[   rZ   zip)r}   r~   r?   rJ   rz   r{   illegal_typesr   r   left_possibleright_possibler   lrr   callotherr   s     ````           @r:   rQ   rQ     s8   " $(J- U
e}
"$&:e]+Ke}%"D%=1KD%
 
 $$
5+(F===u !*k:M$&*UM*J
   dI&''(;(;(=>DeY'(()=)=)?@E&e}5e $ Jug$>((OIdOOy?OO 	"$.$UD1tU+ud+
 !$.2HPT2UP 4;ud#;
 *$/M*51NL %&tU37RSXZ^7_ 	MQ~"dO,e_-A#(A. $   D(!;z%QY?Z!Z $&:e]+K*48MNN	 u	-)$CXYY	D-	(}}	E=	)~(5/%dE3HII	D)	$d#	E9	%u% $!j&A$TYY

;;( $!Zx%@#D%0U4HPT4UU$
#
5*(E(+#%:!:"&
 	
 $
#z%'D$%*UL*I%+#%:!:"&
 	
 DE$%*UH*E:ueF%&:dH+E:tTtD/$'6E$555$T511$%}}%&$$E;)G::$ ==DNNE	D+	&}}	E;	' $!j&A tU#{5$'?::.0TYY5G5GKa5a 99ejj112,T5::>DZZ  !3!34-eTYY?Etyy>S_,  +.tyy%**+E   :e$FdDe&FF$r<   r>   c               D    t        t        |       t        |      |d      S )zMThe same as 'is_overlapping_erased_types', except the types are erased first.T)r?   rJ   )rQ   r   )r}   r~   r?   s      r:   is_overlapping_erased_typesr   L  s'      45+&*	 r<   c                   | j                   D ]7  }||j                  vr y || j                  |   |j                  |         r7 y |j                   D ]7  }|| j                  vr y || j                  |   |j                  |         r7 y y)z?Returns 'true' if left and right are overlapping TypeDictTypes.FT)required_keysrg   )r}   r~   is_overlappingkeys       r:   r   r   X  s    
 !!ekk!djjou{{3/?@	 " ""djj djjou{{3/?@	 # r<   c                d   t        | |f      \  } }t        | |      xs | } t        ||       xs |}t        | t              sJ d|  d       t        |t              sJ d| d       t	        | j
                        }t	        |j
                        }|t        | t        |j
                              } |t        |t        | j
                              }t        | j
                        t        |j
                        k7  ryt        fdt        | j
                  |j
                        D              S )z6Returns true if left and right are overlapping tuples.zType z is not a tupleFc              3  6   K   | ]  \  }} ||        y wrL   r   )rM   r   r   r   s      r:   rN   z)are_tuples_overlapping.<locals>.<genexpr>  s     M0L1~a#0Lr   )
r3   adjust_tupler@   r"   r1   rg   expand_tuple_if_possiblerC   rZ   r   )r}   r~   r   left_unpackright_unpacks     `  r:   r   r   p  s    #D%=1KD%e$,D%.EdI&E%v_(EE&eY'G5)GG' &djj1K&u{{3L'c%++.>?(DJJ@
4::#ekk**MDJJ0LMMMr<   c                   t        | j                        |dz   kD  r| S |dz   t        | j                        z
  }g }| j                  D ]  }t        |t              s|j	                  |       %t        |j                        }t        |t              r|j                  }nt        |t              sJ |}|j                  j                  dk(  sJ |j                  |j                  d   g|z          | j                  |      S )Nr   builtins.tupler   rg   )rC   rg   r@   r0   appendr2   rA   r*   r   r   rY   extendr[   copy_modified)tuptargetextra	new_itemsitunpackedinstances          r:   r   r     s    
399~
"
QJSYY'EIii"j)R "277+h 01..H h111H}}%%)9999(--*+e34  9--r<   c                    t        | t              rX| j                  j                  dk(  r?t        |t              r|j                         nd}t	        | j                  d   g|z  |       S y)zGFind out if `left` is a Tuple[A, ...], and adjust its length to `right`r   r   r   N)r@   r   rA   rY   r"   lengthr[   )r}   r   ns      r:   r   r     sS    $!dii&8&8<L&L$Q	2AHHJ$))A,!+T22r<   c                    t        |       } t        | t              xs+ t        | t              xr | j                  j
                  dk(  S )Nr   )r2   r@   r"   r   rA   rY   )rh   s    r:   r   r     s?    
#
Cc9% 3!Kchh&7&7;K&Kr<   c                      e Zd ZddZddZddZddZddZd dZd!dZ	d"dZ
d#d	Zd$d
Zd%dZd&dZd'dZd(dZd)dZd*dZd+dZd,dZd-dZd.dZd/dZd0dZd1dZd2dZd3dZy)4rG   c                    || _         y rL   r8   )selfr8   s     r:   __init__zTypeMeetVisitor.__init__  s	    r<   c                    t        | j                  t              r&t        j                  r
t               S | j                  S t        | j                  t
              r| j                  S t        t        j                        S rL   )	r@   r8   r   r   r6   r.   r   r'   special_formr   r9   s     r:   visit_unbound_typez"TypeMeetVisitor.visit_unbound_type  sS    dffh'$$&((vv066M91122r<   c                    | j                   S rL   r   r   s     r:   	visit_anyzTypeMeetVisitor.visit_any      vvr<   c                X   t        | j                  t              rTg }|j                  D ]8  }| j                  j                  D ]  }|j	                  t        ||              : t        |      S |j                  D cg c]  }t        || j                         }}t        |      S c c}w rL   )r@   r8   r/   rg   r   rH   r   )r   r9   meetsr`   rq   s        r:   visit_union_typez TypeMeetVisitor.visit_union_type  s    dffi( "EWWALLAq!12 & 
 %U++ 56GG<GqZ466*GE<$U++ =s   =B'c                    t         j                  rct        | j                  t              s=t        | j                  t
              r%| j                  j                  j                  dk(  r|S t               S |S rv   )	r   r6   r@   r8   r   r   rA   rY   r.   r   s     r:   visit_none_typezTypeMeetVisitor.visit_none_type  sP      $&&(+4668,1E1EIZ1Z&((Hr<   c                    |S rL   r   r   s     r:   visit_uninhabited_typez&TypeMeetVisitor.visit_uninhabited_type  s    r<   c                    t        | j                  t              rt        j                  r|S | j                  S t        | j                  t
              r| j                  S |S rL   )r@   r8   r   r   r6   r.   r   s     r:   visit_deleted_typez"TypeMeetVisitor.visit_deleted_type  sC    dffh'$$vv066MHr<   c                    | j                   S rL   r   r   s     r:   visit_erased_typez!TypeMeetVisitor.visit_erased_type  r   r<   c                    t        | j                  t              r/| j                  j                  |j                  k(  r| j                  S | j	                  | j                        S rL   )r@   r8   r+   iddefaultr   s     r:   visit_type_varzTypeMeetVisitor.visit_type_var  s?    dffk*tvvyyADD/@66M<<''r<   c                n    | j                   |k(  r| j                   S | j                  | j                         S rL   )r8   r   r   s     r:   visit_param_specz TypeMeetVisitor.visit_param_spec  s)    66Q;66M<<''r<   c                   t        | j                  t              rT| j                  j                  |j                  k(  r1| j                  j                  |j                  kD  r| j                  S |S | j                  | j                        S rL   )r@   r8   r*   r   min_lenr   r   s     r:   visit_type_var_tuplez$TypeMeetVisitor.visit_type_var_tuple  sY    dff./DFFII4E!VV^^aii7466>Q><<''r<   c                    t         rL   )NotImplementedErrorr   s     r:   visit_unpack_typez!TypeMeetVisitor.visit_unpack_type  s    !!r<   c           
        t        | j                  t              rt        |j                        t        | j                  j                        k7  r| j                  | j                        S ddlm} |j                  t        | j                  j                  |j                        D cg c]  \  }} |||       c}}      S | j                  | j                        S c c}}w )Nr   )
join_types)	arg_types)
r@   r8   r   rC   r   r   	mypy.joinr   r   r   )r   r9   r   s_at_as        r:   visit_parametersz TypeMeetVisitor.visit_parameters  s    dffj)1;;3tvv'7'7#88||DFF++,??@CDFFDTDTVWVaVa@bc@bHC:c3/@bc #   <<'' ds   )C 
c                
   t        | j                  t              r|j                  | j                  j                  k(  rt	        || j                        st	        | j                  |      r4g }|j                  j
                  r| j                  }|j                  j                  J |j                  j                  J |j                  j                  }|j                  j                  }|j                  j                  j                  |   }t        |t              sJ |j                  }t        |j                  ||      \  }}	}
t        |j                  ||      \  }}}|t        t        |	      |      fz   |
z   }|t        t        |      |      fz   |z   }n"|j                  }| j                  j                  }t!        |||j                  j                  j                        D ]  \  }}}| j#                  ||      }t        |t              rdt        |t              r|j%                  |j&                         Ut        |t(              sJ t+        |j                  j-                  |g            }|j/                  |        t        |j                  |      S t0        j2                  r
t)               S t5               S |j                  j6                  }|r$|j                  | j                  j                  u r|S | j                  j                  j6                  }|r$|j                  |j                  u r| j                  S t	        || j                        r|S t	        | j                  |      r| j                  S t0        j2                  r
t)               S t5               S t        | j                  t8              rD|j                  j:                  r.t=        j>                  |      }|rktA        || j                        S t        | j                  t8              r{| j                  jC                         ra|j                  jE                         rGt	        | j                  jF                  |      r| j                  S | jI                  | j                        S t        | j                  tJ              rtA        || j                        S t        | j                  t              rtA        || j                        S t        | j                  tL              rtA        || j                        S t        | j                  tN              rtA        || j                        S | jI                  | j                        S )N)r[   )(r@   r8   r   rA   r   has_type_var_tuple_typetype_var_tuple_prefixtype_var_tuple_suffixdefn	type_varsr*   r   r4   r[   r"   rf   r   meetr   rg   r.   r0   r   r   r   r6   r   rX   r   is_protocolr   unpack_callback_protocolrH   is_type_objrW   rl   r   r(   r   r%   )r   r9   r[   r8   prefixsuffixtvtrl   s_prefixs_middles_suffixt_prefixt_middlet_suffixs_argst_argstasatvr   rX   r   s                         r:   visit_instancezTypeMeetVisitor.visit_instance  s   dffh'vv$a(Jtvvq,A (*D vv55 !FF vv;;GGG vv;;GGG!"!=!=!"!=!=ffkk33F;)#/?@@@#&#5#57SFFFF84(H 8TFFFF84(H "*YtH~x-P,R!RU]!]!)YtH~x-P,R!RU]!]!"!%&)&&!&&++:O:O&P
B#yyR0%b*:;  *$	: $DJJ 7 ('1$'H H'H'1"2C2C2Q2QX\W]2Q2^'_D) 'Q $AFFD11,,.00'z)ff00;#3#3tvv{{#BH"ffkk55;#3#3qvv#=66Ma(H*66M,,.00'z)-!&&2D2D003D!$//-$&&2D2D2F166K^K^K`$&&//1-vv<<'')a((	*a((,a((.a((||DFF##r<   c                   t        | j                  t              rt        j                  || j                        rt        || j                        r t        j                  || j                        S t        || j                        }|j                         r|j                         j                  sE| j                  j                         r$| j                  j                         j                  sd|_        t        t        |j                        t              r| j                  | j                        S |S t        | j                  t               r|j                         r|j#                         spt%        | j                  j&                  |j                        }t        |t(        t        f      st!        j*                  |      S | j                  | j                        S t        | j                  t,              rM| j                  j.                  j0                  r-t        j2                  | j                        }|rt%        ||      S | j                  | j                        S )NT)r@   r8   r   r   is_similar_callablesr   combine_similar_callablesmeet_similar_callablesr   type_objectis_abstractfrom_type_typer2   r   r.   r   r(   
is_genericrH   rV   r   rU   r   rA   r   r   )r   r9   resultresr   s        r:   visit_callable_typez#TypeMeetVisitor.visit_callable_type]  s   dffl+0I0I!TVV0TQ'55a@@+Atvv6F Q]]_%@%@FF&&(TVV-?-?-A-M-M(,%/&//:OL||DFF++M)ammoallnTVV[[!**5CcHo#>?//44<<'')dffkk.E.E008D!!T**||DFF##r<   c                   | j                   }t        |t              rj|j                  |j                  k(  rt	        |j                        S t        ||      r|S t        ||      r|S t        |j                  |j                        S t        | j                   t              rM| j                   j                  j                  r-t        j                  | j                         }|rt        ||      S t        |j                  |      S rL   )r8   r@   r   rg   r   r   rH   rl   r   rA   r   r   r   )r   r9   r8   r   s       r:   visit_overloadedz TypeMeetVisitor.visit_overloadedy  s     FFa&ww!''!!!''**Aq!Aq!!!**ajj99)dffkk.E.E008D!!T**!**a((r<   c                p   t        |j                        }t        |j                        }|||j                         |j                         k(  r]g }t        |j                               D ]=  }|j	                  | j                  |j                  |   |j                  |                ? |S y|||j                         |j                         k(  rf||k(  r`|}|j                  |   }t        |t              sJ t        |j                        }	|j                  |   }
t        |
t              sJ t        |
j                        }t        |	t              rt        |t              sy| j                  |	|      }t        |t              syg }t        |j                  d| |j                  d|       D ]   \  }}|j	                  t        ||             " g }t        |j                  |dz   d |j                  |dz   d       D ]   \  }}|j	                  t        ||             " |t        |      gz   |z   S y||}|}|}n
|J |}|}|}|j                  |   }t        |t              sJ t        |j                        }t        |t              sy|j                         |j                         dz
  k  ry|}|j                         |z
  dz
  }t        t        |j                        ||      \  }}}g }t        ||j                  d|       D ]&  \  }}|j	                  | j                  ||             ( |D ]0  }|j	                  | j                  ||j                  d                2 |rCt        ||j                  | d       D ]&  \  }}|j	                  | j                  ||             ( |S )a  Meet two tuple types while handling variadic entries.

        This is surprisingly tricky, and we don't handle some tricky corner cases.
        Most of the trickiness comes from the variadic tuple items like *tuple[X, ...]
        since they can have arbitrary partial overlaps (while *Ts can't be split). This
        function is roughly a mirror of join_tuples() w.r.t. to the fact that fixed
        tuples are subtypes of variadic ones but not vice versa.
        Nr   r   )r1   rg   r   ranger   r   r@   r0   r2   rA   r   r   rH   r4   tupler[   )r   r8   r9   s_unpack_indext_unpack_indexrg   iunpack_indexs_unpack
s_unpackedt_unpack
t_unpackedr   m_prefixsitim_suffixvariadicfixedunpackr   
prefix_len
suffix_lenr  middler  fivimis                                r:   meet_tupleszTypeMeetVisitor.meet_tuples  s]    -QWW5,QWW5!n&<xxzQXXZ'$&qxxz*ALL1771:qwwqz!BC +%.*D xxzQXXZ'Nn,L-77<0!(J777,X]];
77<0!(J777,X]];
":x8Z
T\=]yyZ8!$1')!!''-<"8!''-<:PQFBOOJr2$67 R')!!'',*:*<"=qww|VWGWGY?Z[FBOOJr2$67 \:d#3"44x??%H)LE!---H)LE-&*---"6;;/(H-<<>HOO-11!
__&3a7
!=%++
J"
 &(..*"=>FBLL2r*+ ?BLL2x}}Q'789 fhnnj[\&BCBTYYr2./ Dr<   c           
        t        | j                  t              rN| j                  | j                  |      }|| j	                  | j                        S t        |t        |            S t        | j                  t              r| j                  j                  j                  t        v ra| j                  j                  rK|j                  |j                  D cg c]%  }t        || j                  j                  d         ' c}      S t        || j                        r|S | j                  j                  j                  rt!        || j                        r|S | j	                  | j                        S c c}w )Nr   r   )r@   r8   r"   r5  r   r   r   rA   rY   r   r[   r   rg   rH   r   r   r   )r   r9   rg   r   s       r:   visit_tuple_typez TypeMeetVisitor.visit_tuple_type  s   dffi($$TVVQ/E}||DFF++UN1$566)vv{{##'@@TVV[[WXW^W^-_W^QSjTVV[[^.LW^-_``"1dff-44Atvv9N ||DFF## .`s   *E5c                R   t        | j                  t              r@| j                  j                  |      D ]V  \  }}}t	        ||      r(||j
                  v || j                  j
                  v k7  s;| j                  | j                        c S  g }| j                  j                  |      D ]3  \  }}}||j                  ||f       |J |j                  ||f       5 t        |      }	| j                  j                         }
|j
                  | j                  j
                  z  }|j                  | j                  j                  z  }t        |	|||
      S t        | j                  t              rt        || j                        r|S | j                  | j                        S rL   )r@   r8   r%   r   r   r   r   zipallr   dictcreate_anonymous_fallbackreadonly_keysr   r   )r   r9   namer   r   	item_list	item_names_item_typet_item_typerg   rl   r   r<  s                r:   visit_typeddict_typez$TypeMeetVisitor.visit_typeddict_type  s^   dffm,"ffjjm
a$Q*tq/FDFF000/  <<//	 ,
 13I7;vv}}Q7G3	;*$$i%=> '222$$i%=> 8H OEvv779HOOdff.B.BBMOOdff.B.BBM }hOO)jDFF.CH<<''r<   c                   t        | j                  t              r| j                  |k(  r|S t        | j                  t              r"t	        |j
                  | j                        r|S | j                  | j                        S rL   )r@   r8   r   r   r   rl   r   r   s     r:   visit_literal_typez"TypeMeetVisitor.visit_literal_type  sV    dffk*tvv{H)jTVV.LH<<''r<   c                    J d       )NzInternal errorr   r   s     r:   visit_partial_typez"TypeMeetVisitor.visit_partial_type  s    &&&ur<   c                   t        | j                  t              rc| j                  |j                  | j                  j                        }t        |t
              s!t        j                  ||j                        }|S t        | j                  t              r%| j                  j                  j                  dk(  r|S t        | j                  t              r| j                  || j                        S | j                  | j                        S )N)liner   )r@   r8   r(   r   rV   r   rU   rH  r   rA   rY   r   r   )r   r9   rh   s      r:   visit_type_typezTypeMeetVisitor.visit_type_type  s    dffh'))AFFDFFKK0Cc8,..s@J)dffkk.B.Bo.UH-99Q''<<''r<   c                    J d|        )Nz!This should be never called, got r   r   s     r:   visit_type_alias_typez%TypeMeetVisitor.visit_type_alias_type   s    =9!==ur<   c                    t        ||      S rL   )rH   )r   r8   r9   s      r:   r   zTypeMeetVisitor.meet#  s    !Qr<   c                    t        |t              rt        t        j                        S t
        j                  r
t               S t               S rL   )	r@   r-   r   r'   r   r   r6   r.   r   )r   rh   s     r:   r   zTypeMeetVisitor.default&  s7    c;'91122$$&((z!r<   N)r8   r!   r   None)r9   r-   r   r!   )r9   r   r   r!   )r9   r/   r   r!   )r9   r   r   r!   )r9   r.   r   r!   )r9   r   r   r!   )r9   r   r   r!   )r9   r+   r   r!   )r9   r   r   r!   )r9   r*   r   r!   )r9   r0   r   r!   )r9   r   r   r!   )r9   r   r   r!   )r9   r   r   r!   )r9   r   r   r!   )r8   r"   r9   r"   r   zlist[Type] | None)r9   r"   r   r!   )r9   r%   r   r!   )r9   r   r   r!   )r9   r    r   r!   )r9   r(   r   r!   )r9   r$   r   r!   r8   r#   r9   r#   r   r!   )rh   r#   r   r!   )__name__
__module____qualname__r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r5  r7  rB  rD  rF  rI  rK  r   r   r   r<   r:   rG   rG     s    	3,		((("
(S$j$8)&L\$((2('(> "r<   rG   c                   ddl m}m}  || |      \  } }g }t        t	        | j
                              D ]4  }|j                   || j
                  |   |j
                  |                6 | j                  j                  j                  dk7  r| j                  }n|j                  }| j                  |t        | j                  |j                        |d       S )Nr   )match_generic_callables	safe_joinzbuiltins.function)r   r   rl   r=  )r   rT  rU  r  rC   r   r   rl   rA   rY   r   rH   r   )r9   r8   rT  rU  r   r"  rl   s          r:   r  r  0  s    <"1a(DAqI3q{{#$1;;q>1;;q>BC %
 	zz#66::::??AJJ

3	   r<   c                r    | st        t        j                        S | d   }| dd  D ]  }t        ||      } |S )Nr   r   )r   r'   re   rH   )typesmetr9   s      r:   meet_type_listrY  F  sB     y8899
(C12Ya  Jr<   c                   t        | |f      \  } }t        | t              rt        |t              rJ t        | t              r| |}}nt        |t              r|| }}nyt        |t              xr |j                  j                  d      S )a  Is this a pair where one type is a TypedDict and another one is an instance of Mapping?

    This case requires a precise/principled consideration because there are two use cases
    that push the boundary the opposite ways: we need to avoid spurious overlaps to avoid
    false positives for overloads, but we also need to avoid spuriously non-overlapping types
    to avoid false positives with --strict-equality.
    Ftyping.Mapping)r3   r@   r%   r   rA   r   )r}   r~   _r   s       r:   r   r   Q  sz     #D%=1KD%$.j6VVV$&5	E=	)$5eX&P5::+>+>?O+PPr<   c                  
 t        | |f      \  } }t        | t              rt        |t              rJ t        | t              rt        |t              sJ | |c
}n)t        | t              sJ t        |t              sJ || c
}t	        d |j
                  j                  D        d      }|
j                  ryt	        d |j
                  j                  D              }t        ||      }t        |j                        \  }
j                         j                  }|j
                  j                  d   j                  d   }t        |t              rt        t              r
j                   S 
j                  r* ||      syt        
fd
j                  D              S  ||      syt!        
j"                  j%                               
j                  z
  }	t'        
fd|	D              S )aS  Check if a TypedDict type is overlapping with a Mapping.

    The basic logic here consists of two rules:

    * A TypedDict with some required keys is overlapping with Mapping[str, <some type>]
      if and only if every key type is overlapping with <some type>. For example:

      - TypedDict(x=int, y=str) overlaps with Dict[str, Union[str, int]]
      - TypedDict(x=int, y=str) doesn't overlap with Dict[str, int]

      Note that any additional non-required keys can't change the above result.

    * A TypedDict with no required keys overlaps with Mapping[str, <some type>] if and
      only if at least one of key types overlaps with <some type>. For example:

      - TypedDict(x=str, y=str, total=False) overlaps with Dict[str, str]
      - TypedDict(x=str, y=str, total=False) doesn't overlap with Dict[str, int]
      - TypedDict(x=int, y=str, total=False) overlaps with Dict[str, str]

    * A TypedDict with at least one ReadOnly[] key does not overlap
      with Dict or MutableMapping, because they assume mutable data.

    As usual empty, dictionaries lie in a gray area. In general, List[str] and List[str]
    are considered non-overlapping despite empty list belongs to both. However, List[int]
    and List[Never] are considered overlapping.

    So here we follow the same logic: a TypedDict with no required keys is considered
    non-overlapping with Mapping[str, <some type>], but is considered overlapping with
    Mapping[Never, Never]. This way we avoid false positives for overloads, and also
    avoid false positives for comparisons like SomeTypedDict == {} under --strict-equality.
    c              3  @   K   | ]  }|j                   d k(  s|  yw)ztyping.MutableMappingNrY   rM   bases     r:   rN   z-typed_dict_mapping_overlap.<locals>.<genexpr>  s     U.$DMM=T,T.   NFc              3  @   K   | ]  }|j                   d k(  s|  yw)r[  Nr_  r`  s     r:   rN   z-typed_dict_mapping_overlap.<locals>.<genexpr>  s     XNDdmmGW6W4Nrb  r   c              3  J   K   | ]  } j                   |           y wrL   r   rM   kr   typed
value_types     r:   rN   z-typed_dict_mapping_overlap.<locals>.<genexpr>  s#     XDWq;u{{1~z:DW    #c              3  J   K   | ]  } j                   |           y wrL   r   re  s     r:   rN   z-typed_dict_mapping_overlap.<locals>.<genexpr>  s!     QLq;u{{1~z:Lri  )r3   r@   r%   r   nextrA   rd   r<  r   r[   as_anonymousrl   basesr.   r   rZ   r   rg   keysrp   )r}   r~   r   r   mutable_mappingmappingkey_typerl   str_typenon_requiredrg  rh  s     `       @@r:   r   r   e  s   D #D%=1KD%$.j6VVV$&%***Uu$)))%///duU%**..UW[O "u':':XEJJNNXXG%eW5E+EJJ7Hj !!#,,H}}""1%**1-H (O,J1X&&&&8X.XEDWDWXXX8X.5;;++-.1D1DDQLQQQr<   rO  )r\   r#   r]   r#   r   r#   )rh   r#   r   
list[Type])r`   r!   rq   r!   r   r   )r9   r!   r   r   )FFFN)r}   r#   r~   r#   r?   r   rJ   r   rz   r   r{   zset[tuple[Type, Type]] | Noner   r   )r}   r#   r~   r#   r?   r   r   r   )r}   r%   r~   r%   r   Callable[[Type, Type], bool]r   r   )r}   r#   r~   r#   r   ru  r   r   )r   r"   r   intr   r"   )r}   r!   r   r!   r   zTupleType | None)rh   r#   r   r   )r9   r   r8   r   r   r   )rW  rt  r   r#   r   )r}   r#   r~   r#   r   ru  r   r   )N
__future__r   typingr   mypyr   mypy.erasetyper   mypy.maptyper   
mypy.stater   mypy.subtypesr	   r
   r   r   r   r   r   mypy.typeopsr   r   r   
mypy.typesr   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r;   rH   rR   ri   rS   rt   rw   rQ   r   r   r   r   r   r   rG   r  rY  r   r   r   r<   r:   <module>r     s   "   % 2    R Q# # # # # # # # #P
'(TL^/d
L $*/"'04|
|| | $(	|
  | .| 
|@
 ;@	
		37			
 -?[	0N
NN-IN	N6.*~"k*- ~"B,Q(GR
GRGR*FGR	GRr<   