
    *eR                       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 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-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8 d:dZ9d:dZ:d;dZ;d<dZ<d=dZ=d=dZ>	 	 	 	 d>d?d%Z?dd&d@d'Z@ddd(dAd*ZAddd(dBd+ZBdCd.ZCdDd/ZD G d0 d1e0e%                   ZEdEd3ZFdFd5ZGdGd6ZHdHd9ZIdS )I    )annotations)Callable)join)
erase_type)map_instance_to_supertype)state)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_suffixsr!   treturnr   c                    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   r0   r   strict_optionalr,   r   r3   r4   s     )lib/python3.11/site-packages/mypy/meet.pytrivial_meetr:   9   sc    !Q q!!!	Aq		 q!!!  	"$$$::    c                   t          | |          rt          | |          S t          |           } t          |          }t          | t                    rt          |t                    r| j        |j        k    ro| j        s|j        rat          | |          rQ| j        r?|j        r8t          | j        j	                  t          |j        j	                  k    r| S |S | j        r| S |S t          | t                    s=t          |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        | |          \  } }|                    t%          |                     S )z-Return the greatest lower bound of two types.Tignore_promotions)r   r:   r0   
isinstancer   typeextra_attrsr   lenattrsr+   r   r   r   r-   r   normalize_callablesacceptTypeMeetVisitorr8   s     r9   
meet_typesrG   F   s   A " Aq!!!AA!X :a#:#: qv?O?O M 	Q] 	Q0B0B 	}  q}*++c!-2E.F.FFFH} Ha%% jK.H.H QT::: 	HQT::: 	H!Z   !W !Y 
1i(@(@ !1 #Aq))DAq88OA&&'''r;   declarednarrowedc                     t          t                    rj        S  }}t                      t                     k    r|S t           t                    r-t          fd                                 D                       S t                     r|S t           d          s(t          j
        rt                      S t                      S t          t                    r-t           fd                                D                       S t          t                    r|S t          t                    rt          j                   rS t           t"                    rAt          t"                    r,t#          j        t'           j        j                            S t           t"                    r0t          t*                    rj                                        r|S t           t*                    rY j        j        r|S t          t*                    r&j        j        rj        j        j         j        u r|S t3          ||          S t           t4          t"          t6          f          rt3          ||          S t           t8                    rbt          t*                    rMj        j        dk    r-t=          d t?          j                   D                       r|S t3          ||          S |S )z7Return the declared type narrowed down to another type.c                x    g | ]6}t          |d           st          |d          &t          |          7S )Tr=   F)is_overlapping_typesr   narrow_declared_type).0xrI   s     r9   
<listcomp>z(narrow_declared_type.<locals>.<listcomp>   sc        )HMMM "(AGGG$Q11  r;   T)prohibit_none_typevar_overlapc                0    g | ]}t          |          S  )rM   )rN   rO   rH   s     r9   rP   z(narrow_declared_type.<locals>.<listcomp>   s$    RRR1!(A..RRRr;   zbuiltins.dictc              3  @   K   | ]}t          |t                    V  d S N)r?   r   )rN   r4   s     r9   	<genexpr>z'narrow_declared_type.<locals>.<genexpr>   s=       =
 =
'(Jq'""=
 =
 =
 =
 =
 =
r;   )!r?   r$   
type_guardr0   r-   r   relevant_itemsis_enum_overlapping_unionrL   r   r7   r,   r   r   r)   r   upper_boundr&   make_normalizedrM   itemr   r@   is_metaclassalt_promoterG   r    r   r#   fullnameallr1   args)rH   rI   original_declaredoriginal_narroweds   ``  r9   rM   rM   p   sL    (O,, #""  x((Hx((H8  (I&& 
$   !0022  
 
 	
 !844 -@  !(HTXYYY +@  	"$$$::	Hi	(	( &@$RRRR8O8O8Q8QRRR
 
 	
 
Hg	&	& "@  	Hk	*	*  @z(:NPX/Y/Y  @	Hh	'	' @Jx,J,J @'(<X]HM(Z(Z[[[8X&&@x**@ M&&((@ ! 	Hh	'	' @=$ 	%$$x**	%)	% ).(-?? %$+->???	Hy(K@	A	A @+->???	Hm	,	, @Hh1O1O @=!_44 =
 =
,<X],K,K=
 =
 =
 :
 :
4 %$+->???r;   typ
list[Type]c                   t          |           } t          | t                    r't          | j                  dk    r| j        S | j        gS t          | t                    r| 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   )r0   r?   r)   rB   valuesrZ   r   r(   r-   listitemsr   rd   s    r9   get_possible_variantsrk      s    2 #

C#{## sz??Q:O$$	C	'	'   	C)	*	* 	  	C	#	# CI	C	$	$  CIur;   rO   yboolc                     t           t                    oW j        j        oKt          |t                    o6t           fdd |                                D             D                       S )zOReturn True if x is an Enum, and y is an Union with at least one Literal from xc              3  l   K   | ].}t          |t                    oj        |j        j        k    V  /d S rU   )r?   r   r@   fallback)rN   prO   s     r9   rV   z,is_enum_overlapping_union.<locals>.<genexpr>   sQ       
 
 q+&&D16QZ_+D
 
 
 
 
 
r;   c              3  4   K   | ]}t          |          V  d S rU   r0   )rN   zs     r9   rV   z,is_enum_overlapping_union.<locals>.<genexpr>   s*      EEQoa((EEEEEEr;   )r?   r   r@   is_enumr-   anyrX   rO   rl   s   ` r9   rY   rY      s     	1h 	
FN	
q)$$	
  
 
 
 
EE!2B2B2D2DEEE
 
 
 
 
	r;   c                     t           t                    o4t          |t                    o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    V  d S rU   rs   )rN   rt   rO   s     r9   rV   z&is_literal_in_union.<locals>.<genexpr>   s0      99A_Q'''999999r;   )r?   r   r-   rv   ri   rw   s   ` r9   is_literal_in_unionrz      sP     	1k"" 	:q)$$	:999999999r;   FNleftrightr>   rQ   ignore_uninhabited
seen_typesset[tuple[Type, Type]] | Nonec                n   t          | t                    st          |t                    rdS t                      | |fv rdS t          | t                    r,t          |t                    r                    | |f           t          | |f          \  } }dfdt          | t                    st          |t                    r
J d
            t          t          t          f}t          | |          st          ||          rdS t          j        st          | t                    r&t          j        |                                           } t          |t                    r&t          j        |                                          }t          | |f          \  } }t          | t                    st          |t                    rdS t!          | |          s0t!          ||           s t#          | |          st#          ||           rdS t%          | |          st%          ||           rdS t'          |           }t'          |          }t          | t(          t*          f          rt          |t(          t*          f          rdS t          | t(                    st          |t(                    rd	S dd}	r |	| |          s |	||           rd	S t-          |          dk    s=t-          |          dk    s*t          | t.                    st          |t.                    r|D ]}
|D ]} |
|          r  dS d	S t          j        r.t          | t0                    t          |t0                    k    rd	S t          | t2                    r't          |t2                    rt5          | |          S t7          | |          rt9          | |          S t          | t2                    r| j        } nt          |t2                    r|j        }t=          |           r!t=          |          rt?          | |          S t          | t@                    rtC          |           } n$t          |t@                    rtC          |          }t          | tD                    r+t          |tD                    r | j#        |j#                  S dfd}t          | tD                    st          |tD                    r || |          p |||           S t          | tH                    r*t          |tH                    rtK          | |d	dd          S t          | tH                    r| j        } nt          |tH                    r|j        }t          | tL                    r6t          |tL                    r!| j'        |j'        k    r| j        } |j        }n;d	S t          | tL                    r| j        } nt          |tL                    r|j        }t          | tP                    r:t          |tP                    r$tS          | |          stS          ||           rdS |j*        j+        dk    r| j*        j+        tX          v rdS | j*        -                    |j*        j+                  rt]          | |j*                  } n<|j*        -                    | j*        j+                  rt]          || j*                  }nd	S t-          | j/                  t-          |j/                  k    r5ta          fdtc          | j/        |j/                  D                       rdS d	S tU          |           tU          |          k    s)J tU          |            dtU          |                       d	S )a9  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).
    TNr{   r!   r|   r5   rm   c           	     R    t          | |                                          S )zEncode the kind of overlapping check to perform.

        This function mostly exists, so we don't have to repeat keyword arguments everywhere.
        )r>   rQ   r}   r~   )rL   copy)r{   r|   r>   r}   rQ   r~   s     r9   _is_overlapping_typesz3is_overlapping_types.<locals>._is_overlapping_types   s8    
 $/*G1!((
 
 
 	
r;   Fz%Unexpectedly encountered partial type)r>   r}   t1t2c                ~    t          | |f          \  } }t          | t                    ot          |t                    S rU   )r1   r?   r   r'   )r   r   s     r9   is_none_typevarlike_overlapz9is_overlapping_types.<locals>.is_none_typevarlike_overlap  s6    !2r(++B"h''KJr?,K,KKr;      r=   )overlappingc                   t          | |f          \  } }t          | t                    r+t          |t                    r | j        |j                  S t          | t                    rt          |t                    rt          | j        t                    r9| j        j        j        }| ||          S |j        	                    d          S t          | j        t                    r|j        	                    d          S dS )z-Special cases for type object types overlaps.Nbuiltins.typeF)r1   r?   r&   r   r\   ret_typer   r@   metaclass_typehas_baser   )r{   r|   	left_metar   s      r9   _type_object_overlapz2is_overlapping_types.<locals>._type_object_overlap  s     'e}55edH%% 	D*UL*I*I 	D((ENCCCdH%% 	<*UH*E*E 	<$)X.. < IN9	(00EBBBz**?;;;DIw// <z**?;;;ur;   )	is_compatr   ignore_pos_arg_namesallow_partial_overlapzbuiltins.intc              3  6   K   | ]\  }} ||          V  d S rU   rS   )rN   left_arg	right_argr   s      r9   rV   z'is_overlapping_types.<locals>.<genexpr>  sH        'Hi &%h	::     r;   z vs r{   r!   r|   r!   r5   rm   )r   r!   r   r!   r5   rm   )2r?   r$   setr"   addr1   r   r+   r   r   r   r7   r-   
make_unionrX   r   rY   rz   r   rk   r   r   rB   r'   r   r#   are_typed_dicts_overlappingtyped_dict_mapping_pairtyped_dict_mapping_overlaprp   is_tupleare_tuples_overlappingr    r   r&   r\   r   r	   r   valuer   r   r@   r_   r   r   r   ra   r`   zip)r{   r|   r>   rQ   r}   r~   illegal_typesleft_possibleright_possibler   lrr   r   s     ````       @r9   rL   rL     s    $(( J- -  tUU
e}
""t$&& &:e]+K+K &e}%%%"D%=11KD%
 
 
 
 
 
 
 
 
 $$$ >
5+(F(F >===== !*k:M$&& *UM*J*J t
   6dI&& 	?'(;(;(=(=>>DeY'' 	A()=)=)?)?@@E&e}55e $   Jug$>$> t 	"$..$UD11 tU++ ud++	 te'8M_   	t'8M_
 
 

 t
 *$//M*511N $]344 
M*: :  t$
## z%'D'D uL L L L % &&tU33 	7R7RSXZ^7_7_ 	5 	MQ~""dO,, #e_-- #  	  	 A#    ((A..  444   u  D(!;!;z%QY?Z?Z!Z!Zu $&& :e]+K+K *4J[\\\\	 u	-	- )$CXYYYY	D-	(	( }	E=	)	) ~~ &(5// &%dEEVWWWW	D)	$	$ &d##	E9	%	% &u%% $!! <j&A&A <$$TY
;;;     ( $!! VZx%@%@ V##D%00U4H4HPT4U4UU$%% *UL*I*I %+#!%"&
 
 
 	
 
D,	'	' }	E<	(	( $$$ E;)G)G :$$ =DNEE5	D+	&	& }	E;	'	'  $!! *j&A&A * %+<Qc
 
 
 	4+<Qc
 
 
	
 4:.00TY5GKa5a5a4 9ej122 	,T5:>>DDZ  !344 	-eTY??EE5ty>>S__,,     +.ty%*+E+E      tu ::e$$$d&F&Fe&F&F$$$5r;   r=   c               \    t          t          |           t          |          |d          S )zMThe same as 'is_overlapping_erased_types', except the types are erased first.Tr>   rQ   )rL   r   )r{   r|   r>   s      r9   is_overlapping_erased_typesr   *  s7      45+&*	   r;   r   r#   c                  | j         D ]:}||j        vr dS t          | j        |         |j        |         ||          s dS ;|j         D ]9}|| j        vr dS t          | j        |         |j        |         |          s dS :dS )z?Returns 'true' if left and right are overlapping TypeDictTypes.Fr   r=   T)required_keysri   rL   )r{   r|   r>   rQ   keys        r9   r   r   6  s     ! 	 	ek!!55#JsOK/*G	
 
 
 	 55	 "  dj  55#JsOU[-AR
 
 
 	 55	 4r;   c                  t          | |f          \  } }t          | |          p| } t          ||           p|}t          | t                    sJ d|  d            t          |t                    sJ d| d            t	          | j                  t	          |j                  k    rdS 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  D   K   | ]\  }}t          ||           V  dS )r   N)rL   )rN   r   r   r>   rQ   s      r9   rV   z)are_tuples_overlapping.<locals>.<genexpr>h  sX         Aq 	/*G		
 	
 	
     r;   )r1   adjust_tupler?   r    rB   ri   r`   r   )r{   r|   r>   rQ   s     ``r9   r   r   Y  s    #D%=11KD%e$$,D%%.EdI&&EE(E(E(E(EEEEeY''GG)G)G)G)GGGG
4:#ek****u      
EK00     r;   r   TupleType | Nonec                    t          | t                    rZ| j        j        dk    rJt          |t                    r|                                nd}t	          | j        d         g|z  |           S dS )zGFind out if `left` is a Tuple[A, ...], and adjust its length to `right`builtins.tupler   r   N)r?   r   r@   r_   r    lengthra   )r{   r   ns      r9   r   r   s  sf    $!! 3di&8<L&L&L$Q	229AHHJJJ$)A,!+T2224r;   c                    t          |           } t          | t                    p$t          | t                    o| j        j        dk    S )Nr   )r0   r?   r    r   r@   r_   rj   s    r9   r   r   {  sC    
#

Cc9%% 3!!Kch&7;K&Kr;   c                      e Zd Zd8dZd9dZd:d
Zd;dZd<dZd=dZd>dZ	d?dZ
d@dZdAdZdBdZdCdZdDdZdEd ZdFd"ZdGd$ZdHd'ZdId(ZdJd*ZdKd,ZdLd.ZdMd0ZdNd2ZdOd4ZdPd6Zd7S )QrF   r3   r   r5   Nonec                    || _         d S rU   r3   )selfr3   s     r9   __init__zTypeMeetVisitor.__init__  s    r;   r4   r+   c                   t          | j        t                    r,t          j        rt          t          j                  S | j        S t          | j        t                    r| j        S t          t          j                  S rU   )	r?   r3   r   r   r7   r   r%   special_formr,   r   r4   s     r9   visit_unbound_typez"TypeMeetVisitor.visit_unbound_type  se    dfh'' 	3$ y5666v00 	36M91222r;   r   c                    | j         S rU   r   r   s     r9   	visit_anyzTypeMeetVisitor.visit_any  	    vr;   r-   c                     t           j        t                    r?g }|j        D ]4} j        j        D ]%}|                    t          ||                     &5n fd|j        D             }t          |          S )Nc                :    g | ]}t          |j                  S rS   )rG   r3   )rN   rO   r   s     r9   rP   z4TypeMeetVisitor.visit_union_type.<locals>.<listcomp>  s%    <<<qZ46**<<<r;   )r?   r3   r-   ri   appendrG   r   )r   r4   meetsrO   rl   s   `    r9   visit_union_typez TypeMeetVisitor.visit_union_type  s    dfi(( 	= "EW 3 3 3 3ALLAq!1!1222233 =<<<AG<<<E$U+++r;   r   c                    t           j        rYt          | j        t                    s/t          | j        t
                    r| j        j        j        dk    r|S t                      S |S )Nzbuiltins.object)	r   r7   r?   r3   r   r   r@   r_   r,   r   s     r9   visit_none_typezTypeMeetVisitor.visit_none_type  s^      	$&(++ )468,,)151EIZ1Z1Z&(((Hr;   r,   c                    |S rU   rS   r   s     r9   visit_uninhabited_typez&TypeMeetVisitor.visit_uninhabited_type  s    r;   r   c                    t          | j        t                    rt          j        r|S | j        S t          | j        t
                    r| j        S |S rU   )r?   r3   r   r   r7   r,   r   s     r9   visit_deleted_typez"TypeMeetVisitor.visit_deleted_type  sM    dfh'' 	$ v00 	6MHr;   r   c                    | j         S rU   r   r   s     r9   visit_erased_typez!TypeMeetVisitor.visit_erased_type  r   r;   r)   c                    t          | j        t                    r| j        j        |j        k    r| j        S |                     | j                  S rU   )r?   r3   r)   iddefaultr   s     r9   visit_type_varzTypeMeetVisitor.visit_type_var  sA    dfk** 	(tvyAD/@/@6M<<'''r;   r   c                Z    | j         |k    r| j         S |                     | j                   S rU   )r3   r   r   s     r9   visit_param_specz TypeMeetVisitor.visit_param_spec  s(    6Q;;6M<<'''r;   r(   c                    t          | j        t                    r3| j        j        |j        k    r| j        j        |j        k    r| j        n|S |                     | j                  S rU   )r?   r3   r(   r   min_lenr   r   s     r9   visit_type_var_tuplez$TypeMeetVisitor.visit_type_var_tuple  sW    df.// 	(DFI4E4E!V^ai77466Q><<'''r;   r.   c                    t           rU   )NotImplementedErrorr   s     r9   visit_unpack_typez!TypeMeetVisitor.visit_unpack_type  s    !!r;   r   c                   t          | j        t                    rt          |j                  t          | j        j                  k    r|                     | j                  S ddlm |                    fdt          | j        j        |j                  D                       S |                     | j                  S )Nr   )
join_typesc                .    g | ]\  }} ||          S rS   rS   )rN   s_at_ar   s      r9   rP   z4TypeMeetVisitor.visit_parameters.<locals>.<listcomp>  s)    cccHC::c3//cccr;   )	arg_types)
r?   r3   r   rB   r   r   	mypy.joinr   copy_modifiedr   )r   r4   r   s     @r9   visit_parametersz TypeMeetVisitor.visit_parameters  s    dfj)) 		(1;3tv'7#8#888||DF+++,,,,,,??ccccDFDTVWVa@b@bccc #    <<'''r;   r   c                	   t          | j        t                    r	|j        | j        j        k    rMt	          || j                  st	          | j        |          rg }|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 ]\  }}}|                     ||          }t          |t                    rpt          |t                    r|                    |j                   at          |t(                    sJ t+          |j                            |g                    }|                    |           t          |j        |          S t0          j        rt)                      S t5                      S |j        j        }|r|j        | j        j        u r|S | j        j        j        }|r|j        |j        u r| j        S t	          || j                  r|S t	          | j        |          r| j        S t0          j        rt)                      S t5                      S t          | j        t8                    r9|j        j        r-t=          j        |          }|rtA          || j                  S nCt          | j        t8                    rm| j        !                                rT|j        "                                r;t	          | j        j#        |          r| j        S | $                    | 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 | $                    | j                  S )N)ra   )(r?   r3   r   r@   r   has_type_var_tuple_typetype_var_tuple_prefixtype_var_tuple_suffixdefn	type_varsr(   r   r2   ra   r    rh   r   meetextendri   r,   r.   r   r   r   r7   r   r^   r   is_protocolr   unpack_callback_protocolrG   is_type_objr]   rp   r   r&   r   r#   )r   r4   ra   r3   prefixsuffixtvtrp   s_prefixs_middles_suffixt_prefixt_middlet_suffixs_argst_argstasatvr   r^   calls                         r9   visit_instancezTypeMeetVisitor.visit_instance  s~   dfh'' Q	)v$$a(( .*Jtvq,A,A .* (*D v5 - !F v;GGG v;GGG!"!=!"!=fk3F;)#/?@@@@@#&#57SFFF8 84(H 8TFFF8 84(H "*YtH~~x-P-P,R!RU]!]!)YtH~~x-P-P,R!RU]!]!"!%&)&&!&+:O&P&P * *
B#yyR00%b*:;; `  *$	:: ` $DJ 7 7 7 ('1$'H'H H H H'1"2C2Q2QX\W]2Q2^2^'_'_D))))#AFD111, *.000'zz)f0 ;#3tv{#B#BH"fk5 ";#3qv#=#=6Ma(( 	*H** *6M, *.000'zz)-- 	)!&2D 	)033D 0!$///0-- 	)$&2D2D2F2F 	)16K^K^K`K` 	)$&/1-- v<<''')) 	)a(((	** 	)a(((,, 	)a(((.. 	)a(((||DF###r;   r   c                   t          | j        t                    rt          j        || j                  rt          || j                  rt          j        || j                  S t          || j                  }|                                r|	                                j
        s>| j                                        r| j        	                                j
        sd|_        t          t          |j                  t                    r|                     | j                  S |S t          | j        t                     r|                                r}|                                sit%          | j        j        |j                  }t          |t(          t          f          st!          j        |          S |                     | j                  S t          | j        t,                    r<| j        j        j        r+t          j        | j                  }|rt%          ||          S |                     | j                  S )NT)r?   r3   r   r   is_similar_callablesr
   combine_similar_callablesmeet_similar_callablesr   type_objectis_abstractfrom_type_typer0   r   r,   r   r&   
is_genericrG   r\   r   r[   r   r@   r   r   )r   r4   resultresr   s        r9   visit_callable_typez#TypeMeetVisitor.visit_callable_type0  s   dfl++ 	+0I!TV0T0T 	+Q'' A5a@@@+Atv66F -%&]]__%@-F&&((--1V-?-?-A-A-M- )-%/&/::OLL ,||DF+++M)) 		+ammoo 		+allnn 		+TV[!*55CcHo#>?? 5/444<<''')) 	+dfk.E 	+088D +!!T***||DF###r;   r   c                   | j         }t          |t                    rb|j        |j        k    rt	          |j                  S t          ||          r|S t          ||          r|S t          |j        |j                  S t          | j         t                    r<| j         j	        j
        r+t          j        | j                   }|rt          ||          S t          |j        |          S rU   )r3   r?   r   ri   r   r   rG   rp   r   r@   r   r   r   )r   r4   r3   r   s       r9   visit_overloadedz TypeMeetVisitor.visit_overloadedL  s     Fa&& 	+w!'!!!!'***Aq!! :Aq!! :!!*aj999)) 	+dfk.E 	+088D +!!T***!*a(((r;   r    list[Type] | Nonec                n   t          |j                  }t          |j                  }|||                                |                                k    rgg }t          |                                          D ]A}|                    |                     |j        |         |j        |                              B|S dS |||                                |                                k    r||k    r|}|j        |         }t          |t                    sJ t          |j	                  }	|j        |         }
t          |
t                    sJ t          |
j	                  }t          |	t                    rt          |t                    sdS |                     |	|          }t          |t                    sdS g }t          |j        d|         |j        d|                   D ](\  }}|                    t          ||                     )g }t          |j        |dz   d         |j        |dz   d                   D ](\  }}|                    t          ||                     )|t          |          gz   |z   S dS ||}|}|}n
|J |}|}|}|j        |         }t          |t                    sJ t          |j	                  }t          |t                    sdS |                                |                                dz
  k     rdS |}|                                |z
  dz
  }t          t          |j                  ||          \  }}}g }t          ||j        d|                   D ].\  }}|                    |                     ||                     /|D ]6}|                    |                     ||j        d                              7|rMt          ||j        | d                   D ].\  }}|                    |                     ||                     /|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   )r/   ri   r   ranger   r   r?   r.   r0   r@   r   r   rG   r2   tuplera   )r   r3   r4   s_unpack_indext_unpack_indexri   iunpack_indexs_unpack
s_unpackedt_unpack
t_unpackedr   m_prefixsitim_suffixvariadicfixedunpackunpacked
prefix_len
suffix_lenr   middler   fivimis                                r9   meet_tupleszTypeMeetVisitor.meet_tuples_  s;    -QW55,QW55!n&<xxzzQXXZZ''$&qxxzz** D DALL171:qwqz!B!BCCCC4%.*D xxzzQXXZZ''Nn,L,L-7<0!(J77777,X];;
7<0!(J77777,X];;
":x88  Z
T\=]=]  4yyZ88!$11  4')!!'-<-"8!'-<-:PQQ 8 8FBOOJr2$6$67777')!!',*:*<*<"=qw|VWGWGYGY?Z[[ 8 8FBOOJr2$6$67777:d#3#3"44x??4%H)LEE!---H)LE-&*-----"6;//(H-- 	4<<>>HOO--1114!
__&&3a7
!=%+
J"
 "
 &(.*"=>> 	, 	,FBLL2r**++++ 	: 	:BLL2x}Q'7889999 	0fhnj[\\&BCC 0 0BTYYr2..////r;   c                \    t           j        t                    rT                      j        |          }|                      j                  S t          |t          |                    S t           j        t                    r j        j        j        t          v r3 j        j
        r'|                     fd|j        D                       S t          | j                  r|S  j        j        j        rt          | j                  r|S                       j                  S )Nc                P    g | ]"}t          |j        j        d                    #S )r   )rG   r3   ra   )rN   itr   s     r9   rP   z4TypeMeetVisitor.visit_tuple_type.<locals>.<listcomp>  s,    -_-_-_QSjTV[^.L.L-_-_-_r;   ri   )r?   r3   r    r'  r   r   r   r@   r_   r   ra   r   ri   r   r   r   )r   r4   ri   s   `  r9   visit_tuple_typez TypeMeetVisitor.visit_tuple_type  s   dfi(( 	$$TVQ//E}||DF+++UN1$5$5666)) 
	v{#'@@@TV[@-_-_-_-_WXW^-_-_-_```"1df-- 4 Atv9N9N  ||DF###r;   r#   c                   t          | j        t                    r| j                            |          D ]K\  }}}t	          ||          r||j        v || j        j        v k    r|                     | j                  c S Lg }| j                            |          D ];\  }}}||                    ||f            |J |                    ||f           <t          |          }	| j        
                                }
|j        | j        j        z  }t          |	||
          S t          | j        t                    rt          || j                  r|S |                     | j                  S rU   )r?   r3   r#   r   r
   r   r   zipallr   dictcreate_anonymous_fallbackr   r   )r   r4   namer   r   	item_list	item_names_item_typet_item_typeri   rp   r   s               r9   visit_typeddict_typez$TypeMeetVisitor.visit_typeddict_type  s   dfm,, 	("fjjmm 0 0
a$Q** 0tq/FDF00/ /  <<////// 13I7;v}}Q7G7G ? ?3	;*$$i%=>>>> '222$$i%=>>>>OOEv7799HOdf.BBM x@@@)) 	(jDF.C.C 	(H<<'''r;   r   c                    t          | j        t                    r| j        |k    r|S t          | j        t                    rt	          |j        | j                  r|S |                     | j                  S rU   )r?   r3   r   r   r   rp   r   r   s     r9   visit_literal_typez"TypeMeetVisitor.visit_literal_type  sg    dfk** 	(tv{{H)) 	(jTV.L.L 	(H<<'''r;   r   c                    J d            )NFzInternal errorrS   r   s     r9   visit_partial_typez"TypeMeetVisitor.visit_partial_type  s    &&&&&r;   r&   c                   t          | j        t                    rW|                     |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                  S |                     | j                  S )N)liner   )r?   r3   r&   r   r\   r   r[   r<  r   r@   r_   r   r   )r   r4   rd   s      r9   visit_type_typezTypeMeetVisitor.visit_type_type  s    dfh'' 
	())AFDFK00Cc8,, A.s@@@J)) 	(dfk.Bo.U.UH-- 	(99Q'''<<'''r;   r"   c                    J d|             )NFz!This should be never called, got rS   r   s     r9   visit_type_alias_typez%TypeMeetVisitor.visit_type_alias_type  s    ==!=====r;   r!   c                "    t          ||          S rU   )rG   )r   r3   r4   s      r9   r   zTypeMeetVisitor.meet  s    !Qr;   rd   c                    t          |t                    rt          t          j                  S t
          j        rt                      S t                      S rU   )	r?   r+   r   r%   r   r   r7   r,   r   )r   rd   s     r9   r   zTypeMeetVisitor.default  sF    c;'' 	"91222$ "&(((zz!r;   N)r3   r   r5   r   )r4   r+   r5   r   )r4   r   r5   r   )r4   r-   r5   r   )r4   r   r5   r   )r4   r,   r5   r   )r4   r   r5   r   )r4   r   r5   r   )r4   r)   r5   r   )r4   r   r5   r   )r4   r(   r5   r   )r4   r.   r5   r   )r4   r   r5   r   )r4   r   r5   r   )r4   r   r5   r   )r4   r   r5   r   )r3   r    r4   r    r5   r  )r4   r    r5   r   )r4   r#   r5   r   )r4   r   r5   r   )r4   r   r5   r   )r4   r&   r5   r   )r4   r"   r5   r   r3   r!   r4   r!   r5   r   )rd   r!   r5   r   )__name__
__module____qualname__r   r   r   r   r   r   r   r   r   r   r   r   r   r   r
  r  r'  r,  r6  r8  r:  r=  r?  r   r   rS   r;   r9   rF   rF     s          	3 	3 	3 	3   , , , ,	 	 	 	   	 	 	 	   ( ( ( (( ( ( (( ( ( (" " " "
( 
( 
( 
(S$ S$ S$ S$j$ $ $ $8) ) ) )&L L L L\$ $ $ $(( ( ( (0( ( ( (' ' ' '( ( ( (> > > >       " " " " " "r;   rF   r   c                n   ddl m} g }t          t          | j                            D ]7}|                     || j        |         |j        |                              8| j        j        j        dk    r| j        }n|j        }| 	                    |t          | j        |j                  |d           S )Nr   )	safe_joinzbuiltins.function)r   r   rp   r1  )r   rG  r  rB   r   r   rp   r@   r_   r   rG   r   )r4   r3   rG  r   r  rp   s         r9   r  r    s    ######I3q{##$$ D D1;q>1;q>BBCCCC 	z#666::??AJ
33	    r;   typesc                    | st          t          j                  S | d         }| dd          D ]}t          ||          }|S )Nr   r   )r   r%   implementation_artifactrG   )rH  metr4   s      r9   meet_type_listrL    sS     : y8999
(C122Y ! !a  Jr;   c                L   t          | |f          \  } }t          | t                    rt          |t                    rJ t          | t                    r| |}}nt          |t                    r|| }}ndS t          |t                    o|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)r1   r?   r#   r   r@   r   )r{   r|   _others       r9   r   r   "  s     #D%=11KD%$..Vj6V6VVVV$&& 5	E=	)	) $5ueX&&P5:+>+>?O+P+PPr;   r   Callable[[Type, Type], bool]c                  	
 t          | |f          \  } }t          | t                    rt          |t                    rJ t          | t                    rt          |t                    sJ | |c	}n3t          | t                    sJ t          |t                    sJ || c	}t	          d |j        j        D                       }t          ||          }t          |j                  \  }
		                                j
        }|j        j        d         j        d         }t          |t                    rt          
t                    r	j         S 	j        r0 ||          sdS t          	
fd	j        D                       S  ||          sdS t          	j                                                  	j        z
  }t%          	
fd|D                       S )a  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]

    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  0   K   | ]}|j         d k    |V  dS )rN  N)r_   )rN   bases     r9   rV   z-typed_dict_mapping_overlap.<locals>.<genexpr>`  s/      XXDdmGW6W6W46W6W6W6WXXr;   r   Fc              3  F   K   | ]} j         |                   V  d S rU   r+  rN   kr   typed
value_types     r9   rV   z-typed_dict_mapping_overlap.<locals>.<genexpr>o  s5      XXq;;u{1~z::XXXXXXr;   c              3  F   K   | ]} j         |                   V  d S rU   r+  rV  s     r9   rV   z-typed_dict_mapping_overlap.<locals>.<genexpr>t  s5      QQq;;u{1~z::QQQQQQr;   )r1   r?   r#   r   nextr@   mror   ra   as_anonymousrp   basesr,   r   r`   r   ri   keysrv   )r{   r|   r   rP  mappingkey_typerp   str_typenon_requiredrX  rY  s     `      @@r9   r   r   6  s   > #D%=11KD%$..Vj6V6VVVV$&& #%*****Uuu$)))))%/////duXXEJNXXXXXG%eW55E+EJ77Hj !!##,H}"1%*1-H (O,, 'J1X1X '&&& R{8X.. 	5XXXXXXEDWXXXXXX{8X.. 	55;++--..1DDQQQQQQLQQQQQQr;   rB  )rH   r!   rI   r!   r5   r!   )rd   r!   r5   re   )rO   r   rl   r   r5   rm   )FFFN)r{   r!   r|   r!   r>   rm   rQ   rm   r}   rm   r~   r   r5   rm   )r{   r!   r|   r!   r>   rm   r5   rm   )
r{   r#   r|   r#   r>   rm   rQ   rm   r5   rm   )
r{   r!   r|   r!   r>   rm   rQ   rm   r5   rm   )r{   r   r   r   r5   r   )rd   r!   r5   rm   )r4   r   r3   r   r5   r   )rH  re   r5   r!   r   )r{   r!   r|   r!   r   rQ  r5   rm   )J
__future__r   typingr   mypyr   mypy.erasetyper   mypy.maptyper   
mypy.stater   mypy.subtypesr	   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-   r.   r/   r0   r1   r2   r:   rG   rM   rk   rY   rz   rL   r   r   r   r   r   rF   r  rL  r   r   rS   r;   r9   <module>rm     s"   " " " " " "             % % % % % % 2 2 2 2 2 2                    R Q Q Q Q Q Q Q Q Q# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #P
 
 
 
'( '( '( '(TL L L L^+ + + +\
 
 
 
    $*/$04d d d d dP	 ;@	 	 	 	 	 	  $*/           N $*/     4      }" }" }" }" }"k*- }" }" }"@   *   Q Q Q Q(>R >R >R >R >R >Rr;   