o
    2gw,                    @  s  U d Z ddlmZ ddlmZmZmZmZ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,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z? dd
l@mAZA ddlBmCZC erddlDmEZE dZFdeGd< dZHdeGd< G dd dZId}d!d"ZJ	#d~dd+d,ZKdd-d.ZLdd0d1ZMdd5d6ZNdd8d9ZOdd;d<ZPdd?d@ZQddBdCZRddFdGZSddJdKZTddLdMZUddNdOZVddSdTZWddVdWZXG dXdY dYe/eY ZZG dZd[ d[e5eeI  Z[dd]d^Z\ddadbZ]ddddeZ^ddidjZ_ddmdnZ`ddqdrZaddudvZbddxdyZcdd{d|ZddS )zType inference constraints.    )annotations)TYPE_CHECKINGFinalIterableListSequenceN)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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                   @  sN   e Zd ZU dZded< dZded< dddZdddZdddZd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                 C  s$   |j | _|| _|| _|| _g | _d S N)idr>   r@   r?   origin_type_varextra_tvars)selfr>   r@   r?    rI   H/home/garg/my-data/venv/lib/python3.10/site-packages/mypy/constraints.py__init__P   s
   
zConstraint.__init__strc                 C  s*   d}| j tkr	d}| j d| d| j S )Nz<:z:> )r@   r<   r>   r?   )rH   op_strrI   rI   rJ   __repr__\   s   
zConstraint.__repr__c                 C  s   t | j| j| jfS rD   )hashr>   r@   r?   rH   rI   rI   rJ   __hash__b   s   zConstraint.__hash__otherobjectboolc                 C  s.   t |tsdS | j| j| jf|j|j|jfkS NF)
isinstancer=   r>   r@   r?   )rH   rS   rI   rI   rJ   __eq__e   s   
 zConstraint.__eq__N)r>   r)   r@   rA   r?   r"   rB   rC   )rB   rL   )rB   rA   )rS   rT   rB   rU   )
__name__
__module____qualname____doc____annotations__r@   rK   rO   rR   rX   rI   rI   rI   rJ   r=   F   s   
 


r=   calleer   	arg_typesSequence[Type | None]	arg_kindslist[ArgKind]	arg_namesSequence[str | None] | Noneformal_to_actuallist[list[int]]contextr9   rB   list[Constraint]c                   s  g }t |}|   g }g }	g }
d}t|D ]\}}|D ]}|du r/| j| ttfv r/d} nqqt|D ]x\}}t| j| tr@| j| }t|tsOJ g }t	|j
}t|tr_|j}nt|trh|j}nJ d|D ]<}|| }|du ryqn|j||| | j| | j| dd}|| tkstt	|tr|| qn|t|j|gd qnt|tr|t|tt||j q5t|tr<|jd }t|tsJ t	|j
}t|jd }t|tr|t|tt|d|  |j n&t|tr|j
jd	ksJ |d|  D ]}|t|jd |t q
|r;t|jdd || d D ]\}}|t||t q,q5J d|D ]k}|| }|du rOqB r| j| ttfv r|s||||| d||  || }|
|ttfvr}tn| |	|r|| nd qB|||| | j| | j| }t| j| |t}|| qBq5 rt fd
d|D s|s|t tt||
|	dd tdd | j D rt!|}|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  s    | ]	}|j  jkV  qd S rD   )r>   rE   .0c
param_specrI   rJ   	<genexpr>   s    z1infer_constraints_for_callable.<locals>.<genexpr>r_   ra   rc   imprecise_arg_kindsc                 s      | ]}t |tV  qd S rD   )rW   r   )rm   vrI   rI   rJ   rq         )"r   rp   	enumeratera   r   r   rW   r_   r0   r2   typer*   tuple_fallbackr!   partial_fallbackexpand_actual_typerc   appendcopy_modifiedr=   r<   itemslenr   fullnameextendinfer_constraintsrj   zipr   anyr   	variablesfilter_imprecise_kinds)r^   r_   ra   rc   re   rg   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_typern   rI   ro   rJ   infer_constraints_for_callablek   s   








&
r   Ftemplater"   r   	directionrA   skip_neg_oprU   c                   s~   t  fddttjD rg S tstttr8t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  s4    | ]\}}t t |kot  t |kV  qd S rD   )r2   )rm   tar   r   rI   rJ   rq   -  s    
z$infer_constraints.<locals>.<genexpr>)r   reversedr8   	inferringr3   rW   r2   r   r4   r|   _infer_constraintspop)r   r   r   r   resrI   r   rJ   r     s   
r   c           
        s  }t t   ttrtjjjddt tr&tjj jdd t tr3 jt	j
kr3g S ttr?t gS t tra j satkrattr\tdd jD sat  j tkr}ttr}g }jD ]}|t|  qo|S tkrt trg } jD ]}|t|| q|S tkrt trt j}tfdd|D ddS tkrttrt fddjD d	d}	|	r|	S trt st S g S t |S )
NT)keep_erasedc                 s  rt   rD   rW   r+   rm   r   rI   rI   rJ   rq   g  s    

z%_infer_constraints.<locals>.<genexpr>c                   s   g | ]}t | qS rI   infer_constraints_if_possible)rm   a_item)r   r   rI   rJ   
<listcomp>      z&_infer_constraints.<locals>.<listcomp>eagerc                   s   g | ]}t | qS rI   r   )rm   t_item)r   r   rI   rJ   r     s    
F)r2   rW   r/   mypytypeopsmake_simplified_unionr~   r   type_of_anyr%   suggestion_enginer+   r=   rE   is_meta_varr<   r   upper_boundr:   r   r   simplify_away_incomplete_typesany_constraintsr3   handle_recursive_unionacceptConstraintBuilderVisitor)
r   r   r   r   orig_templater   r   r   r~   resultrI   )r   r   r   rJ   r   @  sb   


	



r   list[Constraint] | Nonec                 C  sr   |t krtjt| |sdS |tkrtj|t| sdS |tkr3t| tr3tj|t| js3d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<   rW   r+   r   r   )r   r   r   rI   rI   rJ   r     s   r   options!Sequence[list[Constraint] | None]list[list[Constraint]]c                 C  s8   g }| D ]}|du rqt dd |D r|| q|S )zDSelect only those lists where each item is a constraint against Any.Nc                 s  s     | ]}t t|jtV  qd S rD   )rW   r2   r?   r   rl   rI   rI   rJ   rq     s    z!select_trivial.<locals>.<genexpr>)allr|   )r   r   optionrI   rI   rJ   select_trivial  s   
r   
constraintc                 C  s>   | j }t|r	| S ttj}t| j| jt	||g|j
|jS )z?Transform a constraint target into a union with given Any type.)r?   r7   r   r%   implementation_artifactr=   rF   r@   r/   
make_unionlinecolumn)r   r?   any_typerI   rI   rJ   merge_with_any  s   
r   r/   c                 C  sD   dd | j D }dd | j D }tt|||p!tt|||S )Nc                 S  s   g | ]	}t |ts|qS rI   r   r   rI   rI   rJ   r         z*handle_recursive_union.<locals>.<listcomp>c                 S  s   g | ]	}t |tr|qS rI   r   r   rI   rI   rJ   r     r   )r~   r   r/   r   )r   r   r   non_type_var_itemstype_var_itemsrI   rI   rJ   r     s   r   list[list[Constraint] | None]r   c                   s  |r
dd | D  ndd | D   sg S t  dkr d S t fdd dd D r2 d S t fd	d dd D rut }|rut |t  k rug } D ]}||v rZqS|durfd
d |D }nd}|| qStt||S dd | D }|| krt||d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                 S  s   g | ]}|r|qS rI   rI   rm   r   rI   rI   rJ   r         z#any_constraints.<locals>.<listcomp>c                 S  s   g | ]}|d ur|qS rD   rI   r   rI   rI   rJ   r     r   r;   r   c                 3      | ]
}t  d  |V  qdS r   N)is_same_constraintsrl   valid_optionsrI   rJ   rq         z"any_constraints.<locals>.<genexpr>Nc                 3  r   r   )is_similar_constraintsrl   r   rI   rJ   rq     r   c                 S     g | ]}t |qS rI   )r   rl   rI   rI   rJ   r      r   c                 S  r   rI   )filter_satisfiable)rm   orI   rI   rJ   r     r   r   )r   r   r   r|   r   list)r   r   trivial_optionsmerged_optionsr   merged_optionfiltered_optionsrI   r   rJ   r     s2   r   r   c                   sz   | s| S g }| D ]. t  jtr' jjr't fdd jjD r&|  qtj j	 jj
r6|  q|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  s     | ]}t j j|V  qd S rD   )r   r   r   r?   )rm   valuern   rI   rJ   rq     s    
z%filter_satisfiable.<locals>.<genexpr>N)rW   rF   r+   valuesr   r|   r   r   r   r?   r   )r   satisfiablerI   r   rJ   r     s    

r   xyc                   sP   | D ] t  fdd|D s dS q|D ] t  fdd| D s% dS qdS )Nc                 3      | ]}t  |V  qd S rD   is_same_constraintrm   c2c1rI   rJ   rq   *  rv   z&is_same_constraints.<locals>.<genexpr>Fc                 3  r   rD   r   r   r   rI   rJ   rq   -  rv   T)r   r   r   rI   r   rJ   r   (  s   r   r   r   c                 C  sN   t t| jtot t|jt}| j|jko&| j|jkp|o&tj| j|jS rD   )	rW   r2   r?   r   r>   r@   r   r   is_same_type)r   r   skip_op_checkrI   rI   rJ   r   2  s   
r   c                 C  s   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_constraintsr   rI   rI   rJ   r   >  s   r   c                 C  sj   | D ]0}d}|D ]$}t t|jtpt t|jt}|j|jkr,|j|jks(|r,d} nq|s2 dS q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)rW   r2   r?   r   r>   r@   )r   r   r   has_similarr   r   rI   rI   rJ   r   H  s   
r   typesIterable[Type]
list[Type]c                 C  s   dd | D }|r|S t | S )Nc                 S  s   g | ]}t |r|qS rI   )is_complete_type)rm   typrI   rI   rJ   r   ]  r   z2simplify_away_incomplete_types.<locals>.<listcomp>)r   )r   completerI   rI   rJ   r   \  s   r   r   c                 C  s   |  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   rI   rI   rJ   r   d  s   r   c                      s(   e Zd Zd
 fddZddd	Z  ZS )r   rB   rC   c                   s   t  t d S rD   )superrK   r   rQ   	__class__rI   rJ   rK   n  s   zCompleteTypeVisitor.__init__r   r.   rU   c                 C  s   dS rV   rI   )rH   r   rI   rI   rJ   visit_uninhabited_typeq     z*CompleteTypeVisitor.visit_uninhabited_type)rB   rC   )r   r.   rB   rU   )rY   rZ   r[   rK   r  __classcell__rI   rI   r  rJ   r   m  s    r   c                   @  s  e Zd ZU dZded< d^d
dZd_ddZd`ddZdaddZdbddZ	dcddZ
ddddZded!d"Zdfd$d%Zdgd'd(Zdhd*d+Zdid-d.Zdjd0d1Zdkd3d4Zdld6d7Z	8dmdnd>d?ZdodAdBZdpdEdFZdqdHdIZdrdKdLZdsdNdOZdtdQdRZdudVdWZdvdXdYZdwd[d\Zd]S )xr   z-Visitor class for inferring type constraints.r    r   r   rA   r   rU   rB   rC   c                 C  s   || _ || _|| _d S rD   )r   r   r   )rH   r   r   r   rI   rI   rJ   rK   |  s   
z!ConstraintBuilderVisitor.__init__r   r-   rh   c                 C     g S rD   rI   rH   r   rI   rI   rJ   visit_unbound_type  r  z+ConstraintBuilderVisitor.visit_unbound_typer   c                 C  r  rD   rI   r  rI   rI   rJ   	visit_any  r  z"ConstraintBuilderVisitor.visit_anyr   c                 C  r  rD   rI   r  rI   rI   rJ   visit_none_type  r  z(ConstraintBuilderVisitor.visit_none_typer.   c                 C  r  rD   rI   r  rI   rI   rJ   r    r  z/ConstraintBuilderVisitor.visit_uninhabited_typer   c                 C  r  rD   rI   r  rI   rI   rJ   visit_erased_type  r  z*ConstraintBuilderVisitor.visit_erased_typer   c                 C  r  rD   rI   r  rI   rI   rJ   visit_deleted_type  r  z+ConstraintBuilderVisitor.visit_deleted_typer   c                 C  r  rD   rI   r  rI   rI   rJ   visit_literal_type  r  z+ConstraintBuilderVisitor.visit_literal_typer   c                 C     J d)NFzInternal errorrI   r  rI   rI   rJ   visit_partial_type  s   z+ConstraintBuilderVisitor.visit_partial_typer+   c                 C  r  )NFzbUnexpected TypeVarType in ConstraintBuilderVisitor (should have been handled in infer_constraints)rI   r  rI   rI   rJ   visit_type_var     z'ConstraintBuilderVisitor.visit_type_varr   c                 C  r  rD   rI   r  rI   rI   rJ   visit_param_spec  s   z)ConstraintBuilderVisitor.visit_param_specr*   c                 C  s   t rD   )NotImplementedErrorr  rI   rI   rJ   visit_type_var_tuple  r  z-ConstraintBuilderVisitor.visit_type_var_tupler0   c                 C  s   t d)Nz5Mypy bug: unpack should be handled at a higher level.)RuntimeErrorr  rI   rI   rJ   visit_unpack_type  s   z*ConstraintBuilderVisitor.visit_unpack_typer   c                 C  sf   t | jtr| |j| jS tjrt | jtrt|| j| j	S tjr1t | jt
r1t|| jj| j	S g S rD   )rW   r   r   infer_against_anyr_   r8   infer_polymorphicr   $infer_callable_arguments_constraintsr   r   prefixr  rI   rI   rJ   visit_parameters  s   z)ConstraintBuilderVisitor.visit_parametersr   c           &   	     s	  | j  }}g }t|ttfrWjjrWdjjv rWtfddjjD sWjj	 t
jjd|dd}|d us<J t
j|t|rQt||| j}|| jj  t|tr|jd ur| rjjrt|j}t|tryt
j|}t|tr| jtkr}n|}|| j||dd |j}t|trjjrt|jtr| jtkr}n|j}|| j|j|dd | jtkr|j}	t|	t rt|	j!}	t|	tr|	jj"r|| |	jj"| t|tr|jd ur|j}t|t#r|$ j}t|t%r|j}t|tr@| t}
t|
tsJ | jtkrjj& jj'rjt( j}|jj)j*} jj+r jj,d usDJ  jj-d usMJ  jj,} jj-} jj)j*| }t|t.sdJ |j}t/ j0||\}}}t/|j0||\}}}|tt1||f | }|tt1||f | }n|j0} j0}t2|||D ]\}}}t|t r|j3t4kr|t||| j |j3t5kr|t||t6| j qt|t7rVt|t7rV|j8}t|t9r|:|j;t<|j;d  |j=t<|j=d  |j>t<|j>d  }|	t?|t| |	t?|t| qt|t7rT|j:t9|j8j;t<|j;d  |j8j=t<|j=d  |j8j>t<|j>d  d}|	t?|t| |	t?|t| qt|t.rf|t||| j q|S | jtkr j&jj'rt( j}jj)j*}jj+rjj,d usJ jj-d usJ jj,}jj-}jj)j*| }t|t.sJ |j}t/j0||\}}} t/|j0||\}}}|tt1||f |  }!|tt1||f | }n|j0}j0}!t2|||!D ]\}}}"t|t r|j3t4kr	|t|"|| j |j3t5kr|t|"|t6| j qt|t7rt|"t7r|"j8}t|t9re|:|j;t<|j;d  |j=t<|j=d  |j>t<|j>d  }|	t?|"t| |	t?|"t| qt|t7r|j:t9|j8j;t<|j;d  |j8j=t<|j=d  |j8j>t<|j>d  d}|	t?|"t| |	t?|"t| qt|t.r|t|"|t |t|"|t q|S jjr | jtkr tfddt@jjD s t
jjA |
dgd	r jj	 ||  | jj  |S  jjr@| jtkr@t fd
dt@ jjD s@t
jjA|
 dgd	r@ jj	  ||     jj  |S |rE|S t|tBrR| Cj0|S t|trtDtEr| jtkr|jFD ]8}#t|#tGrt|#j}$t|$t.r|qgt|$tr|$jj'dksJ |$j0d }#tj0d |#t}%||% qg|S t|tr| jtkrtt
j|| jS t|t r|jHs|jIJ st|j!| jS g S t|t7rt|j!| jS t|t.rtKg S )N__call__c                 3      | ]} |kV  qd S rD   rI   r   r   rI   rJ   rq         z:ConstraintBuilderVisitor.visit_instance.<locals>.<genexpr>Tis_operator	class_objr  c                 3  r  rD   rI   r   r  rI   rJ   rq     r  )skipc                 3  r  rD   rI   )rm   r   )instancerI   rJ   rq     r  rk   r   )Lr   rW   r   r   rx   is_protocolprotocol_membersr   r   r|   r   r   find_memberr   r	   r   r   r   r   fallbackis_type_objr2   ret_typer!   r   ry   r   r:   'infer_constraints_from_protocol_membersr'   itemr<   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*   r6   rj   r   r   variancer   r   neg_opr   r  r   r}   r_   r   ra   rc   r=   r   is_protocol_implementationr   r  r5   r   r~   r0   r   rE   r   r  )&rH   r   original_actualr   r   callsubresr,  subtyper   erasedmappedtvars
prefix_lenr   tvtr*  i_prefixi_middlei_suffixm_prefixm_middlem_suffixinstance_argsmapped_argstvar
mapped_arginstance_argr  suffixt_prefixt_middlet_suffixtemplate_argstemplate_argr.  unpackedcbrI   )r&  r   rJ   visit_instance  s  
























z'ConstraintBuilderVisitor.visit_instanceFr&  r=  r"   protocolr#  c           
      C  s   g }|j jD ]F}tjj||||d}tj|||}	|du s"|	du r+|dkr'qg   S |t|	|| j tjjtj	||v rL|t|	|t
| j q|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"  Nr  )rx   r(  r   r   r)  r   r   r   IS_SETTABLEget_member_flagsr8  )
rH   r&  r   r=  rW  r#  r   memberinsttemprI   rI   rJ   r-    s   z@ConstraintBuilderVisitor.infer_constraints_from_protocol_membersr   c                 C  sX  |  }d}t| jtrg }| j  }| }|j|j}}|jd ur0|jd ur0|j}|j}n|jd ur;ttj	}n
|jd urEttj	}|j
d urV|j
d urV|j
}|j
}n|j
d urattj	}n
|j
d urkttj	}|t||| j |d u rtjr|jr| jstdd |jD s|t||t| jdd d}|jst|j}|d ur| s|j| }	t|	tsJ t|	}
t||
}t||
}t||t| j}|| n|t||| j n|j}t|j}| }tjr|jr| js|t||t| jdd d}|j|jd | |jd | |j d | d}|t||| j d }|s^tdd |jD }t!||}t"|j|d  |j|d  |j |d  tjsW|jng |j#d	}n-t|jjt|jjkr|jt"|jj|d  |jj|d  |jj |d  |jj#d
d}|d ur|$t%|| j| |r|D ]}| j&|j7  _&q|S t| jtr| }ttj'| jd}|d u r| (|j| j}nt%|t)t"||gt*t+gd d gg}|t|j|| j |S t| jt,r| -| j|S t| jt.rt|j| jj/| jS t| jt0r*t1j2j3d| j| jdd}|r(t||| jS g S g S )NFc                 s  s    | ]}|j  V  qd S rD   )rE   is_self)rm   tvrI   rI   rJ   rq   "  rv   z?ConstraintBuilderVisitor.visit_callable_type.<locals>.<genexpr>T)r   )r_   ra   rc   c                 S  s   g | ]
}|t tfv r|qS rI   )r   r   )rm   krI   rI   rJ   r   c  s    z@ConstraintBuilderVisitor.visit_callable_type.<locals>.<listcomp>)r_   ra   rc   r   rs   rr   r$  )
source_anyr  r   )4with_unpacked_kwargsrW   r   r   rp   r,  
type_guardr   r%   special_formtype_isr   r   r   r8   r  r   r   r   r8  is_ellipsis_argsr1   r_   r0   get_tuple_fallback_from_unpackrepack_callable_args#build_constraints_for_simple_unpackr  r  r   r}   ra   rc   minr   rs   r|   r=   rG   from_another_anyr  r:   r   r   r   infer_against_overloadedr'   r.  r   r   r   r)  )rH   r   rG   r   cactualrp   template_ret_typecactual_ret_typeunpack_presentunpack
tuple_typetemplate_typesr   unpack_constraintsr  rA  
cactual_pscactual_prefixparam_spec_targetmax_prefix_lenrn   r   r;  rI   rI   rJ   visit_callable_type  s   






	







z,ConstraintBuilderVisitor.visit_callable_type
overloadedr   c                 C  s   t ||}t||| jS rD   )find_matching_overload_itemr   r   )rH   ry  r   r.  rI   rI   rJ   rk    s   
z1ConstraintBuilderVisitor.infer_against_overloadedr!   c                 C  s  | j }t|j}t|to|jjdk}t|ts|rmg }|d ur|r||j| }t|ts/J t	|j}t|t
rCt|| j|dg}nt|trN|jjdksPJ t||| j}t|ts^J t|jD ]\}}	||krlqc|t|	|jd | j qc|S t|tsJ t|j|j| j}
d}d}||
 nt|tr)t|j}|d ur|j| }t|tsJ t	|j}t|jd t|jkr|}t|j| d }tt|j||\}}}t|jd | }|r|t|j| d  7 }|| }t|tr|jjdksJ |D ]}|t||jd | j qnd}d}nt|j}t|j}n|S t|ts3J t|t|krk|jjjrS|jjjrS|t|j|j| j S tt|D ]}|t|| || | j qY|S t|trz| |j|S g S )Nrk   )r>   r@   r?   r   rI   r;   )r   r1   r~   rW   r   rx   r   r!   r0   r2   r*   r=   r   r   rw   r   rj   rh  r   r6   tuplerz   is_named_tupleranger   r  )rH   r   r   unpack_indexis_varlength_tupler   r   r   r   tirs  actual_itemstemplate_itemsa_unpack_indexa_unpack
a_unpackeda_prefix_lena_suffix_lenrO  rP  rQ  tmrI   rI   rJ   visit_tuple_type  s   









z)ConstraintBuilderVisitor.visit_tuple_typer$   c                 C  sb   | j }t|tr!g }||D ]\}}}|t||| j q|S t|tr/| |j	
 |S g S rD   )r   rW   r$   r   r   r   r   r   r  r~   r   )rH   r   r   r   	item_nametemplate_item_typeactual_item_typerI   rI   rJ   visit_typeddict_type	  s   

z-ConstraintBuilderVisitor.visit_typeddict_typer/   c                 C  r  )NFz`Unexpected UnionType in ConstraintBuilderVisitor (should have been handled in infer_constraints)rI   r  rI   rI   rJ   visit_union_type  r  z)ConstraintBuilderVisitor.visit_union_typer#   c                 C  s   J d| )NFz!This should be never called, got rI   r  rI   rI   rJ   visit_type_alias_type  s   z.ConstraintBuilderVisitor.visit_type_alias_typer   r   r   c                 C  s   g }|D ]:}t |tr4t |jtr|t|j| j| qt|j}t |ts)J |	t
||| j q|	t
||| j q|S rD   )rW   r0   rx   r*   r|   r=   r   r2   r   r   r   )rH   r   r   r   r   rT  rI   rI   rJ   r     s   

z*ConstraintBuilderVisitor.infer_against_anyc                 C  sH   t | jtrt|| j}n|j}g }|D ]}|t|| j| j q|S rD   )rW   r   r   find_matching_overload_itemsr~   r   r   r   )rH   r   r~   r   r   rI   rI   rJ   visit_overloaded1  s   z)ConstraintBuilderVisitor.visit_overloadedr'   c                 C  s   t | jtrt|j| jj| jS t | jtr#t|j| jjd j| jS t | jt	r3t|j| jj| jS t | jt
rBt|j| j| jS g S )Nr   )rW   r   r   r   r.  r,  r   r   r~   r'   r   r  rI   rI   rJ   visit_type_type;  s   z(ConstraintBuilderVisitor.visit_type_typeN)r   r    r   rA   r   rU   rB   rC   )r   r-   rB   rh   )r   r   rB   rh   )r   r   rB   rh   )r   r.   rB   rh   )r   r   rB   rh   )r   r   rB   rh   )r   r   rB   rh   )r   r   rB   rh   )r   r+   rB   rh   )r   r   rB   rh   )r   r*   rB   rh   )r   r0   rB   rh   )r   r   rB   rh   )r   r   rB   rh   F)r&  r   r   r   r=  r"   rW  r   r#  rU   rB   rh   )r   r   rB   rh   )ry  r   r   r   rB   rh   )r   r!   rB   rh   )r   r$   rB   rh   )r   r/   rB   rh   )r   r#   rB   rh   )r   r   r   r   rB   rh   )r   r   rB   rh   )r   r'   rB   rh   )rY   rZ   r[   r\   r]   rK   r  r	  r
  r  r  r  r  r  r  r  r  r  r  rV  r-  rx  rk  r  r  r  r  r  r  r  rI   rI   rI   rJ   r   u  s@   
 














  
 
'

a




r   r@   c                 C  s&   | 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)r@   rI   rI   rJ   r8  H  s
   r8  ry  r   c                 C  s:   | j }|D ]}tjj||tjjdddr|  S q|d S )z.Disambiguate overload item against a template.FT	is_compatis_proper_subtypeignore_returnr   )r~   r   r   is_callable_compatibler   )ry  r   r~   r.  rI   rI   rJ   rz  S  s   
rz  list[CallableType]c                 C  sH   | j }g }|D ]}tjj||tjjdddr|| q|s"| }|S )zMLike find_matching_overload_item, but return all matches, not just the first.FTr  )r~   r   r   r  r   r|   copy)ry  r   r~   r   r.  rI   rI   rJ   r  f  s   
r  rp  r0   r   c                 C  sl   t | j}t|tr|jjdkr|jS t|tr|jjS t|tr2|jjj	D ]}|jdkr1|  S q&J d)zMGet builtins.tuple type from available types to construct homogeneous tuples.rk   FzInvalid unpack type)
r2   rx   rW   r   r   r*   ry   r!   rz   mro)rp  tpbaserI   rI   rJ   rf  ~  s   



rf  callablerq  c                 C  s   t | jvr| jS | jt }| jd| }| j| }g }t|ts*tt||g}n t|j}t|t	rJt|j
d ts>J |j
d }|j
dd }||g | 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   ra   r_   indexrW   r0   r   r2   rx   r!   r~   )r  rq  
star_indexr_   	star_typesuffix_typesr  rI   rI   rJ   rg    s   





rg  rR  actual_argsc                 C  s  t | }|dus
J |}t| | d }d}g }t |}|du rM| | }|| t|krHt|ts3J t|jtrFt|j|tg |jjgS g S |}	|}
n |}t|| d }t	||}	t	||}
||krm||krm| | }t
t||	|
\}}}t| d|	 |D ]\}}|t||| q|
rt| |
 d |D ]\}}|t||| q|durt|tsJ t|j}t|tr|jjdkr|D ]3}t|ts|t|jd || qt|j}t|tr|jjdkr|t|jd |jd | q|S t|tr|t||tt||j |S |durd|| }t|ts%J t|j}t|trd|jjdkrd| | }t|tsCJ t|j}t|trd|jjdkrd|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;   rk   r   )r1   r   rW   r0   rx   r*   r=   r!   ry   ri  r6   r{  r   r   r   r2   r   r   rj   r|   r   )rR  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  rI   rI   rJ   rh    sv   










rh  leftrightc                 C  sH   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.)rW   r   r0   r:   r   r8  )r  r  r   rI   rI   rJ   infer_directed_arg_constraints  s   r  #NormalizedCallableType | Parametersc                 C  s  g }|t kr| |}}n|| }}| }| }| }| }	|dur4|dur4|t|j|j| |durG|	durG|t|j|	j| | D ]}
tj	||
}|du rYqK|t|j|
j| qK|dur|
d}|dustJ |j}|dus}J |t|jk r|j|  r||}|dusJ |t|j|j| |d7 }|t|jk r|j|  s|	durdd |jD }t }t|j|jD ]\}}|du s| s||v rq|| q|d}|dusJ |D ]}||}|dusJ |t|j|j| q|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                 S  s   h | ]}|d ur|qS rD   rI   )rm   namerI   rI   rJ   	<setcomp>L  r   z7infer_callable_arguments_constraints.<locals>.<setcomp>)r:   var_argkw_argr   r  r   formal_argumentsr   r   callable_corresponding_argument try_synthesizing_arg_from_varargposr   ra   is_positionalargument_by_positionrc   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_namerI   rI   rJ   r    s`   






r  csc                 C  s   t  }| D ]}t|jtsqt|jtst|jtr$|jjs$||j qg }| D ]!}t|jtr6|j|vr;|	| t|jtrE|jjsJ|	| q)|S )zWFor each ParamSpec remove all imprecise constraints, if at least one precise available.)
r  rW   rF   r   r?   r   rs   r  r>   r|   )r  have_precisern   new_csrI   rI   rJ   r   ^  s(   



r   )r^   r   r_   r`   ra   rb   rc   rd   re   rf   rg   r9   rB   rh   r  )
r   r"   r   r"   r   rA   r   rU   rB   rh   )r   r"   r   r"   r   rA   rB   r   )r   r   rB   r   )r   r=   rB   r=   )r   r/   r   r"   r   rA   rB   rh   )r   r   r   rU   rB   rh   )r   r   rB   r   )r   rh   r   rh   rB   rU   )r   r=   r   r=   rB   rU   )r   r   rB   r   )r   r"   rB   rU   )r@   rA   rB   rA   )ry  r   r   r   rB   r   )ry  r   r   r   rB   r  )rp  r0   rB   r   )r  r   rq  r   rB   r   )rR  r   r  r   r   rA   rB   rh   )r  r"   r  r"   r   rA   rB   rh   )r   r  r   r  r   rA   rB   rh   )r  rh   rB   rh   )er\   
__future__r   typingr   r   r   r   r   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-   r.   r/   r0   r1   r2   r3   r4   r5   r6   mypy.types_utilsr7   mypy.typestater8   
mypy.inferr9   r:   r]   r<   r=   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rU   r   r   r8  rz  r  rf  rg  rh  r  r  r   rI   rI   rI   rJ   <module>   sd    (
&
% +
,
]




4







	     
X





`
I