o
    tBhgv                    @  sz  U d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	m
Z
mZmZmZ ddlmZmZmZ ddlZddlmZ ddlmZmZmZmZmZmZmZ ddlm Z m!Z!m"Z" dd	l#m$Z$m%Z% dd
l&m'Z' ddl(m)Z)m*Z*m+Z+ ddl,m-Z-m.Z. ddl/m0Z0m1Z1m2Z2 ddl3m4Z4m5Z5m6Z6 ddl7m8Z8 ddl9m:Z: ddl;m<Z<m=Z= ddl>m?Z? ddl@mAZA ddlBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\m]Z]m^Z^m_Z_m`Z`maZambZbmcZcmdZdmeZemfZfmgZgmhZhmiZimjZjmkZkmlZlmmZmmnZnmoZompZpmqZqmrZrmsZsmtZtmuZumvZvmwZwmZmxZxmyZymzZzm{Z{m|Z|m}Z}m~Z~mZmZmZ ddlmZmZmZmZmZ ddlmZ ddlmZ ddlmZmZmZmZ ddlmZ ddlmZmZmZmZmZ ddlmZmZmZmZmZmZmZmZmZmZmZmZ ddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ ddlmZ ddlmZ ddlmZ dd lmZ eeeeJeeeeee eTeTg
df Zd!ed"< d#Zd$ed%< g d&Zd$ed'< G d(d) d)eՃZdd.d/Zdd3d4ZG d5d6 d6eՃZG d7d8 d8ee Zddd<d=ZG d>d? d?eje Zdd@dAZddBdCZddDdEZddLdMZddQdRZG dSdT dTeje ZG dUdV dVeje ZddXdYZG dZd[ d[eje Zdd\d]ZG d^d_ d_eje ZddbdcZddkdlZddndoZddsdtZddvdwZddzd{Zdd|d}ZdddZdddZdddZdS )zGExpression type checker. This file is conceptually part of TypeChecker.    )annotationsN)contextmanager)CallableClassVarIteratorListOptionalSequencecast)Final	TypeAliasoverload)	applytype	erasetypejoinmessage_registrynodes	operatorstypes)ArgTypeExpandermap_actuals_to_formalsmap_formals_to_actuals)analyze_member_accesstype_object_type)StringFormatterChecker)
erase_type!remove_instance_last_known_valuesreplace_meta_vars)ErrorWatcherreport_internal_error)expand_typeexpand_type_by_instancefreshen_function_type_vars)ArgumentInferContextinfer_function_type_argumentsinfer_type_arguments)literal)map_instance_to_supertype)is_overlapping_typesnarrow_declared_type)ErrorMessage)MessageBuilder)@	ARG_NAMEDARG_POSARG_STAR	ARG_STAR2IMPLICITLY_ABSTRACTLITERAL_TYPEREVEAL_TYPEArgKindAssertTypeExprAssignmentExpr	AwaitExpr	BytesExprCallExprCastExprComparisonExprComplexExprConditionalExprContext	DecoratorDictExprDictionaryComprehensionEllipsisExprEnumCallExpr
Expression	FloatExprFuncDefGeneratorExpr	IndexExprIntExpr
LambdaExprListComprehensionListExpr
MemberExprMypyFileNamedTupleExprNameExprNewTypeExprOpExprOverloadedFuncDefParamSpecExprPlaceholderNodePromoteExprRefExpr
RevealExprSetComprehensionSetExpr	SliceExprStarExprStrExpr	SuperExpr
SymbolNodeTempNode	TupleExprr   TypeAliasExprTypeApplicationTypedDictExprTypeInfoTypeVarExprTypeVarTupleExpr	UnaryExprVar	YieldExprYieldFromExpr)FunctionContextFunctionSigContextMethodContextMethodSigContextPlugin)
ENUM_BASES)state)is_equivalentis_same_type
is_subtypenon_method_protocol_members)has_await_expression)check_for_explicit_anyexpand_type_aliashas_any_from_unimported_typemake_optional_typeset_any_tvars)callable_typecustom_special_methoderase_to_union_or_bound
false_onlyfunction_typeis_literal_type_likemake_simplified_unionsimple_literal_type	true_onlytry_expanding_sum_type_to_uniontry_getting_str_literalstuple_fallback)!LITERAL_TYPE_NAMESTUPLE_LIKE_INSTANCE_NAMESAnyTypeCallableTypeDeletedType
ErasedTypeFunctionLikeInstanceLiteralTypeLiteralValueNoneType
OverloadedParamSpecFlavorParamSpecTypePartialType
ProperTypeStarType	TupleTypeTypeTypedDictType	TypeOfAnyTypeTypeTypeVarTypeUninhabitedType	UnionTypeflatten_nested_unionsget_proper_typeget_proper_typeshas_recursive_typesis_generic_instanceis_named_instanceis_optionalremove_optional)	TypeState)fill_typevars)split_module_names)ExpressionVisitor
_TypeAlias
ArgChecker   r   
MAX_UNIONS)builtins.setzbuiltins.frozensetztyping.KeysViewztyping.ItemsViewzbuiltins._dict_keyszbuiltins._dict_itemsz_collections_abc.dict_keysz_collections_abc.dict_itemsOVERLAPPING_TYPES_ALLOWLISTc                   @     e Zd ZdZdS )TooManyUnionszyIndicates that we need to stop splitting unions in an attempt
    to match an overload in order to save performance.
    N__name__
__module____qualname____doc__ r   r   e/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/mypy/checkexpr.pyr          r   tr   returnboolc                 C  s(   t | tpt | totdd | jD S )Nc                 s  s    | ]	}t t|V  qd S N)allow_fast_container_literalr   .0itr   r   r   	<genexpr>       z/allow_fast_container_literal.<locals>.<genexpr>)
isinstancer   r   allitemsr   r   r   r   r      s   

r   exprrV   set[str]c                 C  s  t  }t| jts| jdurt| jtr| jdur|| j t| tr[t| jto-| jj}t| jt	r@|
t| jj 	 |S | jdurXd| jv rX|sX|| jddd  	 |S t| trmt| jtrj| j} n	 |S tdt|  t| jts| jdus|S )zRecursively extracts all module references from a reference expression.

    Note that currently, the only two subclasses of RefExpr are NameExpr and
    MemberExpr.N.   r   zUnknown RefExpr subclass: )setr   noderM   fullnameaddrO   rh   is_suppressed_importrd   updater   module_namersplitrL   r   rV   AssertionErrortype)r   outputr   r   r   r   extract_refexpr_names   s,   

r   c                   @  r   )FinishedzDRaised if we can terminate overload argument check early (no match).Nr   r   r   r   r   r      r   r   c                   @  s	  e Zd ZU dZded< ded< ded< ded	< d
ed< ded< dddZdddZdddZddddZdd#d$Z	ddd'd(Z
dd+d,Zddd-d.Zdd/d0Zd d5d6Zd!d8d9Zd"d<d=Zdd>d?Zdd@dAZd#dLdMZd$dQdRZd%dSdTZd&dUdVZd'dZd[Zd(d\d]Zd)d_d`Zd*dddeZdfgdgdhgdiZdjedk< dldmgidndogidndogidndpdmgidqZdreds< ddtduZd+dxdyZd,d|d}Zd-ddZ d.ddZ!d/ddZ"d0ddZ#		d1d2ddZ$	d3d4ddZ%d5ddZ&				d6d7ddZ'd8ddZ(d9ddZ)d:ddZ*d;ddZ+e,d<ddZ-d=ddZ.d>ddZ/d?ddZ0d@ddZ1dAddZ2dBddńZ3dCddȄZ4		d1dDdd΄Z5dEddӄZ6dFddՄZ7		d1dGddلZ8dHddZ9dIddZ:dJddZ;dKddZ<dLddZ=	dMdNddZ>dOddZ?e,dPddZ@dQd dZAdRddZB	ddSddZCdTddZDdUd	d
ZEddVddZFdVddZGdWddZHdXddZIdYddZJdZddZKd[d d!ZLd\d#d$ZMd]d&d'ZNd^d)d*ZOd_d,d-ZPd`d/d0ZQdad2d3ZRdbd5d6ZSdcd7d8ZT	d3ddd:d;ZU	d3ded>d?ZV	d3dfd@dAZWdgdCdDZXdhdJdKZY	ddidNdOZZdjdPdQZ[dadRdSZ\dkdUdVZ]dldXdYZ^dmd[d\Z_dmd]d^Z`	d3dnd`daZadodddeZbdpdhdiZcdqdjdkZddrdmdnZedsdpdqZfdtdsdtZgdudvdwZhdvdydzZidwd}d~ZjdxddZkdddyddZldzddZmd{ddZnd|ddZod}ddZpd~ddZqdddZrdddZsdddZtdddZudddZvdddZwdddZxdddZydddZzdddZ{dddZ|dddZ}	d3dddZ~ddĐdńZddǐdȄZdddʐd˄Z	dddΐdτZ				dddҐdӄZdd֐dׄZdOdؐdلZdOdڐdۄZddܐd݄Zddސd߄ZdddZddddZ	ddddZddddZdddZdddZdddZdddZdddZdddZdddZdddZdddZdddZdddZdddZedddZedddZ	ddddZdS (  ExpressionCheckerz^Expression type checker.

    This class works closely together with checker.TypeChecker.
    mypy.checker.TypeCheckerchkr+   msgzlist[Type | None]type_contextzdict[Expression, ProperType]resolved_typer   strfrm_checkerro   pluginr   Nonec                 C  sB   || _ || _|| _dg| _i | _t| | j | j| _i | _d| _dS )z%Construct an expression type checker.NF)	r   r   r   r   type_overridesr   r   r   	is_callee)selfr   r   r   r   r   r   __init__  s   
zExpressionChecker.__init__c                 C  s
   i | _ d S r   )r   r   r   r   r   reset     
zExpressionChecker.reseterO   r   c                 C  s(   | j jt| | |}| ||S )z_Type check a name expression.

        It can be of any kind: local, member or global.
        )r   module_refsr   r   analyze_ref_exprnarrow_type_from_binder)r   r   resultr   r   r   visit_name_expr  s   
z!ExpressionChecker.visit_name_exprFrV   lvaluer   c                 C  s  d }|j }t|tr|jrttjS t|tr,| ||}t|t	r+| j
||||}nt|tr:t|| d}nt|trH|jd urH|j}nt|tr|jrV| |}nt|| j}t|trqt|jtrq|j|j_|j|j_tt| jd trt|}nZt|trz| d}W nM t y   | d}Y n@w t|t!r| |j"|}n2t|t#r| j$||j%||j&p|d}nt|t't(fr| ) }nt|t*rJ d|j+dttj,}|d usJ |S )	Nbuiltins.functionztypes.ModuleTypebuiltins.objectalias_definitionFzPlaceholderNode z leaked to checker)-r   r   rO   is_special_formr   r   special_formrh   analyze_var_refr   r   handle_partial_var_typerE   r   
named_typerR   r   rd   typeddict_typetypeddict_callabler   r   ret_typer   linecolumnr   r   r   r   erase_typevarsrM   KeyErrorr>   varr   alias_type_in_runtime_contextno_argsis_alias_rvaluere   rS   object_typerT   r   
from_error)r   r   r   r   r   r   r   r   r   $  sX   













z"ExpressionChecker.analyze_ref_exprr  rh   contextr=   c                 C  s~   |j r*t|j }t|tr'|  r|jd ur|jS |jdv r'| |jdkdS |j S |js:| j	
 r:| j	|j| ttjS )N>   TrueFalser  builtins.bool)r   r   r   r   is_literal_contextlast_known_valuenameinfer_literal_expr_typeis_readyr   in_checked_functionhandle_cannot_determine_typer   r   r   )r   r  r
  var_typer   r   r   r   i  s   



z!ExpressionChecker.analyze_var_refr8   allow_none_returnc                 C  sF   |j rt|j tr|j js| | | |j | jd S | j||dS )zType check a call expression.r   r  )analyzedr   rN   is_typedvisit_call_expr_inneracceptr   )r   r   r  r   r   r   visit_call_exprx  s
   
z!ExpressionChecker.visit_call_exprbaserC   c                 C  sH   t |tsdS t |jtr|jjd urdS t |jto#t t|jjtS )NFT)	r   rV   r   rd   r   r   r   targetr   )r   r  r   r   r   refers_to_typeddict  s   
z%ExpressionChecker.refers_to_typeddictc              	     sH    jst jtr;  jjr;tj jdd}t|tr;t|j}t|t	s.J 
| j j j |S t jtr jjdv rt jdkrtj jd D ]}d }t|trsz	j|j}W n	 tyr   Y nw t|rj jjd  qW|rt|jtrtt|jjtrj jjd  qWt|trt|jtt fst|tr|rt|jtr|jj!sj"  t|t#rt|jt$r|jj%r܈j jjd  qW|jj&rj jjd	  qW'  d }t jt(r/t) j j jj jj fd
d fddt*t jjD }t| jj jj+ ,dd}tj j|ddd}	jj-j.rXj/ rXt|	trX|	j0rXj1|	  d }
d }d }t jt#r jj2}t jjtrt jjj}t|t3r|j4j2}|d u rt jt5rj6 jj7r jj}j8 jj7}
9|	 ||
|}t jt#rt jdkrӈ jj2dv rǈ:   jj2dkrӈ;  t jt5r jjdkr<  t|}t|t=rt>|j?}t|t@r|jAsjjBC  |s"t|tDr"E jr"jjF|	  ttGjHS |S )NTr   )r   
issubclass   r   LiteralAny	TypedDictNewTypec                   s     j|  S r   r  argsir   r   r   r   <lambda>      z9ExpressionChecker.visit_call_expr_inner.<locals>.<lambda>c                   s*   g | ]}t  fd d| D qS )c                   s   g | ]
}  j| qS r   r'  )r   jr+  r   r   
<listcomp>  s    zFExpressionChecker.visit_call_expr_inner.<locals>.<listcomp>.<listcomp>)r   join_type_listr   r*  r   formal_to_actualr   r   r   r/    s    z;ExpressionChecker.visit_call_expr_inner.<locals>.<listcomp>r   )r   fallback)always_allow_anyr   )zbuiltins.isinstancebuiltins.issubclassr6  format)Ir  calleer   rG   r  r   r  r   r   r   check_typeddict_call	arg_kinds	arg_namesr(  rO   r  lenmypycheckerflattenr   lookup_qualifiedr  is_expr_literal_typer   cannot_use_function_with_typer   r   r  r   r  rb   ra   r  type_arguments_not_allowedrV   rd   r   
is_newtypetry_infer_partial_typerI   r   ranger  r   optionsdisallow_untyped_callsr  implicituntyped_function_callr   r   r   rL   has_typer   lookup_type check_call_expr_with_callee_typecheck_runtime_protocol_testcheck_protocol_issubclasscheck_str_format_callr   r   r   r   	ambiguousbinderunreachabler   always_returns_nonedoes_not_return_valuer   r	  )r   r   r  r   r   typr   r   	arg_typescallee_typer  memberr   r  r   r   r2  r   r    s   







	









z'ExpressionChecker.visit_call_expr_innerc                 C  s   t |jtsJ d}t |jjtr|jjj}n | j|jjr7t| j	|jj}t |t
r7t |jtr7|j}|durD| j|| dS dS )z>More precise type checking for str.format() calls on literals.N)r   r8  rL   r   r\   valuer   rK  try_getting_literalrL  r   strr   rP  )r   r   format_valuebase_typr   r   r   rP    s   z'ExpressionChecker.check_str_format_callr  method_namer\  
str | Nonec                 C  s   t |}t|tr| rt |j}nt|tr|j}d}t|tr'|jj	}n#t|t
tfr?|jj|}|dur<|j	nd}nt|trJt|jj	}|durU| d| S dS )zConvert a method name to a fully qualified name, based on the type of the object that
        it is invoked on. Return `None` if the name of `object_type` cannot be determined.
        Nr   )r   r   r   is_type_objr   r   itemr   r   r   r   r   r4  get_containing_type_infor   r   )r   r  r_  	type_nameinfor   r   r   method_fullname!  s    



z!ExpressionChecker.method_fullnamer   c                 C  s   t |tr| |jrdS t |trT|jdu rTt| j|j}t |t	r)|j
}nt |trB| rBt|j}t |t	r@|j
}ndS dS ||j}|rT| |jrTdS dS )zPCheck if `node` refers to something explicitly annotated as only returning None.TNF)r   rV   defn_returns_noner   rL   r   r   rL  r   r   r   r   ra  r   getr  )r   r   rV  re  r   symr   r   r   rT  =  s"   



z%ExpressionChecker.always_returns_nonedefnSymbolNode | Nonec                   s   t |trt |jtot t|jjtS t |tr%t fdd|j	D S t |t
rVt|j}|jsAt |trAt t|jtrAdS t |trV|jd}|rV |jrVdS dS )z'Check if `defn` can _only_ return None.c                 3      | ]}  |V  qd S r   )rg  r   rb  r   r   r   r   Z      z6ExpressionChecker.defn_returns_none.<locals>.<genexpr>T__call__F)r   rE   r   r   r   r   r   rR   r   r   rh   is_inferredr   rh  rg  r   )r   rj  rV  ri  r   r   r   rg  S  s(   




z#ExpressionChecker.defn_returns_nonec                 C  sb   t j|jd D ]%}t| j|}t|tr.|	 r.|
 jr.|
 js.| jtj| q	d S Nr   )r=  r>  r?  r(  r   r   rL  r   r   ra  type_objectis_protocolruntime_protocolfailr   RUNTIME_PROTOCOL_EXPECTED)r   r   r   tpr   r   r   rN  i  s   z-ExpressionChecker.check_runtime_protocol_testc                 C  sn   t j|jd D ]+}t| j|}t|tr4|	 r4|
 jr4t|
 }|r4| jj|
 || q	d S rq  )r=  r>  r?  r(  r   r   rL  r   r   ra  rr  rs  ru   r   report_non_method_protocol)r   r   r   rw  attr_membersr   r   r   rO  t  s   z+ExpressionChecker.check_protocol_issubclassr8  r   r:  list[ArgKind]r;  Sequence[str | None]r(  list[Expression]orig_calleeType | Nonec           
      C  s   t |dkr0tdd |D r0tdd |D sJ ttt |}|}| |tt||||S t |dkra|d tkra|d }	t	|	t
rM| ||	||S t	|	trat	|	jt
ra| ||	j||S t |dkro| |i ||S | jtj| ttjS )Nr   c                 S  s   g | ]}|t kqS r   )r,   )r   akr   r   r   r/        z:ExpressionChecker.check_typeddict_call.<locals>.<listcomp>c                 s      | ]}|d uV  qd S r   r   )r   arg_namer   r   r   r         z9ExpressionChecker.check_typeddict_call.<locals>.<genexpr>r   )r<  r   r
   r   r\   check_typeddict_call_with_kwargsdictzipr-   r   r?   check_typeddict_call_with_dictr8   r  r   ru  r   INVALID_TYPEDDICT_ARGSr   r   r	  )
r   r8  r:  r;  r(  r
  r}  
item_names	item_args
unique_argr   r   r   r9  |  s*   	


z&ExpressionChecker.check_typeddict_callkwargsr?   dict[str, Expression] | Nonec           
      C  s   dd |j D }g }|j D ]6\}}d }|r+| |}t||}|r+t|dkr+|d }|d u r>|p2|}	| jtj|	  d S || qt	t
||S )Nc                 S     g | ]}|d  qS )r   r   rm  r   r   r   r/    r  z?ExpressionChecker.validate_typeddict_kwargs.<locals>.<listcomp>r   r   )r   r  r   r<  r   ru  r   $TYPEDDICT_KEY_MUST_BE_STRING_LITERALappendr  r  )
r   r  r  r  item_name_expritem_argliteral_valuekey_typevalueskey_contextr   r   r   validate_typeddict_kwargs  s   

z+ExpressionChecker.validate_typeddict_kwargsc                 C  sD   | j |d}|d ur |jt|   kot|j kS   S dS )Nr  F)r  required_keysr   keysr   )r   r8  r  r
  validated_kwargsr   r   r   match_typeddict_call_with_dict  s   ,z0ExpressionChecker.match_typeddict_call_with_dictc                 C  s0   | j |d}|d ur| j||||dS ttjS )Nr  )r  r
  r}  )r  r  r   r   r	  )r   r8  r  r
  r}  r  r   r   r   r    s   
z0ExpressionChecker.check_typeddict_call_with_dictre  rd   r   c                 C  sx   |j dusJ |j j}t|trt|tsJ t|j }tj	gt
| }t|j }t||||| d|jjdS )a  Construct a reasonable type for a TypedDict type in runtime context.

        If it appears as a callee, it will be special-cased anyway, e.g. it is
        also allowed to accept a single positional argument if it is a dict literal.

        Note it is not safe to move this to type_object_type() since it will crash
        on plugin-generated TypedDicts, that may not have the special_alias.
        Nbuiltins.type	variables)special_aliasr  r   r   r   listr   r  r3   r,   r<  r  r   r   rj  	type_vars)r   re  r  expected_typeskindsnamesr   r   r   r     s   	z$ExpressionChecker.typeddict_callablec                 C  s8   t t|j tjgt|j t|j || dS Nr  )	r   r  r   r  r3   r,   r<  r  r   )r   r8  r   r   r   typeddict_callable_from_context  s   z1ExpressionChecker.typeddict_callable_from_contextdict[str, Expression]c                   s   j t   krt j ks7n  fdd j D } }| jj |t||d ttj	S t
|}t|trC|}n jjjd urR|  jj}n|  }| j 9 | j $ | |t tjgt |t d d d \}}	W d    n1 sw   Y  W d    n1 sw   Y  t
|}
t|
ts }
|
j D ]\}}|v r̈| }| jj|||tjd| ddtjd q|S )Nc                   s&   g | ]}| j v s| v r|qS r   )r  r  )r   keyr8  r  r   r   r/    s
    zFExpressionChecker.check_typeddict_call_with_kwargs.<locals>.<listcomp>)expected_keysactual_keysr
  zTypedDict item ""
expression)lvalue_typervaluer
  r   lvalue_namervalue_namecode) r  r   r  r   r   unexpected_typeddict_keysr  r   r   r	  r   r   r   r4  r   r  r   r  filter_errorsr   local_type_mapcheck_callable_callr  r3   r,   r<  r   check_simple_assignmentr   INCOMPATIBLE_TYPEScodesTYPEDDICT_ITEM)r   r8  r  r
  r}  r  r  infer_calleeorig_ret_type_r   	item_nameitem_expected_type
item_valuer   r  r   r    s\   *





 


z2ExpressionChecker.check_typeddict_call_with_kwargsr   rL   
Var | Nonec                 C  sx   t |jtrt |jjtr|jjjsdS | jj }|r"|j	|j
vr$dS |j
|j	 }t |jtr:t |jjtr:|jS dS )zGet variable node for a partial self attribute.

        If the expression is not a self attribute, or attribute is not variable,
        or variable is not partial, return None.
        N)r   r   rO   r   rh   is_selfr   scopeenclosing_classr  r  r   r   )r   r   re  ri  r   r   r   get_partial_self_var+  s   
z&ExpressionChecker.get_partial_self_varr  r   discard)builtins.listr   zClassVar[dict[str, list[str]]]r  extendr  r   builtins.dictr   )r  r  zcollections.OrderedDictr   z)ClassVar[dict[str, dict[str, list[str]]]]container_argsc                 C  sX  t |jtsdS |j}t |jtr7| |j}|du rdS |\}}| ||j|}|dur5||_||= dS dS t |jt	rt |jj
tr|jjdurLdS |jj
}|jj}| |}|du r_dS |\}}t|j}	|	du sq|	jdu rsdS | ||j|}
|
dur| |}tj|r|	jdusJ |	jj}| j|||
g|_||= dS dS dS dS dS )z-Try to make partial type precise from a call.N)r   r8  rL   r   rV   get_partial_var&try_infer_partial_value_type_from_callr  r   rG   r  r  indexget_partial_instance_type
value_typer  r=  r>  is_valid_inferred_typer   r   named_generic_type)r   r   r8  retr  partial_typesrV  r  r  partial_typer  r  typenamer   r   r   rE  M  sH   




z(ExpressionChecker.try_infer_partial_typeref%tuple[Var, dict[Var, Context]] | Nonec                 C  sP   |j }|d u rt|tr| |}t|tsd S | j|}|d u r$d S ||fS r   )r   r   rL   r  rh   r   find_partial_types)r   r  r  r  r   r   r   r  t  s   

z!ExpressionChecker.get_partial_var
methodnameInstance | Nonec           	      C  s@  | j jrdS t|j}|du rdS |jr|jjj}n|jdus!J |jj}|| jv rO|| j| v rO|jtgkrO| 	|j
d }tj|rM| j ||gS dS || jv r|| j| v r|jtgkrt| 	|j
d }t|tr|jj}|| j| | v rtdd |j
D r| j |t|j
S dS t|tr| j |S dS )zCTry to make partial type precise from a call such as 'x.append(y)'.Nr   c                 s  s    | ]	}t j|V  qd S r   )r=  r>  r  )r   	item_typer   r   r   r     s
    

zKExpressionChecker.try_infer_partial_value_type_from_call.<locals>.<genexpr>)r   current_node_deferredr  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  arg_typearg_typenamer   r   r   r    s>   




z8ExpressionChecker.try_infer_partial_value_type_from_callrW  
list[Type]Sequence[str | None] | Noner3  list[list[int]]r   c
                 C  s<  t |j}
dd t|
D }dd t|
D }dd t|
D }dd t|
D }t|D ]/\}}|D ](}|| ||  || ||  |rR|| ||  || ||  q3q-|du r}| j|}|dusmJ |t|||j||j	||	| j
S | j|}|dusJ t|}|t||||j||j	||	| j
	S )a  Use special case logic to infer the return type of a specific named function/method.

        Caller must ensure that a plugin hook exists. There are two different cases:

        - If object_type is None, the caller must ensure that a function hook exists
          for fullname.
        - If object_type is not None, the caller must ensure that a method hook exists
          for fullname.

        Return the inferred return type.
        c                 S     g | ]}g qS r   r   r   r  r   r   r   r/    r-  z;ExpressionChecker.apply_function_plugin.<locals>.<listcomp>c                 S  r  r   r   r  r   r   r   r/    r-  c                 S  r  r   r   r  r   r   r   r/    r-  c                 S  r  r   r   r  r   r   r   r/    r-  N)r<  rW  rF  	enumerater  r   get_function_hookrk   r;  r   r   get_method_hookr   rm   )r   r8  r:  rW  r;  r3  r(  r   r  r
  num_formalsformal_arg_typesformal_arg_exprsformal_arg_namesformal_arg_kindsformalactualsactualcallbackmethod_callbackr   r   r   apply_function_plugin  sX   
z'ExpressionChecker.apply_function_pluginr   hook>Callable[[list[list[Expression]], CallableType], FunctionLike]c                   s   t |tr>t|j}t|||j|j fdd}dd t|D }t|D ]\}	}
|
D ]}||	  |  q,q&|||S t |t	sEJ g }|j
D ]}| |||}t |ts\J || qJt	|S )z@Helper to apply a signature hook for either a function or methodc                        |  S r   r  r)  r(  r   r   r   r,        z8ExpressionChecker.apply_signature_hook.<locals>.<lambda>c                 S  r  r   r   r  r   r   r   r/    r-  z:ExpressionChecker.apply_signature_hook.<locals>.<listcomp>)r   r   r<  r:  r   r;  rF  r  r  r   r   apply_signature_hook)r   r8  r(  r:  r;  r  r  r3  r  r  r  r  r   rb  adjustedr   r  r   r    s,   
	


z&ExpressionChecker.apply_signature_hooksignature_hook,Callable[[FunctionSigContext], FunctionLike]c              	     s    |||| fddS )zKApply a plugin hook that may infer a more precise signature for a function.c                   s   t | | jS r   )rl   r   r(  sigr
  r   r  r   r   r,     r  zAExpressionChecker.apply_function_signature_hook.<locals>.<lambda>)r  )r   r8  r(  r:  r
  r;  r  r   r  r   apply_function_signature_hook  s   
z/ExpressionChecker.apply_function_signature_hook*Callable[[MethodSigContext], FunctionLike]c              
     s(   t ||||| fddS )zIApply a plugin hook that may infer a more precise signature for a method.c                   s   t | | jS r   )rn   r   r  r
  pobject_typer   r  r   r   r,  5  s    z?ExpressionChecker.apply_method_signature_hook.<locals>.<lambda>)r   r  )r   r8  r(  r:  r
  r;  r  r  r   r  r   apply_method_signature_hook#  s   z-ExpressionChecker.apply_method_signature_hookNcallable_namec           
   	   C  st   t |}|dur8t|tr8|dur&| j|}|r$| |||||||S |S | j|}	|	r8| ||||||	S |S )a6  Attempt to determine a more accurate signature for a method call.

        This is done by looking up and applying a method signature hook (if one exists for the
        given method name).

        If no matching method signature hook is found, callee is returned unmodified. The same
        happens if the arguments refer to a non-method callable (this is allowed so that the code
        calling transform_callee_type needs to perform fewer boilerplate checks).

        Note: this method is *not* called automatically as part of check_call, because in some
        cases check_call is called multiple times while checking a single call (for example when
        dealing with overloads). Instead, this method needs to be called explicitly
        (if appropriate) before the signature is passed to check_call.
        N)r   r   r   r   get_method_signature_hookr  get_function_signature_hookr   )
r   r  r8  r(  r:  r
  r;  r  method_sig_hookfunction_sig_hookr   r   r   transform_callee_type;  s   
z'ExpressionChecker.transform_callee_typerX  rY  c              
   C  s   |du r|dur|dusJ |  ||}t|}|r)| |||j|j||j|}n|dur9t|tr9| |||S | j	||j|j||j|j
||d\}}t|}t|j
tret|tre|jdure|j|j
_|S )a?  Type check call expression.

        The callee_type should be used as the type of callee expression. In particular,
        in case of a union type this can be a particular item of the union, so that we can
        apply plugin hooks to each item.

        The 'member', 'callable_name' and 'object_type' are only used to call plugin hooks.
        If 'callable_name' is None but 'member' is not None (member call), try constructing
        'callable_name' using 'object_type' (the base type on which the method is called),
        for example 'typing.Mapping.get'.
        N)callable_noder  r  )rf  r   r
  r(  r:  r;  r   r   check_union_call_expr
check_callr8  rV   r   
type_guard)r   rX  r   r  r  rY  r   proper_calleer   r   r   rM  d  s8   




z2ExpressionChecker.check_call_expr_with_callee_typer   c           
      C  s   g }|  D ]O}| j  t|||ddd| j|| j|  |d}W d   n1 s+w   Y  | j|j|dd}|du r>q| ||}|rH|nd}	|	| 
||||	 qt|S )zH "Type check calling a member expression where the base type is a union.F)original_typer   in_literal_context	self_typeNT)skip_non_overlapping)relevant_itemsr   r  r   r   r  r   r8  rf  r  rM  r   )
r   r   r  rY  resrV  rb  narrowedr  item_object_typer   r   r   r    s4   z'ExpressionChecker.check_union_call_exprr  Expression | Nonetuple[Type, Type]c	                 C  s  t |}t|tr| ||||||||S t|tr%| |||||||S t|ts/| j s5| 	||S t|t
rC| |||||S t|trtd||ddd| j|| j|  d
}	|jjd }| ||	|||||}	| |	|||||||}
|r| j|| |
S t|tr| |j|||||S t|tr| |j|}| ||||||S t|tr| t||||||||S | j||ttjfS )a  Type check a call.

        Also infer type arguments if the callee is a generic function.

        Return (result type, inferred callee type).

        Arguments:
            callee: type of the called value
            args: actual argument expressions
            arg_kinds: contains nodes.ARG_* constant for each argument in args
                 describing whether the argument is positional, *arg, etc.
            context: current expression context, used for inference.
            arg_names: names of arguments (optional)
            callable_node: associate the inferred callable type to this node,
                if specified
            callable_name: Fully-qualified name of the function/method to call,
                or None if unavailable (examples: 'builtins.open', 'typing.Mapping.get')
            object_type: If callable_name refers to a method, the type of the object
                on which the method is being called
        ro  FT)	is_lvalueis_superis_operatorr   r  r   r  z	.__call__)r   r   r   r  r   check_overload_callr   r   r  check_any_type_callr   check_union_callr   r   r   r  r   r   r
  r  
store_typer   upper_boundr   analyze_type_type_calleerb  r   r   not_callabler   r	  )r   r8  r(  r:  r
  r;  r  r  r  call_functionr   rb  r   r   r   r    s   








zExpressionChecker.check_callc	                   s$  |  }|du r|jr|j}t|j}	| r t|	tr |	jj}t|t	r/|jt
v r/|j|fS | rJ| jrJ|jsJjtj| j| n;| r| jr|js| js| }
i }|
jD ]\}}|tkru|
|||< qdd||< qdj| j|| t|||j|j fdd}| rtdd |jD }t |}!||}"| |||}|rt|||j|j fdd}|# }|dur|t$t%gkr& d }& d	 }t|t'rt|t'r|j(t)j*kr|j(t)j+kr|j,|j,  kr|j,krn n|j|fS -| ||}.|||||||| j/|| ||||d
 | rUt0|d	krUt1|j2drU|j3t45|d d}|r_j6|| |r|du rnj78|sz|durj79|r:||||| |||	}|j3|d}|j|fS )zxType check a call that targets a callable value.

        See the docstring of check_call for more information.
        NFc                   r  r   r  r)  r  r   r   r,  [  r  z7ExpressionChecker.check_callable_call.<locals>.<lambda>c                 s      | ]}t |tV  qd S r   )r   r   )r   vr   r   r   r   _  rn  z8ExpressionChecker.check_callable_call.<locals>.<genexpr>c                   r  r   r  r)  r  r   r   r,  n  r  r   r   r  r  r   );with_unpacked_kwargsr  r   r   ra  r   r   r   r   rV   rp   rr  rs  from_type_typer   ru  r   CANNOT_INSTANTIATE_PROTOCOLr7  is_abstractfallback_to_anyabstract_attributesr0   can_return_noner   !cannot_instantiate_abstract_classr   r:  r;  
is_genericanyr  r"   +infer_function_type_arguments_using_contextr$   
param_specr.   r/   r  r   flavorr   ARGSKWARGSidinfer_arg_types_in_contextcheck_argument_countcheck_argument_typesr<  rr   r   copy_modifiedr   make_normalizedr   r   r  r  r  )r   r8  r(  r:  r
  r;  r  r  r  r   r   r.  	attr_nameabstract_statusr3  need_refreshr4  arg1arg2rW  new_ret_typer   r  r   r    s   



"

z%ExpressionChecker.check_callable_callr   r>  c                 C  s   t jsdS |jD ]<}|j|}|du rq|j}t|tr |j}t|t	r(|j
}t|trD|jdurDt|jts:J tt |jj  S qdS )zIs the given attribute a method with a None-compatible return type?

        Overloads are only checked if there is an implementation.
        FN)rq   strict_optionalmror  rh  r   r   rR   implr>   funcrE   r   r   rt   r   r   )r   r   r>  r  symnoder   r   r   r   r/    s"   




z!ExpressionChecker.can_return_nonerb  r   c                   s>  t trttjdS t tr7tjj}t |tr#|j	dd}t
t|}t |tr5|j	g d}|S t trLt fdd D jS t tr~t
j }t
|}t |trk|j	d}|S t |tr|tfdd|jD }|S t trtjjd	krt S j  ttjS )
zzAnalyze the callee X in X(...) where X is Type[item].

        Return a Y that we can pass to check_call(Y, ...).
        
source_anyT)r*  r  c                   s   g | ]
} t| qS r   )r"  r   r   rw  r
  r   r   r   r/        z>ExpressionChecker.analyze_type_type_callee.<locals>.<listcomp>r(  c                   s   g | ]}|j  d qS )r(  r<  r   c)rb  r   r   r/    s    builtins.tuple)r   r   r   from_another_anyr   r   r   r   r   r<  r   r!   r   r  r   r   r"  r!  r   r   r   r   r   r   unsupported_type_typer	  )r   rb  r
  r  expandedr8  r   )r
  rb  r   r   r"    s<   








z*ExpressionChecker.analyze_type_type_calleec                 C  s<   g }|D ]}|  |}t|r|t  q|| q|S )zInfer argument expression types in an empty context.

        In short, we basically recurse on each argument without considering
        in what context the argument was called.
        )r  has_erased_componentr  r   )r   r(  r  argr  r   r   r    infer_arg_types_in_empty_context  s   
z2ExpressionChecker.infer_arg_types_in_empty_contextIterator[None]c                 c  s2    t j}t|rdt _z	d V  W |t _d S |t _w NT)r   infer_unionsr   )r   r   oldr   r   r   allow_unions   s   zExpressionChecker.allow_unionsc           
   
   C  s   dgt | }t|D ]4\}}|D ]-}||  s>| |j|  | || |j| ||< W d   n1 s9w   Y  qqt|D ]\}}	|	sS| || ||< qDtdd |D s_J ttt	 |S )a  Infer argument expression types using a callable type as context.

        For example, if callee argument 2 has type List[int], infer the
        argument expression with List[int] type context.

        Returns the inferred types of *actual arguments*.
        Nc                 s  r  r   r   rK  r   r   r   r   *  r  z?ExpressionChecker.infer_arg_types_in_context.<locals>.<genexpr>)
r<  r  is_starr\  rW  r  r   r
   r   r   )
r   r8  r(  r:  r3  r  r*  r  air   r   r   r   r9    s   z,ExpressionChecker.infer_arg_types_in_contextcallableerror_contextc           	      C  s   | j d }|s	|S t|t }|j}t|r"t|r"t|}t|}t|tr3t|s3t	|s3|
 S t| ||}g }|D ]}t|sIt|rO|d q?|| q?| j|||ddS )a  Unify callable return type to type context to infer type vars.

        For example, if the return type is set[t] where 't' is a type variable
        of callable, and if the context is set[int], return callable modified
        by substituting 't' with 'int'.
        r   NTskip_unsatisfied)r   r   r   r   r   r   r   r   r   r   r<  r%   type_var_idshas_uninhabited_componentrU  r  apply_generic_arguments)	r   r_  r`  ctx
erased_ctxr   r(  new_argsrV  r   r   r   r3  -  s(   
	
z=ExpressionChecker.infer_function_type_arguments_using_contextc                 C  sh  | j  r| j  | ||||}W d   n1 sw   Y  | |j|t|}g }t|D ]\}	}
||	 dkrB|	d q2|	|
 q2t
|||||  | j  d}d|v rg| ||||||\}}|jdkrt|dkrt|v szt|v rt|d }t|ttfr| d|d< n|rt| d|s| j tj| nttjgt|j }| |||S )zInfer the type arguments for a generic callee type.

        Infer based on the types of arguments.

        Return a derived callable type that has the arguments applied.
        Nr   )r
  strictr"  r  r   builtins.str)r   r  r   r  r9  get_arg_infer_passesrW  r<  r  r  r$   argument_infer_context#infer_function_type_arguments_pass2special_sigr,   r/   r   r   r   r   r   rt   ru  r   &KEYWORD_ARGUMENT_REQUIRES_STR_KEY_TYPEr   r   unannotatedr  apply_inferred_arguments)r   rX  r(  r:  r3  r
  rW  arg_pass_nums
pass1_argsr*  rV  inferred_args	first_argr   r   r   r$   y  sJ   
	
z/ExpressionChecker.infer_function_type_argumentsold_inferred_argsSequence[Type | None]&tuple[CallableType, list[Type | None]]c                 C  sx   t |}tt|D ]\}}	t|	ttfst|	rd||< q
| |||}| ||||}
t	||
||| 
 d}||fS )a^  Perform second pass of generic function type argument inference.

        The second pass is needed for arguments with types such as Callable[[T], S],
        where both T and S are type variables, when the actual argument is a
        lambda with inferred types.  The idea is to infer the type variable T
        in the first pass (based on the types of other arguments).  This lets
        us infer the argument and return type of the lambda expression and
        thus also the type variable S in this second pass.

        Return (the callee with type vars applied, inferred actual arg types).
        Nr
  )r  r  r   r   r   r   rU  re  r9  r$   rl  )r   rX  r(  r:  r3  rv  r
  rt  r*  rV  rW  r   r   r   rm    s   z5ExpressionChecker.infer_function_type_arguments_pass2r#   c                 C  s   t | jd| jdS )Ntyping.Mappingtyping.Iterable)r#   r   r   r   r   r   r   rl    s   z(ExpressionChecker.argument_infer_contextnum_actualsint	list[int]c                 C  sB   dg| }t |D ]\}}|t r|| D ]}d||< qq	|S )a  Return pass numbers for args for two-pass argument type inference.

        For each actual, the pass number is either 1 (first pass) or 2 (second
        pass).

        Two-pass argument type inference primarily lets us infer types of
        lambdas more effectively.
        r   r"  )r  r  ArgInferSecondPassQuery)r   rW  r3  r|  r  r*  rV  r.  r   r   r   rk    s   

z&ExpressionChecker.get_arg_infer_passesrt  c                 C  sT   t |D ]\}}|rt|r"| j||d | ttjgt| }q| |||S )zApply inferred values of type arguments to a generic function.

        Inferred_args contains the values of function type arguments.
        r   )	r  rU  r   could_not_infer_type_argumentsr   r   r	  r<  re  )r   rX  rt  r
  r*  inferred_typer   r   r   rq    s   
z*ExpressionChecker.apply_inferred_argumentsactual_typesactual_kindsactual_namesContext | Nonec	                 C  sr  |du rt ttj}i }	|D ]}
|
D ]}|	|dd |	|< qq| |||||	|\}}t|jD ]\}}| rn|| sn|sn|	 r\| j
||| |r[|r[d|v r[| ||| n|j| pbd}| j
||| d}q2| st|| ||r| j stt||| d  tr| j
||| d}q2| r|| r||| d  tjtjfvr| j
|| d}q2|S )aO  Check that there is a value for all required arguments to a function.

        Also check that there are no duplicate values for arguments. Report found errors
        using 'messages' if it's not None. If 'messages' is given, 'context' must also be given.

        Return False if there were any errors. Otherwise return True
        Nr   r   r   ?F)r_   r   r   r   rh   check_for_extra_actual_argumentsr  r:  is_requiredis_positionalr   too_few_argumentsmissing_classvar_callable_noter;  missing_named_argumentr]  is_duplicate_mappingr   r  r   r   r   duplicate_argument_valueis_namedr   r,   r/   too_many_positional_arguments)r   r8  r  r  r  r3  r
  r  r  all_actualsr  aokis_unexpected_arg_errorr*  kindargnamer   r   r   r:    sL   

z&ExpressionChecker.check_argument_countr  dict[int, int]tuple[bool, bool]c                 C  s4  d}d}t |D ]\}	}
|	|vrO|
tjkst||	 rO|
tjkrOd}|
tjkr/| j|| q|s5J d||	 }|dus?J ||	 }| j|||| d}q|
tjkrZtj|j	vs_|
tjkrt
||	 }t|ttfr||	dt|jk r|
tjkst|ts| j|| n
| j||| d}d}q||fS )zCheck for extra actual arguments.

        Return tuple (was everything ok,
                      was there an extra keyword argument error [used to avoid duplicate errors]).
        FTz/Internal error: named kinds without names givenNr   )r  r   r.   is_non_empty_tupler/   r,   r   too_many_argumentsunexpected_keyword_argumentr:  r   r   r   r   rh  r<  r   "too_many_arguments_from_typed_dict)r   r8  r  r  r  r  r
  r  r  r*  r  act_nameact_typeactual_typer   r   r   r  U  s:   


z2ExpressionChecker.check_for_extra_actual_argumentsc                 C  s   t |tr8t |tr:|jddd\}}|j|}|d ur<t |jtr>|jjs@|jj	sB| j
d| d| d S d S d S d S d S d S d S )Nr   r   )maxsplitr  zN" is considered instance variable, to make it class variable use ClassVar[...])r   r   r   r   r   rh  r   rh   rp  is_classvarr   note)r   r  r  r
  r  var_namer   r   r   r   r    s   
z0ExpressionChecker.missing_classvar_callable_note	check_argArgChecker | Nonec	                 C  s   |p| j }t|  }	t|D ]h\}
}|D ]a}|| }|du r q|| }|tjkr5| |s5| j|| |tj	krP| 
|sPt|| jd}| j||| |	|||j|
 |j|
 }||||| |j|
 |d |
d |||| |
 qqdS )zCheck argument types against a callable type.

        Report errors if the argument types are not compatible.

        The check_call docstring describes some of the arguments.
        Nrz  r   )r  r   rl  r  r   r.   is_valid_var_argr   invalid_var_argr/   is_valid_keyword_var_argrt   r   r   invalid_keyword_var_argexpand_actual_typer;  r:  rW  )r   rW  r:  r(  r8  r3  r
  r  r  mapperr*  r  r  r  actual_kind
is_mappingexpanded_actualr   r   r   r;    s@   
z&ExpressionChecker.check_argument_typescaller_typeoriginal_caller_typecaller_kindr3   nmouter_contextc              
   C  s  t |}t |}t |}t|tr| j||	 dS t|trOt|trO| rO| j	s2| j
rOt|jtrO|jjj	sB|jjj
rO| jjsO| j||	 dS t||| jjds| j||grbdS | jj|||||||	|
d}| jj|||	|d | j|||	 dS dS )z.Check the type of a single argument in a call.)rG  N)r  r
  r  r  )r   r   r   r   deleted_as_rvaluer   r   ra  rr  r,  rs  rb  r   r   r   allow_abstract_callconcrete_only_callrt   rG  should_suppress_optional_errorincompatible_argumentincompatible_argument_notecheck_possible_missing_await)r   r  r  r  rX  r  r  r8  r  r
  r  r  r   r   r   r    sT   

	
zExpressionChecker.check_argr   c                   s  |  } |} ||||}	d}
d}d}t fdd|D rhz# j   |	|||||||}W d   n1 s?w   Y  W n tyP   d}Y nw |rht| \}}t	t
||j|j |f} |	|||||||}|dur|durt|d |d rtt|d ts|S |S |dur|S |dur|S  |	|||||}
t|
dkr|
d }nttj} j|st|sd}ntj} jj||||d  j|||||||d}|r jtj | |S )	z(Checks a call to an overloaded function.NFc                 3  rl  r   )
real_unionr   rV  r   r   r   r     rn  z8ExpressionChecker.check_overload_call.<locals>.<genexpr>Tr   r  r  r  )!r)  rW  plausible_overload_call_targetsr2  r   r  union_overload_resultr   r  r   r  r   r  combine_function_signaturesinfer_overload_return_typert   r   r   r   overload_erased_call_targetsr<  r   r	  r   r  is_operator_methodr  OPERATORno_variant_matches_argumentsr  ru  r   TOO_MANY_UNION_COMBINATIONS)r   r8  r(  r:  r;  r  r  r
  rW  plausible_targetserased_targetsunioned_resultunion_interruptedunioned_returnreturnsinferred_typesinferred_resultr  r  r   r   r   r   r    s   
	

	z%ExpressionChecker.check_overload_callr   list[CallableType]c              
     s   ddd}g }g }d}d}	t | D ]\}
}|
tkr ||s d}|
tkr*||s*d}	q|jD ]J}t|||j|j fd	d
}| j - | 	| |||dri|rY|j
rY|| n|	rd|jrd|| n|| W d   n1 ssw   Y  q.|| S )a/  Returns all overload call targets that having matching argument counts.

        If the given args contains a star-arg (*arg or **kwarg argument), this method
        will ensure all star-arg overloads appear at the start of the list, instead
        of their usual location.

        The only exception is if the starred argument is something like a Tuple or a
        NamedTuple, which has a definitive "shape". If so, we don't move the corresponding
        alternative to the front since we can infer a more precise match using the original
        order.rV  r   r   r   c                 S  s.   t | } t| tpt| tpt| to| jjS r   )r   r   r   r   r   r   is_named_tuplerV  r   r   r   	has_shape  s   
zDExpressionChecker.plausible_overload_call_targets.<locals>.has_shapeFTc                       |  S r   r   r)  rW  r   r   r,        zCExpressionChecker.plausible_overload_call_targets.<locals>.<lambda>NrV  r   r   r   )r  r.   r/   r   r   r:  r;  r   r  r:  
is_var_argr  	is_kw_arg)r   rW  r:  r;  r   r  matchesstar_matchesargs_have_var_argargs_have_kw_argr  rV  r3  r   r  r   r    s8   




z1ExpressionChecker.plausible_overload_call_targetsr  tuple[Type, Type] | Nonec	                 C  s  g }	g }
g }t tt|}g }|D ]f}| j| jju sJ | j +}| j }| j|||||||d\}}W d   n1 s@w   Y  W d   n1 sOw   Y  |  }|rw|sc||f  S |		| |
	| |	| |	| qt
|	dkrdS t|	|
|||rt|
r| j|d  |
d |d fS tdd |
D r| j|d  t|
d t|d fS | jttj||||||dS | j|d  |
d |d fS )a  Attempts to find the first matching callable from the given list.

        If a match is found, returns a tuple containing the result type and the inferred
        callee type. (This tuple is meant to be eventually returned by check_call.)
        If multiple targets match due to ambiguous Any parameters, returns (AnyType, AnyType).
        If no targets match, returns None.

        Assumes all of the given targets have argument counts compatible with the caller.
        )r8  r(  r:  r;  r
  r  r  Nr   c                 S  s   g | ]}t |qS r   )r   r   rV  r   r   r   r/    r  z@ExpressionChecker.infer_overload_return_type.<locals>.<listcomp>)r2  maphas_any_typer   r   r  r  r  has_new_errorsr  r<  any_causes_overload_ambiguityall_same_typesstore_typesr   r   r   r   )r   r  r(  rW  r:  r;  r  r  r
  r  return_typesr  args_contain_any	type_mapsrV  wr  r   
infer_typeis_matchr   r   r   r    sd   




z,ExpressionChecker.infer_overload_return_typec           	   	   C  s0   g }|D ]}|  ||||||r|| q|S )zReturns a list of all targets that match the caller after erasing types.

        Assumes all of the given targets have argument counts compatible with the caller.
        )erased_signature_similarityr  )	r   r  rW  r:  r;  r(  r
  r  rV  r   r   r   r  	  s   
z.ExpressionChecker.overload_erased_call_targetsr   levellist[tuple[Type, Type]] | Nonec
                 C  s  |	t krtt|D ]\}
}| |r n,q
| || | ||||||||}W d   n1 s3w   Y  |dur?|gS dS | || | ||||||||}W d   n1 s^w   Y  |durutt|d tt	fsu|gS t||
 }t|tsJ g }|
 D ]%}| }|||
< | |||||||||	d 	}|dur|| q dS t }g }|D ]}||vr|| || q|S )a  Accepts a list of overload signatures and attempts to match calls by destructuring
        the first union.

        Return a list of (<return type>, <inferred variant type>) if call succeeds for every
        item of the desctructured union. Returns None if there is no match.
        Nr   r   )r   r   r  r  type_overrides_setr  r   r   r   r   r  copyr  r  r   r   r  )r   r  r(  rW  r:  r;  r  r  r
  r  idxrV  r  directfirst_union	res_itemsrb  new_arg_types
sub_resultseenr   pairr   r   r   r  	  s~   


z'ExpressionChecker.union_overload_resultrV  c                 C  s"   t |}t|tot| dkS rq  )r   r   r   r<  r  r   rV  r   r   r   r  }	  s   zExpressionChecker.real_unionexprsSequence[Expression]	overridesSequence[Type]c              	   c  sh    t |t |ksJ t||D ]	\}}|| j|< qzdV  W |D ]}| j|= q!dS |D ]}| j|= q,w )z5Set _temporary_ type overrides for given expressions.N)r<  r  r   )r   r  r  r   rV  r   r   r   r  	  s   

z$ExpressionChecker.type_overrides_setr   AnyType | CallableTypec                 C  s  |sJ dt |}tdd |D sttjS ttt |}t|dkr)|d S t	|\}}dd t
t|d jD }t|d j}g }d}|D ]M}t|t|jkrYd	} n?tt||jD ]\}	\}
}|
|krlqa|
 ry| ryt||	< qad	} |r nt|jD ]\}	}||	 | q||j qJt|}|rttj}|d j||gttgd
d
g||d	dS g }|D ]}t|}|| q|d j||||d	dS )aL  Accepts a list of function signatures and attempts to combine them together into a
        new CallableType consisting of the union of all of the given arguments and return types.

        If there is at least one non-callable type, return Any (this can happen if there is
        an ambiguity because of Any in arguments).
        zTrying to merge no callablesc                 s  r%  r   r   r   rO  r   r   r   r   	  rn  z@ExpressionChecker.combine_function_signatures.<locals>.<genexpr>r   r   c                 S  r  r   r   r  r   r   r   r/  	  r-  zAExpressionChecker.combine_function_signatures.<locals>.<listcomp>FTN)rW  r:  r;  r   r  rI  )rW  r:  r   r  rI  )r   r   r   r   r   r
   r	   r   r<  #merge_typevars_in_callables_by_namerF  rW  r  r:  r  r  r  r-   r  r   r   r<  r.   r/   )r   r   	callablesr  rh  	new_kindsnew_returnstoo_complexr  r*  new_kindtarget_kindrV  union_returnr2  
final_args	args_listnew_typer   r   r   r  	  sd   


	z-ExpressionChecker.combine_function_signaturesc           	   	     s   t |||j|j fdd}| j  | | |||ds'	 W d   dS W d   n1 s1w   Y  ddd}z| j ||||||d W dS  tyT   Y dS w )z^Determine whether arguments could match the signature at runtime, after
        erasing types.c                   r  r   r   r)  r  r   r   r,  	  r  z?ExpressionChecker.erased_signature_similarity.<locals>.<lambda>NFr  r   original_ccaller_typer  r3   rX  r  r}  r  r8  r   r  r~  r
  r=   r  r   r   c
           
      S  s   t | |std S r   )arg_approximate_similarityr   )
r  r  r  rX  r  r  r8  r  r
  r  r   r   r   r  	  s   
z@ExpressionChecker.erased_signature_similarity.<locals>.check_arg)r
  r  T)r  r   r  r   r  r3   rX  r   r  r}  r  r}  r8  r   r  r~  r
  r=   r  r=   r   r   )r   r:  r;  r   r  r:  r;  r   )	r   rW  r:  r;  r(  r8  r
  r3  r  r   r  r   r  	  s4   
	z-ExpressionChecker.erased_signature_similarityrb  c                 C  s   t j||| jj||dS )z=Simple wrapper around mypy.applytype.apply_generic_arguments.ra  )r   re  r   incompatible_typevar_value)r   r_  r   r
  rb  r   r   r   re  
  s   z)ExpressionChecker.apply_generic_argumentsc                 C  sL   |  | t|}t|trttj|dttj|dfS ttjttjfS )NrI  )rW  r   r   r   r   rR  r   )r   r(  r8  r   r   r   r  '
  s   

z%ExpressionChecker.check_any_type_callc                   s^   j    fdd| D }W d    n1 sw   Y  tdd |D |fS )Nc              	     s   g | ]} | qS r   )r  )r   subtyper:  r;  r(  r
  r   r   r   r/  ;
  s    z6ExpressionChecker.check_union_call.<locals>.<listcomp>c                 S  r  r   r   )r   r  r   r   r   r/  @
  r  )r   disable_type_namesr  r   )r   r8  r(  r:  r;  r
  resultsr   r  r   r  2
  s   z"ExpressionChecker.check_union_callr  c                 C  s*   | j jt| | ||}| ||S )z'Visit member expression (of form e.id).)r   r   r   r   analyze_ordinary_member_accessr   )r   r   r  r   r   r   r   visit_member_exprB
  s   z#ExpressionChecker.visit_member_exprc                 C  sz   |j dur
| |S | j|j| jd}|j}d}t|tr't|jtr'|jj	}t
|j|||dd| j|| j|  |d}|S )z+Analyse member expression or member lvalue.Nr   F)r  r   r  module_symbol_table)r  r   r  r   r   r   rV   r   rM   r  r   r  r   r   r  )r   r   r  r  r  r  member_typer   r   r   r  H
  s*   

z0ExpressionChecker.analyze_ordinary_member_access	base_typec                 C  s$   t |||ddd| j|| j|  d
S )z}Analyse member access that is external, i.e. it cannot
        refer to private definitions. Return the result type.
        Fr  r   r  )r   r   r   r  )r   rY  r  r
  r   r   r   analyze_external_member_accessf
  s   z0ExpressionChecker.analyze_external_member_accessc                 C  s   t | jd S )Nr   )r   r   r   r   r   r   r  z
     z$ExpressionChecker.is_literal_contextrZ  r   fallback_namec                 C  s:   |  |}|  rt||dS |jt|||j|jddS )az  Analyzes the given literal expression and determines if we should be
        inferring an Instance type, a Literal[...] type, or an Instance that
        remembers the original literal. We...

        1. ...Infer a normal Instance in most circumstances.

        2. ...Infer a Literal[...] if we're in a literal context. For example, if we
           were analyzing the "3" in "foo(3)" where "foo" has a signature of
           "def foo(Literal[3]) -> None", we'd want to infer that the "3" has a
           type of Literal[3] instead of Instance.

        3. ...Infer an Instance that remembers the original Literal if we're declaring
           a Final variable with an inferred type -- for example, "bar" in "bar: Final = 3"
           would be assigned an Instance that remembers it originated from a '3'. See
           the comments in Instance's constructor for more details.
        )rZ  r4  )rZ  r4  r   r  )r  )r   r  r   r<  r   r  )r   rZ  r  rV  r   r   r   r  }
  s   
z)ExpressionChecker.infer_literal_expr_typeleftr   rightc                 C  s   t |j|j | ddS )z$Concatenate two fixed length tuples.rQ  )r   r4  )r   r   r   )r   r  r  r   r   r   concat_tuples
  s   zExpressionChecker.concat_tuplesrH   c                 C     |  |jdS )z(Type check an integer literal (trivial).builtins.intr  rZ  r   r   r   r   r   visit_int_expr
     z ExpressionChecker.visit_int_exprr\   c                 C  r   )z&Type check a string literal (trivial).rj  r"  r#  r   r   r   visit_str_expr
  r%  z ExpressionChecker.visit_str_exprr7   c                 C  r   )z%Type check a bytes literal (trivial).builtins.bytesr"  r#  r   r   r   visit_bytes_expr
  r%  z"ExpressionChecker.visit_bytes_exprrD   c                 C  
   |  dS )z%Type check a float literal (trivial).zbuiltins.floatr   r#  r   r   r   visit_float_expr
     
z"ExpressionChecker.visit_float_exprr;   c                 C  r)  )zType check a complex literal.zbuiltins.complexr*  r#  r   r   r   visit_complex_expr
  r,  z$ExpressionChecker.visit_complex_exprrA   c                 C  r)  )zType check '...'.zbuiltins.ellipsisr*  r#  r   r   r   visit_ellipsis
  r,  z ExpressionChecker.visit_ellipsisrQ   c           
      C  s`  |j dks
|j dkr| ||S |j dkr t|jtr | |S |j dkrJt|jtr;| jjj	dkr;| j
|j|jS t|jtrJ| j
|j|jS | |j}t|}t|tr|j dkr|jjd}|r|jdkrt| |j}t|tr|jjd	}|d
u r| ||S |j tjv rtj|j  }| j|||j|dd\}}	|	|_|S td|j  )z(Type check a binary operator expression.andor*%)   r   +__add__zbuiltins.tuple.__add____radd__NTallow_reversezUnknown operator )opcheck_boolean_opr   r  rK   check_list_multiplyr7   r   rG  python_versionr   check_str_interpolationr  r\   r  r   r   partial_fallbackr   rh  r   r  r   
op_methodscheck_opmethod_typeRuntimeError)
r   r   	left_typeproper_left_typeleft_add_methodproper_right_typeright_radd_methodmethodr   rA  r   r   r   visit_op_expr
  s2   


zExpressionChecker.visit_op_exprr:   c              	   C  s  d}d}t |j|jdd |jD ]S\}}}| |}d}|dks&|dkr| |}	|	du r4| |}	| jjdd}
| jd|	|gtg|d\}}W d   n1 sTw   Y  | 	 }| j
|	}t|	trin|
 r| |	r| j
|\}}t|gtjgdg| 	 | d	}t||s| jd||	| n|
 s|r| j|||	d
r| j||d| n| j|
  n|tjv rtj| }t| jj}| j||||dd\}}W d   n1 sw   Y  | s|dv r| |}	t|dst|	dst|}t|	}	| ||	r| j||	d| n5|dks&|dkrJ| |}	| 	 }t|}t|	}	| ||	rG| j||	d| d}ntd| |j !| |du r_|}qt"#||}q|dusmJ |S )zType check a comparison expression.

        Comparison expressions are type checked consecutive-pair-wise
        That is, 'a < b > c == d' is check as 'a < b and b > c and c == d'
        Nr   inznot inTsave_filtered_errors__contains__rH  r  r(  r:  r
  r   )original_container	containerr7  )z==z!=__eq__equalityiszis notidentityzUnknown comparison operator )$r  operandsr   r  find_partial_type_ref_fast_pathr   r  check_method_call_by_namer-   	bool_typer   analyze_container_item_typer   r   r  r  analyze_iterable_item_typer   r   r   rt   unsupported_operand_typesdangerous_comparison
add_errorsfiltered_errorsr?  r   errorsr@  r}   r[  rB  method_typesr  r   
join_types)r   r   r   r  r  r  operatorrC  rA  
right_typelocal_errorsr  	cont_typeitertyperH  r  r   r   r   visit_comparison_expr
  s   &


	






z'ExpressionChecker.visit_comparison_exprc                 C  sZ   t |tsdS t |jtr+| |j|}t |tr+|jdur+| j|| j	| |S dS )zIf expression has a partial generic type, return it without additional checks.

        In particular, this does not generate an error about a missing annotation.

        Otherwise, return None.
        N)
r   rV   r   rh   r   r   r   r   r   fixup_partial_type)r   r   r   r   r   r   rV  L  s   
z1ExpressionChecker.find_partial_type_ref_fast_pathrO  c                 C  s6  | j jjsdS t||f\}}| j j rdS t|ts!t|tr#dS t|tr=t|tr=t	|}t	|}t||f\}}|rIt
|rIt
|rIdS t|tr{t|tr{|jjtv r{|jjtv r{| j d}t||}t||}t|jd |jd  S t|trt|trt|jtrt|jtrdS t||dd S )a  Check for dangerous non-overlapping comparisons like 42 == 'no'.

        The original_container is the original container type for 'in' checks
        (and None for equality checks).

        Rules:
            * X and None are overlapping even in strict-optional mode. This is to allow
            'assert x is not None' for x defined as 'x = None  # type: str' in class body
            (otherwise mypy itself would have couple dozen errors because of this).
            * Optional[X] and Optional[Y] are non-overlapping if X and Y are
            non-overlapping, although technically None is overlap, it is most
            likely an error.
            * Any overlaps with everything, i.e. always safe.
            * Special case: b'abc' in b'cde' is safe.
        Fztyping.AbstractSetr   )ignore_promotions)r   rG  strict_equalityr   rR  !is_unreachable_warning_suppressedr   r   r   r   has_bytes_componentr   r   r   r   lookup_typeinfor'   r(   r(  r   rZ  r   )r   r  r  rO  abstract_setr   r   r   r\  \  s:   


z&ExpressionChecker.dangerous_comparisonrH  r  c                 C  sf   |p|}t |}t|tr| ||||||S t|||ddd| j|| j|  d
}| ||||||S )zType check a call to a named method on an object.

        Return tuple (result type, inferred method type). The 'original_type'
        is used for error messages.
        FTr  )	r   r   r   check_union_method_call_by_namer   r   r   r  check_method_call)r   rH  r  r(  r:  r
  r  rA  r   r   r   rW    s&   
z+ExpressionChecker.check_method_call_by_namec              
   C  s|   g }g }|  D ]-}	| j  | ||	||||\}
}W d   n1 s&w   Y  ||
 || qt|t|fS )a  Type check a call to a named method on an object with union type.

        This essentially checks the call using check_method_call_by_name() for each
        union item and unions the result. We do this to allow plugins to act on
        individual union items.
        N)r  r   r  rW  r  r   )r   rH  r  r(  r:  r
  r  r  meth_resrV  rb  	meth_itemr   r   r   ro    s   

z1ExpressionChecker.check_union_method_call_by_namerA  c           	      C  sH   |  ||}|dur|nd}| j||||||d}| j||||||dS )zType check a call to a method with the given name and type on an object.

        Return tuple (result type, inferred method type).
        Nr'  r  )rf  r
  r  )	r   r_  r  rA  r(  r:  r
  r  r  r   r   r   rp    s   z#ExpressionChecker.check_method_callop_namerC  	left_exprrc  
right_exprc              
     s  d fdd}ddd}t |}t |}t|tr%ttj|d}	|	|	fS t|tr5ttj|d}	|	|	fS tj| }
|||}||
|}|tjv rUt||rU|||fg}n5t	||rt|t
rt|t
r|jj|jur||||||
kr|||f|||fg}n
|||f|||fg}dd |D }g }g }|D ]<\}}}jjdd}||||gtg }W d    n1 sw   Y  | r||  || q|  S t|t
r|jjst|t
r|jjrttj}	|	|	fS |s)jjdd}|||gtg }W d    n	1 sw   Y  | r'||  || n|S j|d  t|dkr<|d S ttj}||f}|S )Nrs  r\  r  r   r   r~  c                   sr    || sdS j "}t| |ddd| jj d
}| r&dn|W  d   S 1 s2w   Y  dS )zYLooks up the given operator and returns the corresponding type,
            if it exists.NFT)
r  rV  r  r  r  r  r
  r   r   r  )
has_memberr   r  r   r   r  r  )rs  r  r  rY  rL  r   r   lookup_operator  s"   $z>ExpressionChecker.check_op_reversible.<locals>.lookup_operatorrV  r   r>  r`  c                 S  s(   | j jD ]}|j|r|j  S qdS )a  Returns the name of the class that contains the actual definition of attr_name.

            So if class A defines foo and class B subclasses A, running
            'get_class_defined_in(B, "foo")` would return the full name of A.

            However, if B were to override and redefine foo, that method call would
            return the full name of B instead.

            If the attr name is not present in the given class or its MRO, returns None.
            N)r   rE  r  rh  r   )rV  r>  clsr   r   r   lookup_definer   s
   
z=ExpressionChecker.check_op_reversible.<locals>.lookup_definerrI  c                 S  s$   g | ]\}}}|d ur|||fqS r   r   )r   r9  objrV  r   r   r   r/  s  s   $ z9ExpressionChecker.check_op_reversible.<locals>.<listcomp>TrK  r   r   )rs  r\  r  r   r   r~  )rV  r   r>  r\  r   r`  )r   r   r   r   rR  r   reverse_op_methodsop_methods_that_shortcutrs   rt   r   r   alt_promoter   r  rp  r-   r  r  r^  r-  r   rW  r]  r<  r	  )r   rs  rC  rt  rc  ru  r
  rw  ry  any_typerev_op_nameleft_opright_opvariants_rawvariantsr_  r  rH  rz  rV  rd  r   	error_anyr   rL  r   check_op_reversible  sv   	










z%ExpressionChecker.check_op_reversiblerV  r8  c                   sd  |r&|g}t |}t|trdd t| D }| |}g }g }	| j (}
|D ]}| j||t	| d|| d\}}|
| |	
| q+W d   n1 sSw   Y  |
 sht|}t|	}||fS ||fg}t |}t|tr fddt| D }g }g }	| jjdd/}
|D ]$}|D ]\}}| j||t	| d|| d\}}|
| |	
| qqW d   n1 sw   Y  |
 r| j|
  |
 d	 }t	t }|j|_|j|_t|d
krt|d
kr| j| n!t|d
kr	| jjd||d nt|d
kr| jjd||d t|}| |	}||fS | j|||gtg dS )zType check a binary operation which maps to a method call.

        Return tuple (result type, inferred operator method type).
        c                 S  s   g | ]}|qS r   r   rm  r   r   r   r/    s    z.ExpressionChecker.check_op.<locals>.<listcomp>ry  )rs  rC  rt  rc  ru  r
  Nc                   s   g | ]
}|t | d fqS )ry  )r_   rm  ry  r   r   r/    rM  TrK  r   r"  LeftRightrN  )r   r   r   r   r  r  r   r  r  r_   r  r  r   r]  r^  r   r   r  r<  "warn_both_operands_are_from_unionswarn_operand_was_from_unionr  rW  r-   )r   rH  r  rV  r
  r8  left_variantsrc  all_resultsall_inferredrd  left_possible_typer   inferredresults_finalinferred_finalright_variantsright_possible_typeru  errrecent_contextr   ry  r   r@    s   















zExpressionChecker.check_opc                 C  s  | j d }| |j|}t| |j|d}|jdv sJ |jr&di }}n&|jr/i d}}n|jdkr>| j|j\}}n|jdkrL| j|j\}}t	j
| jjjv rd|du rd|jsd| j|j|j | j ry|du ry|jsy| j|j|j | jj|du d | ||j|}W d   n1 sw   Y  |du r|du rt S |du r|dusJ |S |du r|dusJ |S |jdkrt|}	|j }
n|jdkrt|}	|j }
t|	tr|S |
r|S t|	|gS )z/Type check a boolean operation ('and' or 'or').r   r  )r/  r0  Nr/  r0  )r  )r   r  r  r   r9  right_alwaysright_unreachabler   find_isinstance_checkr  REDUNDANT_EXPRrG  enabled_error_codesr   redundant_left_operand should_report_unreachable_issuesunreachable_right_operandr  r  analyze_cond_branchr   r   can_be_truer   can_be_falser   r   )r   r   r
  rf  rC  expanded_left_typeleft_map	right_maprc  restricted_left_typeresult_is_leftr   r   r   r:    s^   
	





z"ExpressionChecker.check_boolean_opc                 C  s`   |  |j}t|| dr| j |j| jd d}n|  |j}| d||j|\}}||_|S )zzType check an expression of form '[...] * e'.

        Type inference is special-cased for this common construct.
        r!  r   )r   __mul__)r  r  rt   r   r  r   r@  rA  )r   r   rc  rC  r   rA  r   r   r   r;  m  s   z%ExpressionChecker.check_list_multiplyr5   c                 C  sJ   |  |j}| j|j|j | j| | j|j| | |j |S r   )r  rZ  r   check_assignmentr  check_finalr   rV  )r   r   rZ  r   r   r   visit_assignment_expr}  s   z'ExpressionChecker.visit_assignment_exprrg   c                 C  sP   |  |j}|j}|dkr|  }|S tj| }| ||g g |\}}||_|S )z7Type check an unary operation ('not', '-', '+' or '~').not)r  r   r9  rX  r   unary_op_methodsrW  rA  )r   r   operand_typer9  r   rH  rA  r   r   r   visit_unary_expr  s   
z"ExpressionChecker.visit_unary_exprrG   c                 C  sD   |  |}| ||}t|}|  r t|tr |jdur |j}|S )zgType check an index expression (base[index]).

        It may also represent type application.
        N)visit_index_expr_helperr   r   r  r   r   r  )r   r   r   p_resultr   r   r   visit_index_expr  s   

z"ExpressionChecker.visit_index_exprc                 C  s*   |j r	| |j S | |j}| ||S r   )r  r  r  visit_index_with_type)r   r   rC  r   r   r   r    s   z)ExpressionChecker.visit_index_expr_helperProperType | Nonec           
        s   j }t|}| t|tr&p|t fdd| D ddS t|trj	 rt|t
r;||S |}|durg }|D ]3}|dk rU|t|j7 }d|  krbt|jk rmn n	||j|  qHjtj  ttj  S t|S ||S t|tr| j S t|tr| r| jr|  j  S t|tr|j ds!|j  S j"d| j gt#g d\}}	|	 _$|S )	zAnalyze type of an index expression for a given type of base expression.

        The 'original_type' is used for error messages (currently used for union types).
        c                      g | ]	} | qS r   )r  r  r   r  r   r   r   r/    s    z;ExpressionChecker.visit_index_with_type.<locals>.<listcomp>F)contract_literalsNr   __getitem__)r  )%r  r   r  r   r   r   r  r   r   r  rZ   visit_tuple_slice_helpertry_getting_int_literalsr<  r   r  ru  r   TUPLE_INDEX_OUT_OF_RANGEr   r   r	  nonliteral_tuple_index_helperr   visit_typeddict_index_exprr   ra  rr  is_enumvisit_enum_index_exprr   rv  r!  r  rW  r-   rA  )
r   rC  r   r  r  nsoutr  r   rA  r   r  r   r    sX   





z'ExpressionChecker.visit_index_with_typeslicrZ   c                 C  s   d g}d g}d g}|j r| |j }|d u r| ||S |}|jr3| |j}|d u r1| ||S |}|jrH| |j}|d u rF| ||S |}g }	t|||D ]\}
}}|	||
|| qQt	|	S r   )
begin_indexr  r  	end_indexstride	itertoolsproductr  slicer   )r   rC  r  beginendr  	begin_rawend_raw
stride_rawr   br   sr   r   r   r    s,   z*ExpressionChecker.visit_tuple_slice_helperr  list[int] | Nonec                 C  s   t |tr	|jgS t |tr!|jdkr!|j}t |tr!d|j gS t| |}t |tr5|j	dur5|j	}t |t
rDt |jtrD|jgS t |trig }t|jD ]}t |t
rdt |jtrd||j qP dS |S dS )a  If the given expression or type corresponds to an int literal
        or a union of int literals, returns a list of the underlying ints.
        Otherwise, returns None.

        Specifically, this function is guaranteed to return a list with
        one or more ints if one one the following is true:

        1. 'expr' is a IntExpr or a UnaryExpr backed by an IntExpr
        2. 'typ' is a LiteralType containing an int
        3. 'typ' is a UnionType containing only LiteralType of ints
        -r   N)r   rH   rZ  rg   r9  r   r   r  r   r  r   r}  r   r   r   r  )r   r  operandrV  r  rb  r   r   r   r    s(   




z*ExpressionChecker.try_getting_int_literalsc                 C  s@   | j d||gtg|d t|j}t|tr| jd|gS |S )Nr  ry  rQ  )rW  r-   r   r   r   rZ   r   r  )r   rC  r  unionr   r   r   r  $  s
   

z/ExpressionChecker.nonliteral_tuple_index_helpertd_typec           
      C  s  t |tr
|jg}nQt| |}t |trt|j}n|g}g }t|D ]5}t |t	r4|j
d ur4|j
}t |trMt |jtrM|jjjdkrM||j q%| j|| ttj  S g }|D ] }|j|}	|	d u rz| j||| ttj  S ||	 q_t|S )Nr'  )r   r\   rZ  r   r  r   r  r   r   r   r  r   r\  r4  r   r   r  r   $typeddict_key_must_be_string_literalr   r   r	  rh  typeddict_key_not_foundr   )
r   r  r  	key_namesrV  	key_typesr  value_typeskey_namer  r   r   r   r  ,  s4   



z,ExpressionChecker.visit_typeddict_index_expr	enum_typec                 C  s.   |  d}| j| |||dd t|g S )Nrj  zEnum index should be a stringzactual index type)r   r   check_subtyper  r   )r   r  r  r
  string_typer   r   r   r  O  s   

z'ExpressionChecker.visit_enum_index_exprr9   c                 C  s   | j |jttjddd}|j}| jj}|jr)t	t
|ts)||kr)| j|| |jr8t|r8| jd|| t|| jj| jj| j|d |S )zType check a cast expression.Tr   r  r5  zTarget type of castry  )r  r   r   r   r   r   r   rG  warn_redundant_castsr   r   r   redundant_castdisallow_any_unimportedry   unimported_type_becomes_anyrw   is_typeshed_stub)r   r   source_typetarget_typerG  r   r   r   visit_cast_expr\  s(   z!ExpressionChecker.visit_cast_exprr4   c                 C  s>   | j |j| jd ddd}|j}t||s| j||| |S )Nr   Tr  )r  r   r   r   rs   r   assert_type_fail)r   r   r  r  r   r   r   visit_assert_type_exprs  s   
z(ExpressionChecker.visit_assert_type_exprrW   c                 C  s   |j tkr3|jdusJ | j|j| jd dd}| jjs1| j||j | j	 s1| j
d|j |S | jjsM|jdurDdd |jD ni }| j|| t S )z$Type check a reveal_type expression.Nr   Tr   r  z9'reveal_type' always outputs 'Any' in unchecked functionsc                 S  s   i | ]}|j |jqS r   )r  r   )r   var_noder   r   r   
<dictcomp>  s    z7ExpressionChecker.visit_reveal_expr.<locals>.<dictcomp>)r  r2   r   r  r   r   r  r   reveal_typer  r  local_nodesreveal_localsr   )r   r   revealed_typenames_to_typesr   r   r   visit_reveal_expr  s&   


z#ExpressionChecker.visit_reveal_exprtapprb   c                 C  s6  t |jtrgt |jjtrgt|jj|j| jj|jjj	|}t
|}t |tr5t|j| j}| ||j|S t |trP|jjjrPt|jj| j}| ||jj|S t |trZ| |S | jtj| ttjS t
| |j}t |ttfr| s| jtj| | ||j|S t |trttj|dS ttj S )a  Type check a type application (expr[type, ...]).

        There are two different options here, depending on whether expr refers
        to a type alias or directly to a generic class. In the first case we need
        to use a dedicated function typeanal.expand_type_aliases. This
        is due to the fact that currently type aliases machinery uses
        unbound type variables, while normal generics use bound ones;
        see TypeAlias docstring for more details.
        rI  )!r   r   rV   r   r   rx   r   r   ru  r  r   r   r   r   r    apply_type_arguments_to_callabler(  r   r>  r  r   r  r   ONLY_CLASS_APPLICATIONr   r   r	  r  r   r   ra  rR  r   )r   r  rb  rw  r   r   r   visit_type_application  s.   






z(ExpressionChecker.visit_type_applicationaliasra   c                 C  s   | j |j|j|ddS )a  Right hand side of a type alias definition.

        It has the same type as if the alias itself was used in a runtime context.
        For example, here:

            A = reveal_type(List[T])
            reveal_type(A)

        both `reveal_type` instances will reveal the same type `def (...) -> builtins.list[Any]`.
        Note that type variables are implicitly substituted with `Any`.
        Tr   )r  r   r  )r   r  r   r   r   visit_type_alias_expr  s   z'ExpressionChecker.visit_type_alias_exprr   r   r  rf  r   c                C  s   t |jtr|jjrttjS | jjj	o| j
}tt||j|j|| jjd}t |tr>t|j| j}|r6|S | ||j|S t |trTt|jjdkrTtt|j| jS t |tr^| |S t |trjttj|dS |rqttjS | dS )a  Get type of a type alias (could be generic) in a runtime expression.

        Note that this function can be called only if the alias appears _not_
        as a target of type application, which is treated separately in the
        visit_type_application method. Some examples where this method is called are
        casts and instantiation:

            class LongName(Generic[T]): ...
            A = LongName[int]

            x = A()
            y = cast(A, ...)
        )disallow_anyru  rQ  rI  r   )r   r  r   invalidr   r   r	  r   rG  disallow_any_genericsr   r   r{   r   r  r   ru  r   r   r   r  r(  r   r   r   r   r  rR  r   )r   r  r  rf  r   r  rb  rw  r   r   r   r    s0   






z/ExpressionChecker.alias_type_in_runtime_contextrw  c                   s   t |}t|tr+t|jt kr$jt|jt  ttj	S 
| S t|tra|jD ]}t|jt krRjt|jt  ttj	  S q3t fdd|jD S ttjS )a{  Apply type arguments to a generic callable type coming from a type object.

        This will first perform type arguments count checks, report the
        error as needed, and return the correct kind of Any. As a special
        case this returns Any for non-callable types, because if type object type
        is not callable, then an error should be already reported.
        c                   r  r   )re  r   r(  rf  r   r   r   r/    s    zFExpressionChecker.apply_type_arguments_to_callable.<locals>.<listcomp>)r   r   r   r<  r  r   incompatible_type_applicationr   r   r	  re  r   r   r   )r   rw  r(  rf  r   r   r  r   r    s   





z2ExpressionChecker.apply_type_arguments_to_callablerK   c                 C     |  |ddS )z#Type check a list expression [...].r  <list>check_lst_exprr#  r   r   r   visit_list_expr   r%  z!ExpressionChecker.visit_list_exprrY   c                 C  r  )Nr   z<set>r  r#  r   r   r   visit_set_expr$  r  z ExpressionChecker.visit_set_exprListExpr | SetExpr | TupleExprcontainer_fullnamec           	      C  s   | j d }|r	dS | j|d}|durt|tr|S dS g }|jD ]}t|tr2t | j|<  dS || 	| q"t
|}t|sLt | j|< dS | j||g}|| j|< |S )aY  
        Fast path to determine the type of a list or set literal,
        based on the list of entries. This mostly impacts large
        module-level constant definitions.

        Limitations:
         - no active type context
         - no star expressions
         - the joined type of all entries must be an Instance or Tuple type
        r   N)r   r   rh  r   r   r   r[   r   r  r  r   r0  r   r   r  )	r   r   r  rf  rtr  rb  vtctr   r   r   fast_container_type'  s&   




z%ExpressionChecker.fast_container_typetagc              	   C  s   |  ||}|r
|S tdddg |  }t|gtjgd g| j||g| d||gd}| 	|dd |j
D dd |j
D |d }t|S )	NTr   r   r  r  c                 S  s    g | ]}t |tr|jn|qS r   )r   r[   r   r1  r   r   r   r/  _  s     z4ExpressionChecker.check_lst_expr.<locals>.<listcomp>c                 S  s"   g | ]}t |trtjntjqS r   )r   r[   r   r.   r-   r1  r   r   r   r/  `  s   " r   )r  r   r  r   r   r.   r   r  r   r  r   r   )r   r   r   r  r   tvconstructorr  r   r   r   r  I  s,   	z ExpressionChecker.check_lst_exprr`   c                   sp  t | jd }d}t|tr& fddt|jD }t|dkr%|d }n	 t|tr/|j}n|rKt|t	rKt|t
s=J |jrK|jd gt j }g }d}tt jD ]Q} j| }t|tr| |j}	t |	}	t|	tr||	j |t|	j7 }qV|  dd  S |r|t|kr| |}	n| ||| }	|d7 }||	 qVttj}
t|| jd|
gS )	zType check a tuple expression.r   Nc                   s8   g | ]}t |trt|jt jkst|tr|qS r   )r   r   r<  r   r   r   r   r   r   r   r   r/  k  s    z6ExpressionChecker.visit_tuple_expr.<locals>.<listcomp>r   r   rQ  z<tuple>)r   r   r   r   r   r   r<  r   r   r   r   r(  rF  r[   r  r   r  r  r  r   r   r   r   r  )r   r   r   type_context_itemstuples_in_contextr   r.  r*  rb  ttfallback_itemr   r  r   visit_tuple_expre  sB   







z"ExpressionChecker.visit_tuple_exprc                 C  sX  | j d }|r	dS | j|d}|durt|tr|S dS g }g }d}|jD ]E\}}|du r[t| |}	t|	trR|	jj	dkrRt
|	jdkrR|	jd |	jd f}q&t | j|<  dS || | || | q&t|}
t|}t|
r~t|st | j|< dS |r|d |
ks|d |krt | j|< dS | jd|
|g}|| j|< |S )a~  
        Fast path to determine the type of a dict literal,
        based on the list of entries. This mostly impacts large
        module-level constant definitions.

        Limitations:
         - no active type context
         - only supported star expressions are other dict instances
         - the joined types of all keys and values must be Instance or Tuple types
        r   Nr  r"  r   r   )r   r   rh  r   r   r   r   r  r   r   r<  r(  r   r  r   r0  r   r   r  )r   r   rf  r  r  r  stargsr  rZ  stktr  dtr   r   r   fast_dict_type  s@   



z ExpressionChecker.fast_dict_typec              
   C  s  |  | jd |}|r%| j|||dd}t|}t|tr!| S | S | |}|r.|S g }g }|jD ]/\}}	|du rC|	|	 q5t
||	g}
|jdkrW|j|
_|j|
_n|	j|
_|	j|
_|	|
 q5tdddg |  }tdddg |  }d}|s|stt||g| dgtjgdg| jd	||g| d
d||gd}| ||tjgt| |d }n	 |r|D ]B}|du rt| jd||ggtjgdg| jd	||g| d
d||gd}| ||gtjg|d }q| d||gtjg| q|dusJ |S )zoType check a dict expression.

        Translate it into a call to dict(), with provisions for **expr.
        r   N)r8  r  r
  r}  r   KTVTrQ  r  r   z<dict>r  rz  r  r   )find_typeddict_contextr   r  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   typeddict_contextr  r   r  r(  r  r  rZ  tupr  r  rvr
  rV  r   r   r   visit_dict_expr  sn   	



"		z!ExpressionChecker.visit_dict_expr	dict_exprTypedDictType | Nonec                 C  s   t |}t|tr|S t|trEg }|jD ]}| ||}|d ur-| |||r-|| qt|dkr8|d S t|dkrE| j	
|| d S )Nr   r   )r   r   r   r   r   r  r  r  r<  r   typeddict_context_ambiguous)r   r
  r  r   rb  item_contextr   r   r   r  #  s"   



z(ExpressionChecker.find_typeddict_contextrI   c                 C  s"  | j j|dd | |\}}|s]| j jttj | j j	|" |j
j
dd D ]}|| j  q+| j| dd}W d   n1 sGw   Y  | d}| j j  t|||S | j j|j | j j||d | j | s~| j| dd | j | }| j j  t||S )	zType check lambda expression.F)body_is_trivialNr   Tr  r   )type_override)r   check_default_argsinfer_lambda_type_using_contextr  r  r   r   r   r  push_functionbodyr  r   r   popr|   r   check_func_itemrK  rL  replace_callable_return_type)r   r   r  r$  stmtr   r4  r   r   r   visit_lambda_expr:  s&   


z#ExpressionChecker.visit_lambda_expr/tuple[CallableType | None, CallableType | None]c                 C  s  t | jd }t|tr!dd t| D }t|dkr!|d }|r(t|ts*dS t t|t	 }t|ts9J |j
dur@dS dd |jD }|jsQ| durg|jd	ttjgt| ||jdd d
}t|v sot|v rs|dfS |j|kr| jtj| dS ||fS )zTry to infer lambda expression type using context.

        Return None if could not infer type.
        The second item in the return type is the type_override parameter for check_func_item.
        r   c                 S  s   g | ]	}t |tr|qS r   r   r  r   r   r   r/  f  s
    
zEExpressionChecker.infer_lambda_type_using_context.<locals>.<listcomp>r   r   NNNc                 S  s   g | ]}|j qS r   )r  r  r   r   r   r/  }  s    F)is_ellipsis_argsrW  r:  r;  )r   r   r   r   r   r  r<  r   r   r   r  	argumentsr0  r4  r<  r   r   r   r;  r.   r/   r:  r   ru  r   CANNOT_INFER_LAMBDA_TYPE)r   r   rf  r  callable_ctxr:  r   r   r   r&  Z  s6   	



z1ExpressionChecker.infer_lambda_type_using_contextr]   c           
      C  sl  |  |}t|tr|\}}n|S t|}|du r&| jtj| tt	j
S t|}|du r;| jtj| tt	j
S |j}z||}W n ty[   | jtj| tt	j
 Y S w t||d krq| jtj| tt	j
S ||d d D ]8}	|j|	jv s|	|d kr|jr|jjr|	|d krtt	j  S t|j|ddd||	|| j| j|  d  S qyJ d)z+Type check a super expression (non-lvalue).Nr   r   FT)r  rV  r  r  r  r  override_infor
  r   r   r  rS  )_super_arg_typesr   tupletype_info_from_typer   ru  r   UNSUPPORTED_ARG_1_FOR_SUPERr   r   r	  UNSUPPORTED_ARG_2_FOR_SUPERrE  r  
ValueError!SUPER_ARG_2_NOT_INSTANCE_OF_ARG_1r<  TARGET_CLASS_HAS_NO_BASE_CLASSr  r  re  r-  r   r   r   r  )
r   r   r   	type_typeinstance_type	type_infoinstance_inforE  r  r  r   r   r   visit_super_expr  sR   






z"ExpressionChecker.visit_super_exprType | tuple[Type, Type]c                 C  s  | j  s
ttjS t|jjdkr\|jsttj	S | j j
 r-| j tj| ttj	S t|j}t|}| j j
 }|dusBJ |jrO|jd jjpM|}nu| j tj| ttj	S t|jjv ro| j tj| ttj	S t|jjthkr| j tj| ttj	S t|jjdkr| j tj| ttj	S t|jjdkrt| |jjd }| |jjd }n| j tj| ttj	S t |tr|jrtt|j}n&ttj!|dS t |tr|j"}t |tr|jrtt|j}nttj!|dS t |tst |t#r|$ s| j%&|| ttj	S t|}t |tr5|jr.t|j}||fS ttj!|dS t |trZ|j"}t |trZ|jrStt|j}||fS ttj!|dS ||fS )a*  
        Computes the types of the type and instance expressions in super(T, instance), or the
        implicit ones for zero-argument super() expressions.  Returns a single type for the whole
        super expression when possible (for errors, anys), otherwise the pair of computed types.
        r   Nr   r"  rI  )'r   r  r   r   rp  r<  callr(  re  r	  r  active_classru  r   %SUPER_OUTSIDE_OF_METHOD_NOT_SUPPORTEDr   r   top_functionr1  variabler   (SUPER_ENCLOSING_POSITIONAL_ARGS_REQUIREDr.   r:  SUPER_VARARGS_NOT_SUPPORTEDr   r-   SUPER_POSITIONAL_ARGS_REQUIRED#SUPER_WITH_SINGLE_ARG_NOT_SUPPORTEDr   r  TOO_MANY_ARGS_FOR_SUPERr   rR  rb  r   ra  r   %first_argument_for_super_must_be_type)r   r   current_typer=  rH  r>  	type_iteminstance_itemr   r   r   r5    sz   














z"ExpressionChecker._super_arg_typesc                 C  sP   t | d}|j|j|jfD ]}|r"| |}| j|||tj	 q| dS )Nr!  zbuiltins.slice)
rz   r   r  r  r  r  r   r  r   INVALID_SLICE_INDEX)r   r   expectedr  r   r   r   r   visit_slice_expr!  s   

z"ExpressionChecker.visit_slice_exprrJ   c                 C     |  |jddS )Nr  z<list-comprehension> check_generator_or_comprehension	generatorr#  r   r   r   visit_list_comprehension)     z*ExpressionChecker.visit_list_comprehensionrX   c                 C  rT  )Nr   z<set-comprehension>rU  r#  r   r   r   visit_set_comprehension.  rY  z)ExpressionChecker.visit_set_comprehensionrF   c                 C  sD   t |js
t|jrd}t g}nd}t t g}| j||d|dS )Nztyping.AsyncGeneratortyping.Generatorz<generator>)additional_args)r2  is_asyncrv   rt  r   rV  )r   r   rV  r\  r   r   r   visit_generator_expr3  s   
z&ExpressionChecker.visit_generator_exprgenrd  id_for_messagesr\  list[Type] | Nonec              
   C  s   |pg }| j jjdddB | | tdddg |  }|g}t|tjgdg| j 	||| | j 
d||gd}| ||jgtjg|d W  d   S 1 sRw   Y  dS )	z:Type check a generator expression or a list comprehension.Tr   can_skipfall_throughr  r   Nr   r  )r   rR  frame_contextcheck_for_compr   r  r   r   r-   r  r   r  rt  )r   r_  rd  r`  r\  r	  tv_listr
  r   r   r   rV  B  s    

	$z2ExpressionChecker.check_generator_or_comprehensionr@   c              
   C  s   | j jjdddS | | tdddg |  }tdddg |  }t||gtjtjgddg| j 	d	||g| j 
d
d||gd}| ||j|jgtjtjg|d W  d   S 1 s_w   Y  dS )z&Type check a dictionary comprehension.Tr   rb  r  r   r  r  Nr  r   z<dictionary-comprehension>r  )r   rR  re  rf  r   r  r   r   r-   r  r   r  r  rZ  )r   r   ktdefvtdefr
  r   r   r   visit_dictionary_comprehension]  s&   


	$z0ExpressionChecker.visit_dictionary_comprehension'GeneratorExpr | DictionaryComprehensionc                 C  s   t |j|j|j|jD ][\}}}}|r| j|\}}n| j|\}}| j||d| |D ]6}| 	| | j
|\}	}
|	rF| j|	 tj| jjjv re|	du rZ| jd| q/|
du re| jd| q/qdS )zCheck the for_comp part of comprehensions. That is the part from 'for':
        ... for x in y if z

        Note: This adds the type information derived from the condlists to the current binder.
        TNF)r  indices	sequences	condlistsr]  r    analyze_async_iterable_item_typerZ  analyze_index_variablesr  r  push_type_mapr  r  rG  r  r   $redundant_condition_in_comprehension)r   r   r  sequence
conditionsr]  r  sequence_type	conditiontrue_map	false_mapr   r   r   rf  s  s(   
z ExpressionChecker.check_for_compr<   c                 C  s@  |  |j | jd }| j|j\}}tj| jjjv r5|d u r)| j	
d|j n|d u r5| j	
d|j | j||j||d}tt|pF|}| j||j||d}tj|su|}	tt|	p`|	}
|d u sjt|
|st| j||j|
|d}n||kr||}	n
| j||j||d}	tt| jd trt||g}|S t||	}|S )Nr   FT)r
  r  )r  condr   r   r  r  r  rG  r  r   redundant_condition_in_ifr  if_exprr   r   	else_exprr=  r>  r  rr   r   r   r   r   ra  )r   r   r  rf  if_mapelse_mapif_typeif_type_fallbackfull_context_else_type	else_typeelse_type_fallbackr  r   r   r   visit_conditional_expr  sN   


z(ExpressionChecker.visit_conditional_exprr  dict[Expression, Type] | Nonec                 C  s~   | j jjddd, |d u r | j|||d t W  d    S | j | | j|||dW  d    S 1 s8w   Y  d S )NTr   rb  r  )r   rR  re  r  r   rq  )r   r  r   r
  r  r   r   r   r    s   $z%ExpressionChecker.analyze_cond_branchr5  r   c           	   
   C  s  || j v r
| j | S | j| | j}|| _zC|r&t|tr&| j|dd}n2|r5t|tr5| j|dd}n#|rDt|t	rD| j
|dd}n|rSt|trS| j|dd}n|| }W n" ty{ } zt|| jjj|j| jj| jj W Y d}~nd}~ww || _| j  |dusJ | j|| | jjjr|s| jjs| j rt|r| jjs| j|| | j r| jjrttj S |S )zType check a node in the given type context.  If allow_none_return
        is True and this expression is a call, allow it to return None.  This
        applies only to this expression and not any subexpressions.
        Tr  N)!r   r   r  r   r   r8   r  rj   visit_yield_from_exprr<   r  r6   visit_await_exprr  	Exceptionr   r   r_  filer   rG  r)  r   disallow_any_expris_stubr  r  r  r   disallowed_any_typer   r   rp  )	r   r   r   r  r5  r   old_is_calleerV  r  r   r   r   r    sT   




zExpressionChecker.acceptr  r   c                 C  s   | j |S )z}Return an instance type with type given by the name and no type
        arguments. Alias for TypeChecker.named_type.
        )r   r   )r   r  r   r   r   r   $  s   zExpressionChecker.named_typec                 C  sB   t |}t|tp t|| jdttjgp t|tp t|t	S )z$Is a type valid as a *args argument?r{  )
r   r   r   rt   r   r  r   r   r   r   r  r   r   r   r  *  s   
z"ExpressionChecker.is_valid_var_argc                 C  sJ   t || jd| dttjgp$t || jdt t gp$t|t	S )z'Is a type valid as a **kwargs argument?rz  rj  )
rt   r   r  r   r   r   r   r   r   r   r  r   r   r   r  7  s    z*ExpressionChecker.is_valid_keyword_var_argc                   s  t |}t|trt |j}t|trt|}t|tr|j}t|tr*|j	
 S t|tr:| r:|jj	
 S t|trAdS t|trVt fdd| D }|S t|tr|j}t|trht |j}t|trqt|}t|tr|j	jdur|j	j S t|trdS dS dS )z*Does type have member with the given name?Tc                 3  s    | ]	} | V  qd S r   )rv  )r   xrY  r   r   r   r   _  r   z/ExpressionChecker.has_member.<locals>.<genexpr>NF)r   r   r   r!  r   r   r   r4  r   r   has_readable_memberr   ra  r   r   r   r  r   rb  metaclass_typerv  )r   rV  rY  r   rb  r   r  r   rv  J  s8   











zExpressionChecker.has_memberc                 C  s   | j || dS )zCalled when we can't infer the type of a variable because it's not ready yet.

        Either defer type checking of the enclosing function to the next
        pass or report an error.
        N)r   r  )r   r  r
  r   r   r   not_ready_callbackq  s   z$ExpressionChecker.not_ready_callbackri   c                 C  s   | j jd }| j |d}|jd u r)tt|ttfs(| j  r(| j 	t
j| n| |j|}| j |||t
jdd | j |dS )Nr   Factual typeexpected type)r   r  get_generator_yield_typer   r   r   r   r   r  ru  r   YIELD_VALUE_EXPECTEDr  r  INCOMPATIBLE_TYPES_IN_YIELDget_generator_receive_type)r   r   return_typeexpected_item_typeactual_item_typer   r   r   visit_yield_expry  s&   
z"ExpressionChecker.visit_yield_exprr6   c                 C  s   | j d }|d ur| jd|g}t| |j|}t|tr&ttj	|dS | 
||tj}|s?tt|tr?| jjd | |S )Nr   typing.AwaitablerI  )r   r   r  r   r  r   r   r   r   rR  check_awaitable_exprr   INCOMPATIBLE_TYPES_IN_AWAITr   r   rU  )r   r   r  expected_typer  r  r   r   r   r    s   

z"ExpressionChecker.visit_await_exprr   str | ErrorMessageignore_binderc                 C  sv   | j || d||ddsttjS | d|g g |d }| j |d}t|}|s9t	|t
r9|js9| j j  |S )zyCheck the argument to `await` and extract the type of value.

        Also used by `async for` and `async with`.
        r  r  r  	__await__r   F)r   r  r   r   r   r   rW  get_generator_return_typer   r   r   rQ  rR  rS  )r   r   rf  r   r  rW  r   r   r   r   r    s   
z&ExpressionChecker.check_awaitable_exprrj   c                 C  s  | j jd }t| |j}t|trttj|d}nP| j 	|rMt
|r1t|s1| j j|| ttj}| j d|||g}| jd|g g |d\}}nt
|rUt|sc| j j|| ttj}n| ||tj}| j |d}	| j |d}
| j |
|	|tjdd t|}t|tr|jjdkr| j |d}nt|
}
t|
trttj|
d}nt }|stt|tr| j jd | |S )	Nr   rI  r[  __iter__ry  Fr  r  )r   r  r   r  r   r   r   r   rR  type_is_iterableis_async_defhas_coroutine_decoratorr   yield_from_invalid_operand_typer   r  rW  r	  r  r    INCOMPATIBLE_TYPES_IN_YIELD_FROMr  r  r   r   r   r  r   rU  )r   r   r  r  subexpr_type	iter_typer~  generic_generator_typer  r  r  	expr_typer   r   r   r    sP   





z'ExpressionChecker.visit_yield_from_exprr_   c                 C     |j S r   r   r#  r   r   r   visit_temp_node     z!ExpressionChecker.visit_temp_nodere   c                 C  
   t tjS r   r   r   r   r#  r   r   r   visit_type_var_expr  r   z%ExpressionChecker.visit_type_var_exprrS   c                 C  r  r   r  r#  r   r   r   visit_paramspec_expr  r   z&ExpressionChecker.visit_paramspec_exprrf   c                 C  r  r   r  r#  r   r   r   visit_type_var_tuple_expr  r   z+ExpressionChecker.visit_type_var_tuple_exprrP   c                 C  r  r   r  r#  r   r   r   visit_newtype_expr  r   z$ExpressionChecker.visit_newtype_exprrN   c                 C  sT   |j j}|r%| jjjrt|r| jd|| t|| jj| jj	| j|d t
tjS )NzNamedTuple typery  )re  
tuple_typer   rG  r  ry   r   r  rw   r  r   r   r   )r   r   r  r   r   r   visit_namedtuple_expr  s   
z'ExpressionChecker.visit_namedtuple_exprrB   c                 C  sf   t |j|jD ]&\}}|d ur-| |}tt|ts-|jj| j	}t|t
r-||_d|_qttjS rY  )r  r   r  r  r   r   r   re  r  r   rh   r   rp  r   r   )r   r   r  rZ  rV  r  r   r   r   visit_enum_call_expr  s   


z&ExpressionChecker.visit_enum_call_exprrc   c                 C  r  r   r  r#  r   r   r   visit_typeddict_expr"  r   z&ExpressionChecker.visit_typeddict_exprrU   c                 C  r  r   r  r#  r   r   r   visit__promote_expr%  r  z%ExpressionChecker.visit__promote_exprr[   r   c                 C  s   t | |jS r   )r   r  r   r#  r   r   r   visit_star_expr(     z!ExpressionChecker.visit_star_exprc                 C  r)  )zReturn instance type 'object'.r   r*  r   r   r   r   r  +  r,  zExpressionChecker.object_typec                 C  r)  )zReturn instance type 'bool'.r  r*  r   r   r   r   rX  /  r,  zExpressionChecker.bool_type
known_typec                 C     d S r   r   )r   r   r  r   r   r   r   3  s   z)ExpressionChecker.narrow_type_from_binderr  c                 C  r  r   r   )r   r   r  r  r   r   r   r   7  s   c                 C  sX   t |tkr*| jj|}|r*tt|tr| jjs*|r%t	||dds%dS t
||S |S )zNarrow down a known type of expression using information in conditional type binder.

        If 'skip_non_overlapping' is True, return None if the type and restriction are
        non-overlapping.
        T)prohibit_none_typevar_overlapN)r&   r1   r   rR  rh  r   r   r   r  r(   r)   )r   r   r  r  restrictionr   r   r   r   =  s   
)r   r   r   r+   r   ro   r   r   r   r   )r   rO   r   r   F)r   rV   r   r   r   r   )r  rh   r
  r=   r   r   )r   r8   r  r   r   r   )r  rC   r   r   )r   r8   r   r   )r  r   r_  r\  r   r`  r   rC   r   r   )rj  rk  r   r   )r8  r   r:  rz  r;  r{  r(  r|  r
  r=   r}  r~  r   r   )r  r?   r   r  )r8  r   r  r?   r
  r=   r   r   )
r8  r   r  r?   r
  r=   r}  r~  r   r   )re  rd   r   r   )r8  r   r   r   )
r8  r   r  r  r
  r=   r}  r~  r   r   )r   rL   r   r  )r  rV   r   r  )r   r8   r  r\  r  rh   r   r  )r8  r   r:  rz  rW  r  r;  r  r3  r  r(  r|  r   r\  r  r~  r
  r=   r   r   )r8  r   r(  r|  r:  rz  r;  r  r  r  r   r   )r8  r   r(  r|  r:  rz  r
  r=   r;  r  r  r  r   r   )r8  r   r(  r|  r:  rz  r
  r=   r;  r  r  r   r  r  r   r   r/  )r  r`  r8  r   r(  r|  r:  rz  r
  r=   r;  r  r  r~  r   r   r   )rX  r   r   r8   r  r`  r  r~  rY  r`  r   r   )r   r8   r  r   rY  r\  r   r   )NNNN)r8  r   r(  r|  r:  rz  r
  r=   r;  r  r  r  r  r`  r  r~  r   r  )r8  r   r(  r|  r:  rz  r
  r=   r;  r  r  r  r  r`  r  r~  r   r  )r   rd   r>  r\  r   r   )rb  r   r
  r=   r   r   )r(  r|  r   r  )r   r   r   rX  )
r8  r   r(  r|  r:  rz  r3  r  r   r  )r_  r   r`  r=   r   r   )rX  r   r(  r|  r:  rz  r3  r  r
  r=   r   r   )rX  r   r(  r|  r:  rz  r3  r  rv  rw  r
  r=   r   rx  )r   r#   )rW  r  r3  r  r|  r}  r   r~  )rX  r   rt  rw  r
  r=   r   r   )r8  r   r  r  r  rz  r  r  r3  r  r
  r  r  r~  r  r`  r   r   )r8  r   r  r  r  rz  r  r  r  r  r
  r=   r   r  )r  r   r  r\  r
  r=   r   r   )rW  r  r:  rz  r(  r|  r8  r   r3  r  r
  r=   r  r  r  r~  r   r   )r  r   r  r   r  r3   rX  r   r  r}  r  r}  r8  r   r  r~  r
  r=   r  r=   r   r   )r8  r   r(  r|  r:  rz  r;  r  r  r`  r  r~  r
  r=   r   r  )
rW  r  r:  rz  r;  r  r   r   r   r  )r  r  r(  r|  rW  r  r:  rz  r;  r  r  r`  r  r~  r
  r=   r   r  )r  r  rW  r  r:  rz  r;  r  r(  r|  r
  r=   r   r  r  )r  r  r(  r|  rW  r  r:  rz  r;  r  r  r`  r  r~  r
  r=   r  r}  r   r  r  )r  r  r  r  r   rX  )r   r  r   r  )rW  r  r:  rz  r;  r  r(  r|  r8  r   r
  r=   r   r   )
r_  r   r   rw  r
  r=   rb  r   r   r   )r(  r|  r8  r   r   r  )r8  r   r(  r|  r:  rz  r;  r  r
  r=   r   r  )r   rL   r  r   r   r   )rY  r\  r  r   r
  r=   r   r   )r   r   )rZ  r   r  r\  r   r   )r  r   r  r   r   r   )r   rH   r   r   )r   r\   r   r   )r   r7   r   r   )r   rD   r   r   )r   r;   r   r   )r   rA   r   r   )r   rQ   r   r   )r   r:   r   r   )r   rC   r   r~  )r  r   r  r   rO  r~  r   r   )rH  r\  r  r   r(  r|  r:  rz  r
  r=   r  r~  r   r  )rH  r\  r  r   r(  r|  r:  rz  r
  r=   r  r~  r   r  )r_  r\  r  r   rA  r   r(  r|  r:  rz  r
  r=   r   r  )rs  r\  rC  r   rt  rC   rc  r   ru  rC   r
  r=   r   r  )rH  r\  r  r   rV  rC   r
  r=   r8  r   r   r  )r   rQ   r
  r=   r   r   )r   r5   r   r   )r   rg   r   r   )r   rG   r   r   )rC  r   r   rG   r  r  r   r   )rC  r   r  rZ   r   r   )r  rC   r   r  )rC  r   r  rC   r   r   )r  r   r  rC   r   r   )r  rd   r  rC   r
  r=   r   r   )r   r9   r   r   )r   r4   r   r   )r   rW   r   r   )r  rb   r   r   )r  ra   r   r   )
r  r   r  r   rf  r=   r   r   r   r   )rw  r   r(  r  rf  r=   r   r   )r   rK   r   r   )r   rY   r   r   )r   r   r  r\  r   r~  )r   r   r   r\  r  r\  r   r   )r   r`   r   r   )r   r?   r   r~  )r   r?   r   r   )r
  r~  r  r?   r   r   )r   rI   r   r   )r   rI   r   r.  )r   r]   r   r   )r   r]   r   rB  )r   rZ   r   r   )r   rJ   r   r   )r   rX   r   r   )r   rF   r   r   )
r_  rF   rd  r\  r`  r\  r\  ra  r   r   )r   r@   r   r   )r   rk  r   r   )r   r<   r  r   r   r   )
r  r  r   rC   r
  r~  r  r   r   r   )NFFF)r   rC   r   r~  r  r   r5  r   r   r   r   r   )r  r\  r   r   )rV  r   rY  r\  r   r   )r  r\  r
  r=   r   r   )r   ri   r   r   )r   r6   r  r   r   r   )
r   r   rf  r=   r   r  r  r   r   r   )r   rj   r  r   r   r   )r   r_   r   r   )r   re   r   r   )r   rS   r   r   )r   rf   r   r   )r   rP   r   r   )r   rN   r   r   )r   rB   r   r   )r   rc   r   r   )r   rU   r   r   )r   r[   r   r   )r   r   )r   rC   r  r   r   r   )r   rC   r  r   r  r   r   r~  )r   r   r   r   __annotations__r   r   r   r   r   r  r  r  rP  rf  rT  rg  rN  rO  r9  r  r  r  r   r  r  r  r  r  rE  r  r  r  r  r   r  r
  rM  r  r  r  r/  r"  rW  r   r\  r9  r3  r$   rm  rl  rk  rq  r:  r  r  r;  r  r  r  r  r  r  r  r  r  r  re  r  r  r  r  r  r  r  r  r$  r&  r(  r+  r-  r.  rI  rg  rV  r\  rW  ro  rp  r  r@  r:  r;  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r-  r&  rA  r5  rS  rX  rZ  r^  rV  rj  rf  r  r  r  r   r  r  rv  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rX  r   r   r   r   r   r   r      s  
 	E
 &		A
'+G /3#j ,LH)@=217NaS: vJ+ 8qN>"#&5">/Q 9<RQ4'Er   Fr   ignore_in_type_objc                 C  s   |  t|S )zWhether t contains an Any type)r  
HasAnyType)r   r  r   r   r   r  V  r%  r  c                      s6   e Zd Zd fddZdd	d
Zd fddZ  ZS )r  r  r   r   r   c                   s   t  t || _d S r   )superr   r2  r  )r   r  	__class__r   r   r   \  s   
zHasAnyType.__init__r   r   c                 C  s   |j tjkS r   )type_of_anyr   r   r   r   r   r   r   	visit_any`  s   zHasAnyType.visit_anyr   c                   s   | j r	| r	dS t |S )NF)r  ra  r  visit_callable_typer  r  r   r   r  c  s   zHasAnyType.visit_callable_type)r  r   r   r   )r   r   r   r   r   r   r   r   )r   r   r   r   r  r  __classcell__r   r   r  r   r  [  s    
r  c                 C  s   t | } t| to| jjdkS )z;Whether t came from a function decorated with `@coroutine`.typing.AwaitableGenerator)r   r   r   r   r   r   r   r   r   r  i  s   r  c                 C  sP   t | } t| tr| jjdkrt| jdkrt | jd } t| to'| jjdkS )z9Whether t came from a function defined using `async def`.r     r3  ztyping.Coroutine)r   r   r   r   r   r<  r(  r   r   r   r   r  o  s   r  c                 C  s   t | } t| tot| jS r   )r   r   r   r   r   r   r   r   r   r    s   r  mappingr~  r  r  r  rz  c                   sX   t | dko+t | dko | d  tjko | d  tjk o+t fdd| D  S )Nr   r"  r   c                 3  s2    | ]} | t jkott| t V  qd S r   )r   r/   r   r   r   )r   r  r  r  r   r   r     s    
z'is_duplicate_mapping.<locals>.<genexpr>)r<  r   r.   r/   r   )r  r  r  r   r  r   r    s   r  rP  r   rC  c                 C  s   | j |dS )z>Return a copy of a callable type with a different return type.r(  rN  )rP  rC  r   r   r   r+    s   r+  c                      ,   e Zd ZdZd fddZdd	d
Z  ZS )r  zQuery whether an argument type should be inferred in the second pass.

    The result is True if the type has a type variable in a callable return
    type anywhere. For example, the result for Callable[[], T] is True if t is
    a type variable.
    r   r   c                      t  t d S r   r  r   r2  r   r  r   r   r     r  z ArgInferSecondPassQuery.__init__r   r   r   c                 C  s   |  |jp|t S r   )query_typesrW  r  HasTypeVarQueryr  r   r   r   r    s   z+ArgInferSecondPassQuery.visit_callable_typer  r  )r   r   r   r   r   r  r  r   r   r  r   r    s    r  c                      r  )r  zBVisitor for querying whether a type has a type variable component.r   r   c                   r  r   r  r   r  r   r   r     r  zHasTypeVarQuery.__init__r   r   r   c                 C     dS rY  r   r  r   r   r   visit_type_var     zHasTypeVarQuery.visit_type_varr  )r   r   r   r   )r   r   r   r   r   r  r  r   r   r  r   r        r  r~  c                 C     | d uo	|  t S r   )r  HasErasedComponentsQueryr   r   r   r   rU       rU  c                      r  )r  z<Visitor for querying whether a type has an erased component.r   r   c                   r  r   r  r   r  r   r   r     r  z!HasErasedComponentsQuery.__init__r   r   r   c                 C  r  rY  r   r  r   r   r   visit_erased_type  r  z*HasErasedComponentsQuery.visit_erased_typer  )r   r   r   r   )r   r   r   r   r   r  r  r   r   r  r   r    r  r  c                 C  r  r   )r  HasUninhabitedComponentsQueryr   r   r   r   rd    r  rd  c                      r  )r  zEVisitor for querying whether a type has an UninhabitedType component.r   r   c                   r  r   r  r   r  r   r   r     r  z&HasUninhabitedComponentsQuery.__init__r   r   r   c                 C  r  rY  r   r  r   r   r   visit_uninhabited_type  r  z4HasUninhabitedComponentsQuery.visit_uninhabited_typer  )r   r   r   r   )r   r   r   r   r   r  r  r   r   r  r   r    r  r  r  r  c                   sP  t   t t trt  ttrtddd}ttr.t tttfr.dS | r8|r8dS t trJtfdd	 	 D S ttr\t fd
d		 D S t t
rntt
rhdS t jS ttrt tr{ j t tr jd j t trt  t trj jjv rdS tt tS )a  Return if caller argument (actual) is roughly compatible with signature arg (formal).

    This function is deliberately loose and will report two types are similar
    as long as their "shapes" are plausibly the same.

    This is useful when we're doing error reporting: for example, if we're trying
    to select an overload alternative and there's no exact match, we can use
    this function to help us identify which alternative the user might have
    *meant* to match.
    rV  r   r   r   c                 S  s2   t | tpt | to|  pt | to| jjdkS r  )r   r   r   ra  r   r   r   r  r   r   r   is_typetype_like  s
   
z4arg_approximate_similarity.<locals>.is_typetype_likeTc                 3  s    | ]}t | V  qd S r   r  rm  )r  r   r   r     rn  z-arg_approximate_similarity.<locals>.<genexpr>c                 3  s    | ]}t  |V  qd S r   r  rm  )r  r   r   r     rn  r   N)rV  r   r   r   )r   r   r   r~   r   r   r   r   r2  r  r   r  r4  r   r   r   r   r   rE  rt   r   r   )r  r  r  r   )r  r  r   r    s<   











r  r   r  r  rW  r:  r;  r  c                   s   t |rdS fdd| D }tD ]B\ }t|ddrW fddt|D }g }g }	|D ]\}
}| |
 }||j |D ]
}|	|j|  q@q0t |	sWt |sW dS qdS )a  May an argument containing 'Any' cause ambiguous result type on call to overloaded function?

    Note that this sometimes returns True even if there is no ambiguity, since a correct
    implementation would be complex (and the call would be imprecisely typed due to Any
    types anyway).

    Args:
        items: Overload items matching the actual arguments
        arg_types: Actual argument types
        arg_kinds: Actual argument kinds
        arg_names: Actual argument names
    Fc              
     s(   g | ]}t  |j|jfd dqS )c                   r  r   r   r)  r  r   r   r,  6  r  z:any_causes_overload_ambiguity.<locals>.<listcomp>.<lambda>)r   r:  r;  rm  )r:  r;  rW  r   r   r/  4  s    z1any_causes_overload_ambiguity.<locals>.<listcomp>T)r  c                   s$   g | ]\}}|  r||  fqS r   r   )r   item_idxlookup)arg_idxr   r   r/  @  s    
)r  r  r  r  r   rW  )r   r  rW  r:  r;  actual_to_formalr  matching_formals_unfilteredmatching_returnsmatching_formalsr  formalsmatched_callabler  r   )r  r:  r;  rW  r   r    s,   
r  r   c                   s.   t  dkrdS t fdd dd  D S )Nr   Tc                 3  s    | ]
}t | d  V  qdS )r   N)rs   r  r   r   r   r   [  s    z!all_same_types.<locals>.<genexpr>r   )r<  r   r  r   r  r   r  X  s   r  r  Sequence[CallableType],tuple[list[CallableType], list[TypeVarType]]c                 C  s   g }i }g }| D ]C}|  rFt|}i }|jD ]&}|j}||vr6t|tr&qt|ts-J |||< || || ||j< qt	t
t||}|| q||fS )a<  Takes all the typevars present in the callables and 'combines' the ones with the same name.

    For example, suppose we have two callables with signatures "f(x: T, y: S) -> T" and
    "f(x: List[Tuple[T, S]]) -> Tuple[T, S]". Both callables use typevars named "T" and
    "S", but we treat them as distinct, unrelated typevars. (E.g. they could both have
    distinct ids.)

    If we pass in both callables into this function, it returns a list containing two
    new callables that are identical in signature, but use the same underlying TypeVarType
    for T and S.

    This is useful if we want to take the output lists and "merge" them into one callable
    in some way -- for example, when unioning together overloads.

    Returns both the new list of callables and a list of all distinct TypeVarType objects used.
    )r1  r"   r  r   r   r   r   r  r8  r
   r   r    )r  r   unique_typevarsr  r  renamer	  r  r   r   r   r  ^  s&   


r  rV  c                 C  s&   t | } t| tr| jdur| jS | S )z>If possible, get a more precise literal type for a given type.N)r   r   r   r  r  r   r   r   r[    s   r[  r   rC   c                 C  sR   t | tr| j}t |to|jtv S t | tr'| j}t |to&t t	|j
tS dS )z-Returns 'true' if the given node is a LiteralF)r   rG   r  rV   r   r   rO   r   r   r   r  r   )r   r  
underlyingr   r   r   rA    s   


rA  c                 C  sL   t | } ddh}t| trtdd | jD S t| tr$| jj|v r$dS dS )z?Is this one of builtin byte types, or a union that contains it?r'  zbuiltins.bytearrayc                 s  s    | ]}t |V  qd S r   )rl  r  r   r   r   r     r  z&has_bytes_component.<locals>.<genexpr>TF)r   r   r   r2  r   r   r   r   )rV  
byte_typesr   r   r   rl    s   
rl  TypeInfo | Nonec                 C  sl   t | } t| tr|  r|  S t| tr| j} t| tr#t | j} t| t	r,t
| } t| tr4| jS dS )zXGets the TypeInfo for a type, indirecting through things like type variables and tuples.N)r   r   r   ra  rr  r   rb  r   r!  r   r   r   r   r  r   r   r   r7    s   




r7  r   r`  c                 C  sD   | d u rdS |  dd }|tj v p!|tj v p!|tj v S )NFr   r   )splitr   r?  r  r{  r  )r   
short_namer   r   r   r    s   r  PartialType | Nonec                 C  s$   | d u st | tr| jd u rd S | S r   )r   r   r   r   r   r   r   r    s   r  )r   r   r   r   )r   rV   r   r   r  )r   r   r  r   r   r   )r   r   r   r   )r  r~  r  r  r  rz  r   r   )rP  r   rC  r   r   r   )r   r~  r   r   )r  r   r  r   r   r   )r   r  r  r  rW  r  r:  rz  r;  r  r   r   )r   r  r   r   )r  r  r   r  )rV  r   r   r   r  r  )rV  r   r   r  )r   r`  r   r   )r   r~  r   r  )r   
__future__r   r  
contextlibr   typingr   r   r   r   r   r	   r
   typing_extensionsr   r   r   r   mypy.checkerr=  mypy.errorcodes
errorcodesr  r   r   r   r   r   r   r   mypy.argmapr   r   r   mypy.checkmemberr   r   mypy.checkstrformatr   mypy.erasetyper   r   r   mypy.errorsr   r   mypy.expandtyper    r!   r"   
mypy.inferr#   r$   r%   mypy.literalsr&   mypy.maptyper'   	mypy.meetr(   r)   mypy.message_registryr*   mypy.messagesr+   
mypy.nodesr,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   rf   rg   rh   ri   rj   mypy.pluginrk   rl   rm   rn   ro   mypy.semanal_enumrp   
mypy.staterq   mypy.subtypesrr   rs   rt   ru   mypy.traverserrv   mypy.typeanalrw   rx   ry   rz   r{   mypy.typeopsr|   r}   r~   r   r   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   r   r   r   r   mypy.typestater   mypy.typevarsr   	mypy.utilr   mypy.visitorr   r}  r   r  r   r   r  r   r   r   r   r   r  	TypeQueryr   r  r  r  r  r  r+  r  r  rU  r  rd  r  r  r  r  r  r[  rA  rl  r7  r  r  r   r   r   r   <module>   s    $$
 B8#

                                      











?
:

-



