
    X-Pht7                      U d Z ddlmZ ddlmZmZ ddlmZmZm	Z	 ddl
mZ ddlZddlZddlmZ ddlmZ dd	lmZ dd
lmZmZmZmZmZ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/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZC ddlDmEZE ddlFmGZG erddlHmIZI dZJdeKd<   dZLdeKd<    G d d          ZMdnd#ZN	 dodpd,ZOdpd-ZPdqd1ZQdrd3ZRdsd5ZSdtd9ZTdud;ZUdvd=ZVdwd@ZWdxdBZXdxdCZYdydFZZdzdIZ[dydJZ\dydKZ]d{dOZ^d|dQZ_ G dR dSe2e`                   Za G dT dUe8ebeM                            Zcd}dWZdd~dZZedd\Zfdd`ZgddcZhddfZiddiZjddkZkddmZldS )zType inference constraints.    )annotations)IterableSequence)TYPE_CHECKINGFinalcast)	TypeGuardN)ArgTypeExpander)erase_typevars)map_instance_to_supertype)ARG_OPTARG_POSARG_STAR	ARG_STAR2CONTRAVARIANT	COVARIANTArgKindTypeInfo)%TUPLE_LIKE_INSTANCE_NAMESAnyTypeCallableTypeDeletedType
ErasedTypeInstanceLiteralTypeNoneTypeNormalizedCallableType
Overloaded
ParametersParamSpecTypePartialType
ProperType	TupleTypeTypeTypeAliasTypeTypedDictType	TypeOfAny	TypeQueryTypeType	TypeVarIdTypeVarLikeTypeTypeVarTupleTypeTypeVarTypeTypeVisitorUnboundTypeUninhabitedType	UnionType
UnpackTypefind_unpack_in_listflatten_nested_tuplesget_proper_typehas_recursive_typeshas_type_varsis_named_instancesplit_with_prefix_and_suffix)is_union_with_any)
type_state)ArgumentInferContextr   
SUBTYPE_OF   SUPERTYPE_OFc                  L    e Zd ZU dZded<   dZded<   ddZddZddZddZ	dS )
ConstraintzpA representation of a type constraint.

    It can be either T <: type or T :> type (T is a type variable).
    r*   type_varr   r$   targetr+   opintreturnNonec                V    |j         | _        || _        || _        || _        g | _        d S N)idrB   rD   rC   origin_type_varextra_tvars)selfrB   rD   rC   s       P/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/mypy/constraints.py__init__zConstraint.__init__S   s3      ' 35    strc                R    d}| j         t          k    rd}| j         d| d| j         S )Nz<:z:> )rD   r?   rB   rC   )rM   op_strs     rN   __repr__zConstraint.__repr___   s9    7l""F-88&884;888rP   c                D    t          | j        | j        | j        f          S rI   )hashrB   rD   rC   )rM   s    rN   __hash__zConstraint.__hash__e   s    T]DGT[9:::rP   otherobjectboolc                    t          |t                    sdS | j        | j        | j        f|j        |j        |j        fk    S NF)
isinstancerA   rB   rD   rC   )rM   rY   s     rN   __eq__zConstraint.__eq__h   s@    %,, 	5tw4SXS_8```rP   N)rB   r+   rD   rE   rC   r$   rF   rG   )rF   rQ   )rF   rE   )rY   rZ   rF   r[   )
__name__
__module____qualname____doc____annotations__rD   rO   rU   rX   r_    rP   rN   rA   rA   I   s          
 	
BLLL
5 
5 
5 
59 9 9 9; ; ; ;a a a a a arP   rA   calleer   	arg_typesSequence[Type | None]	arg_kindslist[ArgKind]	arg_namesSequence[str | None] | Noneformal_to_actuallist[list[int]]contextr<   rF   list[Constraint]c                
   g }t          |          }|                                 g }g }	g }
d}t          |          D ]+\  }}|D ]#}|| j        |         t          t
          fv rd} n$,t          |          D ]p\  }}t          | j        |         t                    r#| j        |         }t          |t                    sJ g }t          |j
                  }t          |t                    r|j        }n't          |t                    r|j        }n
J d            |D ]}||         }||                    |||         | j        |         | j        |         d          }||         t          k    s"t          t          |          t                    r|                    |           |                    t          |                    |g                               t          |t                    r>|                    t'          |t(          t          ||j                                       t          |t                    rl|j        d         }t          |t                    sJ t          |j
                  }t-          |j                  dz
  }t          |t                    rF|                    t'          |t(          t          |d|          |j                                       nit          |t.                    r|j
        j        d	k    sJ |d|          D ]6}|                    t5          |j        d         |t(                               7|rUt9          |j        dd         || d                   D ].\  }}|                    t5          ||t(                               /@J d            |D ]"}||         }|r| j        |         t          t
          fv r|s|                    |                    |||         d||                              ||         }|
                    |t          t
          fvrt:          n|           |	                    |r||         nd           |                    |||         | j        |         | j        |                   }t5          | j        |         |t(                    }|                    |           $rrWt=          fd
|D                       s<|s:|                    t'          t(          t?          ||
|	d                               t=          d | j         D                       rtC          |          }|S )zlInfer type variable constraints for a callable and actual arguments.

    Return a list of constraints.
    FNTz-mypy bug: unhandled constraint inference case)allow_unpack)argsr   r>   builtins.tuplec              3  8   K   | ]}|j         j        k    V  d S rI   )rB   rJ   ).0c
param_specs     rN   	<genexpr>z1infer_constraints_for_callable.<locals>.<genexpr>   s,      EEAAJ*-/EEEEEErP   rg   ri   rk   imprecise_arg_kindsc              3  @   K   | ]}t          |t                    V  d S rI   )r^   r    )rv   vs     rN   ry   z1infer_constraints_for_callable.<locals>.<genexpr>  s,      
B
BA:a''
B
B
B
B
B
BrP   )"r
   rx   	enumerateri   r   r   r^   rg   r2   r5   typer,   tuple_fallbackr#   partial_fallbackexpand_actual_typerk   appendcopy_modifiedrA   r?   itemslenr   fullnameextendinfer_constraintsrs   zipr   anyr   	variablesfilter_imprecise_kinds)rf   rg   ri   rk   rm   ro   constraintsmapperparam_spec_arg_typesparam_spec_arg_namesparam_spec_arg_kindsincomplete_star_mappingiactualsactualunpack_typeactual_typesunpacked_typetuple_instanceactual_arg_typeexpanded_actualinner_unpackinner_unpacked_type
suffix_lenatttactual_kindactual_typerw   rx   s                                @rN   infer_constraints_for_callabler   n   sY    %'KW%%F""$$J# 011  
7 	 	F~&"21"5(I9N"N"N +/' 011 r* r*
7f&q):66 q	* *1-Kk:66666
 L+K,<==M-)9:: N!.!=M955 N!.!?MMMMM!  "+F"3"*"(";";#f%$Q'$Q'!% #< # # V$00J#O44i5 50 !''8888 !''">#?#?oEV#?#W#WXX    -)9:: *N""%$!,0LMM     M955 "N  -215!,
;;;;;&5l6G&H&H# !4559
13CDD &&"/(% ,\zk\ :<O<^      ##6AA/4=AQQQQQ*<ZK<8  #**-.A.Fq.I2|\\     T"%m&9!""&=|ZKLL?Y"Z"Z T TB#**+<R\+R+RSSSSMMMMM! * *"+F"3"* *&"21"5(I9N"N"N
 3 
^,33"55 /61BD)TZJ[   
 '0&7,33'28Y:O'O'OGGU`   -334\If4E4EX\]]]"(";";'!&)(+(+	# #K *&*:1*={LYYA&&q))));*> 	
EEEEEEEEE
 (
 	222(,	  	 		
 	
 	
 
B
B1A
B
B
BBB :,[99rP   Ftemplater$   r   	directionrE   skip_neg_opr[   c                    t           fdt          t          j                  D                       rg S t	                     s"t          t                     t                    rdt                     sg S t          j        	                     f           t           ||          }t          j                                         |S t           ||          S )a  Infer type constraints.

    Match a template type, which may contain type variable references,
    recursively against a type which does not contain (the same) type
    variable references. The result is a list of type constrains of
    form 'T is a supertype/subtype of x', where T is a type variable
    present in the template and x is a type without reference to type
    variables present in the template.

    Assume T and S are type variables. Now the following results can be
    calculated (read as '(template, actual) --> result'):

      (T, X)            -->  T :> X
      (X[T], X[Y])      -->  T <: Y and T :> Y
      ((T, T), (X, Y))  -->  T :> X and T :> Y
      ((T, S), (X, Y))  -->  T :> X and S :> Y
      (X[T], Any)       -->  T <: Any and T :> Any

    The constraints are represented as Constraint objects. If skip_neg_op == True,
    then skip adding reverse (polymorphic) constraints (since this is already a call
    to infer such constraints).
    c              3     K   | ]G\  }}t                    t          |          k    ot                    t          |          k    V  Hd S rI   )r5   )rv   tar   r   s      rN   ry   z$infer_constraints.<locals>.<genexpr>0  so         Q 	!!_Q%7%77 	:F##q'9'99     rP   )r   reversedr;   	inferringr6   r^   r5   r   r7   r   _infer_constraintspop)r   r   r   r   ress   ``   rN   r   r     s    2       z344     
 	8$$ 	
?83L3Lh(W(W 	 X&& 	I##Xv$6777 69kJJ  """
h	;GGGrP   c                     }t                      t                    t           t                    r&t          j                             j        d           t          t                    r&t          j                            j        d          t          t                    rj        t          j
        k    rg S d}t                     r/t                    r d}t                      t                    t           t                    rt                     gS t          t                    rkj                                        sRt"          k    rGt           t                    rt%          d  j        D                       st          j                  t(          k    rGt           t                    r2g } j        D ]&}|                    t-          |                     '|S t"          k    r]t          t                    rHg }j        D ]<}|rt/          j        |          }|                    t-          ||                     =|S t(          k    rGt          t                    r2t3          j                  }	t5           fd|	D             d          S t"          k    rmt           t                    rXt5          fd j        D             d          }
|
r|
S t7                     r t7                    st9                     S g S                      t=          |                    S )NT)keep_erasedFc              3  @   K   | ]}t          |t                    V  d S rI   r^   r-   rv   r   s     rN   ry   z%_infer_constraints.<locals>.<genexpr>t  s=       :
 :
+,Jq+&&:
 :
 :
 :
 :
 :
rP   c                2    g | ]}t          |          S re   infer_constraints_if_possible)rv   a_itemr   r   s     rN   
<listcomp>z&_infer_constraints.<locals>.<listcomp>  s&    \\\F*8VYGG\\\rP   eagerc                2    g | ]}t          |          S re   r   )rv   t_itemr   r   s     rN   r   z&_infer_constraints.<locals>.<listcomp>  s5        .ffiHH  rP   )r5   r^   r1   mypytypeopsmake_simplified_unionr   r   type_of_anyr'   suggestion_engine_is_type_type_unwrap_type_typer-   rA   rJ   is_meta_varr?   r   upper_boundr=   r   r   r)   make_normalizedsimplify_away_incomplete_typesany_constraintsr6   handle_recursive_unionacceptConstraintBuilderVisitor)r   r   r   r   orig_templatetype_type_unwrappedr   r   r   r   results   ```        rN   r   r   C  s    Mx((HV$$F
 (I&& X<55hnRV5WW&)$$ T33FLd3SS &'"" v'9Y=X'X'X	  X +=#8#8 +"$X.."6** (K(( 98Y7788 	6;''
9	%%''
9 %% (I.. 	9c :
 :
08:
 :
 :
 7
 7
 	9 %V%788F
 J:h	#B#Bn 	E 	EFJJ(CCDDDD
L  Z	%B%B l 	L 	LF # :!1&99JJ(	JJKKKK
 J:fi#@#@.v|<< \\\\\V[\\\
 
 
 	
 L  Z)%D%D  !    &n   
 
 
  	GM ** 	G3Fv3N3N 	G)(FIFFF	 ??3FI{SSTTTrP   tpr"   TypeGuard[TypeType | UnionType]c                    t          | t                    p2t          | t                    ot          d | j        D                       S )zIs ``tp`` a ``type[...]`` or a union thereof?

    ``Type[A | B]`` is internally represented as ``type[A] | type[B]``, and this
    troubles the solver sometimes.
    c              3  Z   K   | ]&}t          t          |          t                    V  'd S rI   )r^   r5   r)   rv   os     rN   ry   z _is_type_type.<locals>.<genexpr>  s4      KKQ
?1--x88KKKKKKrP   )r^   r)   r1   allr   r   s    rN   r   r     sK     	2x   	Lb)$$ LKK"(KKKKKrP   TypeType | UnionTypec                    t          | t                    r| j        S t          j        d | j        D                       S )zHExtract the inner type from ``type[...]`` expression or a union thereof.c                \    g | ])}t          t          t          |                    j        *S re   )r   r)   r5   itemr   s     rN   r   z%_unwrap_type_type.<locals>.<listcomp>  s-     [ [ [Qh0B0B!C!C!H [ [ [rP   )r^   r)   r   r1   
make_unionr   r   s    rN   r   r     s?    "h w [ [RTRZ [ [ [\\\rP   list[Constraint] | Nonec                   |t           k    r/t          j                            t	          |           |          sdS |t
          k    r/t          j                            |t	          |                     sdS |t
          k    rIt          | t                    r4t          j                            |t	          | j                            sdS t          | ||          S )a2  Like infer_constraints, but return None if the input relation is
    known to be unsatisfiable, for example if template=List[T] and actual=int.
    (In this case infer_constraints would return [], just like it would for
    an automatically satisfied relation like template=List[T] and actual=object.)
    N)
r=   r   subtypes
is_subtyper   r?   r^   r-   r   r   )r   r   r   s      rN   r   r     s     Jt}'?'?x@X@XZ`'a'atL  )A)Ax((* *  t\!!x-- 	"((@T1U1UVV 	" tXvy999rP   options!Sequence[list[Constraint] | None]list[list[Constraint]]c                v    g }| D ]3}|t          d |D                       r|                    |           4|S )zDSelect only those lists where each item is a constraint against Any.Nc              3  d   K   | ]+}t          t          |j                  t                    V  ,d S rI   )r^   r5   rC   r   rv   rw   s     rN   ry   z!select_trivial.<locals>.<genexpr>  s6      NN!z/!(33W==NNNNNNrP   )r   r   )r   r   options      rN   select_trivialr     sU    
C  >NNvNNNNN 	JJvJrP   
constraintc           	         | j         }t          |          r| S t          t          j                  }t          | j        | j        t          j	        ||g|j
        |j                            S )z?Transform a constraint target into a union with given Any type.)rC   r:   r   r'   implementation_artifactrA   rK   rD   r1   r   linecolumn)r   rC   any_types      rN   merge_with_anyr     sh    F   y899H"fh/fmLL  rP   r1   c                    d | j         D             }d | j         D             }t          t          j        |          ||          p"t          t          j        |          ||          S )Nc                <    g | ]}t          |t                    |S re   r   r   s     rN   r   z*handle_recursive_union.<locals>.<listcomp>  s'    VVV:a;U;UV!VVVrP   c                <    g | ]}t          |t                    |S re   r   r   s     rN   r   z*handle_recursive_union.<locals>.<listcomp>  s'    NNNA:a3M3MNaNNNrP   )r   r   r1   r   )r   r   r   non_type_var_itemstype_var_itemss        rN   r   r     sv     WVX^VVVNNNNNN/00&)  T	9/??	S	STrP   list[list[Constraint] | None]r   c                  |rd | D             nd | D             sg S t                    dk    rd         S t          fddd         D                       rd         S t          fddd         D                       rzt                    }|rit          |          t                    k     rIg }D ]&}||v r|                    d |D                        't	          t          |          |	          S d
 | D             }|| k    rt	          ||	          S d | D             }|| k    rt	          ||	          S g S )aE  Deduce what we can from a collection of constraint lists.

    It's a given that at least one of the lists must be satisfied. A
    None element in the list of options represents an unsatisfiable
    constraint and is ignored.  Ignore empty constraint lists if eager
    is true -- they are always trivially satisfiable.
    c                    g | ]}||S re   re   rv   r   s     rN   r   z#any_constraints.<locals>.<listcomp>  s    @@@F@@@@rP   c                    g | ]}||S rI   re   r   s     rN   r   z#any_constraints.<locals>.<listcomp>  s    LLLF9K9K9K9KrP   r>   r   c              3  D   K   | ]}t          d          |          V  dS r   N)is_same_constraintsrv   rw   valid_optionss     rN   ry   z"any_constraints.<locals>.<genexpr>  s3      
O
O}Q/33
O
O
O
O
O
OrP   Nc              3  D   K   | ]}t          d          |          V  dS r   )is_similar_constraintsr   s     rN   ry   z"any_constraints.<locals>.<genexpr>  s3      
R
R1!-"2A66
R
R
R
R
R
RrP   c                ,    g | ]}t          |          S re   )r   r   s     rN   r   z#any_constraints.<locals>.<listcomp>%  s     &I&I&IQ~a'8'8&I&I&IrP   r   c                ,    g | ]}t          |          S re   )filter_satisfiabler   s     rN   r   z#any_constraints.<locals>.<listcomp>*  s!    ???!*1--???rP   c                ,    g | ]}t          |          S re   )exclude_non_meta_varsr   s     rN   r   z#any_constraints.<locals>.<listcomp>2  s!    BBBQ-a00BBBrP   )r   r   r   r   r   list)r   r   trivial_optionsmerged_optionsr   filtered_optionsr   s         @rN   r   r     s     M@@g@@@LLgLLL 	
=QQ

O
O
O
O]122=N
O
O
OOO  Q

R
R
R
Rabb@Q
R
R
RRR F
 )77 	Fs?33c-6H6HHHN' K K_,,%%&I&I&&I&I&IJJJJ"4#7#7uEEEE @?w???7""/u==== CB'BBB7""/u==== IrP   r   c                p   | s| S g }| D ]t          j        t                    rGj        j        r;t	          fdj        j        D                       r|                               ct          j                            j	        j        j
                  r|                               |sdS |S )a  Keep only constraints that can possibly be satisfied.

    Currently, we filter out constraints where target is not a subtype of the upper bound.
    Since those can be never satisfied. We may add more cases in future if it improves type
    inference.
    c              3  b   K   | ])}t           j                            j        |          V  *d S rI   )r   r   r   rC   )rv   valuerw   s     rN   ry   z%filter_satisfiable.<locals>.<genexpr>H  sH        >C((599     rP   N)r^   rK   r-   valuesr   r   r   r   r   rC   r   )r   satisfiablerw   s     @rN   r  r  ;  s      K " "a'55 	"!:K:R 	"    GHGXG_     & ""1%%%]%%ah0A0MNN 	"q!!! trP   c                &    | s| S d | D             pd S )Nc                D    g | ]}|j                                         |S re   )rB   r   r   s     rN   r   z)exclude_non_meta_vars.<locals>.<listcomp>Y  s+    :::!!7!7!9!9:A:::rP   re   )r   s    rN   r  r  S  s*      ;:v:::BdBrP   xyc                    | D ] t          fd|D                       s dS !|D ] t          fd| D                       s dS !dS )Nc              3  8   K   | ]}t          |          V  d S rI   is_same_constraintrv   c2c1s     rN   ry   z&is_same_constraints.<locals>.<genexpr>^  .      ::"%b"--::::::rP   Fc              3  8   K   | ]}t          |          V  d S rI   r  r  s     rN   ry   z&is_same_constraints.<locals>.<genexpr>a  r  rP   T)r   )r  r  r  s     @rN   r   r   \  s      ::::::::: 	55	  ::::::::: 	55	4rP   r  r  c                6   t          t          | j                  t                    o&t          t          |j                  t                    }| j        |j        k    o;| j        |j        k    p|o)t          j                            | j        |j                  S rI   )	r^   r5   rC   r   rB   rD   r   r   is_same_type)r  r  skip_op_checks      rN   r  r  f  s    ry997CC 
	""GI IM 	r{" 	=Ube^,}	=M&&ry")<<rP   c                B    t          | |          ot          ||           S )a  Check that two lists of constraints have similar structure.

    This means that each list has same type variable plus direction pairs (i.e we
    ignore the target). Except for constraints where target is Any type, there
    we ignore direction as well.
    )_is_similar_constraints)r  r  s     rN   r  r  r  s$     #1a((J-DQ-J-JJrP   c                   | D ]}d}|D ]v}t          t          |j                  t                    p&t          t          |j                  t                    }|j        |j        k    r|j        |j        k    s|rd} nw|s dS dS )zCheck that every constraint in the first list has a similar one in the second.

    See docstring above for definition of similarity.
    FT)r^   r5   rC   r   rB   rD   )r  r  r  has_similarr  r  s         rN   r!  r!  |  s    
    	 	B&ry'A'A7KK z	**GP PM {bk))ru~~~" 	55	4rP   typesIterable[Type]
list[Type]c                @    d | D             }|r|S t          |           S )Nc                0    g | ]}t          |          |S re   )is_complete_type)rv   typs     rN   r   z2simplify_away_incomplete_types.<locals>.<listcomp>  s&    >>>(8(=(=>>>>rP   )r  )r$  completes     rN   r   r     s.    >>u>>>H E{{rP   r*  c                D    |                      t                                S )zIs a type complete?

    A complete doesn't have uninhabited type components or (when not in strict
    optional mode) None components.
    )r   CompleteTypeVisitor)r*  s    rN   r)  r)    s     ::)++,,,rP   c                  (     e Zd Zd fdZd	dZ xZS )
r-  rF   rG   c                T    t                                          t                     d S rI   )superrO   r   )rM   	__class__s    rN   rO   zCompleteTypeVisitor.__init__  s!    rP   r   r0   r[   c                    dS r]   re   )rM   r   s     rN   visit_uninhabited_typez*CompleteTypeVisitor.visit_uninhabited_type  s    urP   )rF   rG   )r   r0   rF   r[   )r`   ra   rb   rO   r3  __classcell__)r1  s   @rN   r-  r-    sQ                    rP   r-  c                      e Zd ZU dZded<   dEd
ZdFdZdGdZdHdZdIdZ	dJdZ
dKdZdLdZdMdZdNdZdOd ZdPd"ZdQd$ZdRd&ZdSd(Z	 dTdUd/ZdVd1ZdWd4ZdXd6ZdYd8ZdZd:Zd[d<Zd\d@Zd]dAZd^dCZdDS )_r   z-Visitor class for inferring type constraints.r"   r   r   rE   r   r[   rF   rG   c                0    || _         || _        || _        d S rI   )r   r   r   )rM   r   r   r   s       rN   rO   z!ConstraintBuilderVisitor.__init__  s     " 'rP   r   r/   rp   c                    g S rI   re   rM   r   s     rN   visit_unbound_typez+ConstraintBuilderVisitor.visit_unbound_type      	rP   r   c                    g S rI   re   r8  s     rN   	visit_anyz"ConstraintBuilderVisitor.visit_any  r:  rP   r   c                    g S rI   re   r8  s     rN   visit_none_typez(ConstraintBuilderVisitor.visit_none_type  r:  rP   r0   c                    g S rI   re   r8  s     rN   r3  z/ConstraintBuilderVisitor.visit_uninhabited_type  r:  rP   r   c                    g S rI   re   r8  s     rN   visit_erased_typez*ConstraintBuilderVisitor.visit_erased_type  r:  rP   r   c                    g S rI   re   r8  s     rN   visit_deleted_typez+ConstraintBuilderVisitor.visit_deleted_type  r:  rP   r   c                    g S rI   re   r8  s     rN   visit_literal_typez+ConstraintBuilderVisitor.visit_literal_type  r:  rP   r!   c                    J d            )NFzInternal errorre   r8  s     rN   visit_partial_typez+ConstraintBuilderVisitor.visit_partial_type  s    &&&&&rP   r-   c                    J d            )NFzbUnexpected TypeVarType in ConstraintBuilderVisitor (should have been handled in infer_constraints)re   r8  s     rN   visit_type_varz'ConstraintBuilderVisitor.visit_type_var      	
?	
 	
 	
rP   r    c                    g S rI   re   r8  s     rN   visit_param_specz)ConstraintBuilderVisitor.visit_param_spec  s    	rP   r,   c                    t           rI   )NotImplementedErrorr8  s     rN   visit_type_var_tuplez-ConstraintBuilderVisitor.visit_type_var_tuple  s    !!rP   r2   c                     t          d          )Nz5Mypy bug: unpack should be handled at a higher level.)RuntimeErrorr8  s     rN   visit_unpack_typez*ConstraintBuilderVisitor.visit_unpack_type  s    RSSSrP   r   c                   t          | j        t                    r |                     |j        | j                  S t
          j        r5t          | j        t                    rt          || j        | j	                  S t
          j        r:t          | j        t                    r t          || j        j        | j	                  S g S rI   )r^   r   r   infer_against_anyrg   r;   infer_polymorphicr   $infer_callable_arguments_constraintsr   r    prefixr8  s     rN   visit_parametersz)ConstraintBuilderVisitor.visit_parameters  s    dk7++ 	K))(*<dkJJJ' 	_Jt{J,O,O 	_ 8$+t~^^^' 	Jt{M,R,R 	7$+,dn   	rP   r   c                2!  $ | j         x}}g }t          |t          t          f          rIj        j        r<dj        j        v r-t          fdj        j        D                       sj        j        	                               t          j                            d|d          }|J | j        t          k    r-t          j                            |t!          |                    s=| j        t"          k    rVt          j                            t!          |          |          r)|                    t'          ||| j                             j        j                                         t          |t                    r|j        |                                rj        j        r| j        t          k    rt/          |j                  }t          |t2                    rt          j                            |          }t          |t8                    r-|                    |                     ||d                     |j        }t          |t<                    rj        j        r| j        t          k    r|j        }t          |t8                    r-|                    |                     ||d                     t          |t@                    rt/          |j!                  }t          |t8                    rA|j        j"        r5|                    |                     |j        j"        |                     t          |t                    r|j        |j        }t          |tF                    r|$                                j        }t          |tJ                    r|j        }t          |t8                    
r|$t!                    }t          |t8                    sJ | j        t"          k    rDj        &                    $j        j'                  rtQ          $j                  }	|	j        j)        j*        }
$j        j+        r$j        j,        J $j        j-        J $j        j,        }$j        j-        }$j        j)        j*        |         }t          |t\                    sJ |j        }t_          $j0        ||          \  }}}t_          |	j0        ||          \  }}}|t3          tc          |          |          fz   |z   }|t3          tc          |          |          fz   |z   }n|	j0        }$j0        }te          |
||          D ]\  }}}t          |t@                    r|j3        th          k    r)|                    t'          ||| j                             |j3        tj          k    r6|                    t'          ||tm          | j                                       t          |tn                    rt          |tn                    r|j8        }t          |tr                    r|:                    |j;        ty          |j;                  d          |j=        ty          |j=                  d          |j>        ty          |j>                  d                    }|	                    t          |t"          |                     |	                    t          |t          |                     t          |tn                    r|:                    ts          |j8        j;        ty          |j;                  d          |j8        j=        ty          |j=                  d          |j8        j>        ty          |j>                  d                              }|	                    t          |t"          |                     |	                    t          |t          |                     t          |t\                    r)|                    t'          ||| j                             |S | j        t          k    rm$j        &                    j        j'                  rHtQ          $j                  }	j        j)        j*        }
j        j+        rj        j,        J j        j-        J j        j,        }j        j-        }j        j)        j*        |         }t          |t\                    sJ |j        }t_          j0        ||          \  }}}t_          |	j0        ||          \  }}}|t3          tc          |          |          fz   |z   }|t3          tc          |          |          fz   |z   }n|	j0        }j0        }te          |
||          D ]\  }}} t          |t@                    r|j3        th          k    r)|                    t'          | || j                             |j3        tj          k    r6|                    t'          | |tm          | j                                       t          |tn                    rt          | tn                    r| j8        }t          |tr                    r|:                    |j;        ty          |j;                  d          |j=        ty          |j=                  d          |j>        ty          |j>                  d                    }|	                    t          | t"          |                     |	                    t          | t          |                     t          |tn                    r|:                    ts          |j8        j;        ty          |j;                  d          |j8        j=        ty          |j=                  d          |j8        j>        ty          |j>                  d                              }|	                    t          | t"          |                     |	                    t          | t          |                     t          |t\                    rR|                    t'          | |t"                               |                    t'          | |t                               |S j        j        r| j        t          k    rt          fdt          j        j                  D                       st          j        A                    $|dg          rjj        j        	                               |                    |                     $|                     j        j                                         |S $j        j        r| j        t"          k    rt          $fd	t          $j        j                  D                       st          j        A                    |$dg          rj$j        j        	                    $           |                    |                     $$                     $j        j                                         |S |r|S t          |t                    r| C                    j0        |          S t          |t2                    rt          t                    r| j        t          k    r|jF        D ]}!t          |!t                    r^t/          |!j                  }"t          |"t\                    rAt          |"t8                    r|"j        j'        d
k    sJ |"j0        d         }!t'          j0        d         |!t                    }#|                    |#           |S t          |t2                    rC| j        t          k    r3t'          t          j                            |          | j                  S t          |t@                    r=|jH        s4|jI        J                                st'          |j!        | j                  S g S t          |tn                    rt'          |j!        | j                  S t          |t\                    rt          g S )N__call__c              3  $   K   | ]
}|k    V  d S rI   re   rv   r   r   s     rN   ry   z:ConstraintBuilderVisitor.visit_instance.<locals>.<genexpr>   s'      JJQ8q=JJJJJJrP   Tis_operator	class_objrW  c              3  $   K   | ]
}|k    V  d S rI   re   r\  s     rN   ry   z:ConstraintBuilderVisitor.visit_instance.<locals>.<genexpr>  '      QQ!AQQQQQQrP   )skipc              3  $   K   | ]
}|k    V  d S rI   re   )rv   r   instances     rN   ry   z:ConstraintBuilderVisitor.visit_instance.<locals>.<genexpr>  rc  rP   rt   r   )Lr   r^   r   r   r   is_protocolprotocol_membersr   r   r   r   r   find_memberr   r?   r   r   r=   r   r   r   fallbackis_type_objr5   ret_typer#   r   r   r   'infer_constraints_from_protocol_membersr)   r   r-   r   metaclass_typer&   as_anonymousr   has_baser   r   defn	type_varshas_type_var_tuple_typetype_var_tuple_prefixtype_var_tuple_suffixr,   r9   rs   r  r   variancer   r   neg_opr    rW  r   r   rg   r   ri   rk   rA   r   is_protocol_implementationr   rT  r8   r   r   r2   r  rJ   r   rN  )%rM   r   original_actualr   r   callrl  r   erasedmappedtvars
prefix_lenr   tvtrj  i_prefixi_middlei_suffixm_prefixm_middlem_suffixinstance_argsmapped_argstvar
mapped_arginstance_argrW  suffixt_prefixt_middlet_suffixtemplate_argstemplate_argr   unpackedcbrf  s%    `                                  @rN   visit_instancez'ConstraintBuilderVisitor.visit_instance  s   #';.& "f|Z899 	2hm>W 	2X];;;JJJJ(-2IJJJJJ 2M+228<<<=44"Hf$ 5  D  +++,66 M44V^D=Q=QRR 7>Z77 M44^D5I5I6RR 8 

#4T64>#R#RSSSM+//111fl++ 	%0K""$$M- Nl22*6?;;h	22 E#|::8DDHh11 JJDD$h(d E    
 _Ffh'' 	HM,E 	~--fh// JJDD"Hfh$ E     fk22 A,V-?@@Ffh// FK4N JJDD"K6&(    fj)) 	%fo.I_Ffm,, 	4((**3Ffk** 	%_Ffh'' p	H#H--Ffh///// ~++0F0Fx}G]0^0^+28X]KK(2=8 2 $=>JJJ#=>JJJ!)!DJ!)!DJ"-,6zBC%c+;<<<<<"1H3O z:4 40Hh 4PZ4 40Hh %-	$x..(0S0S/U$UX`$`M"*iX.Q.Q-S"SV^"^KK"(+K$,MM 7:%m6\6\ #` #`2D*l!$44 "`  =M99JJ'8\SWSa'b'bccc=I55JJ 1*lFSWSaLbLb c c   $D-88 `Z
Ta=b=b `!+!2%lJ?? U ,8+E+E , 6s6;K7L7L7N7N O , 6s6;K7L7L7N7N O , 6s6;K7L7L7N7N O, ,F
  JJz*j&'Q'QRRRJJz*lF'S'STTTT'mDD 	U%1%?%?'1$0$7$A#fFVBWBWBYBY$Z$0$7$A#fFVBWBWBYBY$Z$0$7$A#fFVBWBWBYBY$Z(" (" &@ & &F  JJz*j&'Q'QRRRJJz*lF'S'STTT#D*:;; `

#4Zt~#^#^___
<//HM4J4J8=Ka4b4b/28X]KK *4=8 2 $=>JJJ#=>JJJ!)!DJ!)!DJ"-,6zBC%c+;<<<<<"1H3O z:4 40Hh 4PZ4 40Hh %-	$x..(0S0S/U$UX`$`M"*iX.Q.Q-S"SV^"^KK"(+K$,MM 7:%m6\6\ &^ &^2D*l!$44 %^  =M99JJ'8zSWSa'b'bccc=I55JJ 1,
FSWSaLbLb c c   $D-88 ^Z$m> > ^ ".!4%j*== W &0%=%= * 4S9I5J5J5L5L M * 4S9I5J5J5L5L M * 4S9I5J5J5L5L M& &F
  JJz,
F'S'STTTJJz,f'U'UVVVV'
MBB 	W%/%=%='1$.$5$?FDT@U@U@W@W$X$.$5$?FDT@U@U@W@W$X$.$5$?FDT@U@U@W@W$X(" (" &> & &F  JJz,
F'S'STTTJJz,f'U'UVVV#D*:;; ^

#4\:z#Z#Z[[[

#4\:|#\#\]]]
)&Nl22 QQQQx8O/P/PQQQQQ 3 M<<XvU_T`<aa 3 '..x888

@@ (OX   
 '++---
)Nj00 QQQQx8O/P/PQQQQQ 1 M<<VXU_T`<aa	 1 '..x888

@@ (Hh   
 '++---
 	Jfg&& 	A))(-@@@vy))	!(,EFF	 ,..  dJ// 	,.ty99H!(,<== ! "8X66$M26FFFFF#=+D&x}Q'7|LL

2J	** 	t~/M/M$Xt|/J/J6/R/RTXTbccc,, 		= W)>)>)@)@ W(63Et~VVVI.. 	$Xv/A4>RRR 011 	%%IrP   Frf  subtyper$   protocolr`  c           
        g }|j         j        D ]8}t          j                            ||||          }t          j                            |||          }	||	|dk    rRg c S |r0t          j                            |t          |	          d          s|dk    r|j                                         r|                    t          |	|| j
                             t          j        j        t          j                            ||          v r6|                    t          |	|t          | j
                                       :|S )a]  Infer constraints for situations where either 'template' or 'instance' is a protocol.

        The 'protocol' is the one of two that is an instance of protocol type, 'subtype'
        is the type used to bind self during inference. Currently, we just infer constrains for
        every protocol member type (both ways for settable members).
        r_  NrZ  T)ignore_pos_arg_names)r   rh  r   r   ri  r   r   is_metaclassr   r   r   IS_SETTABLEget_member_flagsrw  )
rM   rf  r   r  r  r`  r   memberinsttemps
             rN   rm  z@ConstraintBuilderVisitor.infer_constraints_from_protocol_members  sJ    m4 	R 	RF=,,VXwR[,\\D=,,VXwGGD|t|Z''			   }//...T 0    ##(B(B(D(D#JJ(tT^DDEEE}(DM,J,J6S[,\,\\\

,T49O9OPPQQQ
rP   r   c           
        |                                                                 }d}t          | j        t                    rcg }| j                                                                         }|                                }|j        |j        }}|j        |j        |j        }|j        }|j        |j        |j        }|j        }|	                    t          ||| j                             |[t          j        rf|j        r_| j        sXt!          d |j        D                       s:|	                    t          ||t#          | j                  d                     d}|j        st'          |j                  }||                                s|j        |         }	t          |	t*                    sJ t-          |	          }
t/          ||
          }t/          ||
          }t1          ||t#          | j                            }|	                    |           nb|	                    t3          ||| j                             n7|j        }t7          |j                  }|                                }t          j        rH|j        rA| j        s:|	                    t          ||t#          | j                  d                     d}|                    |j        d |         |j        d |         |j        d |                   }|	                    t3          ||| j                             d }|st7          d |j        D                       }t?          ||          }tA          |j        |d          |j        |d          |j        |d          t          j        s|j        ng |j!                  }nt7          |j        j                  t7          |j        j                  k    rg|                    tA          |j        j        |d          |j        j        |d          |j        j        |d          |j        j!                  	          }|)|"                    tG          || j        |                     |r|D ]}|xj$        |j        z  c_$        |S t          | j        tJ                    r|                                }tK          tL          j'        | j        
          }|!| (                    |j        | j                  }n6tG          |tR          tA          ||gtT          tV          gd d g                    g}|	                    t          |j        || j                             |S t          | j        tX                    r| -                    | j        |          S t          | j        t\                    r%t          |j        | j        j/        | j                  S t          | j        t`                    rGtb          j2        3                    d| j        | j        d          }|rt          ||| j                  S g S g S )NFc              3  H   K   | ]}|j                                         V  d S rI   )rJ   is_self)rv   tvs     rN   ry   z?ConstraintBuilderVisitor.visit_callable_type.<locals>.<genexpr>Y  s,      LLBLLLLLLrP   T)r   )rg   ri   rk   c                2    g | ]}|t           t          fv |S re   )r   r   )rv   ks     rN   r   z@ConstraintBuilderVisitor.visit_callable_type.<locals>.<listcomp>  s)    )b)b)b!PWY`OaJaJa!JaJaJarP   )rg   ri   rk   r   r{   rz   ra  )
source_anyrZ  r]  )4with_unpacked_kwargswith_normalized_var_argsr^   r   r   rx   rl  
type_guardtype_isr   r   r   r;   rU  r   r   r   rw  is_ellipsis_argsr3   rg   r2   get_tuple_fallback_from_unpackrepack_callable_args#build_constraints_for_simple_unpackrV  rW  r   r   ri   rk   minr   r{   r   rA   rL   r   r'   from_another_anyrT  r=   r   r   r   infer_against_overloadedr)   r   r   r   r   ri  )rM   r   rL   r   cactualrx   template_ret_typecactual_ret_typeunpack_presentunpack
tuple_typetemplate_typesr   unpack_constraintsrW  r~  
cactual_pscactual_prefixparam_spec_targetmax_prefix_lenrw   r   rz  s                          rN   visit_callable_typez,ConstraintBuilderVisitor.visit_callable_type6  s}    0022KKMMdk<00 T	$&Ck6688QQSSG!,,..J2:2CWEU/".73E3Q$,$7!#*#5 +0K$,$4!#*? JJ():<Ldn]]^^^! 0')' !,'  LL':KLLLLL' JJ)#Xvdn/E/ESW    
 #'K  0 %89K%L%LN
 &1':L:L:N:N1 "*!3N!C)&*=====%CF%K%K
)=h
)S)S';GZ'P'P-P*L&:P:P. .* 

#56666 

@7TXTbcc   $* !122
$//11
/ 'G4E 'dN^ 'JJ)#Xvdn/E/ESW    
 #'K ")!6!6%/<%/<%/< "7 " "
 

8QUQ_``   26!! %()b)bW5F)b)b)b%c%cN!$Z!@!@J(2")"3JKK"@")"3JKK"@")"3JKK"@;E;W"_'"3"3]_,3,G) ) )%% :,6773z?P?Z;[;[[[,6,D,D#-*4*;*Ejkk*R*4*;*Ejkk*R*4*;*Ejkk*R4>4E4Y	$ $ $ -E - -) %0JJz*dnFWXXYYY 7 7 7AMMW%66MMMJW-- 	!,,..Jy9dkRRRH!,,X-?MM """Hh#7(I9NQUW[P\]]  JJ():HdnUUVVVJZ00 	00hGGGX.. 	$X%68H$.YYYX.. 	 =,,DK$ -  D  (4HHH	IrP   
overloadedr   c                N    t          ||          }t          ||| j                  S rI   )find_matching_overload_itemr   r   )rM   r  r   r   s       rN   r  z1ConstraintBuilderVisitor.infer_against_overloaded  s'     +:x@@ 4@@@rP   r#   c                   | j         }t          |j                  }t          |t                    o|j        j        dk    }t          |t                    s|rg }|f|r|j        |         }t          |t                    sJ t          |j                  }t          |t                    rt          || j        |          g}n=t          |t                    r|j        j        dk    sJ t          ||| j                  }t          |t                    sJ t          |j                  D ]@\  }}	||k    r|                    t          |	|j        d         | j                             A|S t          |t                    sJ t#          |j        |j        | j                  }
d}d}|                    |
           nt          |t                    rt          |j                  }|Q|j        |         }t          |t                    sJ t          |j                  }t%          |j                  dz   t%          |j                  k    r|}t%          |j                  |z
  dz
  }t'          t)          |j                  ||          \  }}}t)          |j        d |                   }|r |t)          |j        | d                    z  }||z   }t          |t                    rK|j        j        dk    sJ |D ]6}|                    t          ||j        d         | j                             7n0d}d}n+t)          |j                  }t)          |j                  }n|S t          |t                    sJ t%          |          t%          |          k    r|j        j        j        r4|j        j        j        r#|t          |j        |j        | j                  z   S t/          t%          |                    D ]7}|                    t          ||         ||         | j                             8|S t          |t0                    r|                     |j        |          S g S )Nrt   )rB   rD   rC   r   re   r>   )r   r3   r   r^   r   r   r   r#   r2   r5   r,   rA   r   r   r~   r   rs   r  r   r9   tupler   is_named_tupleranger   rT  )rM   r   r   unpack_indexis_varlength_tupler   r   r   r   tir  actual_itemstemplate_itemsa_unpack_indexa_unpack
a_unpackeda_prefix_lena_suffix_lenr  r  r  tms                         rN   visit_tuple_typez)ConstraintBuilderVisitor.visit_tuple_type  s   *8>::vx((UV[-AEU-U 	 fi(( X	,> X	$&C'% 3 #+.">K%k:>>>>>$3K4D$E$EM!-1ABB 	W&$.Y_```
 '}h?? - 2 ;?O O O O O/vt~VV%fh77777!*8>!:!: Z Z2,,$

#4RQ#X#XYYYYJ%fi88888)L dn* *& 68L79NJJ12222FI..  !4V\!B!B!-  &|N;H%h
;;;;;!0!?!?J6<((1,HN0C0CCC'5'*6<'8'8>'IA'M7S!(.11<8 84(H (-V\-<--H'I'I' P(E&,}~~2N,O,OOL)1H)<%j(;; "#-?#;?O#O#O#O#O&. " " #

$5b*/!:Ldn$]$]!" !" !" !" (*)+#(#6#6L%*8>%:%:NN
 fi00000<  C$7$777+0? 16E
 !2 163JDN" "   s>2233  AJJ).*;\!_dn]]    J(( 	))(.&AAAIrP   r&   c                T   | j         }t          |t                    rIg }|                    |          D ]/\  }}}|                    t          ||| j                             0|S t          |t                    r-|                     |j	        
                                |          S g S rI   )r   r^   r&   r   r   r   r   r   rT  r   r  )rM   r   r   r   	item_nametemplate_item_typeactual_item_types          rN   visit_typeddict_typez-ConstraintBuilderVisitor.visit_typeddict_type@  s    fm,, 
	$&C DL<<PVCWCW d d?	-/?

,-?AQSWSabbccccJ(( 	))(.*?*?*A*A6JJJIrP   r1   c                    J d            )NFz`Unexpected UnionType in ConstraintBuilderVisitor (should have been handled in infer_constraints)re   r8  s     rN   visit_union_typez)ConstraintBuilderVisitor.visit_union_typeN  rJ  rP   r%   c                    J d|             )NFz!This should be never called, got re   r8  s     rN   visit_type_alias_typez.ConstraintBuilderVisitor.visit_type_alias_typeT  s    DD(DDDDDrP   r$  r%  r   c                   g }t          |          D ]}t          |t                    rt          |j        t                    r/|                    t          |j        | j        |                     `t          |j                  }t          |t                    sJ |
                    t          ||| j                             |
                    t          ||| j                             |S rI   )r4   r^   r2   r   r,   r   rA   r   r5   r   r   r   )rM   r$  r   r   r   r  s         rN   rT  z*ConstraintBuilderVisitor.infer_against_anyW  s     " 'u-- 	K 	KA!Z(( Kaf&677 VJJz!&$.(KKLLLL.qv66H%h99999JJ08T^TTUUUU
 

,Q$.IIJJJJ
rP   c                    t          | j        t                    rt          || j                  }n|j        }g }|D ]0}|                    t          || j        | j                             1|S rI   )r^   r   r   find_matching_overload_itemsr   r   r   r   )rM   r   r   r   r   s        rN   visit_overloadedz)ConstraintBuilderVisitor.visit_overloadedj  sq    dk<00 	#04;GGEENE " 	J 	JAJJ(DKHHIIII
rP   r)   c                
   t          | j        t                    r%t          |j        | j        j        | j                  S t          | j        t                    r0t          |j        | j        j        d         j        | j                  S t          | j        t                    r%t          |j        | j        j        | j                  S t          | j        t                    r t          |j        | j        | j                  S g S )Nr   )r^   r   r   r   r   rl  r   r   r   r)   r   r8  s     rN   visit_type_typez(ConstraintBuilderVisitor.visit_type_typet  s    dk<00 		$X]DK4H$.YYYZ00 	$X]DK4Ea4H4QSWSabbbX.. 	$X]DK4DdnUUUW-- 	$X]DKPPPIrP   N)r   r"   r   rE   r   r[   rF   rG   )r   r/   rF   rp   )r   r   rF   rp   )r   r   rF   rp   )r   r0   rF   rp   )r   r   rF   rp   )r   r   rF   rp   )r   r   rF   rp   )r   r!   rF   rp   )r   r-   rF   rp   )r   r    rF   rp   )r   r,   rF   rp   )r   r2   rF   rp   )r   r   rF   rp   )r   r   rF   rp   F)rf  r   r   r   r  r$   r  r   r`  r[   rF   rp   )r   r   rF   rp   )r  r   r   r   rF   rp   )r   r#   rF   rp   )r   r&   rF   rp   )r   r1   rF   rp   )r   r%   rF   rp   )r$  r%  r   r   rF   rp   )r   r   rF   rp   )r   r)   rF   rp   )r`   ra   rb   rc   rd   rO   r9  r<  r>  r3  rA  rC  rE  rG  rI  rL  rO  rR  rX  r  rm  r  r  r  r  r  r  rT  r  r  re   rP   rN   r   r     sD        77 ' ' ' '                     
' ' ' '
 
 
 
   " " " "T T T T   $Q Q Q Qr  ' ' ' ' 'R\ \ \ \|	A 	A 	A 	A_ _ _ _B   
 
 
 
E E E E   &   
 
 
 
 
 
rP   r   rD   c                n    | t           k    rt          S | t          k    rt           S t          d|            )z,Map SubtypeOf to SupertypeOf and vice versa.zInvalid operator )r=   r?   
ValueError)rD   s    rN   rw  rw    s=     
Z	|		1R11222rP   r  r   c                    | j         }|D ]9}t          j                            ||t          j        j        dd          r|c S :|d         S )z.Disambiguate overload item against a template.FT	is_compatis_proper_subtypeignore_returnr   )r   r   r   is_callable_compatibler   )r  r   r   r   s       rN   r  r    si    E 
 
 =//m.# 0 
 
 	 KKK	 8OrP   list[CallableType]c                    | j         }g }|D ]J}t          j                            ||t          j        j        dd          r|                    |           K|s|                                }|S )zMLike find_matching_overload_item, but return all matches, not just the first.FTr  )r   r   r   r  r   r   copy)r  r   r   r   r   s        rN   r  r    s     E
C 
 
 =//m.# 0 
 
 	 JJt  jjllJrP   r  r2   r   c                H   t          | j                  }t          |t                    r|j        j        dk    r|j        S t          |t
                    r|j        j        S t          |t                    r#|j        j        j	        D ]}|j        dk    r|c S J d            )zMGet builtins.tuple type from available types to construct homogeneous tuples.rt   FzInvalid unpack type)
r5   r   r^   r   r   r,   r   r#   r   mro)r  r   bases      rN   r  r    s    		%	%B"h BG$48H$H$Hw"&'' & %%"i   ',0 	 	D} 000 1'''''rP   callabler  c                   t           | j        vr| j        S | j                            t                     }| j        d|         }| j        |         }g }t	          |t
                    st          t          ||g                    }ngt          |j                  }t	          |t                    r>t	          |j
        d         t
                    sJ |j
        d         }|j
        dd         }||gz   |z   S )ag  Present callable with star unpack in a normalized form.

    Since positional arguments cannot follow star argument, they are packed in a suffix,
    while prefix is represented as individual positional args. We want to put all in a single
    list with unpack in the middle, and prefix/suffix on the sides (as they would appear
    in e.g. a TupleType).
    Nr   r>   )r   ri   rg   indexr^   r2   r   r5   r   r#   r   )r  r  
star_indexrg   	star_typesuffix_typesr   s          rN   r  r    s     x)))!!#))(33J";J;/I":.ILi,, (x
YK@@AA		Y^,,b)$$ 	(bhqk:66666I8ABB<L	{"\11rP   r  actual_argsc                &   t          |           }|J |}t          |           |z
  dz
  }d}g }t          |          }|| |         }||z   t          |          k    rbt          |t                    sJ t          |j        t
                    r/t          |j        |t          g |j        j                            gS g S |}	|}
nK|}t          |          |z
  dz
  }t          ||          }	t          ||          }
||k    r||k    r| |         }t          t          |          |	|
          \  }}}t          | d|	         |          D ])\  }}|                    t          |||                     *|
rCt          | |
 d         |          D ])\  }}|                    t          |||                     *|it          |t                    sJ t          |j                  }t          |t                     r|j        j        dk    r|D ]}t          |t                    s0|                    t          |j        d         ||                     Gt          |j                  }t          |t                     rJ|j        j        dk    r:|                    t          |j        d         |j        d         |                     nFt          |t
                    rD|                    t          ||t          t)          |          |j                                       n|||         }t          |t                    sJ t          |j                  }t          |t                     r|j        j        dk    r| |         }t          |t                    sJ t          |j                  }t          |t                     rJ|j        j        dk    r:|                    t          |j        d         |j        d         |                     |S )a  Infer constraints between two lists of types with variadic items.

    This function is only supposed to be called when a variadic item is present in templates.
    If there is no variadic item the actuals, we simply use split_with_prefix_and_suffix()
    and infer prefix <: prefix, suffix <: suffix, variadic <: middle. If there is a variadic
    item in the actuals we need to be more careful, only common prefix/suffix can generate
    constraints, also we can only infer constraints for variadic template item, if template
    prefix/suffix are shorter that actual ones, otherwise there may be partial overlap
    between variadic items, for example if template prefix is longer:

        templates: T1, T2, Ts, Ts, Ts, ...
        actuals:   A1, As, As, As, ...

    Note: this function can only be called for builtin variadic constructors: Tuple and Callable.
    For instances, you should first find correct type argument mapping.
    Nr>   rt   r   )r3   r   r^   r2   r   r,   rA   r#   r   r  r9   r  r   r   r   r5   r   r   rs   r   r  )r  r  r   template_unpacktemplate_prefixtemplate_suffixt_unpackr   actual_unpackcommon_prefixcommon_suffixactual_prefixactual_suffixstartmiddleendr   r   r   a_tpactual_unpack_typer  s                         rN   r  r    s,   & *-88O&&&%O-((?:Q>OH
C'44M 1_,s;/?/???h
33333(-)9::   y)B@\2]2]   	''%K((=81<O];;O];;O++0P0P %_5H 6kM= E63 M.=.1599 7 71

$Q9556666 ;}noo6<< 	; 	;DAqJJ(Ay99::::(J/////X]++b(## 	^(8<L(L(L [ [!!Z00 [JJ0Q	JJKKKK*1622D!$11 [di6HL\6\6\

#4RWQZ1y#Y#YZZZ[ ,-- 	^JJz"i4<<IZ1[1[\\]]]		" )7,j99999$%7%<==
j(++ 	Y
0HL\0\0\$_5Hh
33333 //B"h'' YBG,<@P,P,P

,RWQZ9KYWWXXXJrP   leftrightc                   t          | t          t          f          st          |t          t          f          rg S |t          k    rt	          | |t          |                    S t	          || t          |                    S )zSInfer constraints between two arguments using direction between original callables.)r^   r    r2   r=   r   rw  )r  r  r   s      rN   infer_directed_arg_constraintsr  ?  sz    $
344 
z*9 9 
 	J ufY.?.?@@@ fY.?.?@@@rP   #NormalizedCallableType | Parametersc                   g }|t           k    r| |}}n|| }}|                                }|                                }|                                }|                                }	|0|.|                    t	          |j        |j        |                     |0|	.|                    t	          |j        |	j        |                     |                                D ]S}
t          j        	                    ||
          }|%|                    t	          |j        |
j        |                     T||
                    d          }|J |j        }|J |t          |j                  k     r|j        |                                         r|                    |          }|J |                    t	          |j        |j        |                     |dz  }|t          |j                  k     r|j        |                                         |	d |j        D             }t#                      }t%          |j        |j                  D ]5\  }}||                                s||v r |                    |           6|                    d          }|J |D ]I}|                    |          }|J |                    t	          |j        |j        |                     J|S )a  Infer constraints between argument types of two callables.

    This function essentially extracts four steps from are_parameters_compatible() in
    subtypes.py that involve subtype checks between argument types. We keep the argument
    matching logic, but ignore various strictness flags present there, and checks that
    do not involve subtyping. Then in place of every subtype check we put an infer_constraints()
    call for the same types.
    Nr>   c                    h | ]}||S rI   re   )rv   names     rN   	<setcomp>z7infer_callable_arguments_constraints.<locals>.<setcomp>  s    LLL4;Kt;K;K;KrP   )r=   var_argkw_argr   r  r*  formal_argumentsr   r   callable_corresponding_argument try_synthesizing_arg_from_varargposr   ri   is_positionalargument_by_positionrk   setr   is_staraddtry_synthesizing_arg_from_kwargargument_by_name)r   r   r   r   r  r  	left_star
left_star2
right_starright_star2	right_argleft_argright_by_positionr   left_by_positionright_namesleft_only_namesr  kindright_by_nameleft_by_names                        rN   rV  rV  N  s    CJeheIJJ,,..K !7

1)-QZ[[\\\+"9

1*.+/S\]]^^^ ++-- [ [	<??iPP

1(,	yYYZZZZ !BB4HH ,,,N}}}#dn%%%%$.*;*I*I*K*K%#88;;#///JJ.$(*;*?   
 FA #dn%%%%$.*;*I*I*K*K% LLLLL%%dndn== 	& 	&JD$|t||~~|1D1D%%%%==dCC(((# 	 	D0066L+++JJ.|/?ART]^^    JrP   csc                
   t                      }| D ]w}t          |j        t                    st          |j        t                    s&t          |j        t
                    r&|j        j        s|                    |j                   xg }| D ]u}t          |j        t                    r	|j        |vr|	                    |           t          |j        t
                    r|j        j        s|	                    |           v|S )zWFor each ParamSpec remove all imprecise constraints, if at least one precise available.)
r  r^   rK   r    rC   r   r{   r  rB   r   )r'  have_preciserw   new_css       rN   r   r     s   55L ) )!+];; 	qx//	)!(J//	) H0	)
 QZ(((F  !+];; 	qzQ]?]?]MM!!(J// 	qx7S 	MM!MrP   )rf   r   rg   rh   ri   rj   rk   rl   rm   rn   ro   r<   rF   rp   r  )
r   r$   r   r$   r   rE   r   r[   rF   rp   )r   r"   rF   r   )r   r   rF   r"   )r   r$   r   r$   r   rE   rF   r   )r   r   rF   r   )r   rA   rF   rA   )r   r1   r   r$   r   rE   rF   rp   )r   r   r   r[   rF   rp   )r   r   rF   r   )r  rp   r  rp   rF   r[   )r  rA   r  rA   rF   r[   )r$  r%  rF   r&  )r*  r$   rF   r[   )rD   rE   rF   rE   )r  r   r   r   rF   r   )r  r   r   r   rF   r  )r  r2   rF   r   )r  r   r  r   rF   r&  )r  r&  r  r&  r   rE   rF   rp   )r  r$   r  r$   r   rE   rF   rp   )r   r  r   r  r   rE   rF   rp   )r'  rp   rF   rp   )mrc   
__future__r   collections.abcr   r   typingr   r   r   typing_extensionsr	   mypy.subtypesr   mypy.typeopsmypy.argmapr
   mypy.erasetyper   mypy.maptyper   
mypy.nodesr   r   r   r   r   r   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/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   mypy.types_utilsr:   mypy.typestater;   
mypy.inferr<   r=   rd   r?   rA   r   r   r   r   r   r   r   r   r   r   r  r  r   r  r  r!  r   r)  r[   r-  r  r   rw  r  r  r  r  r  r  rV  r   re   rP   rN   <module>r9     s   ! ! ! " " " " " " . . . . . . . . - - - - - - - - - - ' ' ' ' ' '         ' ' ' ' ' ' ) ) ) ) ) ) 2 2 2 2 2 2	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &N / . . . . . % % % % % % 0//////
        "a "a "a "a "a "a "a "aJf f f fT GL)H )H )H )H )HXiU iU iU iUX
 
 
 
] ] ] ]: : : :2      
T 
T 
T 
T5 5 5 5p   0C C C C   	 	 	 	K K K K   (   - - - -    )D/   U U U U U{4
+;< U U Up3 3 3 3   &   0( ( ( (2 2 2 24] ] ] ]@A A A AF F F FR     rP   