
    *eu                   
   U d Z ddlmZ ddlZddlmZ ddlmZmZ ddl	m
Z
mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ ddlmZ ddlZddlmZ m!Z!m"Z"m#Z# dd	l$m%Z%m&Z&m'Z' dd
l(m)Z)m*Z*m+Z+m,Z,m-Z- ddl.m/Z/ ddl0m1Z1 ddl2m3Z3m4Z4m5Z5 ddl6m7Z7m8Z8m9Z9m:Z: ddl;m<Z<m=Z=m>Z> ddl?m@Z@mAZAmBZB ddlCmDZDmEZEmFZFmGZG ddlHmIZI ddlJmKZKmLZLmMZM ddlNmOZO ddlPmQZQmRZRmSZSmTZTmUZUmVZVmWZWmXZX ddlYmZZZm[Z[ ddl\m]Z]m^Z^m_Z_m`Z`maZambZbmcZcmdZdmeZemfZfmgZgmhZhmiZimjZjmkZkmlZlmmZmmnZnmoZompZpmqZqmrZrmsZsmtZtmuZumvZvmwZwmxZxmyZymzZzm{Z{m|Z|m}Z}m~Z~mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ ddlmZmZmZ ddlmZmZ ddlmZmZ ddlmZmZ ddlmZ ddlmZ ddlmZmZmZ ddlmZmZ dd lmZ dd!lmZ dd"lmZmZmZmZmZmZmZmZmZ dd#lmZmZmZ dd$lmZ dd%lmZmZmZ dd&lmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ dd'lmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z mZmZmZmZmZmZmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZ dd(lmZmZ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"m#Z#  ed-          Z$d.Z%d/e&d0<   d1Z'd/e&d2<   ee~eeevf         Z(d3e&d4<   ee~eef         Z)d3e&d5<    G d6 d7e          Z* G d8 d9e          Z+eeeyef                  Z,d3e&d:<    G d; d<e          Z- G d= d>e          Z. G d? d@e#d         e          Z/ G dA dBe          Z0e	 dddK            Z1eddM            Z1	 dddOZ1ddUZ2ddZZ3dd]Z4dd^Z5dd_Z6dd`Z7ddcZ8ddeZ9dddjZ:dddlZ;	 dddoZ<ddqZ=ddtZ>ddvZ?ddzZ@ G d{ d|eԦ          ZAddZBddZCddZDddZEddZFddZGddZHddZIdddZJ G d de          ZK G d de          ZLddZM G d d          ZN ed          ZO ed          ZP G d deeOePf                   ZQddZRddZSddZTddZUddZVddZWddZXddZYddZZddZ[ddZ\ddZ]ddZ^ G dÄ deЦ          Z_dS )zMypy type checker.    )annotationsN)defaultdict)contextmanagernullcontext)AbstractSetCallableDictFinalGenericIterableIteratorMapping
NamedTupleOptionalSequenceTupleTypeVarUnioncastoverload)	TypeAlias)
errorcodesmessage_registrynodes	operators)ConditionalTypeBinderFrameget_declaration)MemberContext$analyze_decorator_or_funcbase_accessanalyze_descriptor_accessanalyze_member_accesstype_object_type)PatternChecker)SUPERTYPE_OF)
erase_typeerase_typevars!remove_instance_last_known_values)TYPE_VARUNUSED_AWAITABLEUNUSED_COROUTINE	ErrorCode)ErrorsErrorWatcherreport_internal_error)expand_self_typeexpand_typeexpand_type_by_instance)Keyextract_var_from_literal_hashliteralliteral_hash)map_instance_to_supertype)is_overlapping_erased_typesis_overlapping_types
meet_types)ErrorMessage)SUGGESTED_TEST_FIXTURESMessageBuilderappend_invariance_notesformat_typeformat_type_bareformat_type_distinctlymake_inferred_type_note
pretty_seq)MroErrorcalculate_mro)L	ARG_NAMEDARG_POSARG_STARCONTRAVARIANT	COVARIANTFUNC_NO_INFOGDEFIMPLICITLY_ABSTRACT	INVARIANTIS_ABSTRACTLDEFLITERAL_TYPEMDEFNOT_ABSTRACT
AssertStmtAssignmentExprAssignmentStmtBlock	BreakStmt	BytesExprCallExprClassDefComparisonExprContextContinueStmt	DecoratorDelStmtEllipsisExpr
ExpressionExpressionStmt	FloatExprForStmtFuncBaseFuncDefFuncItemIfStmtImport	ImportAll
ImportBase
ImportFrom	IndexExprIntExpr
LambdaExprListExprLvalue	MatchStmt
MemberExprMypyFileNameExprNodeOperatorAssignmentStmtOpExprOverloadedFuncDefPassStmtPromoteExpr	RaiseStmtRefExpr
ReturnStmtStarExpr	StatementStrExpr
SymbolNodeSymbolTableSymbolTableNodeTempNodeTryStmt	TupleExprr   TypeInfoTypeVarExpr	UnaryExprVar	WhileStmtWithStmt	YieldExpris_final_node)flip_opsint_op_to_methodneg_ops)PRECISE_TUPLE_TYPESOptions)	AsPatternStarredPattern)CheckerPluginInterfacePlugin)dataclasses)Scope)is_trivial_bodyrefers_to_fullnameset_callable_name)
ENUM_BASESENUM_SPECIAL_PROPS)BINARY_MAGIC_METHODS)state)	find_memberis_callable_compatibleis_equivalentis_more_preciseis_proper_subtypeis_same_type
is_subtyperestrict_subtype_awayunify_generic_callable)TraverserVisitorall_return_statementshas_return_statement)TransformVisitor)check_for_explicit_anyhas_any_from_unimported_typemake_optional_type)	bind_selfcoerce_to_literalcustom_special_methoderase_def_to_union_or_bounderase_to_bounderase_to_union_or_bound
false_onlyfixup_partial_typefunction_typeget_type_varsis_literal_type_likeis_singleton_typemake_simplified_unionmap_type_from_supertype	true_onlytry_expanding_sum_type_to_union"try_getting_int_literals_from_typetry_getting_str_literals"try_getting_str_literals_from_typetuple_fallback)%ANY_STRATEGYMYPYC_NATIVE_INT_NAMESOVERLOAD_NAMESAnyTypeBoolTypeQueryCallableTypeDeletedType
ErasedTypeFunctionLikeInstanceLiteralTypeNoneType
OverloadedPartialType
ProperType	TupleTypeTypeTypeAliasTypeTypedDictTypeTypeGuardedType	TypeOfAnyTypeTranslatorTypeType	TypeVarIdTypeVarLikeTypeTypeVarTupleTypeTypeVarTypeUnboundTypeUninhabitedType	UnionType
UnpackTypefind_unpack_in_listflatten_nested_unionsget_proper_typeget_proper_typesis_literal_typeis_named_instance)is_overlapping_noneremove_optionalstore_argument_type
strip_type)TypeTraverserVisitor)fill_typevarsfill_typevars_with_anyhas_no_typevars)	is_dunder	is_sunder)NodeVisitorT   r
   DEFAULT_LAST_PASS   MAX_PRECISE_TUPLE_SIZE
_TypeAliasDeferredNodeTypeFineGrainedDeferredNodeTypec                  $    e Zd ZU ded<   ded<   dS )DeferredNoder   nodeTypeInfo | Noneactive_typeinfoN__name__
__module____qualname____annotations__     ,lib/python3.11/site-packages/mypy/checker.pyr   r      s*         $$$$$$r
  r   c                  $    e Zd ZU ded<   ded<   dS )FineGrainedDeferredNoder   r  r  r  Nr  r	  r
  r  r  r     s*         %%%%$$$$$$r
  r  TypeMapc                  $    e Zd ZU ded<   ded<   dS )	TypeRanger   itemboolis_upper_boundNr  r	  r
  r  r  r    s'         JJJr
  r  c                  .    e Zd ZU ded<   ded<   ded<   dS )PartialTypeScopezdict[Var, Context]mapr  is_functionis_localNr  r	  r
  r  r  r    s1         NNNNNr
  r  c                     e Zd ZU dZdZded<   ded<   ded<   d	ed
<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded <   d!ZeZdZ	dZ
d"ed#<   d$Zd%ed&<   dZd'ed(<   d)ed*<   d+ed,<   d-ed.<   dd7Zedd9            Zdd:Zdd;Z	 ddd>ZddAZd	dBZd
dFZddJZddMZ	 dd$dNddTZddWZddXZdd]Zdd^ZddbZddcZddeZddfZ ddgZ!ddhZ"ddjZ#ddkZ$	 	 	 dddpZ%e&ddr            Z'	 dddtZ(ddyZ)ddzZ*dd}Z+ddZ,ddZ-ddZ.ddZ/d dZ0d!dZ1d"dZ2d#dZ3d$dZ4d%dZ5d%dZ6d&dZ7d'dZ8	 dd(dZ9d)dZ:d*dZ;d+dZ<d,dZ=d-dZ>d.dZ?d/dZ@d0dZAd1dZBd0dZCd0dZDd2dZEd3dZFd0dZGd0dZHd0dZId1dZJd4dZKd5dZLd1dZMd6dĄZNd7dƄZOd8dȄZPd9dʄZQd:d̈́ZRd;d΄ZSd<dЄZTd=d҄ZUd=dӄZV	 	 d>d?dۄZWddhZXded<   d@dZYdAdZZdBdZ[dCdZ\dDdZ]dEdZ^dEdZ_dFdZ`d;dZae&dGd            ZbdHdZcdIdZddJdZedKdZf	 dLdMdZg	 ddNdZh	 	 	 dOdPdZidQdZjdRd	Zk	 dLdSdZldTdZmdUdZndVdZo	 dLdWdZpdXdZqdYdZrdZdZsd[dZtd\dZud]dZvd]d Zwd^d!Zxd_d"Zyezj{        d#d$fd$d%d`d+Z|dad/Z}dbd1Z~dcd2Zddd4Zded6Zdfd8Zdfd9Zdgd;Zdhd=Zdid?ZdjdAZdkdCZddldFZdmdHZdndJZdodMZdpdNZdqdQZdrdRZdsdTZdtdWZdtdXZdudYZdvdZZdwd[Zdxd^Zdyd`ZdzdbZdd{dcZd|dgZdzdhZd}djZd~dkZddmZddnZddpZddrZddtZddxZdd|ZddZddZddZddZddZddZddZd\dZddZddZddZddZddZddZddZddZddZddZddZddZddZddZddZe	 	 dd$d$d$ddd            Ze	 	 dd$d$ddd            Z	 	 dd$d$d$dddZddĄZe&ddń            ZddƄZddǄZddɄZdd˄ZÐdd̄ZĐdd̈́ZŐdd΄ZƐddτZǐddЄZȐddфZɐddԄZe&ddք            Zːd;dׄZ̐dd؄Z͐ddلZe&dddڜdd݄            ZϐddZАddZѐddZҐddZӐdddZd$dddZ	 dd$dddZ֐ddZאddZؐddZِddZe	 ddd            Zedd            Z	 dddZېddZܐddZݐddZސddZߐddZddZdd ZdddZd$S (  TypeCheckerzMypy type checker.

    Type check mypy source files that have been semantically analyzed.

    You must create a separate instance for each source file.
    Fr-   errorsr=   msgzlist[dict[Expression, Type]]
_type_mapsr   binderz mypy.checkexpr.ExpressionCheckerexpr_checkerr$   pattern_checkerr   tscopeCheckerScopescope
list[Type]return_typesz
list[bool]dynamic_funcszlist[PartialTypeScope]partial_typeszset[Var]partial_reportedr   globalsdict[str, MypyFile]moduleszlist[DeferredNode]deferred_nodesr   r   optionsNzdict[Var, Type] | Noneinferred_attribute_typesr  no_partial_typesset[str]module_refszdict[Var, set[int]]var_decl_framesr   plugintreeru   pathstrper_line_checking_time_nsdict[int, int]returnNonec                   || _         || _        || _        || _        || _        t          ||          | _        || _        t                      | _	        t          |          | _        t                      | _        |j        | _        g | _        g | _        g | _        t'                      | _        i | _        g | _        i g| _        t'                      | _        d| _        d| _        |j        | _        |                    |          | _        d| _        d| _        d| _         d| _!        d| _"        tF          j$        %                    | | j        | j        |          | _&        tO          | | j        | j        |          | _(        dS )zSConstruct a type checker.

        Use errors to report type check errors.
        r   FNT))r  r+  r-  r4  r5  r=   r  r3  r   r!  r"  r#  r   r  namesr)  r%  r&  r'  setr(  r2  r,  r  r1  pass_numcurrent_node_deferredis_stubis_typeshed_fileis_typeshed_stubr.  recurse_into_functions_is_final_defchecking_missing_awaitallow_abstract_callmypy	checkexprExpressionCheckerr  r$   r   )selfr  r+  r-  r4  r5  r3  r7  s           r  __init__zTypeChecker.__init__o  sb    		!&'22gg!$''
+--z #! $55%*"| $ 5 5g > >(,% '+# # ',#
 $)  !N<<$(DK)B
 
  .dDHdk7SSr
  list[Type | None]c                    | j         j        S N)r  type_contextrJ  s    r  rO  zTypeChecker.type_context  s     --r
  c                   | j                                          | j                                         t                      | _        g | j        dd<   | j        d                                          d| _        | j                                         | j	        J | j
        g k    sJ | j        g k    sJ t          | j        j                  dk    sJ | j
        g k    sJ dS )zCleanup stale state that might be left over from a typechecking run.

        This allows us to reuse TypeChecker objects in fine-grained
        incremental mode.
        r   Nr   )r(  clearr1  r   r  r  temp_type_mapr  resetr.  r'  r,  lenr#  stackrP  s    r  rT  zTypeChecker.reset  s     	##%%%   +--   """!!!!,444!R''''"b((((4:#$$))))!R''''''r
  c           	        d| _         t          j        | j        j                  5  | j                            | j        | j        j	        | j
        | j                   | j
                            | j        j	                  5  |                                 5  | j                                        5  | j        j        D ]x}| j                                        rH|                                 s nH|                     |          s| j                            |            nc|                     |           yddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   | j        rJ | j                            d          }||j        |j        }|J |                     d|                     d          g          }t;          |j        |          sSt=          ||j        | j                  \  }}|                     t@          j!        "                    ||          |           ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w Y   dS )a  Type check the entire file, but defer functions with unresolved references.

        Unresolved references are forward references to variables
        whose types haven't been inferred yet.  They may occur later
        in the same file or in a different file that's being processed
        later (usually due to an import cycle).

        Deferred functions will be processed by check_second_pass().
        Tr#  r-  N__all__ztyping.Sequencebuiltins.strr-  )#rC  r   strict_optional_setr-  strict_optionalr  set_filer5  r4  fullnamer!  module_scopeenter_partial_typesr  top_frame_contextdefsis_unreachable should_report_unreachable_issuesis_noop_for_reachabilityr  unreachable_statementacceptr?  r)  gettyper  named_generic_type
named_typer   rA   failr   ALL_MUST_BE_SEQ_STRformat)rJ  dall_all_nodeseq_str	str_seq_sall_ss          r  check_first_passzTypeChecker.check_first_pass  s`    '+#&t|'CDD 	 	K  	49-T[$, !    ))$)*<==  --// 	+ 	+1N1N1P1P 	+ 	+!Y^ + +;5577 +#'#H#H#J#J & %#'#@#@#C#C & $ > >q A A A %& !KKNNNN	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+  5555|''	22#	(=#yH#///"55)DOON,K,K+L G &di99 +A#TY, , ,(	5 		,@GG	SXYY[c  3              		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   AIH:E1BE	7EEE
EEH:EH:!E"CH:.I:H>	>IH>	IIItodo7Sequence[DeferredNode | FineGrainedDeferredNode] | Nonec           
        d| _         t          j        | j        j                  5  |s| j        s	 ddd           dS | j                            | j        | j	        j
        | j        | j                   | j                            | j	        j
                  5  | xj        dz  c_        |s| j        }n	| j        rJ g | _        t                      }|D ]\  }}||v r
|                    |           |r| j                            |          nt#                      5  |r| j                            |          nt#                      5  |                     |           ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   	 ddd           n# 1 swxY w Y   	 ddd           dS # 1 swxY w Y   dS )zRun second or following pass of type checking.

        This goes through deferred nodes, returning True if there were any.
        TNFrX  r   )rC  r   r\  r-  r]  r,  r  r^  r5  r4  r_  r!  r`  r>  r=  addclass_scoper   r#  
push_classcheck_partial)rJ  rw  doner  r  s        r  check_second_passzTypeChecker.check_second_pass  s	    '+#&t|'CDD 	 	  3 	 	 	 	 	 	 	 	 K  	49-T[$, !    ))$)*<== 5 5" 3.DD#2222&(#LOEE-1 5 5)D/t||  HHTNNN )<00'  .9mm5 5
 -@TZ22+  2=--5 5 !..t4445 5 5 5 5 5 5 5 5 5 5 5 5 5 55 5 5 5 5 5 5 5 5 5 5 5 5 5 555 5 5 5 5 5 5 5 5 5 5 5 5 5 5, 9	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   F3AF3BF+F	E*F	*E..F	1E.2F	5FFFF	FF3F	F3!F	"F33F7:F7r  .DeferredNodeType | FineGrainedDeferredNodeTypec                    t          |t                    r|                     |           d S d| _        t          |t                    r| j                            |           d S |                     |           d S NT)
isinstanceru   check_top_levelrC  rp   r  rh  rJ  r  s     r  r}  zTypeChecker.check_partial  s}    dH%% 	"  &&&&&*.D'$
++ "!((.....D!!!!!r
  c                    d| _         |                                 5  | j                                        5  |j        D ]}|                    |            	 ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   | j        rJ dS )zDCheck only the top-level of a module, skipping function definitions.FN)rC  ra  r  rb  rc  rh  r?  )rJ  r  rp  s      r  r  zTypeChecker.check_top_level$  s   &+#%%'' 	# 	#..00 # # # #AHHTNNNN## # # # # # # # # # # # # # #	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	#
 ------s4   A: A#A:#A'	'A:*A'	+A::A>A>r   enclosing_classr  c                V    | j                             t          ||                     dS )a  Defer a node for processing during next type-checking pass.

        Args:
            node: function/method being deferred
            enclosing_class: for methods, the class where the method is defined
        NOTE: this can't handle nested functions/methods.
        N)r,  appendr   )rJ  r  r  s      r  
defer_nodezTypeChecker.defer_node/  s+     	""<o#F#FGGGGGr
  namecontextr]   c                (   | j                                         }| j        | j        k     rMt	          |t
                    r8| j                                         }|                     ||           d| _        d S | j	        
                    ||           d S r  )r#  top_non_lambda_functionr>  	last_passr  rg   r  r  r?  r  cannot_determine_type)rJ  r  r  r  r  s        r  handle_cannot_determine_typez(TypeChecker.handle_cannot_determine_type<  s    z1133=4>))jw.G.G)
 #j88::OOOD/222 *.D&&&H**499999r
  stmtr   c                    	 |                     |            dS # t          $ r7}t          || j        j        |j        | j        | j                   Y d}~dS d}~ww xY w)z,Type check a node in the given type context.N)rh  	Exceptionr/   r  fileliner-  )rJ  r  errs      r  rh  zTypeChecker.acceptL  sx    	_KK 	_ 	_ 	_!#t{'7DKQUQ]^^^^^^^^^	_s    
A,AAexit_conditionbody	else_bodyStatement | Noner  Expression | Nonec                  | j                             dd          5  	 | j                             ddd          5  |                     |           ddd           n# 1 swxY w Y   | j         j        snX|r-|                     |          \  }}|                     |           |r|                     |           ddd           dS # 1 swxY w Y   dS )zRepeatedly type check a loop body until the frame doesn't change.
        If exit_condition is set, assume it must be False on exit from the loop.

        Then check the else_body.
        FT)can_skipconditional_frame   r   )r  break_framecontinue_frameN)r  frame_contextrh  last_pop_changedfind_isinstance_checkpush_type_map)rJ  r  r  r  _else_maps         r  accept_loopzTypeChecker.accept_loopS  sq    [&&&NN 
	' 
	'[..!\].^^ & &KK%%%& & & & & & & & & & & & & & &{3 	
  -"88HH8""8,,, 'I&&&
	' 
	' 
	' 
	' 
	' 
	' 
	' 
	' 
	' 
	' 
	' 
	' 
	' 
	' 
	' 
	' 
	' 
	's5   C
AC
A"	"C
%A"	&AC

CCdefnrz   c                    | j         sd S | j                            |          5  |                     |           d d d            d S # 1 swxY w Y   d S rN  )rC  r!  function_scope_visit_overloaded_func_defrJ  r  s     r  visit_overloaded_func_defz%TypeChecker.visit_overloaded_func_defp  s    * 	F[''-- 	2 	2++D111	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2   AAAc                   d}|j         sd S t          |j                   dk    r |                     t          j        |           |j        rBt          |j         d         t                    sJ |                     |j         d                    |j         D ]~}t          |t                    sJ |j        r(| 	                    |j
        |j
        j        d           n|                     |d           |j
        j        t          t          fv r|dz  }|dt          |j                   fvr |                     t          j        |           |j        r|j                            |            |j        s|                     |           |j        pg }|j         D ]P}t          |t                    sJ |                     |j        j        |          }||                    |           Q|rt1          |          |_        |j        rq|                     |          }|j        r$|s"| | j                            |j        |           |                     |||j                   |                     |           d S )Nr   r   Tr  allow_empty)r  ) itemsrU  rm  r   MULTIPLE_OVERLOADS_REQUIREDis_propertyr  r_   visit_decoratorcheck_func_itemfuncr  visit_decorator_innerabstract_statusrO   rM   INCONSISTENT_ABSTRACT_OVERLOADimplrh  check_overlapping_overloadsrj  extract_callable_typevarr  r   infocheck_method_overrideis_explicit_overrider  no_overridable_method!check_explicit_override_decoratorcheck_inplace_operator_method)rJ  r  num_abstractfdef
item_typesr  	item_typefound_method_base_classess           r  r  z&TypeChecker._visit_overloaded_func_defv  s   z 	 4tz??aII&BDIII 	0djmY77777  A///J 		" 		"DdI..... C$$TYTY^QU$VVVV **4T*BBBy([:M,NNN!3tz??333II&EtLLL9 	#IT""" 
	7,,T222y 
 J 5 5D%dI66666 $ : :48=$ O OI ,")))444 7 *: 6 6DI9 		5(,(B(B4(H(H%)@1@ .9..ty$???2249RTXT]^^^..t444tr
  
inner_typeType | NonectxCallableType | Nonec                l   t          |          }d}|t          |t                    st          |t                    r|}nVt          |t                    rAt          t          d||ddd| j        || 	  	                  }t          |t                    r|}|| j                            ||           |S )z,Get type as seen by an overload item caller.N__call__FT)	r  typr  	is_lvalueis_superis_operatorr  original_typechk)r   r  r   r   r   r"   r  not_callable)rJ  r  r  
outer_type
inner_calls        r  r  z!TypeChecker.extract_callable_type  s    $Z00
*.
!*Z*I*I!*l33 ,'

J11 ,,)'& #"'!&$( H&0 
 
 
 
 j,77 ,!+J!%%j#666r
  c                6   | j         j        j        | j         j        j        v rd S d }|j        rxt          |j        t                    r|j        j        }n6t          |j        t                    r|j        j	        j        }n
J d            | 
                    ||j                  }|j        o
|j        dk    }t          |j                  D ]\  }}t          |t                    sJ | 
                    |j	        j        |          }|@t          |j        |dz   d                    D ]\  }}	t          |	t                    sJ | 
                    |	j	        j        |	          }
|
@t          ||
          sQt!          ||
          r+| j                             |dz   ||z   dz   |	j                   |s| j                                        }|r|j        j        ng }t/          j        d          5  t3          ||
|          r*| j                             |dz   ||z   dz   |j                   d d d            n# 1 swxY w Y    |I|j        J |j        rdt9          |                                |                                dt<                    }|%| j                             |dz   |j                   n|}|j         r9|j        r2|!                    |j         d         g|j         dd          z   	          }tE          ||tF          dd
          s#| j         $                    |dz   |j                   tG          |j%        |j%                  s=tG          |j%        |j%                  s#| j         &                    |dz   |j                   d S )NFzImpl isn't the right type__get__r   r  T)ignore_returnreturn_constraint_directionr   )	arg_types	is_compatr   r  )'r  r  r  ignored_filesr  r  rg   rj  r_   r  r  r  r  	enumerater  are_argument_counts_overlappingoverload_can_never_match%overloaded_signature_will_never_matchr  r#  active_classr  	type_varsr   r\  )is_unsafe_overlapping_overload_signaturesoverloaded_signatures_overlap	variablesr   with_unpacked_kwargsr%   &overloaded_signatures_typevar_specificr  copy_modifiedr   r   "overloaded_signatures_arg_specificret_type"overloaded_signatures_ret_specific)rJ  r  	impl_typer  is_descriptor_getir  sig1jitem2sig2current_classr  r  s                 r  r  z'TypeChecker.check_overlapping_overloads  s0    8?48?#@@@
 F *.	9 
	J$)W-- :*.).

DIy11 :!Y]/

99999 22:tyIII I@$)y*@ ,, N	R N	RGAtdI.....--dhmTBBD|%djQ&9:: ` `5!%3333311%).%HH<6tTBB +D$77 `HBB1q5!a%RS)UZU_````* ` %)J$;$;$=$=M@M U 2 < <SUI2488 ` `DT4QZ[[ ` HBB1q5!a%RS)UYU^___` ` ` ` ` ` ` ` ` ` ` ` ` ` ` $y,,, & %0F!66881133&+4@1 1 1D |GGAtyYYY  $ %D > bdi b--9J8Kdn]^]_]_N`8`-aaD .$*]a   R H??AtyQQQ t}dm<<R!$-??R H??AtyQQQ]N	R N	Rs   6<H>>IIr  r   is_coroutinec                x   t          |          }|rA|                     dt          t          j                  g          }t          ||          rdS nDt          t          j                  }|                     d|||g          }t          ||          rdS t          |t                    o|j        j	        dk    S )zIs `typ` a valid type for a generator/coroutine?

        True if `typ` is a *supertype* of Generator or Awaitable.
        Also true it it's *exactly* AwaitableGenerator (modulo type parameters).
        typing.AwaitableTtyping.Generatortyping.AwaitableGenerator)
r   rk  r   r   special_formr   r  r   rj  r_  )rJ  r  r  atany_typegts         r  is_generator_return_typez$TypeChecker.is_generator_return_typeY  s     c"" 		(();giF\>]>]=^__B"c"" t y566H(();hRZ=[\\B"c"" t#x((]SX->B]-]]r
  c                    	 t          t          j                  }|                     d||g          }n# t          $ r Y dS w xY wt          ||          S )zoIs `typ` a valid type for an async generator?

        True if `typ` is a supertype of AsyncGenerator.
        typing.AsyncGeneratorF)r   r   r   rk  KeyErrorr   )rJ  r  r  agts       r  is_async_generator_return_typez*TypeChecker.is_async_generator_return_typel  sh    
	y566H))*AHhCWXXCC 	 	 	55	 #s###s   14 
AAreturn_typec                    t          |          }t          |t                    rt          t          j        |          S t          |t
                    r!t           fd|j        D                       S                      |          s. 	                    |          st          t          j
                  S t          |t                    st          t          j
                  S |j        j        dk    rt          t          j                  S |j        r|j        d         }|S t          t          j                  S )zRGiven the declared return type of a generator (t), return the type it yields (ty).
source_anyc                <    g | ]}                     |          S r	  )get_generator_yield_type.0r  r  rJ  s     r  
<listcomp>z8TypeChecker.get_generator_yield_type.<locals>.<listcomp>  s)    aaat..t\BBaaar
  r  r   )r   r  r   r   from_another_anyr   r   r  r  r	  
from_errorr   rj  r_  r   args)rJ  r
  r  r  s   ` ` r  r  z$TypeChecker.get_generator_yield_typey  s8   %k22k7++ 	395+NNNNY// 	3(aaaaa{O`aaa   ..
 
 	355kBB	3
 9/000K22 	39/000&*<<<91222 
	3"'*HO 91222r
  c                    t          |          }t          |t                    rt          t          j        |          S t          |t
                    r!t           fd|j        D                       S                      |          s. 	                    |          st          t          j
                  S t          |t                    st          t          j
                  S |j        j        dk    rt          t          j                  S |j        j        dv r%t          |j                  dk    r|j        d         S |j        j        dk    r%t          |j                  dk    r|j        d         S t#                      S )	zTGiven a declared generator return type (t), return the type its yield receives (tc).r  c                <    g | ]}                     |          S r	  )get_generator_receive_typer  s     r  r  z:TypeChecker.get_generator_receive_type.<locals>.<listcomp>  s)    ccc00|DDcccr
  r  r  r     r   r  r  )r   r  r   r   r  r   r   r  r  r	  r  r   rj  r_  r   rU  r  r   rJ  r
  r  s   ` `r  r  z&TypeChecker.get_generator_receive_type  s   %k22k7++ 	95+NNNNY// 	(cccccQ\Qbccc   ..
 
 	55kBB	
 9/000K22 	9/000&*<<<91222%)ZZZK$%%** #A&&&*AAAc+JZF[F[_`F`F`#A&& ::r
  c                    t          |          }t          |t                    rt          t          j        |          S t          |t
                    s
J d            |j        d         S )Nr  z-Should only be called on coroutine functions.r  )r   r  r   r   r  r   r  )rJ  r
  s     r  get_coroutine_return_typez%TypeChecker.get_coroutine_return_type  se    %k22k7++ 	O95+NNNN+x00aa2aaaa""r
  c                    t          |          }t          |t                    rt          t          j        |          S t          |t
                    r!t           fd|j        D                       S                      |          st          t          j	                  S t          |t                    st          t          j	                  S |j        j        dk    r%t          |j                  dk    r|j        d         S |j        j        dv r%t          |j                  dk    r|j        d         S t          t          j                  S )	zSGiven the declared return type of a generator (t), return the type it returns (tr).r  c                <    g | ]}                     |          S r	  )get_generator_return_typer  s     r  r  z9TypeChecker.get_generator_return_type.<locals>.<listcomp>  s)    bbb//lCCbbbr
  r  r   r   r  r  r  )r   r  r   r   r  r   r   r  r  r  r   rj  r_  rU  r  r   r  s   ` `r  r   z%TypeChecker.get_generator_return_type  sL   %k22k7++ 	395+NNNNY// 	3(bbbbbP[Pabbb   ..{LII 	3 9/000K22 	39/000&*<<<[EUAVAVZ[A[A[#A&&%)ZZZK$%%** #A&& 91222r
  rg   c                    | j         sd S | j                            |          5  |                     |           d d d            d S # 1 swxY w Y   d S rN  )rC  r!  r  _visit_func_defr  s     r  visit_func_defzTypeChecker.visit_func_def  s    * 	F[''-- 	' 	'  &&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	'r  c                   |                      ||j                   |j        rb|                                s9|j        s2|j        s+|                     |          }|                     ||           |                     |           |j	        rt| 
                    |          }t          |j	        t                    rJ| 
                    |j	                  }t          ||          s| j                            |||           dS dS |j	        j        }|dS t          |t"                    rt|j        K|j	        }t          |t$                    r|j        }n|}|                     |          }|||_        ||= dS dS |                     t,          j        |           dS t1          |j                  }	|j	        |	_        | j                            |	||           |                     |||t,          j        dd           dS dS )!Type check a function definition.)r  Nzredefinition with typezoriginal type)r  r  r  
is_dynamicis_overloadis_decoratedr  r  r  original_defr   r  rg   r   r  %incompatible_conditional_function_defrj  r   r_   r  find_partial_typesrm  r   INCOMPATIBLE_REDEFINITIONrv   r  r  assign_typecheck_subtype)
rJ  r  r  new_typeold_type	orig_typeorig_defr  r'  	name_exprs
             r  r"  zTypeChecker._visit_func_def  sA   T	2229 	5??$$ XT-= XdFW X
 -1,F,Ft,L,L)66t=VWWW..t444 *	))$//H$+W55 '--d.?@@#Hh77 ]HBB4S[\\\\\] ]
 !-2	$ Fi55  ~-#'#4%h	:: +"*,CC"*C(,(?(?(D(D(4'/CH -c 2 2 2 54
 		"2"LdSSSSS ( 3 3I%)%6INK++IxKKK&& !(B0'    G*	 *	r
  rh   type_override
str | Noner  c                   | j                             |                                o|            |                     d          5  |                     |          }|r!|                    |j        |j                  }t          |t                    rE| 
                                5  |                     ||||           ddd           n# 1 swxY w Y   nt          d          ddd           n# 1 swxY w Y   | j                                          d| _        |dk    r|                     |           |dk    r6t!          j        |j                  rt!          j        | ||j                   dS dS dS )	zcType check a function.

        If type_override is provided, use it as the function type.
        T)r  )r  columnNzNot supportedF__exit____post_init__)r&  r  r&  ra  r   r  r  r7  r  r   !enter_attribute_inference_contextcheck_func_defRuntimeErrorpopr?  check__exit__return_typedataclasses_pluginis_processed_dataclassr  check_post_init)rJ  r  r4  r  r  r  s         r  r  zTypeChecker.check_func_item"  s    	!!$//"3"3"IM8IJJJ%%$%77 	4 	4$$T**C T#11sx
1SS#|,, 4;;== F F''c4EEEF F F F F F F F F F F F F F F #?333	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	   %*":))$/// ?""!8CC J"24tyIIIII #"J Js7   A"C4(CC4C	C4C	C44C8;C8Iterator[None]c              #  <   K   | j         }i | _         d V  || _         d S rN  )r.  )rJ  	old_typess     r  r:  z-TypeChecker.enter_attribute_inference_contextD  s-      1	(*%


(1%%%r
  r   c           
        |                      ||          }|}|D ]\  }}| j        }t                      | _        | j                                        5  |j                            |           t          |t                    r|}	|	j        r||	j	        dv rst          t          |j                  t          t          f          sE| j        d         s8|                     t           j                            |	j	                  |           |	j        r!|	j	        dk    r|                     |	|           |                     |	           | j        j        r|	j        rt          |	j        t0                    r|	j        j        }
t3          |
          r| j                            d|
|	           t9          |	j        j                  D ]A\  }}t3          |          r-d|dz    d|	j	         d}| j                            |||	           Bt=          |	j        | j        | j        | j        |		           |rm|j        r.|                      |          r| !                    ||||           n8|d
v r| "                    |||           n|dk    r| #                    ||           t          |j        tH                    rP|j        j%        tL          k    r%|                     t           j'        |j                   | (                    |           n;t          |j        tH                    r!|j        j)        r| (                    |           |j*        r|j+        r;| ,                    |j                  s |                     t           j-        |           n@| .                    |j        |j/                  s |                     t           j0        |           |j1        r|j        }|j/        }| 2                    ||          }| 3                    ||          }|r| 4                    |          }n| 5                    ||          }| 6                    d||||g          }
|7                    |
          }||_        | j8                            |j                   | j9        :                    |          5  | j9        ;                                }ddd           n# 1 swxY w Y   ty          t{          |j                            D ]\}|j        |         }t          |t                    r||dk    r|j>        r|j	        dk    r|j?        d         t          jA        t          jB        fvrk|jC        s|j	        dk    r$t          jE        jF        G                    |          }t          ||          s#t          t          t          |                              }t          ||d          st          |t                    r|j        jM        s@t          |t                    r.t          |jN        t                    r|jN        j        jM        rd}nl|jO        |         dv rQt           jP                            |Q                    | j                  |Q                    | j                            }nt           jR        }|r|                     ||           nqt          |tH                    r\|j%        t          k    rL|j	        dvrCt          |j	                  s/|}|jU        dk     r|}|                     t           jV        |           t          |||| j6                   ^t          |jY                  }| Z                    ||           ddd           n# 1 swxY w Y   | j                                        5  d}|j[        D ]}|jE        \                                D ]p\  }}t          |          }|Z| ^                    ||jU                  s?|| j        _                                }||jE        |<   |j`        |         | j        j`        |<   q| j9        :                    |          5  t          |          st{          |          dk    r| j        b                                 | c                    |jY                   ddd           n# 1 swxY w Y   | j        d                                }|| j        e                    dd           ddd           n# 1 swxY w Y   |s|j*        st          | j8        d         d          r'| 5                    | j8        d         |j/                  }n5|j/        r!| 4                    | j8        d                   }n| j8        d         }t          |          }|p| j        jg        }| p0| o-t          |t                    o|jh        t          k     o| jj         } |j        t          ur1|j	        |j        jl        vs|j        jl        |j	                 jm        rd} t          |t                    r	|jn        rd} |rt          t                      |          rd} |o?|j        t          uo1|j        jo        duo#|j        jo        j        p                    d          }!| j        jq        r| jr        st          |t          t          f          s| rt          |t                    rt           jt        }nt           ju        }|r |v                    t          jx                  }|                     ||           |!r | y                    t           jz        |           ny| rwt           j{        }|r |v                    t          jx                  }| |                    dt                      d|||          s"|!r | y                    t           jz        |           | j8        }                                 || _        dS )r%  )rK  __init_subclass____new__zReturn typez	Argument r   z to ""r  )__getattr____getattribute____setattr__r  )r  Nr   T)ignore_type_params>   clsrJ  )rK  rH  r9  r  Fabc.ABCMetacodezimplicitly returnsexpected)subtype_labelsubtypesupertype_label	supertyper  r  )~expand_typevarsr  r   rb  expandedr  r  rg   r  r  r   r  r   r   r&  rm  r   MUST_HAVE_NONE_RETURN_TYPEro  check___new___signaturecheck_for_missing_annotationsr-  disallow_any_unimportedrj  r   r   r  unimported_type_becomes_anyr  r  r   rB  is_reverse_op_methodcheck_reverse_op_methodcheck_getattr_methodcheck_setattr_methodr   variancerI   #RETURN_TYPE_CANNOT_BE_CONTRAVARIANTcheck_unbound_return_typevarvaluesis_generatoris_async_generatorr	  'INVALID_RETURN_TYPE_FOR_ASYNC_GENERATORr  r  !INVALID_RETURN_TYPE_FOR_GENERATORis_awaitable_coroutiner  r  r  r   rk  r  r%  r#  push_functionactive_self_typerangerU  	is_static	arg_kindsr   rH   	ARG_STAR2is_classrG  typesr   make_normalizedr   r'   r   r   r   is_protocolr  	arg_namesERASED_SELF_TYPE_NOT_SUPERTYPEstr_with_optionsMISSING_OR_INVALID_SELF_TYPErJ   
is_privater  &FUNCTION_PARAMETER_CANNOT_BE_COVARIANTr   r   r  check_default_argsframesr  r4   !is_var_redefined_in_outer_context
push_framedeclarations_is_empty_generator_functionsuppress_unreachable_warningsrh  rd  	pop_framer   allow_empty_bodiesr  rS   r@  rK   r<  plugin_generatedis_mypy_onlymetaclass_typehas_basewarn_no_returnr?  r   INVALID_IMPLICIT_RETURNMISSING_RETURN_STATEMENT_replacecodes
EMPTY_BODYnoteEMPTY_BODY_ABSTRACTINCOMPATIBLE_RETURN_VALUE_TYPEr.  r=  )"rJ  r  r  r  r  rY  original_typr  
old_binderr  r  idxarg_typeprefixtctytctrref_typer  erasedr  r  body_is_trivial	new_frameframekeynarrowed_typekey_varunreachabler
  
show_errormay_be_abstracts"                                     r  r;  zTypeChecker.check_func_defK  s   
 ''c22! Y	% Y	%ID#J/11DK..00 X? X?$$T***
 dG,, D 	
 I)JJJ *+CL99Ho;V! ! K !% 22 6	 K 		,GNNtyYY[_  
 y @TY)%;%;44T3???66t<<<|; a9 aDI|)L)L a'+y'9H;HEE d $ D D]T\^b c c c1:49;N1O1O a aX#?#I#I !a-Sq-S-Sty-S-S-SF$(H$H$HQY[_$`$`$`*	4<1FZ^     =y =T%>%>t%D%D =44T3dKKKK!DDD11#tTBBBB..11#t<<< clK88 D|,==		,PRUR^   55c::::|4kBBDGSG\GcD
 55lCCC $ _. _#BB3<PP  II 0 XZ]    $<<S\4K\]] _ II&6&XZ]^^^ . $ A)A66q!<<B88A>>B B!;;A>>!;;AqAA#663b"b!_   H ++X+>>C #DI !((666Z--d33 J J -1J,G,G,I,IHJ J J J J J J J J J J J J J J s3=1122 1O 1OA"}Q/H"411-d$0FF!% #379	3I3IM!,U^U_4UUU= UDI,B,B'+z':'J'J8'T'TH+Hh?? 9 &5^NS[D\D\5]5]%^%^F#-hSW#X#X#X 9$.vx$@$@!X(.(?!X (2&('C'C!X )36;(I(I	!X
 )/(8(D!X +/CC%(]1%5%H%H*:*Y*`*`(.(?(?(M(M(0(A(A$,(O(O+& +&CC
 +;*WC#& !9$(IIc4$8$8$8#Hk:: d %-:: $	1Y Y Y$.ty$9$9 !Z ,4C"x!||&) II&6&]_bccc'ad6MNNNN #2$)"<"<''o>>>qX? X? X? X? X? X? X? X? X? X? X? X? X? X? X?v ..00  3  3 +/	'. 
Y 
YE.3k.?.?.A.A 	Y 	Y*]"?"D"D".t7]7]#TY8 8.  )0,0K,B,B,D,D	3@IOC0<F<STW<XDK4S9	Y Z--d33 + + 4D99 DS]]a=O=OAACCCKK	***+ + + + + + + + + + + + + + + #k88::(K))$222A 3  3  3  3  3  3  3  3  3  3  3  3  3  3  3D  UN$ 	8(9%b)+F) ) 	8 #'"@"@)"-t/@# #KK & 8"&"@"@ARSUAV"W"WKK"&"3B"7K-k::)LT\-L (' 	 ( -&tW55^$:NR^:^- !%,  9L00ITY_44		8R8c4!&J dG,, '1B '!&J # 'z(**k'J'J '!&J $ N	5N	0<N 	05>>}MM	   <. "N 6R *;78K L LR 'R &k?CC L"2"JCC"2"KC* F"%,,E4D,"E"EC		#t,,,* R II&6&JDQQQ N*IC& B!ll0@lAA !..*>$,JJ,6&1$( # /  N ,N 		"2"FMMM!!###$DKKsY	% Y	%sp   Q^*S^S^SJ,^^	^	4B1d%Ac;dcdc9dd	d	vr   
after_lineintc                    | j                                         }|sdS |D ]3}t          |t                    rt	          |j        |          |k    r dS 4dS )zCan the variable be assigned to at module top level or outer function?

        Note that this doesn't do a full CFG analysis but uses a line number based
        heuristic that isn't correct in some (rare) cases.
        TF)r!  outer_functionsr  rg   find_last_var_assignment_liner  )rJ  r  r  outersouters        r  r~  z-TypeChecker.is_var_redefined_in_outer_contextm  sk     ,,.. 	 4 	  	 E%))  0Q??:MM44ur
  c                *   t          |j        t                    r|j        |j        v rt	                      }|j        D ]}|                    |           |j        |j        vr|                     t          j	        |j        t                     t          |j        j                  }t          |t                    r|j        j        dk    sE|                     dt#          |j        j        | j                   d|j                   dS dS dS dS dS )z:Fails when the return typevar is not defined in arguments.rQ  builtins.objectzConsider using the upper bound z insteadrJ  N)r  r  r   r  CollectArgTypeVarTypesr  rh  rm  r   UNBOUND_TYPEVARr)   r   upper_boundr   rj  r_  r  r?   r-  )rJ  r  arg_type_visitorargtyper  s        r  re  z(TypeChecker.check_unbound_return_typevar~  s3   clK00 	S\S]5R5R577= 1 1/0000|#3#===		*:CLx	XXX-cl.FGG{H55#(15FFFIIY&s|'?NNY Y Y #      	 	5R5R
 >=
 GFr
  r  r  c           
        |j         D ]}|j        
|rt          |j        t                    r'|j        j        }d}|                    d          r|d|dd           z  }n	|d| dz  }| j        j        s/t          |j        t                    r|j        j
        dk    rdd	g}nd }|                     |j        j        |j        |j        t          |t          j        
          dd|           d S )NzIncompatible default for __tuple_arg_ztuple argument    z
argument "rI  builtins.NonezkPEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=Truez_Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebaserQ  argumentdefault)r  r  lvalue_namervalue_namenotes)	argumentsinitializerr  ra   variabler  
startswithr-  implicit_optionalrv   r_  check_simple_assignmentrj  r;   r  
ASSIGNMENT)rJ  r  r  argr  r  r  s          r  r|  zTypeChecker.check_default_args  s:   >  	  	C& :co|#L#L <$D-C~.. ,4bcc444+D++++L2s99 O,??], ((! 5+;<<<&% )    1 	  	r
  method_namec                    |t           j        v S rN  )r   reverse_op_methodsrJ  r  s     r  is_forward_op_methodz TypeChecker.is_forward_op_method  s    i:::r
  c                    |t           j        v S rN  )r   reverse_op_method_setr  s     r  r_  z TypeChecker.is_reverse_op_method  s    i===r
  r  c                `   ddt          |j        t                    o2t          fd|j        j        |j        j        gz   D                       }| j         p| j        j        }| j        j	        o|}|r0| j        j
        s|r#|j        | j        j
        r|j        r:t          |j                  dk    r|j        d         d	k    s|j        d         d
k    r^|                     t          j        |           t#          |          s+|j        s&|                     d|t(          j                   d S d S d S |                     t          j        |           d S t          |j        t                    r6t/          |j        j                  } |          r!|                     t          j        |           n|j        rE |                     ||j                            r |                     t          j        |           nZ|j        rSt          |t4                    r> |                     |                    r |                     t          j        |           t          fd|j        j        D                       r(|                     t          j        |           d S d S d S d S d S )Nr  r   r9  r  c                    t          | t                    sdS t          | t                    o| j        t          j        k    S NF)r  r   r   type_of_anyr   unannotatedr  s    r  is_unannotated_anyzETypeChecker.check_for_missing_annotations.<locals>.is_unannotated_any  s8    a,, ua))Tamy?T.TTr
  c              3  0   K   | ]} |           V  d S rN  r	  r  r  r  s     r  	<genexpr>z<TypeChecker.check_for_missing_annotations.<locals>.<genexpr>  sK       N
 N
*+""1%%%N
 N
 N
 N
 N
 N
r
  r   r   rJ  rO  z1Use "-> None" if function does not return a valuerQ  c              3  .   K   | ]} |          V  d S rN  r	  r  s     r  r  z<TypeChecker.check_for_missing_annotations.<locals>.<genexpr>  s/      JJ))!,,JJJJJJr
  r  r   r9  r  )r  rj  r   anyr  r  rB  r-  warn_incomplete_stubdisallow_incomplete_defsdisallow_untyped_defsr  rU  rv  rm  r   RETURN_TYPE_EXPECTEDr   rg  r  r  NO_UNTYPED_DEFFUNCTION_TYPE_EXPECTEDr   r   r  r   r  ARGUMENT_TYPE_EXPECTED)rJ  r  has_explicit_annotationshow_untypedcheck_incomplete_defsr  r  s         @r  r\  z)TypeChecker.check_for_missing_annotations  s.   	U 	U 	U 	U
 #-TY"E"E #
# N
 N
 N
 N
/3y/BdiFXEY/YN
 N
 N
 K
 K
  00UDL4U $ E aJa 	MT\? 	MCX 	My T\%G ~ M''1,,*f44q8IU8R8RII.CTJJJ/55 d>O 		O !&!5 "         II.EtLLLLLDI|44 M*49+=>>%%h// 	OII.CTJJJJ& O))66xARSS  O 		"2"GNNN& O:h+I+I O))$*H*H*R*RSS O		"2"GNNNJJJJdi6IJJJJJ MII.EtLLLLL9	M 	M 	M 	MM MM Mr
  c                ^   t          |j                  }t          ||d          }|j                                        r=|                     |j        |                                 |t          j        dd           d S t          t          |j                  t          t          t          t          f          sM|                     t          j                            t%          |j        | j                            |           d S |                     |j        ||t          j        dd           d S )NTis_classmethodreturnszbut must return a subtype of)r   r  r   is_metaclassr.  r  	type_typer   INVALID_NEW_TYPEr  r   r   r   r   r   rm  NON_INSTANCE_NEW_TYPEro  r?   r-  )rJ  r  r  	self_type
bound_types        r  r[  z#TypeChecker.check___new___signature  s:   *4955	sIdCCC
 9!!## 	#   1.     J/007HiQ`2a
 
 	 II 6==
 3T\BB  	     # 1.    r
  reverse_typereverse_namec           	        |j         sJ t          t          t          j                  t          t          j                  gt
          j        t
          j        gddgt          t          j                  |                     d                    }t          ||          s| j	        
                    ||           dS |dv rdS t          |j                  }t          |t                    rdS t          |t                    r|j        j        dk    rdS |j        d         t$          k    r=|                    |j        d         gdz  t          gdz  |j        d         dg          }t-          |j                  dk    sJ t.          j        |         }t          |j        d	                   }t          |t2                    rt          |j                  }nNt          |t6                    rt9          |          }n)t          |t:          t<          t>          f          r|j         }t          |tB                    r,|j"        }	t          |	t                    r|	j        j#        }
|
|
}dfdt          |t          tH          f          r ||          sdS |j        d	         }| j%        &                    |||          }| '                    |||j         ||||           dS )z1Check a reverse operator method such as __radd__.Nbuiltins.function)__eq____ne__r  r   r  r  r  rp  rv  r   r  UnionType | Instancer  r6  r9  r  c                    t          | t                    r| j                                      S t	          fdt          |                                           D                       S )Nc              3     K   | ]T}t          |t                    r |          p.t          |t                    o|j                                      V  Ud S rN  )r  r   r   rj  has_readable_member)r  xr  r  s     r  r  zSTypeChecker.check_reverse_op_method.<locals>.has_readable_member.<locals>.<genexpr>H  s~          Ay))J.A.A!T.J.J Rq(++P0J0J40P0P     r
  )r  r   rj  r  allr   relevant_items)r  r  r  s    `r  r  z@TypeChecker.check_reverse_op_method.<locals>.has_readable_memberD  s    #x(( :x33D999      *#*<*<*>*>??     r
  rJ  )r  r  r  r6  r9  r  )(r  r   r   r   r   r   rG   rl  r   r  invalid_signaturer   r  r  r   rj  r_  rp  rH   r  r  rv  rU  r   normal_from_reverse_opr   r  r   r   r   r   r   fallbackr   r  r  r   r  analyze_external_member_accesscheck_overlapping_op_methods)rJ  r  r  r  r  method_typer  forward_nameforward_instr  opt_metaforward_baseforward_typer  s                @r  r`  z#TypeChecker.check_reverse_op_method	  s    y #Y+,,gi6L.M.MN]EM*4LI*++OO/00
 
 ,44 	H&&|W===F///F #<#899h(( 	Fh)) 	}%):::!!$00'55'1!459")a-'1!4c: 6  L
 <)**a//// 7E&|'=a'@AAlK00 	1*<+CDDLLi00 	1),77LL|]K&PQQ 	1'0LlH-- 	,$D$)) ,93'#+L	 	 	 	 	 	 |h	%:;;	##L,??	 F#-a0(GG, H 
 
 	))I 	* 	
 	
 	
 	
 	
r
  reverse_classr   r  r  r  c           	        t          |g          D ]}t          |          }t          |t                    r6|                     |||          r| j                            |||||           \t          |t                    r@|j        D ]7}	|                     |	||          r| j                            |||||           8t          |t                    s| j        
                    ||           dS )a  Check for overlapping method and reverse method signatures.

        This function assumes that:

        -   The reverse method has valid argument count and kinds.
        -   If the reverse operator method accepts some argument of type
            X, the forward operator method also belong to class X.

            For example, if we have the reverse operator `A.__radd__(B)`, then the
            corresponding forward operator must have the type `B.__add__(...)`.
        N)r   r   r  r   is_unsafe_overlapping_opr  "operator_method_signatures_overlapr   r  r   forward_operator_not_callable)
rJ  r  r  r  r  r  r  r  forward_itemr  s
             r  r  z(TypeChecker.check_overlapping_op_methodsa  s(   ` 2<.AA 	N 	NL*<88L,55 N00|\ZZ H??%|\<QX   L*55 N(.  D44T<VV CC)<|U\  
  g66 N66|WMMM	N 	Nr
  r  c                   t          |j                  dk     rdS |}t          |t                    rt	          |          }|                    ||j        d         gt          j        gdz  d gdz            }|                    |j        d         |j        d         gt          j        gdz  d gdz            }|j        d         }t          |t                    rt	          |          }t          ||          rdS t          ||          r|}|}	n|}|}	| j
                                        }
|
r|
j        j        ng }t          ||	|          S )Nr   Fr   r  r  )rU  r  r  r   r   r  r   rG   r   r   r#  r  r  r  r  )rJ  r  r  r  forward_base_erasedforward_tweakedreverse_tweakedreverse_base_erasedfirstsecondr  r  s               r  r
  z$TypeChecker.is_unsafe_overlapping_op  s    |%&&**5
 +lK00 	?"0">"> '44*L,B1,EF}o)fqj 5 
 

 '44#-a0,2H2KL}o)fqj 5 
 
 +4Q7);77 	F"01D"E"E+-@AA 	%5+-@AA 	%#E$FF#E$F
//114AIM&00r	8	RRRr
  rf   c                   |j         }|t          j        vrdS t          |                     |                    }|j        }d|dd         z   }|                    |          rt          |          }t          | j	        
                    |||                    }d}t          |t                    rt          ||          sd}nd}|r | j                            |||           dS dS dS )zrCheck an inplace operator method such as __iadd__.

        They cannot arbitrarily overlap with __add__.
        N__r  FT)r  r   inplace_operator_methodsr   r   r  r  r   r   r  r   r  r   is_more_general_arg_prefixr  signatures_incompatible)	rJ  r  methodr  rO  other_methodinstancetyp2rm  s	            r  r  z)TypeChecker.check_inplace_operator_method  s   
 ;;;F**40011ifQRRj(""<00 	M$S))H"!@@xY]^^ D D$-- 1#t<<  D  M00tLLLLL	M 	MM Mr
  c           	        t          | j        j                  dk    r|dk    r"|                     t          j        |           d S t          |                     d          gt          j	        gd gt          t          j                  |                     d                    }n| j                                        rt          t          t          j                  |                     d          gt          j	        t          j	        gd d gt          t          j                  |                     d                    }nd S t          ||          s| j                            |||           d S d S )Nr   rL  rZ  r  )rU  r#  rV  rm  r   MODULE_LEVEL_GETATTRIBUTEr   rl  r   rG   r   r   r   r  r   r  $invalid_signature_for_special_method)rJ  r  r  r  r  s        r  ra  z TypeChecker.check_getattr_method  sQ   tz  A%%)))		*DgNNN '001	.// 344 KK Z$$&& 		&/00$//.2Q2QR.t	.// 344 KK F#{++ 	NH99#wMMMMM	N 	Nr
  c           	        | j                                         sd S t          t          t          j                  |                     d          t          t          j                  gt          j        t          j        t          j        gg dt                      |                     d                    }t          ||          s| j                            ||d           d S d S )NrZ  )NNNr  rM  )r#  r  r   r   r   r   rl  r   rG   r   r   r  r   )rJ  r  r  r  s       r  rb  z TypeChecker.check_setattr_method  s    z&&(( 	F"	.////	.//
 ]EM5=9JJOO/00

 

 #{++ 	WH99#wVVVVV	W 	Wr
  c           	         |                      d          }t          ||                     d|g          g          }|                     |||t          j        ddt          j                   dS )zCheck the type of __slots__.rZ  typing.Iterablezactual typezexpected typerQ  N)rl  r   rk  r.  r   INVALID_TYPE_FOR_SLOTSr  r  )rJ  r  r  str_typeexpected_types        r  check_slots_definitionz"TypeChecker.check_slots_definition  s    ??>22!t../@8*MMN
 
 	3! 	 	
 	
 	
 	
 	
r
  r  c                   | j                                         sdS t          |          }t          |t                    rt          d |j        D                       s)| j                            d|t          j
                   dS dS )z2Check that __match_args__ contains literal stringsNc                ,    g | ]}t          |          S r	  )is_string_literalr  r  s     r  r  z0TypeChecker.check_match_args.<locals>.<listcomp>/  s!    ;;;t$$;;;r
  zb__match_args__ must be a tuple containing string literals for checking of match statements to workrQ  )r#  r  r   r  r   r  r  r  r  r  LITERAL_REQ)rJ  r  r  r  s       r  check_match_argszTypeChecker.check_match_args)  s    z&&(( 	Fc""#y)) 	;;;;;2
 2
 	 HMM.&	      	 	r
  #list[tuple[FuncItem, CallableType]]c                   g }t          |j                  pg }|j        r||j        j        j        pg z  }|D ]Dt          t                    r-j        r&|                    fdj        D                        E|ro| j	        j
        sc| j	        j        sWg }t          j        | D ]D}t          |          }|                    t          ||          t!          ||          f           E|S ||fgS )Nc                "    g | ]}j         |fS r	  id)r  valuetvars     r  r  z/TypeChecker.expand_typevars.<locals>.<listcomp>C  s     HHH5tw.HHHr
  )listr  r  r  r  r  r   rf  r  r-  mypycinspections	itertoolsproductdictexpand_funcr1   )	rJ  r  r  substtvarsresultsubstitutionsmappingr4  s	           @r  rX  zTypeChecker.expand_typevars8  s"    57S]##)r9 	4TY^-33E 	J 	JD$,, J JHHHHDKHHHIII  	!$,, 	!0H 	!:<F!*!2E!: W W}--{499;sG;T;TUVVVVM3K= r
  FuncDef | OverloadedFuncDefr  list[TypeInfo] | NoneContext | Nonec                    |rT|j         sO|j        dvrHt          |j                  s6| j                            |j        |d         j        |p|           d S d S d S d S d S )NrK  rH  r   )r  r  rz  r  #explicit_override_decorator_missingr_  )rJ  r  r  r  s       r  r  z-TypeChecker.check_explicit_override_decoratorP  s     &	-	 	!888ty)) 9 H88	4Q7@'/T    	 	 	 	 9888r
  'FuncDef | OverloadedFuncDef | Decoratorc                    g }|j         j        dd         D ]4}|                     ||          }| dS |r|                    |           5|S )a&  Check if function definition is compatible with base classes.

        This may defer the method if a signature is not available in at least one base class.
        Return ``None`` if that happens.

        Return a list of base classes which contain an attribute with the method name.
        r   N)r  mro*check_method_or_accessor_override_for_baser  )rJ  r  r  baser>  s        r  r  z!TypeChecker.check_method_override`  sj     57!IM!""% 	7 	7DDDT4PPF~tt 7)00666((r
  rK  bool | Nonec                   d}|r|j         }|j                            |          }|rit          |j                  r0t          |          s!| j                            ||j         |           |j        r| 	                    ||j        |           d}|dvrM| 
                    |||          rdS |t          j        v r&d|dd         z   }| 
                    |||          rdS |S )a  Check if method definition is compatible with a base class.

        Return ``None`` if the node was deferred because one of the corresponding
        superclass nodes is not ready.

        Return ``True`` if an attribute with the method name was found in the base class.
        FT)rK  rH  rF  r9  Nr  r  )r  r<  ri  r   r  rz  r  cant_override_finalis_final$check_if_final_var_override_writable(check_method_override_for_base_with_namer   r  )rJ  r  rK  found_base_methodr  	base_attrr  s          r  rJ  z6TypeChecker.check_method_or_accessor_override_for_baseu  s    " 	$9D
t,,I ) 00 HD9I9I HH00ty$GGG= Z==dINTXYYY$(! XXX @@tTRR  49===!DH_F
 DDT6SWXX $#t  r
  c           
        |j                             |          }|rYt          |t                    s|}n|j        }t          |t
          t          f          r+|                     |          }|j        p|j	        }|j        }nL|j
        j        sJ |j
        j        J |j
        j        }|j        j        p|j        j	        }|j        j        }t          |          }t          |t                    r8t          |          s)t          || j                                        |          }t          |j                  }	|j        }
|	t          |	t&                    r| j        | j        k     r|                     ||j                   dS t          |
t
          t          f          r|                     |
          }	n~t          |
t                    r|                     |
j                  }	nNt          |
t0                    r+|
j        t          |
j                  }	nt3                      }	nt3                      }	t          |
t
          t          f          r|
j        p|
j	        }n-t          |
t                    r|
j        }|j        p|j	        }nd}t          |	t                    r=|                     ||	|j        |          }	|
rt7          |
          rt9          |	          }	t          |t                    rt7          |          rt9          |          }t          |
t0                    rk|
j        sd|
j        r|
j        rVt          |t                    rAt          |	t>                    s,| j         !                    d|j        tD          j#                   t          |	t>                    st          |t>                    rnt          |	t                    r<t          |t                    r'| $                    ||	|j%        ||j%        |||           nctM          |	|          rnR|
r&| '                    |
          stQ          ||	          rn*| j         )                    |j%        ||j%        ||	|           dS )zCheck if overriding an attribute `name` of `base` with `defn` is valid.

        Return True if the supertype node was not analysed yet, and `defn` was deferred.
        Nr  TFz;Cannot override writeable attribute with read-only propertyrQ  originaloverride)*r<  ri  r  r_   r  rg   rz   r   rr  ro  r  is_readyrj  r   r   r   r#  rm  r  r   r>  r  r  r  r   r   bind_and_map_methodr  get_property_typerO  is_settable_propertyr   r  rm  r  OVERRIDEcheck_overrider  r   is_writable_attributer   %signature_incompatible_with_supertype)rJ  r  r  rK  rS  r  r  override_class_or_staticoverride_classr  original_nodeoriginal_class_or_staticr  s                r  rQ  z4TypeChecker.check_method_override_for_base_with_name  s    JNN4((	 q	
 dI.. $) $*; <== 	4 ..t44+/=+JDN(!%x((((x}000hm+/9+=+TAT(!%!3!#&&C#|,, cYw5G5G cTZ%@%@%B%BSabbb ,IN;;M%NM $
=+(N(N$=4>11 OOD$)44449J/KLL /$($6$6}$E$EMMy99 /$($6$6}7I$J$JMMs33 / %)5(78J(K(K(0

 %-JJM-'3D)EFF 1+8+A+\]E\((M955 1$)+/=+JDN((+0(-66 E $ 8 8MSWS\^b c c  E[%?%? E$5m$D$DM#|,, T1B1B ',,}c22)2 +6 ;H:\ #433	 &mW== Y I!&	 &    -11 ZW5M5M M<88 Z\=Z=Z ##!II,,	 	 	 	 }c22  
22=AA
 sM22
 >>ItTY-Z] ?    ur
  symr   r   sub_info
super_infoc                   t          |j        t          t          t          f          r9t          |j                  s$t          |j        t                    r|j        j        j        }n|j        j        }t          t          t          |||                    }| j                                        }t          |t                    r|rg }|j        D ]l}	|	j        s|                    |	           |	j        d         }
t          |
t"                    r|
j        }
t'          ||
          r|                    |	           m|rt          |          }t)          |||          S t          t          t          |||                    S )a*  Bind self-type and map type variables for a method.

        Arguments:
            sym: a symbol that points to method definition
            typ: method type on the definition
            sub_info: class where the method is used
            super_info: class where the method was defined
        r   )r  r  rg   rz   r_   ro  r  rr  r   r   r   r#  rm  r   r  r  r  r   r  r   r   )rJ  rd  r  re  rf  is_class_method
mapped_typrm  filtered_itemsr  item_args              r  rY  zTypeChecker.bind_and_map_method  s    ch*;Y GHH  	ZQZHR
 R
  	Z #(I.. 4"%(-"8"%("3l,CCS],^,^__J#z::<<*j11 <6F < "$&, 4 4D> 4&--d333#~a0H!(K88 8#+#7!"2H== 4&--d333
 " <!+N!;!;JZ)9?KKK&=c8Z&X&XYYYr
  tpc                (    t          |t                    r,|j        r#|j        d         t          k    r|j        d         S d S t          |t
                    r2 fd|j        D             }d |D             }|rt          |          S d S J d            )Nr   c                :    g | ]}                     |          S r	  )get_op_other_domainr  itrJ  s     r  r  z3TypeChecker.get_op_other_domain.<locals>.<listcomp>L  s'    III"11"55IIIr
  c                    g | ]}||S r	  r	  r  rq  s     r  r  z3TypeChecker.get_op_other_domain.<locals>.<listcomp>M  s    222Br2R222r
  Fz,Need to check all FunctionLike subtypes here)r  r   rp  rG   r  r   r  r   )rJ  rl  	raw_itemsr  s   `   r  ro  zTypeChecker.get_op_other_domainF  s    b,'' 	I| 'Q7 : :|A&4J'' 	IIIIIIIII22)222E 4,U3334HHHHHr
  rW  rV  name_in_superrW  rc  r`  c	                X   d}	d}
t          ||d          sd}	nlt          |t                    rW|                     |          rB|                     |          }|                     |          }|r|rt          ||          sd}	d}
t          |t
                    rA|r|sd}	n:t          |t                    r%t          |t                    r|j        	|j        d}	t          |          rd}	|	r7d}t          |t          t          f          r|	                                }t          |t          t          f          r|	                                }t          |t                    rt          |t                    rt          |j                  t          |j                  k    rh|j        |j        k    rW|                                d}t          |j        t                    r|j        j        j        }dfd}t%          t          |j                            D ]}t          |j        |          ||j        |                             sn|j        |         }t          |t                    r#|j        |t          |j                  z            }n|}| j                            |d	z   |||||||
           d}t           ||j                  |j                  s+| j                            ||||j        |j        |           d}nt          |t                    rt          |t                    rg }|j        D ]C}t5          |j                  D ],\  }}t          ||          r|                    |            n-Dt          |          t          |j                  k    r2|t9          |          k    r| j                            ||||           d}|s | j                            ||||||           |
r&|                     d|t@          j!                   dS dS dS )a  Check a method override with given signatures.

        Arguments:
          override:  The signature of the overriding method.
          original:  The signature of the original supertype method.
          name:      The name of the subtype. This and the next argument are
                     only used for generating error messages.
          supertype: The name of the supertype.
        FTignore_pos_arg_namesNr  r   r9  c                &    t          |           S )N)ids_to_erase)r'   )r  override_idss    r  erase_overridez2TypeChecker.check_override.<locals>.erase_override  s    )!,GGGGr
  r   )secondary_contextrU  zHOverloaded operator methods can't have wider argument types in overridesrQ  )r  r   r9  r   )"r   r  r   r  ro  r   r   
type_guardrz  r  rU  r  min_argstype_var_ids
definitionrg   r  r  rn  r  
bound_argsr  $argument_incompatible_with_supertyper  'return_type_incompatible_with_supertyper  r  r  sorted.overload_signature_incompatible_with_supertyper_  r  r  r\  )rJ  rW  rV  r  ru  rW  rc  r`  r  rm  op_method_wider_noteoriginal_domainoverride_domainemitted_msg	type_namer|  r  arg_type_in_superr  orderchild_variantparent_variantr{  s                         @r  r]  zTypeChecker.check_overrideT  s   * $(H4HHH 	,DD*-- 	,$2K2KD2Q2Q 	, #66x@@O"66x@@O,#, #?ODD,
 '+$h-- 	 '  0H  Hl33  
8\8Z8Z  &2x7J7RDd 	D [	K (\:$>?? ;#88::(\:$>?? ;#88:: 8\22F'x66F' *++s83E/F/FFF%):::  (4466 	h17;; > ( 3 8 =IH H H H H H s8#56677 + +A% *1-~~h>PQR>S/T/T  + -5,>q,A)%dG44 +/3~a#hFYBZBZ>Z/[GG&*GEEE %)-%#.2 F 	 	 	 '+!..1B"C"CXEVWW 'HDDmY8I8K\^b   #'KHj11 'j:6V6V ' %-^ " "M-6x~-F-F " ")>%m^DD "!LLOOO!E" u::X^!4!444&--9O9OHKKmY   #'K >>-D8V^ ?    $ 		^      o[	 [	l r
  c                0   |j         rt          |j         t                    sdS t          |j         j                  }t          |          sdS t          |          }|sdS t          d |D                       r| j        	                    |           dS dS )a;  Generate error if the return type of __exit__ is problematic.

        If __exit__ always returns False but the return type is declared
        as bool, mypy thinks that a with statement may "swallow"
        exceptions even though this is not the case, resulting in
        invalid reachability inference.
        Nc              3  j   K   | ].}t          |j        t                    o|j        j        d k    V  /dS )builtins.FalseN)r  exprrv   r_  )r  rets     r  r  z7TypeChecker.check__exit__return_type.<locals>.<genexpr>  sR       
 
 sx**Tsx/@DT/T
 
 
 
 
 
r
  )
rj  r  r   r   r  has_bool_itemr   r  r  incorrect__exit__return)rJ  r  r  r  s       r  r>  z$TypeChecker.check__exit__return_type  s     y 	
49l C C 	F"49#566X&& 	F'-- 	F 
 

 
 
 
 
 	3 H,,T22222		3 	3r
  r[   c           
        |j         }|j        dd         D ]A}|j        r8|                     t          j                            |j                  |           B| j        	                    |j                   5  | 
                    d          5  | j        }t                      | _        | j                                        5  | j                            |j                   5  |                     |j                   ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   || _        |j         j        s-|j         j        s!|j         j        s|                     |           |j        s|                     |           |                     |           |                     |           |j        rt7          |j         | j                  }t;          |j                  D ]}t=          |t>                    rt=          |j         tB                    r2| j"                            |          }| #                    ||          }d}	t=          |tH                    r	|j%        pd}	| j&        }
d| _&        | j"        '                    ||gtP          j)        g||	          \  }}|
| _&        ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   |j*        j+        r|j,        D ]}t[          |j.        |j/        j*        j+                  D ]z\  }}t=          |t`                    r`|j1        td          k    rPt=          |t`                    r;|j1        |j1        k    r+|                     d|j         d|tf          j4        	           {|j5        r!|j*        j+        r| 6                    |           |j        s#|j         j        r| 7                    |           dS dS dS )
zType check a class definition.r   NT)rr  rJ  )callable_namezVariance of TypeVar "z+" incompatible with variance in parent type)r  rR  )8r  rI  rO  rm  r   CANNOT_INHERIT_FROM_FINALro  r  r!  r{  ra  r  r   rb  r#  r|  rh  rc  typeddict_type
tuple_typeis_enumcheck_init_subclasshas_incompatible_baseclasscheck_multiple_inheritancecheck_metaclass_compatibilitycheck_final_deletable
decoratorsr#   rl  reversedr  rZ   analyzedr|   r  	temp_noder~   r_  rF  
check_callr   rG   r  r  baseszipr  rj  r   rc  rN   r  r)   ru  check_protocol_variance
check_enum)rJ  r  r  rK  r  sig	decoratordectempr_  old_allow_abstract_callr  	base_inst	base_tvarbase_decl_tvars                  r  visit_class_defzTypeChecker.visit_class_def  s*   iGABBK 	^ 	^D} ^		*DKKDIVVX\]]][$$TY// '	G '	G1I1ISW1I1X1X '	G '	GJ/11DK..00 + +Z**4955 + +KK	***+ + + + + + + + + + + + + + ++ + + + + + + + + + + + + + + %DKI, /	0D /	HY /((...2 5//444..s333&&s+++ G,TYHH!)$/!:!: G GI!)X66 !:!*K< < ! !+229==C>>#y>AAD#H!)W55 >#,#5#= /3.F+/3D,!.99dVem_d( :  FC 0GD,,O'	G '	G '	G '	G '	G '	G '	G '	G '	G '	G '	G '	G '	G '	G '	G '	G '	G '	G '	G '	G '	G '	G '	G '	G '	G '	G '	G '	G '	G '	GT 8 	 Y  	14NIN$7$A2 2  -I~ #9k::%.);;&~{CC <*3y7III		;IN ; ; ;$(!&	 "     ? 	/sx1 	/((.... 	"493D 	"OOD!!!!!	" 	" 	" 	"s   8K4J: D!#D
	>D!
DD!DD!J:!D%%J:(D%)FJ:.K:J>	>KJ>	KKKc                    |j         D ]i}|j                            |          }|rKt          |j        t
                    r1|j        j        r%|                     t          j	        |j                   jd S rN  )
deletable_attributesr<  ri  r  r  r   rO  rm  r   CANNOT_MAKE_DELETABLE_FINAL)rJ  r  attrr  s       r  r  z!TypeChecker.check_final_deletable@	  s|     , 	S 	SD9==&&D S
49c22 Sty7I S		*F	RRR	S 	Sr
  c                   |j         j        r|j         j        j        j        dvrdS |j         j        dd         D ]3}d|j        vrt          |j                  }||_        t          |d          }t          |j                                                  }t          |j                                                  }d|v r?|                    d          }|                    |           |                    |           t           gt#          |          z  }t%          ||||          }	|j        |	_        |j        |	_        |j        |	_        | j                            |	dd            dS dS )a  Check that keywords in a class definition are valid arguments for __init_subclass__().

        In this example:
            1   class Base:
            2       def __init_subclass__(cls, thing: int):
            3           pass
            4   class Child(Base, thing=5):
            5       def __init_subclass__(cls):
            6           pass
            7   Child()

        Base.__init_subclass__(thing=5) is called at line 4. This is what we simulate here.
        Child.__init_subclass__ is never called.
        )builtins.typerP  Nr   rF  	metaclassTallow_none_returnalways_allow_any)r  r  rj  r_  rI  r<  rv   r  r  rt   r5  keywordsrf  keysindexr=  rF   rU  rZ   r  r7  end_liner  rh  )
rJ  r  rK  r3  calleer  rv  r  rp  	call_exprs
             r  r  zTypeChecker.check_init_subclassH	  sr    9# 		(@(E(N W
 )
 )
 F IM!""% 	 	D"$*44 ++I!IN	+>??F,,..//D*.t}/A/A/C/C*D*DI i''ook22c""""c$ii/I y)DDI!YIN#{I!%I$$Y$Y]$^^^ EE-	 	r
  c                   |j         j        sJ |j         j        t          vr||j         j                                        D ]]}t          |j        t                    rA|j        j	        r5|j        j
        dk    r%|                     t          j        |j                   ^|j         j        dd         D ]-}|j        r$|j        t          vr|                     ||           .|                     |           |                     |           d S )N__members__r   rG  )r  r  r_  r   r<  rf  r  r  r   has_explicit_valuer  rm  r   #ENUM_MEMBERS_ATTR_WILL_BE_OVERRIDENrI  check_final_enumcheck_enum_basescheck_enum_new)rJ  r  rd  rK  s       r  r  zTypeChecker.check_enumx	  s   y    9Z//y--// ^ ^sx--^3^ 66 II.RTWT\]]]IM!B$' 	2 	2D| 2Z ? ?%%dD111d###D!!!!!r
  c                    |j                                         D ]9}|                     |          r"|                     d|j         d|            d S :d S )Nz+Cannot extend enum with existing members: "rI  )r<  rf  is_final_enum_valuerm  r  )rJ  r  rK  rd  s       r  r  zTypeChecker.check_final_enum	  sn    :$$&& 	 	C'',, 		T	TTTVZ[[[	 	r
  c                   t          |j        t          t          f          rdS t          |j        t                    sdS t          |j        j                  s^t          |j        j                  sEt          |j        j                  s,t          t          |j        j
                  t                    rdS | j        p|j        j        S )NFT)r  r  rf   r_   r   rz  r  r   r   r   rj  r   r@  r  rJ  rd  s     r  r  zTypeChecker.is_final_enum_value	  s    ch9 566 	5#(C(( 	4 sx}%%		''		 ''		 /#(-88,GG		 5|:sx::r
  c                    d}|j         j        D ]V}||j        j        r|}|A|j        j        s5|                     d|                    | j                   d|            dS WdS )a  
        Non-enum mixins cannot appear after enum bases; this is disallowed at runtime:

            class Foo: ...
            class Bar(enum.Enum, Foo): ...

        But any number of enum mixins can appear in a class definition
        (even if multiple enum bases define __new__). So this is fine:

            class Foo(enum.Enum):
                def __new__(cls, val): ...
            class Bar(enum.Enum):
                def __new__(cls, val): ...
            class Baz(int, Foo, Bar, enum.Flag): ...
        Nz-No non-enum mixin classes are allowed after "rI  )r  r  rj  r  rm  rx  r-  )rJ  r  	enum_baserK  s       r  r  zTypeChecker.check_enum_bases	  s      &*	IO 		 		D TY%6  	&ty/@&		oID^D^_c_kDlDlooo   		 		r
  c                ^   ddd}|j         j        D ]}d}|j        j        r.t	          fd|j        j        dd	         D                       }n |j                  }|rD|rB|                     d
                    |                    | j	                            |           |rd}d S )Nr  r   r9  r  c                x    |                      d          }t          |o|j        o|j        j        dk              S )NrH  zbuiltins.object.__new__)ri  r  r  r_  )r  
new_methods     r  has_new_methodz2TypeChecker.check_enum_new.<locals>.has_new_method	  sG    ),,J JOJO,0II  r
  Fc              3  >   K   | ]}|j          o
 |          V  d S rN  )r  )r  br  s     r  r  z-TypeChecker.check_enum_new.<locals>.<genexpr>	  s6      aaAI C..2C2Caaaaaar
  r   rG  zLOnly a single data type mixin is allowed for Enum subtypes, found extra "{}"T)r  r   r9  r  )
r  r  rj  r  r  rI  rm  ro  rx  r-  )rJ  r  has_newrK  	candidater  s        @r  r  zTypeChecker.check_enum_new	  s    	 	 	 	 IO 	 	DIy  6aaaaTY][\]_[_M`aaaaa		*N4955	 W 		''-vd.C.CDL.Q.Q'R'R   
  !	 	r
  c                D  
 |j         }t          |j        d         g           |j        j        }t          |          D ]\  
}t          |t                    s
fdt          |          D             }
fdt          |          D             }t          ||          t          ||          }}t          ||d          rt          }	n!t          ||d          rt          }	nt          }	|	|j        k    r'| j                            |j        |j        |	|           dS )a/  Check that protocol definition is compatible with declared
        variances of type variables.

        Note that we also prohibit declaring protocol classes as invariant
        if they are actually covariant/contravariant, since this may break
        transitivity of subtyping, see PEP 544.
        rG  c                X    g | ]&\  }}|k    rnt          t          j                  'S r	  )r   r   r   )r  r  r  r  object_types      r  r  z7TypeChecker.check_protocol_variance.<locals>.<listcomp>	  sD     # # #Aq  !Avv793I+J+J# # #r
  c                p    g | ]2\  }}|k    rt                      nt          t          j                  3S r	  )r   r   r   r   )r  r  r  r  s      r  r  z7TypeChecker.check_protocol_variance.<locals>.<listcomp>	  sK     % % %Aq &'!VV!!!9O1P1P% % %r
  T)ignore_declared_varianceN)r  r   rI  r  r  r  r  r   r   rJ   rI   rN   rc  r  bad_proto_variancer  )rJ  r  r  r=  r4  up_args	down_argsupdownrS  r  r  s             @@r  r  z#TypeChecker.check_protocol_variance	  s^    ytx|R00	# '' 	V 	VGAtdK00 # # # # #%e,,# # #G% % % %%e,,% % %I  g..y0I0IB$TBBB %$BtDDD %($4=((++DM49hPTUUU+	V 	Vr
  c           	        t          |j                  dk    rdS |j        dd         }t          |          D ]\  }}|j                                        |j                                        z
  }|D ]L}t          |          r||dz   d         D ],}||j        v r!||j        vr|                     ||||           -MdS )z.Check for multiple inheritance related errors.r   N)rU  r  rI  r  r<  r  rz  check_compatibility)rJ  r  rI  r  rK  non_overridden_attrsr  base2s           r  r  z&TypeChecker.check_multiple_inheritance
  s    sy>>QFgabbk ~~ 	I 	IGAt $(:??#4#4sy~~7G7G#G , I Id##  Q\ I IE u{**uDH/D/D00tUCHHHII		I 	Ir
  c                   |j         |j         S t          |j        t                    r|                     |j                  S t          |j        t
                    rE|j        j        r| j                            |j                  S t          |j        | j
                  S t          |j        t                    rt          t          j                  S t          |j        t                    rW| j                                        5  | j                            |j        |j                  cd d d            S # 1 swxY w Y   d S )N)r  )rj  r  r  rf   r   r   r  r  typeddict_callabler#   rl  r   r   r   r   r   r  filter_errorsalias_type_in_runtime_contextr  s     r  determine_type_of_memberz$TypeChecker.determine_type_of_member
  so   88Och)) 	0%%ch///ch)) 	Cx& C(;;CHEEE'$/BBBch,, 	3 91222ch	** 	_'')) _ _ (FFsxUXU]F^^_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
 ts   &D;;D?D?base1r  c                F   |dv rdS |j         |         }|j         |         }t          |                     |                    }t          |                     |                    }d}	t          |t                    rt          d||d          }	|	rAt          |t                    r,|                     ||||          }
t          |	|
d          }nt          |t                    rt          |t                    r|	                                re|	                                rQt          t          |                                          t          |                                                    }no|                     ||||          }|                     ||||          }
t          ||
d          }n+|r|rt          |j        t                    r#t          |j        |t          |                    }t          |j        t                    r#t          |j        |t          |                    }t!          ||          }|sR||         j        }t          |t                    r0|.t#          |          rt%          |          }t          ||          }nH|!| j                            ||j        |           |!| j                            ||j        |           d}t-          |j                  r0t/          |          s!| j                            ||j        |           t-          |j                  r|                     ||j        |           t          |j        t                    r|j        j        rd}|s| j                            ||||           dS dS )ay  Check if attribute name in base1 is compatible with base2 in multiple inheritance.

        Assume base1 comes before base2 in the MRO, and that base1 and base2 don't have
        a direct subclass relationship (i.e., the compatibility requirement only derives from
        multiple inheritance).

        This check verifies that a definition taken from base1 (and mapped to the current
        class ctx), is type compatible with the definition taken from base2 (also mapped), so
        that unsafe subclassing like this can be detected:
            class A(Generic[T]):
                def foo(self, x: T) -> None: ...

            class B:
                def foo(self, x: str) -> None: ...

            class C(B, A[int]): ...  # this is unsafe because...

            x: A[int] = C()
            x.foo  # ...runtime type is (str) -> None, while static type is (int) -> None
        )rK  rH  rF  Nr  Tr  rw  leftright)r<  r   r  r  r   r   r   rY  r   is_type_objr   type_objectr  r   r0   r   r   r  rZ  r  cannot_determine_type_in_baser  r   rz  rN  rP  allow_incompatible_override#base_class_definitions_incompatible)rJ  r  r  r  r  r  r  
first_typesecond_typecall
second_sigok	first_sigsecond_nodes                 r  r  zTypeChecker.check_compatibility/
  s   . ???FD!T"$T%B%B5%I%IJJ
%d&C&CF&K&KLL j(++ 	Uz:ztTTTD $	J{L99 $	11&+sERRJD*4HHHBB
L11 !	jl6[6[ !	%%'' RK,C,C,E,E R  /
0F0F0H0HII01H1H1J1JKK   !44UJUSS	!55fk3PUVV
	:DQQQ 	K 	%*c** Z-ej*mTWFXFXYY
&+s++ ].v{KWZI[I[\\z;77B =#Dk.{L99=#/#K00 0 #4K"@"@K#J<<B!66tUZMMM"66tUZMMMB %% 	@j.>.> 	@H((uz3???$$ 	N55dFKMMM fk3'' 	FK,S 	B 	RH88ueSQQQQQ	R 	Rr
  c                                                    sj        sj        sj        sj        dS d j        dd         D             }|sdS j        t          fd|D                       rdS |                     d           dS )z<Ensures that metaclasses of all parent types are compatible.Nc                T    g | ]%}|j         	t          |j         d           |j         &S )r  )r  r   )r  entrys     r  r  z=TypeChecker.check_metaclass_compatibility.<locals>.<listcomp>
  sK     
 
 
#
 &e&:OLL	
 
 
 
r
  r   rG  c              3  B   K   | ]}t          j        |          V  d S rN  )r   r  )r  metar  s     r  r  z<TypeChecker.check_metaclass_compatibility.<locals>.<genexpr>
  sA       2
 2
59Js)4002
 2
 2
 2
 2
 2
r
  zxMetaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases)	r  ru  is_named_tupler  r  rI  r  r  rm  )rJ  r  metaclassess    ` r  r  z)TypeChecker.check_metaclass_compatibility
  s     		 !	 {		
 !-F
 
2
 
 
  	F)c 2
 2
 2
 2
=H2
 2
 2
 /
 /
) F		J	
 	
 	
 	
 	
r
  rm   c                0    |                      |           d S rN  check_importr  s     r  visit_import_fromzTypeChecker.visit_import_from
      $r
  rk   c                0    |                      |           d S rN  r  r  s     r  visit_import_allzTypeChecker.visit_import_all
  r	  r
  rj   c                0    |                      |           d S rN  r  r  s     r  visit_importzTypeChecker.visit_import
  r	  r
  rl   c           	     f   |j         D ]}|j        d         }|                     |          \  }}}|t          t          j                  }t          |j        t                    sJ t          j
                            |j        j                  }|                     ||j        ||dd           d S )Nr   z
local namezimported name)r  r  r  )assignmentslvaluescheck_lvaluer   r   r   r  rvaluerv   r   INCOMPATIBLE_IMPORT_OFro  r  r  )rJ  r  assignlvaluelvalue_typer  r  messages           r  r  zTypeChecker.check_import
  s    & 	 	F^A&F!%!2!26!:!:KB"%i&<==fmX66666&=DDV]EWXXG(((+ )    	 	r
  r  rW   c                N   |j         r| j                                         d S |j        D ]z}| j                                         rJ|                                 s d S |                     |          s| j                            |            d S e|                     |           {d S rN  )	rd  r  r  r  re  rf  r  rg  rh  )rJ  r  ss      r  visit_blockzTypeChecker.visit_block
  s     	 K##%%%F 	 	A{))++ <<>> EE44Q77 H221555EE A	 	r
  c                    |                                  o-| j        j        o!| j         o| j                                         S rN  )in_checked_functionr-  warn_unreachabler?  r  !is_unreachable_warning_suppressedrP  s    r  re  z,TypeChecker.should_report_unreachable_issues
  sO    $$&& D-D..D KAACCC		
r
  r  c                &   t          |t                    rt          |j                  rdS t          |t          t
          f          rdS t          |t                    rt          |j        t                    rdS t          |j        t                    r|| j	        j
                                        5  t          | j	                            |j        dd                    }ddd           n# 1 swxY w Y   t          |t                    rdS dS )a  Returns 'true' if the given statement either throws an error of some kind
        or is a no-op.

        We use this function while handling the '--warn-unreachable' flag. When
        that flag is present, we normally report an error on any unreachable statement.
        But if that statement is just something like a 'pass' or a just-in-case 'assert False',
        reporting an error would be annoying.
        Tr  NF)r  rT   is_false_literalr  r}   r{   rc   ra   rZ   r  r  r  r   rh  r   )rJ  r  r  s      r  rf  z$TypeChecker.is_noop_for_reachability
  sF    a$$ 	 )9!&)A)A 	 4Ix011 	 4>** 	 !&,//  tAFH-- 	 &*88::  ))00FdT 1   C               c?33  4us   30C//C36C3rV   c                   |j         r| j        sf|                     |j                  5  |                     |j        d         |j        |j        du |j                   ddd           n# 1 swxY w Y   |j         r| 	                    |           |j        | j
        j        rt          |j                  rut          |j        d         t                    r4| j                            dt#          t$          j                  |           n!| j                            d|j        |           t)          |j        | j
        | j        | j        |           t-          |j                  dk    r|                     |j                  s| j                            |j                   |                     |                     |j                  |          }|j        dd         D ]R}|                     |j                  5  |                     |||j        du            ddd           n# 1 swxY w Y   S|                     |           |j        rT|j        rMt;          |j                  s9| j                                         |                      tB          j"        |           |j#        r1| $                                s| j        %                    |           dS dS dS )z|Type check an assignment statement.

        Handle all kinds of assignment statements (simple, indexed, multiple).
        rG  NzA type on this linezType of variablerJ  r   )&is_alias_defr@  enter_final_contextis_final_defcheck_assignmentr  r  rj  
new_syntaxcheck_type_alias_rvaluer-  r]  r   r  r   r  r^  r   r   r   r   rB  rU  has_typer  rh  r  lookup_typecheck_finalr   r#  r  rm  r   DEPENDENT_FINAL_IN_CLASS_BODYunanalyzed_typer   annotation_in_unchecked_function)rJ  r  r  lvs       r  visit_assignment_stmtz!TypeChecker.visit_assignment_stmt
  s|     	]4< 	]))!.99 ] ]%%aimQXqv~q|\\\] ] ] ] ] ] ] ] ] ] ] ] ] ] ] > 	,((+++ F4 ,QV44  !)B-33 T 44)793I+J+JA    445GQRSSSqvt|T5JDH^_````qy>>A ==** 3!((222^^D$4$4QX$>$>BBFin F F--an== F F))"fafnEEEF F F F F F F F F F F F F F F 	N	I	I $AF++	I 
''))5II&DaHHH 	AT%=%=%?%? 	AH55a5@@@@@	A 	A 	A 	As#   5A**A.1A.?H**H.	1H.	c                    | j                             |j                  }|                     |j        d         |           d S NrG  )r  rh  r  
store_typer  )rJ  r  
alias_types      r  r'  z#TypeChecker.check_type_alias_rvalue,  s:    &--ah77
	"z22222r
  Tr  rr   r  rb   infer_lvalue_typer&  c                   t          |t          t          f          r|                     |j        |||           dS |                     ||d           |                     |          \  }}}t          |t                    r3|j        r+|j        j	        }|dv rU|r|}	n| j
                            |          }	|	r4|dk    r|                     |	|           n|                     |	||           |dk    r2|p| j
                            |          }
|                     |
|           |dk    r3|1| j
                            |          }
|                     ||
|           |dk    rO| j                                        }|r4t%          j        |          r |                     t*          j        |           t          |t.                    rUt          |t0                    r|j        9t          |t                    r|j	        dk    s|                     |||          rdS t          |t6                    r+|j	        dk    r |                     t*          j        |           |r.t          |t0                    r#|j        | j
                            |          }t          t;          |          t<                    rdS |j        }tA          ||j!                  rt| "                    |          }|\| j#        sDtI          |          }tK          |t=                      g          }| &                    |||           nd|_        ||= |j        }n| '                    |j        ||           t          |t.                    r|                     |||          rdS ntQ          |          rAt          |t                    r,t          |j        tR                    r|j        j*        r|s|}nkt          |t6                    rE|j+        >| j
                            |j,                  }| -                    ||||	          \  }}}ntQ          |          rt          |t                    r|j+        t\          k    rt          |j        tR                    r|j        j        r|j        | j/        v rt          t;          |          t`                    so| j/        |j                 }d
}tc          | j2        j3                  D ]}|j4        |v r	|j5        } n|r+tm          |          }| &                    |j        ||           | 7                    |||	          }t;          |          }t;          |          }t          |tp                    r|9                                r|:                                j;        s|:                                j<        rnt          |tz                    rYt          |j>        t~                    r?|j>        j        j;        s|j>        j        j<        r| j@        A                    ||           dS |rP|rNt          |t0                    s9t          |t                    r|jB        s| j2        C                    |||d
           n|r| D                    |||           |r| E                    |          }| j
                            ||          }|j!        s/t          |t                    r|j	        dk    stI          |          }| F                    ||||           | G                    |           dS )z0Type check a single assignment: lvalue = rvalue.=)rM  rL  rK  rM  	__slots____match_args__Nr9  is_lvalue_finalrJ  FrO  )Hr  r   rq   $check_assignment_to_multiple_lvaluesr  .try_infer_partial_generic_type_from_assignmentr  rv   r  r  r  rh  rb  ra  r'  r-  r#  r  r?  r@  rm  r   &DATACLASS_POST_INIT_MUST_BE_A_FUNCTIONr~   r   rj  check_compatibility_all_supersrt   CANNOT_MODIFY_MATCH_ARGSr   r   r  is_valid_inferred_typerO  r+  r?  r(   r   set_inferred_typeinfer_partial_typeis_literal_noner   is_initialized_in_classkindr  check_member_assignmentrP   r2  r   r  r  r}  r2  r  r   r  r   r  r  is_abstractru  r   r  r   r  concrete_only_assignis_special_formr-  check_indexed_assignmentget_variable_type_contextinfer_variable_typecheck_assignment_to_slots)rJ  r  r  r4  r&  r  index_lvalueinferredr  	signaturer  r  rvalue_typer  r'  inferred_typeinstance_typedecl_frame_maphas_if_ancestorr  p_rvalue_typep_lvalue_typerO  s                          r  r%  zTypeChecker.check_assignment0  s    fy(344 a	355ff.?     ??PSTTT262C2CF2K2K/Kx &(++ c c{'MMM" E$/		$($5$<$<V$D$D	  O=00 55iHHHH 55iNNN;&&%I):)A)A&)I)IC//V<<<+++0D+226::C))(C@@@?**#':#:#:#<#<L# c(:(QR^(_(_ c		"2"Y[abbb 67++#K==BMBRBZ#FH55 C[:@+IY:Y:Y66v{FSS F&*-- M&+AQ2Q2Q		*CVLLL gLk;77 KdK<L<T"&"3":":6"B"BK!/+">">II  %/C-k3<XXX V(,(?(?(D(D(4#'#= 0.OP[.\.\0E{T\T^T^F_0`0` $ 6 6sFM R R R R+/ -c 2*-(K //UUU!&'22 t7Z7ZV8 8  #F++,d"6844,d #6;44,d ;	,d
 ',d #.KKvz22#d7={7J$($5$<$<V[$I$IMBFB^B^%{FF C_ C C?K.?.? (//U&vx88U #K4//&v{C88 0"K, 0 #K4+??? *?;+G+G Q Q @ *.)=fk)J +0%-dk.@%A%A & &E$x>99272I %  : + U*<[*I*IK 226;TTT"&">">{F\b">"c"cK !0 < < / < <}l;;%1133 &1133?	
 )4466B #=(;; #=#5x@@ &*/; @M?Q?V?b H11-HHHF Y#4 YZU`=a=a Y&vx88 YV=S Y//[RWXXX L--lFFKKK P#==hGG"/66vL6YY%Q"6844Q9?HX9X9X"CK"P"PK((6;OOO**622222r
  )builtins.list+)builtins.set|r
   partial_type_augmented_opsrP  c                v   g }|j         rx|j         j        dd          D ]c}|                     ||          \  }}|rFt          |t                    r|j        s*t          |t                    s|                    |           d|sd S |d         }|D ](}t          ||          r|}t          ||          s d S )|S Nr   r   )
r  rI  lvalue_type_from_baser  r   invalid_partial_typer   r  r   r   )rJ  rP  type_contextsrK  	base_type	base_noder  others           r  rL  z%TypeChecker.get_variable_type_context  s    = 	4 )!""- 4 4'+'A'A(D'Q'Q$	94'	3774<E<Z4 'y+>>4
 "((333 	4!!$	" 	 	E 	22 !			511 tt r
  opc                   d}t          |t                    rAt          |j        t                    r't          |j        j        t
                    r|j        }n/t          |t                    r| j                            |          }||j        }t          |t
                    sJ |j        dS |dk    r|j        j	        |f| j
        vrdS |                     |          }|dS | j                            |          }t          |          }t          |t                    r/|j        |j        k    rt          |          r||_        ||= dS dS dS t          |t                     r t#          |j                  |_        ||= dS dS dS )aE  Try to infer a precise type for partial generic type from assignment.

        'op' is '=' for normal assignment and a binary operator ('+', ...) for
        augmented assignment.

        Example where this happens:

            x = []
            if foo():
                x = [1]  # Infer List[int] as type of 'x'
        Nr6  )r  rv   r  r   rj  r   rt   r  get_partial_self_varr_  r]  r+  rh  r   r   rA  r   r   )rJ  r  r  rf  r  r  r'  rR  s           r  r=  z:TypeChecker.try_infer_partial_generic_type_from_assignment  s    vx((	A6;,,	A 6;+[99	A
 +CC
++ 	A#88@@C?(Cc;/////xSyych/4D<[[[ !33C88M$+226::K)+66K+x00 '#sx//4J;4W4W/*CH%c*** 0/// K11 '1#(;;!#&&&+ ?&' 'r
  r~   r  c           	        |j         }t          |t                    rk|j        t          d fv rZt          |j        j                  dk    r<|j        j        dd          D ]_}|j	        
                    |j                  }|<|                     |||j                   s n|                     |||j                   s n`|j                                        }|r|d         nd }|j        j        dd          D ]}|j        r|j        dk    r|j        dk    s t#          |j                  r5|                     ||          \  }	}
t          |	t&                    rd }	|	r'|
J |                     |||||	|
          s dS ||u r ndS )Nr   r   rG  r7  r  TF)r  r  r   rF  rR   rU  r  r  rI  r<  ri  r  "check_compatibility_classvar_supercheck_compatibility_final_superdirect_base_classesr  r_  rz  r`  r   check_compatibility_super)rJ  r  r  r  lvalue_noderK  tnodedirect_baseslast_immediate_baserc  rd  s              r  r?  z*TypeChecker.check_compatibility_all_supers#  s    k {C((.	d|++K$*++a//#(,QRR0 	 	
{'788$BB;PTV[V`aa ??TSXS]^^ &+??AAL6B"L,r"2"2#(,QRR0   : $33IZ8Z8Zk.// '+'A'A+t'T'T$	9i55 % $I $00099VT9i  $
  $tt222 ur
  rc  rd  rw   c           
        |j         }t          |t                    sJ d }|r
|}	|j         }nS| j                            ||          }	t          |t
                    r#|j         }t          |t                    r|j        }t          |          }t          |	          }	|	rQt          |t                    rt          |	t                    rt          |          }
t          |          }||	j        rt          |	j                  }|
du rR|du rNt          || j                                                  }t          |	| j                                                  }	|
r	|rd|_        |                     |	||t"          j        dd|j         d          }|rZt(          j        | j        j        v rB|                     |          r-|                     ||	|t"          j        d|j         dd          }|S dS )NFTzexpression has typezbase class "z" defined the type as)r  r  r   r  rh  rv   r_   r  r   r   is_node_staticr  r   r#  rm  is_staticmethodr.  r    INCOMPATIBLE_TYPES_IN_ASSIGNMENTr  r  MUTABLE_OVERRIDEr-  enabled_error_codesr^  'COVARIANT_OVERRIDE_OF_MUTABLE_ATTRIBUTE)rJ  r  r  r  rK  rc  rd  rn  compare_nodecompare_typebase_staticcompare_staticr  s                r  rm  z%TypeChecker.check_compatibility_superY  s%    k+s+++++  	5&L!;LL,33FIFFL&(++ 5%{lI66 5#/#4L#I..	&|44 /	)\22 7z,P\7]7] 7,Y77!/!=!= ")l.E)%3L4K%L%LN %''Ne,C,C !*)TZ5P5P5R5R S SI#,\4:;V;V;X;X#Y#YL  7> 726K/## A%?ty??? B *dl.NNN..y99 O '' $LC49CCC)  Itr
  	expr_nodetuple[Type | None, Node | None]c                V   |j         }|j                            |          }|r|j        }|j        }t          |t                    r%|#t          ||t          |j	                            }t          |t                    r|j        }|j        }|rt          |          sl| j                                        }|
J d            t          |t                    rt!          |          }n|}t#          ||          }	t%          ||	          }t'          |          }t          |t(                    r0t          |t*                    r|j        rt'          |j                  }t          |t0                    r.t          |t2                    r|j        r|j        d         j        }||fS dS )zFor a NameExpr that is part of a class, walk all base classes and try
        to find the first class that defines a Type for the same name.Nz)Internal error: base lookup outside classr   NN)r  r<  ri  r  rj  r  r   r0   r   r  r_   r  r   r#  rm  r   r   r7   r2   r   r   rg   r  r  r   rz   r  )
rJ  r}  rK  	expr_namebase_varrd  rc  r  r  itypes
             r  r`  z!TypeChecker.lvalue_type_from_base  s   
 N	:>>),, !	, I I)S)) bi.C,Y	=QZQ_C`C`aa	)Y// +%N	%N	 ,&y11 J $
 ; ; = =I$002]000!)Y77 -#1)#<#<#,5hEEE 7	5 I II+I66	i66 H:iQX;Y;Y H !, H$3I4F$G$G	i66 @:0< < @ !, @$-OA$6$?	 )++zr
  Node | Nonec                T   t          |t                    sdS |j        rA|j        s:|                     t          j                            |j                  |           dS |j        sA|j        r:|                     t          j                            |j                  |           dS dS NTF)	r  r   is_classvarrm  r   CANNOT_OVERRIDE_INSTANCE_VARro  r  CANNOT_OVERRIDE_CLASS_VARrJ  r  rK  rd  s       r  rj  z.TypeChecker.check_compatibility_classvar_super  s     )S)) 	4 	I$9 	II&CJJ49UUW[\\\5! 	i&; 	II&@GG	RRTXYYY5tr
  c                   t          |t          t          t          f          sdS t	          |j                  rdS |j        rD|j        st          |t                    s(| j                            |j        |j        |           dS |j        r:|j	        t          v s|j        t          v rdS |                     |j        ||           dS )a  Check if an assignment overrides a final attribute in a base class.

        This only checks situations where either a node in base class is not a variable
        but a final method, or where override is explicitly declared as final.
        In these cases we give a more detailed error message. In addition, we check that
        a final variable doesn't override writeable attribute, which is not safe.

        Other situations are checked in `check_final()`.
        TF)r  r   rf   r_   rz  r  rO  r  rN  r_  r   r   rP  r  s       r  rk  z+TypeChecker.check_compatibility_final_super  s     )c8Y%?@@ 	4di   	4 	4= 	
9c8R8R 	
 H((DItDDD5= 	R}
**di;M.M.Mt55diDQQQtr
  c                v    d}|r|                      |          }|r| j                            ||           dS dS )a  Check that a final variable doesn't override writeable attribute.

        This is done to prevent situations like this:
            class C:
                attr = 1
            class D(C):
                attr: Final = 2

            x: C = D()
            x.attr = 3  # Oops!
        TN)r^  r  final_cant_override_writable)rJ  r  rd  r  writables        r  rP  z0TypeChecker.check_if_final_var_override_writable  sT      	=11)<<H 	=H11$<<<<<	= 	=r
  c                    | j         S )z:Check whether we a currently checking a final declaration.rD  rP  s    r  get_final_contextzTypeChecker.get_final_context  s    !!r
  r$  c              #  V   K   | j         }|| _         	 dV  || _         dS # || _         w xY w)zDStore whether the current checked assignment is a final declaration.Nr  )rJ  r$  old_ctxs      r  r#  zTypeChecker.enter_final_context  sH       $)	)EEE!(DD((((s    	(8AssignmentStmt | OperatorAssignmentStmt | AssignmentExprc                x   t          |t                    r|                     |j                  }n&t          |t                    r	|j        g}n|j        g}t          |t                    r|j        nd}|r| j        	                                r|d         }t          |t                    sJ |j        qt          |j        t                    sJ |j        j        rI|j        j        s=| j        s6t          |t                    r!|j        | j                            |           |D ]}t          |t                    rt          |j        t                    r|j        j        }| j        	                                }||j        dd         D ]p}|j                            |          }|rRt          |j        t                    r8|j        j        r,|s*| j                            ||j        j        du |            nq|j        j        r*|s(| j                            ||j        j        du |           dS )a  Check if this assignment does not assign to a final attribute.

        This function performs the check only for name assignments at module
        and class scope. The assignments to `obj.attr` and `Cls.attr` are checked
        in checkmember.py.
        Fr   Nr   )r  rV   flatten_lvaluesr  rU   targetr  r$  r#  r  r~   r  r   final_unset_in_classfinal_set_in_initr@  rj  r  final_without_valuer  rI  r<  ri  rO  cant_assign_to_finalr  )	rJ  r  lvsis_final_declr.  r  rO  rK  rd  s	            r  r*  zTypeChecker.check_final!  sL    a(( 	&&qy11CC>** 	8*CC8*C*4Q*G*GRU 	4TZ4466 	4QBb'*****w"!"'3/////G0
4G5
4 !L
4 q.11
4 *H00333 	Q 	QB"g&& Q:bgs+C+C Qw|j--//? !$ 	& 	&"jnnT22  &:ch#<#< &"x0 & & $ = =dCHMUYDY[\ ] ] ] %7# QM QH11$8LaPPP'	Q 	Qr
  c                n   t          |t                    sd S t          | j                            |j                            }t          |t                    sd S |j        j        d S |j	        |j        j        v rd S |j        j
        d d         D ]}|j                            d           d S  |j                            |j	                  }|d S |                     ||j                  rd S |                     t          j                            |j	        |j        j                  |           d S )NrG  rM  )r  rt   r   r  rh  r  r   rj  slotsr  rI  r<  ri  is_assignable_slotrm  r   NAME_NOT_IN_SLOTSro  r_  )rJ  r  inst	base_infor  s        r  rN  z%TypeChecker.check_assignment_to_slotsT  s8   &*-- 	Ft077DDEE$)) 	F9?"F;$)/))Fss+ 	 	I""=11= 	 > Y]]6;//
 F""6:?;; 	F		.55fk49CUVVX^	
 	
 	
 	
 	
r
  c                |    t          dd           rdS t          |          }|t          |t                    rdS t          |t                    r|j                            d          d uS t          |t                    rdS t          |t                    r!t           fd|j
        D                       S dS )Nr  FT__set__c              3  D   K   | ]}                     |          V  d S rN  )r  )r  ur  rJ  s     r  r  z1TypeChecker.is_assignable_slot.<locals>.<genexpr>  s3      MMat..vq99MMMMMMr
  )getattrr   r  r   r   rj  ri  r   r   r  r  )rJ  r  r  s   `` r  r  zTypeChecker.is_assignable_slots  s    664(( 	5c"";*S'22;4c8$$ 	7
 8<<	**$66c<(( 	4c9%% 	NMMMMM39MMMMMMur
  rvalueslist[Expression]c           	        g }|D ]P}t          |t                    s|                    |           .t          | j                            |j                            }t          |t                    s|                    |           |j        D ]}t          |t                    s#|                    t          |                     :t          |j                  }t          |t                    r|j        }n)t          |t                    r|j        j        dk    sJ |}|                    t          t          |                               ĐR|S )zFlatten expression list by expanding those * items that have tuple type.

        For each regular type item in the tuple type use a TempNode(), for an Unpack
        item use a corresponding StarExpr(TempNode()).
        builtins.tuple)r  r   r  r   r  rh  r  r   r  r   r   rj  r   r  r   r_  )rJ  r  new_rvaluesrvr  r  unpackedr  s           r  flatten_rvalueszTypeChecker.flatten_rvalues  s`     	E 	EBb(++ ""2&&&!$"3":":27"C"CDDCc9-- ""2&&&Y E E!!Z00 E&&x{{3333.qv66H!(,<== ,#+#7 'x:: ( 6:J J J J J#+&&x0B0B'C'CDDDDE r
  r  list[Lvalue]c                :   t          |t          t          f          rg }d d }t          |                     |j                            D ]T\  }}t          |t                    r#t          | j        	                    |j
                            }	|                     |	          rt          |	t                    r;|                     |	|          k    r!|                     t          j        |           |	|dz   |k    r.|                    |           |}|                     |	|          |                     t          j        |           
|                     t          j                            |	          |           ?|                    |           Vd }
d }t          |          D ]v\  }}t          |t                    r\t          | j        	                    |j
                            }	|                     |	          rt          |	t                    r|
|}
|}w|
h|fd||
z
  dz   }t)          |          t)          |          |z
  z
  }|dk    r4|d|
         fdt+          |          D             z   ||dz   d          z   }|                     |t)          |          |          rDt/          d t          |          D             t)          |                    }|d |         }|t)          |          k    rt1          t          ||                   nd }||dz   d          }|                     ||t)          |                    \  }}}t5          t7          ||                    }|r@t          |          }|                    |           |                    |j
        |f           |                    t7          ||                     |D ]\  }}|                     |||           d S d S |                     ||||           d S )Nr   r   c                .    g | ]}t                    S r	  r   )r  r  iterable_types     r  r  zDTypeChecker.check_assignment_to_multiple_lvalues.<locals>.<listcomp>  s!    QQQq8M22QQQr
  c              3  J   K   | ]\  }}t          |t                    |V  d S rN  r  r   r  r  r.  s      r  r  zCTypeChecker.check_assignment_to_multiple_lvalues.<locals>.<genexpr>  s5      SS51b*R:R:RSQSSSSSSr
  ) r  r   rq   r  r  r  r   r   r  rh  r  type_is_iterabler   iterable_item_typerm  r   CONTIGUOUS_ITERABLE_EXPECTEDr  ITERABLE_TYPE_EXPECTEDro  rU  rn   check_rvalue_count_in_assignmentnextr   split_around_starr5  r  set_lineextendr%  check_multi_assignment)rJ  r  r  r  r4  r  last_idxidx_rvalrvaltypsiterable_startiterable_endr  iterable_numrvalue_needed
star_indexleft_lvsstar_lv	right_lvsleft_rvsstar_rvs	right_rvslr_pairsrv_listr.  r  r  s                             @r  r<  z0TypeChecker.check_assignment_to_multiple_lvalues  sw    fy(344 K	U
 )+G)-M#'H"+D,@,@,N,N"O"O ) )$dH-- )*4+<+C+CDI+N+NOOD,,T22 az$7Q7Q a(4$JaJa &K K : : !II&6&SU\]]]]'/8a<83K3K 't 4 4 4+3040G0Gf0U0U $		*:*WY` a a a a		"2"I"P"PQU"V"VX_````NN4(((()-N'+L$W-- ) )4dH-- )*4+<+C+CDI+N+NOOD,,T22 )z$7Q7Q ))1-.N'(* ,!-+n<q@ #GG|0K L 1$$. 01QQQQE-<P<PQQQR!,"2"4"456  44Wc'llGTT E!SSIg$6$6SSSUXY`UaUa 
 #;J;/;EW;U;UD7:#6777[_  $JN$4$45	040F0FZW1 1-(I  Hh 7 788 =&x00G$$V,,,OOW\7$;<<<Iy 9 9:::& E EFB))"b2CDDDD/E E,E E ''BSTTTTTr
  rvalue_countrvalue_unpack
int | Nonec                   |t          d |D                       s|                     d|           dS t          |          |k    r"|                     t          j        |           dS t          d t          |          D                       }|}t          |          |z
  dz
  }|}||z
  dz
  }	||	k    s||k    r |                     t          j        |           dS t          d |D                       rEt          |          dz
  |k    r.| j                            |t          |          dz
  |           dS n>|t          |          k    r+| j                            |t          |          |           dS dS )Nc              3  @   K   | ]}t          |t                    V  d S rN  r  )r  es     r  r  z?TypeChecker.check_rvalue_count_in_assignment.<locals>.<genexpr>  s,      @@1z!X..@@@@@@r
  z/Variadic tuple unpacking requires a star targetFc              3  J   K   | ]\  }}t          |t                    |V  d S rN  r  r  s      r  r  z?TypeChecker.check_rvalue_count_in_assignment.<locals>.<genexpr>  s7      "a"aB
SUW_H`H`"a1"a"a"a"a"a"ar
  r   Tc              3  @   K   | ]}t          |t                    V  d S rN  r  )r  r  s     r  r  z?TypeChecker.check_rvalue_count_in_assignment.<locals>.<genexpr>  s,      BBz&(++BBBBBBr
  )	r  rm  rU  r   $TOO_MANY_TARGETS_FOR_VARIADIC_UNPACKr  r  r  wrong_number_values_to_unpack)
rJ  r  r  r  r  left_star_indexleft_prefixleft_suffixright_prefixright_suffixs
             r  r  z,TypeChecker.check_rvalue_count_in_assignment  s    $@@@@@@@ 		KWUUUu7||l**		*OQXYYYu""a"a)G2D2D"a"a"aaaO)Kg,,81<K(L'-7!;L\))[<-G-G 		*OQXYYY4BB'BBBBB 	7||a,..66|S\\TUEUW^___u / S\\))H22<WwWWW5tr
  rv_typeundefined_rvaluec                .   t          |p| j                            |                    }t          |t                    rt          |j                  }t          |t                    r<|                                }t          |          dk    rt          |d                   }t          |t                    r$t          |j                  t          |i           }t          |t                    ri|D ]d}	t          |	t                    r|	j        }	|                     t          t"          j        |          |          }
|                     |	|
|           edS t          |t                    r|                     ||||||           dS t          |t                    r|                     |||||           dS t          |t,                    r,|j        j        dk    r| j                            |           dS |                     ||||           dS )z:Check the assignment of one rvalue to a number of lvalues.r   r   Nr  rZ  )r   r  rh  r  r   r  r   r  rU  r   r   r  r1   r   r   r  r  r   r  r%  !check_multi_assignment_from_tuple!check_multi_assignment_from_unionr   rj  r_  r  unpacking_strings_disallowed$check_multi_assignment_from_iterable)rJ  r  r  r  r4  r  r  rR  r  r.  r  s              r  r  z"TypeChecker.check_multi_assignment  sL    &g&Q1B1I1I&1Q1QRRk?33 	C)+*ABBKk9-- 	A(7799N>""a''-nQ.?@@ {I..	7#K$566B &k266Kk7++ 	 H Hb(++ !B NNI6;OOOQX 	 %%b)5FGGGGH H Y// 	22g7GIZ     Y// 		22g7H     X.. 	;3C3LP^3^3^H11':::::55g/@    r
  rR  r   c                   d| _         t          d |                     |          D                       }| j                                        5 }|j        D ]}|                     |||||d           t          ||                     |                    D ]P\  }	}
|	                    | j	        d         
                    |
t          t          j                                       Q	 ddd           n# 1 swxY w Y   t          d |D                       }|                                D ]\  }}t          |t                    r|j        }g }|D ] \  }}|J |                    ||f           !t          | \  }}| j                            |t%          t'          |                    t%          t'          |                    d           t          ||                     |                    D ]N\  }}
|                     |
          \  }}}|r|                     ||
|           8|                     |
|           Od| _         dS )a  Check assignment to multiple lvalue targets when rvalue type is a Union[...].
        For example:

            t: Union[Tuple[int, int], Tuple[str, str]]
            x, y = t
            reveal_type(x)  # Union[int, str]

        The idea in this case is to process the assignment for every item of the union.
        Important note: the types are collected in two places, 'union_types' contains
        inferred types for first assignments, 'assignments' contains the narrowed types
        for binder.
        Tc              3     K   | ]}g V  d S rN  r	  )r  r  s     r  r  z@TypeChecker.check_multi_assignment_from_union.<locals>.<genexpr>h  s"      2]2]!22]2]2]2]2]2]r
  )r4  r  r  r   Nc              3  4   K   | ]}t          |          V  d S rN  )r   )r  cols     r  r  z@TypeChecker.check_multi_assignment_from_union.<locals>.<genexpr>z  s+      MM31#66MMMMMMr
  F)r/  tupler  r  accumulate_type_assignmentsr  r  r  r  r  r=  r   r   r   r  r   r  r-  r   r5  r  rB  r2  )rJ  r  r  rR  r  r4  
transposedr  r  r  r.  union_typesr  r  clean_itemsrj  declared_typers  declared_typesunion_1_2rP  s                          r  r  z-TypeChecker.check_multi_assignment_from_unionS  s   ( !%-22]2]t?S?ST[?\?\2]2]2]-]-]
[4466 	Z+#) Z Z ++&7 %) ,    !T-A-A'-J-JKK Z ZEAr HHT_Q/33B	@V8W8WXXYYYYZZ	Z 	Z 	Z 	Z 	Z 	Z 	Z 	Z 	Z 	Z 	Z 	Z 	Z 	Z 	Z  MM*MMMMM&,,.. 	 	KD%$)) !y 46K', : :#m$000""D-#89999$'$5!E>K##%d5kk22%d>&:&:;;	    [$*>*>w*G*GHH 	+ 	+IE2#0044BH +&&xU;;;;E**** %s   BC44C8;C8c                   g }|D ]|}t          |t          t          f          r-|                    |                     |j                             t          |t                    r|j        }|                    |           }|S rN  )	r  r   rq   r  r  r  r   r  r  )rJ  r  resr.  s       r  r  zTypeChecker.flatten_lvalues  s~     " 	 	B"y(344 ;

4//99:::"h'' WJJrNNNN
r
  r   c                l    t          |j                  }                     |t          |j                  |          rlt	          d t          |          D             t          |                    }|d |         }	|t          |          k    rt          t          ||                   nd }
||dz   d          }|s                     ||          }t           j
                            ||                    }t          |t                    r<|                                }t          |          dk    rt          |d                   }t          |t                    r                     ||||           d S t          |t                     rd S t          |t"                    sJ |}                     |j        |t          |                    \  }}}t'          |	|          D ]0\  }}                     |                     |          |           1|
rMt-           fd|D                       }|                                                    |
j        ||           t'          ||          D ]0\  }}                     |                     |          |           1d S |r |D ]}t          |t2                    rJt          |j        t6                    r0|j        j        $t!          t:          j                  |j        _        at          |t                    rt          |j        t2                    rmt          |j        j        t6                    rN|j        j        j        =                     dt!          t:          j                  g          |j        j        _        d S d S )N)r  c              3  J   K   | ]\  }}t          |t                    |V  d S rN  r  r  s      r  r  z@TypeChecker.check_multi_assignment_from_tuple.<locals>.<genexpr>  s5      OOuq"jX6N6NOOOOOOOr
  r   r   c                    g | ]U}t          |t                    s                    |          n't                              |j                            VS r	  )r  r   r  r   rj  )r  r  r  rJ  s     r  r  zATypeChecker.check_multi_assignment_from_tuple.<locals>.<listcomp>  si        $  *':>>Mw888%dnnW\7&K&KLL  r
  rY  ) r   r  r  rU  r  r  r   r   lvalue_type_for_inferencer   r  rh  r  r   r  r  r   r   r  r  r%  r  rq   r  r  rv   r  r   rj  r   r  rk  )rJ  r  r  rR  r  r  r4  r  r  r  r  r  r  reinferred_rvalue_typer  left_rv_typesstar_rv_typesright_rv_typesr.  r  	list_exprs   `   `                r  r  z-TypeChecker.check_multi_assignment_from_tuple  s    ,K,=>>00S*++WM 1 
 
 M	 OO	' 2 2OOOQTU\Q]Q] J {
{+H=G3w<<=W=Wd8WZ%8999]aG
Q 0 01I# 5"<<WkRR)8%,,V[AA* *& 4i@@ T%;%J%J%L%LN>**a//1@PQAR1S1S.4i@@ ::)?J[   F4g>>  F!"8)DDDDD4;?;Q;Q!:s7||< <8M=.  #8];; _ _G%%b$..'*J*JL]^^^^ 
R$     (5	   	 ""7+++%%glI?PQQQ"9n== _ _G%%b$..'*J*JL]^^^^_ _
 ! !  B"2x00&rw44 GL0'.y/C'D'D#B11 &rw99 *27< = = !# 1 9040G0G /'):N2O2O1P1 1BGL-  r
  c                    t          d t          |          D             t          |                    }|d |         }|t          |          k    rt          t          ||                   nd }||dz   d          }                     |j        |t          |                    \  }}}	g d fd	}
 |
||           |rq                     |j                  \  }}}|r<t          |t                    s'                    |gt          |          z             n                    |            |
||	           t                               d
                    S )Nc              3  J   K   | ]\  }}t          |t                    |V  d S rN  r  r  s      r  r  z8TypeChecker.lvalue_type_for_inference.<locals>.<genexpr>  s5      KK51b*R2J2JKQKKKKKKr
  r   r  r  rv_typesr$  r9  r:  c                    t          | |          D ]`\  }}                    |          \  }}}|r+t          |t                    s                    |           K                    |           ad S rN  )r  r  r  r   r  )	r  r  r.  r  sub_lvalue_type
index_exprrP  rJ  type_parameterss	          r  append_types_for_inferencezITypeChecker.lvalue_type_for_inference.<locals>.append_types_for_inference  s    "311 4 4G8<8I8I"8M8M5X" 4:o{+S+S 4#**?;;;; $**733334 4r
  r  )r  r  r  r$  r9  r:  )r  r  rU  r   r   r  r  r  r  r  r   r  r   rl  )rJ  r  rR  r  r  r  r  r  r   r  r
  r  r  rP  r	  s   `             @r  r  z%TypeChecker.lvalue_type_for_inference  s   KKIg..KKKSQX\\
 

 ;J;'9Cs7||9S9S$x!4555Y]JN,,-	7;7M7Mz3w<<8
 8
4}n ')	4 	4 	4 	4 	4 	4 	4 	#"8];;; 	6484E4Egl4S4S1OZ 6z/;'O'O 6&&'83};M;M'MNNNN  &&}555""9n===$//:J*K*KLLLr
  r  list[T]r  length tuple[list[T], list[T], list[T]]c                    ||z
  dz
  }|dk    r| nt          |          }|d|         }|||         }||d         }|||fS )a  Splits a list of items in three to match another list of length 'length'
        that contains a starred expression at 'star_index' in the following way:

        star_index = 2, length = 5 (i.e., [a,b,*,c,d]), items = [1,2,3,4,5,6,7]
        returns in: ([1,2], [3,4,5], [6,7])
        r   r   N)rU  )	rJ  r  r  r  nr_right_of_starright_indexr  starr  s	            r  r  zTypeChecker.split_around_star!  sj     "J.2+;q+@+@'''c%jj[j[!Z+,kll#T5  r
  rj  c           	         t          |          }t          |t                    r|                                r|j        }t          ||                     dt          t          j	                  g                    S )Nr#  )
r   r  r   r  r  r   rk  r   r   r   rJ  rj  s     r  r  zTypeChecker.type_is_iterable1  sq    t$$dL)) 	!d.>.>.@.@ 	!=D$))*;giF\>]>]=^__
 
 	
r
  c                   t          |          }|                     |          rt          |t          t          t
          t          f          r|                     ||          }|D ]}t          |t                    rH| 	                    d|g          }| 
                    |j        |                     ||          |           _| 
                    ||                     ||          |           d S | j                            ||           d S )NrY  )r   r  r  r   r   r   r   r  r   rk  r%  r  r  r  type_not_iterable)rJ  r  rR  r  r4  r  r.  
items_types           r  r  z0TypeChecker.check_multi_assignment_from_iterable9  s)    &k22  -- 	=*(L(JG3
 3
 	= //WEEI 	 	b(++ !%!8!89+!V!VJ))
G!D!DFW    ))DNN9g>>@Q   	 	 H&&{G<<<<<r
  0tuple[Type | None, IndexExpr | None, Var | None]c                &    d }d }d }                      |          rt          |t                    rt          |j        t                    ryt          |t                    r%t          |j        t                    sJ |j        }nt          |t
                    sJ  j                            |j                   |j	        }nIt          |t                    r|}n0t          |t
                    r2 j                            |d          }                     ||           nt          |t                    r3 j                            |d          }                     ||           nt          |t          t          f          r7 fd|j        D             }t#          |                     d                    }nNt          |t&                    r                     |j                  \  }}}n j                            |          }|||fS )NT)r  c                b    g | ]+}                     |          d          pt                      ,S r   )r  r   )r  sub_exprrJ  s     r  r  z,TypeChecker.check_lvalue.<locals>.<listcomp>j  sN       
 	 !!(++A. "  !!	  r
  r  )is_definitionr  rv   r  r   rt   r  rh  r  def_varrn   analyze_ordinary_member_accessr2  analyze_ref_exprr   rq   r  r   rl  r   r  )rJ  r  r  rO  rP  rs  r  s   `      r  r  zTypeChecker.check_lvalueR  s   f%% 	;68,,	;0:6;0L0L	; &(++ *!&+s33333!;!&*55555!((555!>	** 	;!LL
++ 	;+JJ6SWXXKOOFK0000)) 	;+<<VD<QQKOOFK0000H 566 	;   
 !'  E $E4??;K+L+LMMKK)) 	; $ 1 1&+ > >KAA+226::KL(22r
  c                    t          |t                    r/|j        rdS |j        }t          |t                    r	|j        d u S nt          |t                    r|j        S dS r  )r  rv   is_inferred_defr  r   rj  rt   )rJ  r  r  s      r  r  zTypeChecker.is_definitiony  sq    a"" 	%  t 6D$$$ )yD(():&& 	%$$ur
  	init_typec                   t          |t                    r| j                            ||           dS t	          ||j                  s_| j        sX|                     |||          s?| j                            ||| j	        j
                   |                     |||           dS dS t          |t                    r| j        {|j        rt|j        | j        v rft          | j        |j                 |          sF| j                            ||| j	        j
                   t!          t"          j                  |_        dS t)          |          }|                     |||           dS )z>Infer the type of initialized variables from initializer type.r9  N)r  r   r  deleted_as_rvaluerA  rO  r/  rC  need_annotation_for_varr-  python_version!set_inference_error_fallback_typert   r.  r  r   r   r   r  rj  r   rB  )rJ  r  r  r"  r  s        r  rM  zTypeChecker.infer_variable_type  su    i-- 	<H&&y':::::&y$-PPP	<)	< **4CC P00w@[\\\66tVYOOOOOP P vz**	<-9 :$"??? !>v~!NPYZZ @ H,,T7DL<WXXX	 455DIII
 #9--I""4;;;;;r
  c                N   t          |          }t          |t                    rt          d |          }nBt          |t                    r*|j        j        }t          |t                    }|rY|dk    s|dk    s|dk    s|dk    rAt          d t          |j
                  D                       rt          |j        |          }n|r|dk    rt          |j
        d                   }t          |j
        d                   }t          |t          t          f          rR|                     |          r=t          |          }t          |t                    sJ t          |j        ||          }nd	S d	S d	S |                     |||           || j        d
         j        |<   dS )NrY  r[  builtins.dictcollections.OrderedDictc              3  N   K   | ] }t          |t          t          f          V  !d S rN  )r  r   r   r  r  s     r  r  z1TypeChecker.infer_partial_type.<locals>.<genexpr>  sE         q8_"=>>     r
  collections.defaultdictr   r   FrG  T)r   r  r   r   r   rj  r_  r~   r  r   r  r   'is_valid_defaultdict_partial_value_typer&   rB  r'  r  )	rJ  r  r  r"  partial_typer_  is_refarg0arg1s	            r  rC  zTypeChecker.infer_partial_type  s   #I..	i** !	&tT22LL	8,, 	 ~.H00F //>11?22#<<<  -in==     =  +9>4@@ H(AAA&y~a'899&y~a'8998_5  !BB4HH! &d++D%dH55555#.y~tT#J#JLL 5u5tV\:::+12"4(tr
  r  r   c                j   t          |t                    sdS t          |j                  dk    rdS t          |j                  dk    rit	          |j        d                   }| j        j        r#t          |t          t          t          f          }nt          |t          t          f          }|rdS dS )a  Check if t can be used as the basis for a partial defaultdict value type.

        Examples:

          * t is 'int' --> True
          * t is 'list[Never]' --> True
          * t is 'dict[...]' --> False (only generic types with a single type
            argument supported)
        Fr   Tr   )
r  r   rU  r  r   r-  old_type_inferencer   r   r   )rJ  r  r  alloweds       r  r.  z3TypeChecker.is_valid_defaultdict_partial_value_type  s     !X&& 	5qv;;!4qv;;!!!&),,C|. G$S?Hk*RSS$S?H*EFF tur
  c                   |r| j         s||_        d|_        || j        vrd | j        j        D             | j        |<   t          |t                    r| j        |j	        || j        |j	        <   | 
                    ||           dS dS dS )zStore inferred variable type.

        Store the type to both the variable node and the expression node that
        refers to the variable (lvalue). If var is None, do nothing.
        Tc                    h | ]	}|j         
S r	  r1  )r  r  s     r  	<setcomp>z0TypeChecker.set_inferred_type.<locals>.<setcomp>  s    ,V,V,V%UX,V,V,Vr
  N)r?  rj  is_inferredr2  r  r}  r  rt   r.  r  r2  )rJ  r  r  rj  s       r  rB  zTypeChecker.set_inferred_type  s      
	*t1 
	*CH"CO$...,V,V4;CU,V,V,V$S)&*-- I$2O2[>-DHD1&.AOOFD)))))
	* 
	* 
	* 
	*r
  c                ^    |                      |          }|                     |||           dS )a  Store best known type for variable if type inference failed.

        If a program ignores error on type inference error, the variable should get some
        inferred type so that if can used later on in the program. Example:

          x = []  # type: ignore
          x.append(1)   # Should be ok!

        We implement this here by giving x a valid type (replacing inferred Never with Any).
        N)inference_error_fallback_typerB  )rJ  r  r  rj  r  s        r  r'  z-TypeChecker.set_inference_error_fallback_type  s5     55d;;sFH55555r
  c                b    |                     t                                }t          |          S rN  )rh  SetNothingToAnyr'   )rJ  rj  r  s      r  r;  z)TypeChecker.inference_error_fallback_type  s)    ;;0011 h'''r
  c                   t          |t          t          t          t          t
          f          rdS t          |t                    rt          |j        t
                    rt          |j        j        t                    rb|j        j        j
        }t          |t                    r|j         S t          |t                    rt          d |j        D                        S dS )a  Returns True for expressions for which inferred type should not depend on context.

        Note that this function can still return False for some expressions where inferred type
        does not depend on context. It only exists for performance optimizations.
        Tc              3  $   K   | ]}|j         V  d S rN  r  r+  s     r  r  z,TypeChecker.simple_rvalue.<locals>.<genexpr>!  s$      "H"Hd4>"H"H"H"H"H"Hr
  F)r  ro   r   rY   rd   r~   rZ   r  r  rf   rj  r   r  r   r  r  )rJ  r  r  s      r  simple_rvaluezTypeChecker.simple_rvalue  s     fwIwOPP 	4fh'' 	I&-11 IjASU]6^6^ Im(-c<00 I"},,Z00 I""H"Hci"H"H"HHHHHur
  r  
expressionr  r;   r  r  r  list[str] | Nonec          	        | j         r.t          |t                    rt          t          j                  S |d uo"t          t          |          t                     }| j                            |||          }	t          t          |          t                    rt          t          |	          t                    s|                     |          s| j                                        5 }
|                                 5 }| j                            |d |          }d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   |
                                sXt          t          |          t                    s6t!          |          r't#          ||	          r|}	|                     |           t          |	t&                    r| j                            |	|           t          |t&                    r| j                            ||           n$|r"|                     |	|||| d| d|           |	S )N)r  z	 has typerC  )r@  r  ra   r   r   r   r   r  rh  r   r   rA  r  r  local_type_maphas_new_errorsrA  r   store_typesr   r$  deleted_as_lvaluer.  )rJ  r  r  r  r  r  r  r  r  rR  local_errorstype_mapalt_rvalue_types                r  r  z#TypeChecker.check_simple_assignment$  s    < 1	Jv|<< 1	91222*$6  z,,g@ @ < +226F 3  K ?;77CC/ #?;#?#?MM/ **622	/ X++-- t?R?R?T?T X`&*&7&>&>7G '? ' 'O                             
 %3355/ ''G'GQQ/ /??	/
 */;GG/ #2K$$X...+{33 A**;@@@+{33 **;@@@@ 
"""---"--- # 	 	 	 s6   -ED, E,D0	0E3D0	4EE
ErT  attribute_typetuple[Type, Type, bool]c           
        t          |          }t          |          }t          |t                    r|                                st          |t                    r|                     |||          }||dfS t          |t                    s|                     |||          }||dfS t          ddd||d| j        |           }t          ||          }|j
                            d          s|                     |||          }||dfS |j
                            d          }|g|                     t          j                            |                    | j                            |           t'          t(          j                  |dfS t-          |||j
        |d|          }	t/          ||j                  }
t3          |	|
          }| j                            |d          }| j                            ||t;          ||          |gt<          j        t<          j        g||          }|}| j                                         5  | j        !                    |t;          ||          |gt<          j        t<          j        g|||	          \  }}ddd           n# 1 swxY w Y   t          |          }t          |tD                    r(t;          t'          t(          j#                  |          }| j        !                    |t;          ||          |gt<          j        t<          j        g|||	           t          |tD                    rtI          |j%                  d
k     rt'          t(          j                  |dfS |j%        d         }|                     |||          }tM          ||          otM          ||          }|r|n|||fS )a  Type member assignment.

        This defers to check_simple_assignment, unless the member expression
        is a descriptor, in which case this checks descriptor semantics as well.

        Return the inferred rvalue_type, inferred lvalue_type, and whether to use the binder
        for this assignment.

        Note: this method exists here and not in checkmember.py, because we need to take
        care about interaction between binder and __set__().
        TFN)r  r  r  r  r  r  r  r  r  )r  r  r  r  r  mxrJ  )r  )r  r  r  r   )'r   r  r   r  r   r  r   r   r  r!   rj  r  
get_methodrm  r   DESCRIPTOR_SET_NOT_CALLABLEro  rx  r-  r   r   r  r    r7   r  r2   r  method_fullnametransform_callee_typer   r   rG   r  r  r   r   rU  r  r   )rJ  rT  rM  r  r  rR  rP  get_type
dunder_setbound_methodr  dunder_set_typer  rO  r  inferred_dunder_set_typeset_typeinfers                     r  rG  z#TypeChecker.check_member_assignmentb  sM    (66(88}l33 	58Q8Q8S8S 	5Xb8Y
 Y
 	5 66~vwWWK44.(33 	566~vwWWK44'	
 	
 	
 -^R@@"66yAA 	/
 66xQQK$..#(33I>>
II <CC"33DLAA  	   9/00(EAA; $$
 
 
 (
HH1,DD)99.)TT+AAmW555v>]EM*& B 
 
 X##%% 	 	*.*;*F*F-999<H.*+ +G + +'A'	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 $33K#L#L .== 	V#GI,B$C$CWUUUL$$mW555|D]EM*&' 	% 	
 	
 	
 3\BB 	B(233a779/00(EAA+5a8
 228VWMM;11Tj86T6T#1{{8UBBs   AJ""J&)J&rn   c                   |                      ||           t          | j                            |j                            }| j                            d||          }||_        | j                            d|||j        |gt          j
        t          j
        g|          \  }}t          |          }t          |t                    r"|j        s| j                                         dS dS dS )zxType check indexed assignment base[index] = rvalue.

        The lvalue argument is the base[index] expression.
        __setitem__N).try_infer_partial_type_from_indexed_assignmentr   r  rh  rK  r   r  check_method_callr  r   rG   r  r   	ambiguousr  r  )rJ  r  r  r  basetyper  res_typer  s           r  rK  z$TypeChecker.check_indexed_assignment  s     	;;FFKKK"4#4#;#;FK#H#HII'FF8V
 
 )'99\6"]EM*
 
! #8,,h00 	&9K 	&K##%%%%%	& 	& 	& 	&r
  c                \   d }t          |j        t                    r,t          |j        j        t                    r|j        j        }n9t          |j        t
                    r| j                            |j                  }t          |t                    rt          |j        t                    r|j        j        }|d S | 
                    |          }|d S |j        }|dk    s|dk    s|dk    r| j                            |j                  }| j                            |          }t          |          rht          |          r]| j        sX|dk    r&|j        j        t#          ||j        j                  r.|                     |||g          |_        ||= d S d S d S d S d S d S d S d S )Nr)  r*  r-  )r  rK  r~   r  r   rt   r  rh  rj  r   r+  r_  rh  r  rA  r?  
value_typer   rk  )	rJ  r  r  r  r  r'  typenamekey_typerd  s	            r  r^  z:TypeChecker.try_infer_partial_type_from_indexed_assignment  s    fk7++ 	F
6;;KS0Q0Q 	F+"CCZ00 	F#88EECc3 	/#(K00 /HM	$F $ 7 7 < < (F$-//#<<<#<<<  $077EEH!%!2!9!9&!A!AJ.x88/2:>>/ !% :/
 %(AAA # 3 ?$1*ch>Q$R$R !@ $(#:#:8hPZE[#\#\)#...9	/ 	// / / / =<
/ / / / !@ ?r
  tuple[str, ErrorCode] | Nonec                    t          |          }t          |t                    r<|j        j        dk    r	dt
          fS |j                            d          	dt          fS dS )zSome types require usage in all cases. The classic example is
        an unused coroutine.

        In the case that it does require usage, returns a note to attach
        to the error message.
        ztyping.CoroutinezAre you missing an await?	__await__N)r   r  r   rj  r_  r+   ri  r*   )rJ  r  proper_types      r  type_requires_usagezTypeChecker.type_requires_usage  sl     &c**k8,, 	G (,>>>35EFF##K00<35EFFtr
  rc   c                F   | j                             |j        dd          }|                     |          }|rg|\  }}|                     t
          j                            t          || j	                            ||           | 
                    |||           d S d S )NTr  rQ  )r  rh  r  rk  rm  r   TYPE_MUST_BE_USEDro  r?   r-  r  )rJ  r  	expr_typeerror_note_and_code
error_noterR  s         r  visit_expression_stmtz!TypeChecker.visit_expression_stmt,  s    %,,QVt^b,cc	"66yAA 	02JII 299+iQUQ]:^:^__    
 IIj!$I/////	0 	0r
  r   c                b    |                      |           | j                                         dS )zType check a return statement.N)check_return_stmtr  r  rJ  r  s     r  visit_return_stmtzTypeChecker.visit_return_stmt8  s0    q!!!!!!!!r
  c           	     N   | j                                         }||j        r'|                     | j        d         |j                  }n5|j        r!|                     | j        d                   }n| j        d         }t          |          }t          | j                                         t                    }t          |t                    r+|r|j        s"|                     t          j        |           d S |j        rt          |t                     }t          |t"                    }|p|p|}t          | j                            |j        ||                    }|j        r"|                     t          j        |           d S t          |t"                    r| j        j        r| j        st3          t#          t4          j                  |          sd|j        t:          v rt=          |j                  sBt          |t>                    r|j         j!        dk    s|s| j"        #                    ||           d S |r;|st          |t                     rd S |                     t          j$        |           d S | %                    d|d||j        |t          j&                   d S |j        r|j        st          |t"                    rd S t          |t           t"          f          rd S | '                                r$|                     t          j(        |           d S d S d S )NrG  r  r  gotrS  )rT  rU  rV  rW  r  outer_contextr  ))r#  top_functionrg  r   r%  r  r  r   r  rp   r   r`  rm  r   NO_RETURN_EXPECTEDr  r   r   r  rh  rh  RETURN_IN_ASYNC_GENERATORr-  warn_return_anyr?  r   r   r   r  r   is_literal_not_implementedr   rj  r_  r  incorrectly_returning_anyNO_RETURN_VALUE_EXPECTEDr.  r  r  RETURN_VALUE_EXPECTED)	rJ  r  r  r
  	is_lambdadeclared_none_returndeclared_any_returnallow_none_func_callr  s	            r  rs  zTypeChecker.check_return_stmt=  sq   z&&((  4"<<%b)4+<  " 4"<<T=Nr=RSS"/3)+66K"4:#:#:#<#<jIII+77   (= II.A1EEEFv JI'1+x'H'H$&0g&F&F# (1'_4H'_L_$ &%,,?S -    * II.H!LLLFc7++  4K $ :K !2'):P2Q2QS^ _ _K
 !I)=== :16 B B > '{H==	 >
 !, 0 9=N N N ) !O ::;JJJF (  ! JsH$=$= II.GKKKKK&&&+ #(2"- !&',K '      % - #;88
 FkHg+>?? F++-- III.DaHHHHHy vI Ir
  ri   c                    | j                             ddd          5  t          |j        |j                  D ]\  }}t          | j                            |                    }t          |t                    r| j
                            ||           |                     |          \  }}| j                             dd          5  |                     |           |                     |           ddd           n# 1 swxY w Y   |                     |           | j                             dd          5  |j        r|                     |j                   ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w Y   dS )zType check an if statement.FTr   r  r  fall_throughr  r  r  N)r  r  r  r  r  r   r  rh  r  r   r  r$  r  r  r  )rJ  r  r  r  r  if_mapr  s          r  visit_if_stmtzTypeChecker.visit_if_stmt  sC    [&&\]&^^ 	- 	-AFAF++ - -1#D$5$<$<Q$?$?@@a-- 5H..q!444#'#=#=a#@#@  [..1.MM # #&&v...KKNNN# # # # # # # # # # # # # # #
 ""8,,,,**E*JJ - -; -KK,,,- - - - - - - - - - - - - - -#	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-sZ   B+F	+D 4F DFD5F="E+F+E/	/F2E/	3FF
Fr   c                    t          |j        g|j        gd          }|                    |           |                     ||j        |j                   dS )zType check a while statement.Nr  )ri   r  r  r  r  r  )rJ  r  if_stmts      r  visit_while_stmtzTypeChecker.visit_while_stmt  sT    !&AF8T22!+afEEEEEr
  rx   c                   |                      |j        |j        |j                   t	          |j        t
                    r!| j                            |j        d          }n| j                            |j                  }t          ||j                  \  }}|rV| j        
                    |||j        |          \  }}t          ||          s | j                            |j        |           nSt          |j        |j        |j                  }|                    |           |                     |j        |dd           |                     |           dS )z9Type check an operator assignment statement, e.g. x += 1.TF)r  r  r4  r&  N)r=  r  r  rf  r  rt   r  visit_member_exprrh   infer_operator_assignment_methodcheck_opr   r   incompatible_operator_assignmentry   r  r%  r*  )rJ  r  r  inplacer  rR  r  r  s           r  visit_operator_assignment_stmtz*TypeChecker.visit_operator_assignment_stmt  sM   ;;AHahPQPTUUUah
++ 	= +==ahMMKK+2218<<K:;MM 	'+'8'A'A&+WXW_ab'c'c$Kk;77 C99!$BBB !$!(33DMM!!!xQV "    	r
  rT   c                   | j                             |j                   t          |j        t                    r=t          |j        j                  dk    r |                     t          j	        |           | 
                    |j                  \  }}|j        !| j                             ||j        d            |                     |           d S Nr   )r  rh  r  r  r   rU  r  rm  r   MALFORMED_ASSERTr  r  analyze_cond_branchr  )rJ  r  true_mapr  s       r  visit_assert_stmtzTypeChecker.visit_assert_stmt  s      (((afi(( 	<S->->-B-BII&7;;; "77??(511(AE4HHH8$$$$$r
  r}   c                    |j         r|                     |j         |           |j        r|                     |j        |d           | j                                         dS )zType check a raise statement.T)optionalN)r  type_check_raise	from_exprr  r  rt  s     r  visit_raise_stmtzTypeChecker.visit_raise_stmt  sf    6 	-!!!&!,,,; 	A!!!+q4!@@@!!!!!r
  r  r  c                   t          | j                            |                    }t          |t                    r| j                            ||           d S |                     d          }|t          |          g}|r!|	                    t                                 |                     |t          j        |          |t          j                   t          |t                     r| j                            |g g |           d S d S )Nbuiltins.BaseException)r   r  rh  r  r   r  r$  rl  r   r  r   r.  r   
make_unionr   INVALID_EXCEPTIONr   r  )rJ  r  r  r  r  exc_typeexpected_type_itemss          r  r  zTypeChecker.type_check_raise  s   d/66q99::c;'' 	H&&sA...F??#;<<'(););< 	3  &&xzz222%&9::A?O?a	
 	
 	
 c<(( 	9((b"a88888	9 	9r
  r   c                ~   | j                             dd          5  |                     |t          |j                             |j        r|                     |j                   ddd           n# 1 swxY w Y   |j        r5| j                                         s|                     |j                   dS dS dS )zType check a try statement.Fr   r  )	try_frameN)r  r  visit_try_without_finallyr  finally_bodyrh  rd  rt  s     r  visit_try_stmtzTypeChecker.visit_try_stmt  s	   
 [&&A&FF 	, 	, **1Q^8L8L*MMM~ ,AN+++	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, > 	, ;--// ,AN+++++	, 	,, ,s   AA44A8;A8r  c                N   | j                             dd|          5  | j                             ddd          5  | j                             ddd          5  |                     |j                   ddd           n# 1 swxY w Y   t	          t          |j                            D ]%}| j                             dd          5  |j        |         }|r[|                     ||j	                  }|j
        |         }|r1d|_        |                     ||                     ||                     |                     |j        |                    |j
        |         }|rU|j        }t          |j        t"                    rt%          |	          |j        _        | j                             |           ddd           n# 1 swxY w Y   '	 ddd           n# 1 swxY w Y   |j        r|                     |j                   ddd           dS # 1 swxY w Y   dS )
a2  Type check a try statement, ignoring the finally block.

        On entry, the top frame should receive all flow that exits the
        try block abnormally (i.e., such that the else block does not
        execute), and its parent should receive all flow that exits
        the try block normally.
        Fr  )r  r  r  Tr  N   r  source)r  r  rh  r  rn  rU  handlersrs  check_except_handler_testis_starvarsr!  r%  r  r  r  r  r   r   rj  cleanser  )rJ  r  r  r  r  r  r  r  s           r  r  z%TypeChecker.visit_try_without_finally  s    [&&AQZ&[[ 	) 	)**ET`a*bb 5 5[..AY].^^ ( (KK'''( ( ( ( ( ( ( ( ( ( ( ( ( ( (s1:// 5 5A22Dq2QQ 5 5gaj S $ > >sAI N NA"#&)C" S 7; 3 $ 5 5c4>>!S;Q;Q R R RAJqM222fQi 5 &)XF)#(C88 K0;60J0J0J K//444'5 5 5 5 5 5 5 5 5 5 5 5 5 5 55	5 5 5 5 5 5 5 5 5 5 5 5 5 5 52 { )AK(((9	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)s~   HG!B5G!BG!B	AG!C/G	;G!GG!GG!H!G%	%H(G%	)$HH!Hnr  c                   | j                             |          }g }|                     ||          }t          |          D ]s}t	          |t
                    r|                    |           .t	          |t                    rm|j        d         }|	                                s7| 
                    t          j        |           |                     |          c S t          |j                  }nTt	          |t                     r|j        }n7| 
                    t          j        |           |                     |          c S t%          ||                     d                    s7| 
                    t          j        |           |                     |          c S |                    |           u|rg }	|D ]}t)          ||                     d                    rM| 
                    t          j        |           |	                    t          t,          j                             r|	                    |           |                     |	          S t3          |          S )z,Type check an exception handler test clause.r   r  builtins.BaseExceptionGroup)r  rh  get_types_from_except_handlerr   r  r   r  r   r  r  rm  r   INVALID_EXCEPTION_TYPEdefault_exception_typer'   r  r   r  r   rl  r   INVALID_EXCEPTION_GROUPr   r  wrap_exception_groupr   )
rJ  r  r  r  	all_types
test_typesttyper  r  new_all_typess
             r  r  z%TypeChecker.check_except_handler_testG  sJ   &&q)) "	77Q??
%j11 	' 	'E%))   '''%.. 
<{1~'')) @II.EqIII66w?????)$-88E8,, < :		*A1EEE227;;;;;h8P(Q(QRR <		*A1EEE227;;;;;X&&&& 	<(*M  . .$S$//:W*X*XYY .II.FJJJ!((1E)F)FGGGG!((----,,];;;$Y///r
  c                j    t          t          j                  }|r|                     d|g          S |S )z:Exception type to return in case of a previous type error.builtins.ExceptionGroup)r   r   r  rk  )rJ  r  r  s      r  r  z"TypeChecker.default_exception_typep  s9    9/00 	R**+DxjQQQr
  rs  Sequence[Type]c                    t          |          }t          ||                     d                    rd}nd}|                     ||g          S )zDTransform except* variable type into an appropriate exception group.zbuiltins.Exceptionr  r  )r   r   rl  rk  )rJ  rs  r  rK  s       r  r  z TypeChecker.wrap_exception_groupw  sR    #E**c4??+?@@AA 	1,DD0D&&tcU333r
  c                
    t          |          }t          |t                    r|j        S t          |t                    r! fd|                                D             S t          |d          r|j        d         gS |gS )z?Helper for check_except_handler_test to retrieve handler types.c                F    g | ]}                     |          D ]}|S r	  )r  )r  r  	union_typr  rJ  s      r  r  z=TypeChecker.get_types_from_except_handler.<locals>.<listcomp>  sP       !%!C!CD!!L!L      r
  r  r   )r   r  r   r  r   r  r   r  )rJ  r  r  s   ` `r  r  z)TypeChecker.get_types_from_except_handler  s    c""c9%% 	9Y'' 
	    ..00   
 s$455 	HQK= 5Lr
  re   c                .   |j         r|                     |j                  \  }}n|                     |j                  \  }}||_        ||_        |                     |j        ||j        du |           | 	                    |j
        |j                   dS )zType check a for statement.N)is_async analyze_async_iterable_item_typer  analyze_iterable_item_typeinferred_item_typeinferred_iterator_typeanalyze_index_variablesr  
index_typer  r  r  )rJ  r  iterator_typer  s       r  visit_for_stmtzTypeChecker.visit_for_stmt  s    : 	O'+'L'LQV'T'T$M99'+'F'Fqv'N'N$M9(#0 $$QWi9MqQQQ-----r
  r  tuple[Type, Type]c                    | j         }|                    |          }|                    d|g g |          d         }|                    d|g g |          d         }|                    ||t          j                  }||fS )zNAnalyse async iterable expression and return iterator and iterator item types.	__aiter__r   	__anext__)r  rh  check_method_call_by_namecheck_awaitable_exprr   INCOMPATIBLE_TYPES_IN_ASYNC_FOR)rJ  r  echkiterableiterator	awaitabler  s          r  r  z,TypeChecker.analyze_async_iterable_item_type  s     ;;t$$11+xRQUVVWXY22;"bRVWWXYZ	--t-M
 
	 ""r
  c                    |                      | j                            |          |          \  }}|                     |          }|r||fS ||fS )zHAnalyse iterable expression and return iterator and iterator item types.rJ  )-analyze_iterable_item_type_without_expressionr  rh  analyze_range_native_int_type)rJ  r  r  r  int_types        r  r  z&TypeChecker.analyze_iterable_item_type  sk    !OO$$T**D P 
 
( 55d;; 	&X%%!!r
  c                >   | j         }t          |          }|                    d|g g |          d         }t          |t                    r1|j        j        j        dk    r|t          |          j	        d         fS |                    d|g g |          d         }||fS )zBAnalyse iterable type and return iterator and iterator item types.__iter__r   r  __next__)
r  r   r  r  r   partial_fallbackrj  r_  r   r  )rJ  rj  r  r  r  r  s         r  r  z9TypeChecker.analyze_iterable_item_type_without_expression  s      "4((11*hBPWXXYZ[ x++	&).7;KKK^H55:1=== 55j(BPRT[\\]^_HX%%r
  c                   t          |t                    rt          |j        t                    r|j        j        dk    rdt          |j                  cxk    rdk    rn nt          d |j        D                       rkd}d}|j        D ]Y}t          | 
                    |                    }t          |t                    r |j        j        t          v r||}Q||k    rd}Z|r|r|S dS )zTry to infer native int item type from arguments to range(...).

        For example, return i64 if the expression is "range(0, i64(n))".

        Return None if unsuccessful.
        zbuiltins.ranger   r  c              3  ,   K   | ]}|t           k    V  d S rN  )rG   )r  rF  s     r  r  z<TypeChecker.analyze_range_native_int_type.<locals>.<genexpr>  s&      ??DGO??????r
  NTF)r  rZ   r  r~   r_  rU  r  r  rp  r   r)  r   rj  r   )rJ  r  
native_intr  r  argts         r  r  z)TypeChecker.analyze_range_native_int_type  s    tX&&	"4;00	" $(888S^^((((q(((((??????? ) '+JBy # #&t'7'7'<'<==dH-- #$)2DH^2^2^!)%)

++" "j "!!tr
  c                T   t          |          }t          |t                    rLg }|j        D ].}|                     |          }|r|                    |           /t          j        |          S t          |t                    rk|j        	                    d          rQ| 
                    d          j        }t          ||          }t          |j                  dk    sJ |j        d         S t          |t                    r"|                     t          |                    S dS )zzCheck if a type is a nominal container of a union of such.

        Return the corresponding container item type.
        ztyping.Containerr   r   N)r   r  r   r  analyze_container_item_typer  r  r   rj  r  rl  r7   rU  r  r   r   )rJ  r  rs  r  c_typerW  super_instances          r  r  z'TypeChecker.analyze_container_item_type  s$   
 c""c9%% 	/ "E	 ) )99$?? )LL((('...c8$$ 	*):):;M)N)N 	*(:;;@I6sIFFN~*++q0000!&q))c9%% 	I33N34G4GHHHtr
  r  r  c                \    |                      ||                     ||          |           dS )z>Type check or infer for loop or list comprehension index vars.N)r%  r  )rJ  r  r  r4  r  s        r  r  z#TypeChecker.analyze_index_variables  s1     	eT^^Iw%G%GIZ[[[[[r
  r`   c                f   t          |j        t                    r|j        }t          |j        d          }|j        |_        |j        |_        t          ||j        gt          j
        gd g          }|j        |_        |j        |_        | j                            |d           d S |j                            | j                   t          |j                  D ]T}t          |t                    r=| j                            |t#          |j                  t'          |          d           Ud S )N__delitem__Trw  r  F)r  r  rn   rt   rK  r  r7  rZ   r  r   rG   r  rh  flattenrv   r  r-  r   r  r   )rJ  r  r  mr  elts         r  visit_del_stmtzTypeChecker.visit_del_stmt  s   afi(( 	A16=11AVAFxAHQWI??AVAFxAH$$Q$$?????FMM$+,,,qv  c8,, K++[999?3;O;OQV   r
  r_   c                    |j         D ]T}t          |t                    r=|j        dk    r2t	          t
          j                  |j        _        d|j        _	         d S U| 
                    |           d S )Nztyping.no_type_checkT)r  r  r~   r_  r   r   r   r  rj  rX  r  )rJ  r  rp  s      r  r  zTypeChecker.visit_decorator  sr     	 	A!W%% :!777!()?!@!@AEJ%)AENFF""1%%%%%r
  c           	     P   | j         r^| j                            |j                  5  |                     |j        |j        j        |           d d d            n# 1 swxY w Y   |                     |j                  }t          |j                  D ]D}t          |t                    r#|s |                     t          j        |           ;| j                            |          }|                     ||          }d }t#          |t$                    r	|j        pd }d }|it#          |t(                    rT|                     |j                  r:|                     |j                  }| j                            ||j                  }|                     |j        ||           | j                            ||gt6          j        g|||          \  }}	F|                     ||j                   t=          ||j                  }||j        _         d|j        _!        |j        j"        rgt#          |tF                    r=tI          d |j%        D                       dk    r| j&                            d|           | '                    |           |rd S |j        j(        r|j        )                                sq|j*        sj| +                    |          }
|j        j,        r.|
s,|
*| j&        -                    |j        j        |j                   | .                    |j        |
           |j        j(        re|j        j        dv rY|j         rTt#          t_          |j                   t`          tb          f          s(|                     t          j2        |           d S d S d S d S d S )	Nr  rJ  )r  r  Tc                :    g | ]}|                                 |S r	  )is_required)r  ks     r  r  z5TypeChecker.visit_decorator_inner.<locals>.<listcomp>8  s%    DDDaAMMOODDDDr
  r   zToo many arguments for propertyrE  )3rC  r!  r  r  r  r  r   r  r  r   r   rm  r   r  r  rh  r  r  r~   r_  rt   r(  r  r)  rS  check_for_untyped_decoratorr  r   rG   check_untyped_after_decoratorr   r  rj  rX  r  r   rU  rp  r  $check_incompatible_property_overrider  r&  r'  r  r  r  r  r   r   r   BAD_CONSTRUCTOR_TYPE)rJ  r  r  r  rp  r  r  r_  r  t2r  s              r  r  z!TypeChecker.visit_decorator_inner  s   & 	X++AF33 X X$$QV!&+;$WWWX X X X X X X X X X X X X X X
 &&qv..!,'' 	 	A!!^44 " OII.JANNN#**1--C>>#q>11DH!W%% .:- (,KJq*$=$=$--PQPVBWBW"..qv66,<<[!&QQ,,QVS!<<<'22dVem_axU` 3  GC 	**3777QV,,
6 	9#|,, HDD3=DDDEEIIHMM"CQGGG55a888 	F6; 	Vqv0022 	V1= 	V(,(B(B1(E(E%+D1D .9..qv{AFCCC2216;TUUU6; 	D16;*AAAv Dj)@)@<QXBYZZ D		*?CCCCC	D 	DAAD D D Ds   (AA"Ar  dec_typedec_exprc                    | j         j        rEt          |j                  r3t	          |          r&| j                            |j        |           d S d S d S d S rN  )r-  disallow_untyped_decoratorsis_typed_callablerj  is_untyped_decoratorr   typed_function_untyped_decoratorr  )rJ  r  r  r  s       r  r  z'TypeChecker.check_for_untyped_decoratorL  s~     L4	K!$),,	K %X..	K
 H55diJJJJJ	K 	K 	K 	K 	K 	Kr
  c                   |j         j        s|j        j        r|j        j        }|j        j        j        dd          D ]}|j                            |          }|st          |j	        t                    r[|j	        j        rOt          t          |j	        j        d                   j         j        r |                     t           j        |           d S d S d S r_  )r  r[  r  r  r  rI  r<  ri  r  r  rz   r  r   r_   r  rm  r   'READ_ONLY_PROPERTY_OVERRIDES_READ_WRITE)rJ  r  r  rK  rS  s        r  r  z0TypeChecker.check_incompatible_property_overrideV  s    u) 	[afk 	[6;D+ 	[ 	[ JNN400	  y~/@AA[!2[ Y	(<Q(?@@DY[
 II.VXYZZZ	[ 	[ 	[ 	[	[ 	[r
  r   c                v   d}t          |j        |j                  D ]\  }}|j        r|                     |||j        d u           }n|                     |||j        d u           }t          |          }t          |dd          rjt          |dd          s1t          |t                    r|j        j        dk    rt          j        rd}|rQ| j                            dd          5  |                     |j                   d d d            d S # 1 swxY w Y   d S |                     |j                   d S )NFbuiltins.boolT)r  r  )r  r  r  r  check_async_with_itemr,  check_with_itemr   r   r  r   rj  r_  r   r]  r  r  rh  r  )rJ  r  exceptions_maybe_suppressedr  r  exit_ret_types         r  visit_with_stmtzTypeChecker.visit_with_stmtd  s   &+#11 	3 	3LD&z ^ $ : :4IZ^bIb c c $ 4 4T61CTX\C\ ] ] ,M::M}ouEE }otDD 3=(333!&/?BB) C
 /3+& 	  **DD*II $ $AF###$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ KKs   *DDDc                    | j         j        r| j        rd S t          j                            |          r| j                            ||           d S d S rN  )r-  disallow_any_decoratedr@  rG  rH  has_any_typer  untyped_decorated_function)rJ  r  r  s      r  r  z)TypeChecker.check_untyped_after_decorator  s]    |2 	dl 	F>&&s++ 	;H//T:::::	; 	;r
  r  c                   | j         }|                    |          }|                    d|g g |          d         }|                    ||t          j                  }|r+|                     ||                     ||          |           |                     t          t          j
                  |          }|                    d||gdz  t          j        gdz  |          \  }}	|                    ||t          j                  S )N
__aenter__r   	__aexit__r  )r  rh  r  r  r   'INCOMPATIBLE_TYPES_IN_ASYNC_WITH_AENTERr%  r  r   r   r   r   rG   &INCOMPATIBLE_TYPES_IN_ASYNC_WITH_AEXIT
rJ  r  r  r4  r  r  objr  r  r  s
             r  r	  z!TypeChecker.check_async_with_item  s     kk$,,\3BMMaP'''O
 
  	X!!&$..d*C*CEVWWWnnWY%;<<dCC//seai%-1)<d
 
Q (('N
 
 	
r
  c                   | j         }|                    |          }|                    d|g g |          d         }|r+|                     ||                     ||          |           |                     t          t          j                  |          }|                    d||gdz  t          j	        gdz  |          \  }}	|S )N	__enter__r   r8  r  )
r  rh  r  r%  r  r   r   r   r   rG   r  s
             r  r
  zTypeChecker.check_with_item  s      kk$,,[#r2tLLQO 	X!!&$..d*C*CEVWWWnnWY%;<<dCC//cUQY!(;T
 
Q 
r
  rX   c                8    | j                                          d S rN  )r  handle_breakrt  s     r  visit_break_stmtzTypeChecker.visit_break_stmt  s      """""r
  r^   c                8    | j                                          d S rN  )r  handle_continuert  s     r  visit_continue_stmtzTypeChecker.visit_continue_stmt  s    ##%%%tr
  rs   c           
          j                             dd          5  t           j                            |j                            t          t                    r j        	                    |            fd|j
        D             }d |D             }                     |          }t          |j
        |j        |j                  D ]\  }}} j                            |j                  } j                            ||          }	 j                             dd          5  |j        s't          t          |	j                  t&                    r                     d            i }
nmt+          |j        |	j        |	j                  \  }}
                     |	j        |                                |                                |	j                   |h j                             dd          5  t           j                            |                    }t          |t                    r j        	                    ||                                |          \  }}t5          |
|          }
t          |t6                    ru|j        p|j        }t          |t<                    rR||
fD ]M}|st?          |          D ]8}t          |t<                    r|j         |j         k    s(||         ||j        <   9N                     |                                |           d d d            n# 1 swxY w Y   n                     |           d d d            n# 1 swxY w Y                        |
            j                             dd          5  	 d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )	NFr   r  c                F    g | ]}j                             |          S r	  )r   rh  )r  prJ  subject_types     r  r  z0TypeChecker.visit_match_stmt.<locals>.<listcomp>  s,    ^^^aT188LII^^^r
  c                    g | ]	}|j         
S r	  )capturesr,  s     r  r  z0TypeChecker.visit_match_stmt.<locals>.<listcomp>  s    'J'J'Jq
'J'J'Jr
  Tr  r  )!r  r  r   r  rh  subjectr  r   r  r$  patterns#infer_variable_types_from_type_mapsr  guardsbodiesnarrow_type_from_binderr   rd  rj  r   r  conditional_types_to_typemaps	rest_typeremove_capture_conflictsr&  r  or_conditional_mapsr   patternr  rv   r5  r_  )rJ  r  pattern_types	type_mapsinferred_typesr#  gr  current_subject_typepattern_typer  pattern_mapr  	guard_mapguard_else_mapcase_targetrK  r  r$  s   `                 @r  visit_match_stmtzTypeChecker.visit_match_stmt  s   [&&A&FF D	 D	*4+<+C+CAI+N+NOOL,44 <**<;;; _^^^^STS]^^^M'J'JM'J'J'JI!EEiPPN qz18QX>> /- /-1a'+'8'P'PI|( ($  $3::1>RSS[..1.MM )' )'' B:'(9::O, , B **4000,.0MI|'8,:P1 1-X 55l6K^\\\**;777**<+@AAA}![66TU6VV + +!01B1I1I!1L1L!M!MB)"k:: B $ : :2q A A A8<8R8RST8U8U5I~':8^'T'TH  *!Y77 Q./i.A16#-k8#D#D 
!Q5>4I 	%Q 	%Q/7 )5,448NN )Q )QD0:40J0J-948M[EY4Y4Y08BJ4.HQY,?,?)Q !..y999 KKNNN3+ + + + + + + + + + + + + + +6 AS)' )' )' )' )' )' )' )' )' )' )' )' )' )' )'T ""8,,,, **E*JJ                GD	 D	 D	 D	 D	 D	 D	 D	 D	 D	 D	 D	 D	 D	 D	 D	 D	 D	s   DO/CM/D4M	6M/MM/	M
M/#O/M33O6M376O-N;/O;N?	?ON?	OOOr3  list[TypeMap]dict[Var, Type]c           
        t          t                    }|D ]n}|j|                                D ]U\  }}t          |t                    r;|j        }t          |t                    sJ ||                             ||f           Voi }|                                D ]\  }}	d}
g }|	D ]a\  }}|                    |           |                     |          \  }}}|,d}
| 	                    |||t          j        dd          r|||<   b|
s<t          j        |          }|	d         \  }}|||<   |                     ||||           |S )NFTzpattern captures typezvariable has type)r  rT  rV  r   )r   r5  r  r  rv   r  r   r  r  r.  r   INCOMPATIBLE_TYPES_IN_CAPTUREr   r  rM  )rJ  r3  all_capturestmr  r  r  r4  r  r&  already_existsrs  previous_typer  r/  first_occurrences                   r  r)  z/TypeChecker.infer_variable_types_from_type_maps  s   ?J4?P?P 	? 	?B~!# ? ?ID#!$11 ?#y)$44444$T*114+>>>*,)//11 	\ 	\MC"N "E% < <	cS!!!&*&7&7&=&=#q! ,%)N))%,J&=(; *   < /<s+! \$/66&.qk# !&.s#((.>JZ[[[r
  rK  r  r4  c                    |rwt          |                                          D ]W\  }}t          |t                    r;|j        }t          |t
                    sJ ||vst          |||                   s||= Vd S d S rN  )r5  r  r  rv   r  r   r   )rJ  rK  r4  r  r  r  s         r  r/  z$TypeChecker.remove_capture_conflicts%  s     	+!(.."2"233 + +	cdH-- +9D%dC00000>11CX\I]9^9^1$TN	+ 	++ +r
  curr_module_fullnameclass_gen_nameclass_short_namer  list[Instance]tuple[ClassDef, TypeInfo]c                   t          |t          g                     }|dz   |z   |_        t          t	                      ||          }||_        ||_        t          |           |                                |_	        ||fS )N.)
r[   rW   r_  r   r   r  r  rE   calculate_metaclass_typer  )rJ  rG  rH  rI  r  cdefr  s          r  make_fake_typeinfozTypeChecker.make_fake_typeinfo.  s{     (%))44,s2^Ct-ABB	
d";;==Tzr
  	instancestuple[Instance, Instance]list[tuple[str, str]]Instance | Nonec                     j         j        d         }t          |t                    sJ |\  }}t	          ||d          r|S t	          ||d          r|S dd}d fd} ||          }t          t          | j        ddd          }	g }
|D ]3}|j        j	        r%|

                    |	d|j        j         df           4|
r|                    |
           dS 	  |||          \  }} j                                        5 }                     |           ddd           n# 1 swxY w Y   |                                ri ||ddd                   } |||          \  }} j                                        5 }                     |           ddd           n# 1 swxY w Y   d|_        n(# t$          $ r |
                    |	df           Y dS w xY w|                                r|
                    |	df           dS t'          t(          |          |j        |<   t-          |g |d         j        p|d         j                  S )a{  Try creating an ad-hoc intersection of the given instances.

        Note that this function does *not* try and create a full-fledged
        intersection type. Instead, it returns an instance of a new ad-hoc
        subclass of the given instances.

        This is mainly useful when you need a way of representing some
        theoretical subclass of the instances the user may be trying to use
        the generated intersection can serve as a placeholder.

        This function will create a fresh subclass every time you call it,
        even if you pass in the exact same arguments. So this means calling
        `self.intersect_intersection([inst_1, inst_2], ctx)` twice will result
        in instances of two distinct subclasses of inst_1 and inst_2.

        This is by design: we want each ad-hoc intersection to be unique since
        they're supposed represent some other unknown subclass.

        Returns None if creating the subclass is impossible (e.g. due to
        MRO errors or incompatible signatures). If we do successfully create
        a subclass, its TypeInfo will automatically be added to the global scope.
        r   Fignore_promotions
instances_rR  r9  rJ  c                    g }| D ]8}|j         j        r|j         j        }n|g}|D ]}|                    |           9|S rN  )rj  is_intersectionr  r  )rX  base_classes_r  rY  expanded_insts        r  _get_base_classesz:TypeChecker.intersect_instances.<locals>._get_base_classesk  sf    M" 8 89, &#yHH $vH%- 8 8M!((77778  r
  r[  curr_module_ru   tuple[TypeInfo, str]c                    t          d | D             d          }d| d}t          ||j                  }                    |j        |||           \  }}||fS )Nc                &    g | ]}|j         j        S r	  )rj  r  r  r  s     r  r  z^TypeChecker.intersect_instances.<locals>._make_fake_typeinfo_and_full_name.<locals>.<listcomp>z  s    $H$H$HQQV[$H$H$Hr
  andz<subclass of >)rC   gen_unique_namer<  rP  r_  )r[  r^  
names_list
short_name
full_name_rO  info_rJ  s          r  !_make_fake_typeinfo_and_full_namezJTypeChecker.intersect_instances.<locals>._make_fake_typeinfo_and_full_namew  sw     $$H$H-$H$H$H%PPJ6666J(\5GHHJ11%z:} KD% *$$r
  T)r-  barerc  rI  z
" is finalNrG  z/would have inconsistent method resolution orderz)would have incompatible method signaturesr   extra_attrs)rX  rR  r9  rJ  )r[  rJ  r^  ru   r9  r_  )r#  rV  r  ru   r   rC   rA   r-  rj  rO  r  r  r  r  r  r  rG  rZ  rD   r   rL   r<  r   rm  )rJ  rQ  r  curr_moduler  r  r]  rj  base_classespretty_names_list
new_errorsrK  r  	full_namerJ  s   `              r  intersect_instanceszTypeChecker.intersect_instancesD  sX   2 j&q)+x00000  eT5EBBB 	KUDEBBB 	L
	! 
	! 
	! 
	!		% 		% 		% 		% 		% 		% )(33 '"L$,TRRRTY
 
 
  	W 	WDy! W!!#46T$).6T6T6T"UVVV 	MM*%%%4	??kZZOD)'')) 6\//5556 6 6 6 6 6 6 6 6 6 6 6 6 6 6**,, :0044R4AA"C"CLR]"^"^iX++-- :33D999: : : : : : : : : : : : : : :#'D   	 	 	MM,._`aaa44	 &&(( 	MM,.YZ[[[4'6tT'B'B)$bil.F.b)TU,Jbccccs[   (F9 D)F9 )D--F9 0D-1AF9 F&F9 &F**F9 -F*.
F9 9!GGr   callable_typec                   | j         j        d         }t          |t                    sJ t	          d|j        j         d|j                  }t          || j	                  }| 
                    |j        |||g          \  }}t          dg t          g           |          }|j        dz   |_        ||_        t!          t"          |          |j        d<   t!          t$          |          |j        |<   t'          |g |j                  S )zCreates a fake type that represents the intersection of an Instance and a CallableType.

        It operates by creating a bare-minimum dummy TypeInfo that
        subclasses type and adds a __call__ method matching callable_type.
        r   z<callable subtype of rd  r  z	.__call__rl  )r#  rV  r  ru   re  rj  r  r<  r@   r-  rP  r_  rg   rW   	_fullnamer  r   rR   rL   r   rm  )	rJ  r  rt  
cur_modulegen_namerg  rO  r  func_defs	            r  intersect_instance_callablez'TypeChecker.intersect_instance_callable  s     Z%a(
*h/////"#K38=#K#K#KZM]^^ &c4<88
,,Z-@(JY\X]^^
d :r599mDD!][8!0x!@!@
:%4T4%@%@
"bco>>>>r
  c                @   |                      d          }t          t          t          j                  t          t          j                  gt
          j        t
          j        gddgt          t          j                  |d          }|                     ||          S )zIProduce a new type that makes type Callable with a generic callable type.r  NT)r  r  is_ellipsis_args)	rl  r   r   r   explicitr   rH   rq  rz  )rJ  r  r  rt  s       r  make_fake_callablezTypeChecker.make_fake_callable  s     ??#677$Y'((')2D*E*EF^U_-4LY/00!
 
 
 //]CCCr
  unsound_partitiontuple[list[Type], list[Type]]c                   t          |          }t          |t          t          f          r|gg fS t          |t                    r|g|gfS t          |t
                    rg |gfS t          |t                    rVg }g }|j        D ]F}|                     |d          \  }}|	                    |           |	                    |           G||fS t          |t                    r1|                     t          |          |          \  }}|r|gng }||fS |}t          |t                    rt          |          }t          |t                    r|j                            d          }	|	r/|	j        r(|                     |	j        d          \  }}|r|s|gg fS |s_|                     |          }
t          |t                    r/t          |j        |
          |
j        _        |
j        j        g|gfS |
g|gfS |rg |gfS |g|gfS )a(  Partitions a type into callable subtypes and uncallable subtypes.

        Thus, given:
        `callables, uncallables = partition_by_callable(type)`

        If we assert `callable(type)` then `type` has type Union[*callables], and
        If we assert `not callable(type)` then `type` has type Union[*uncallables]

        If unsound_partition is set, assume that anything that is not
        clearly callable is in fact not callable. Otherwise we generate a
        new subtype that *is* callable.

        Guaranteed to not return [], [].
        Tr  r  F)r   r  r   r   r   r   r   r  partition_by_callabler  r   r   r   r   r   rj  rQ  r~  r  )rJ  r  r  	callablesuncallablesrU  subcallablessubuncallablesitypr  fakes              r  r  z!TypeChecker.partition_by_callable  s   " c""cL(344 	5"9c7## 	 53%<c8$$ 	u9c9%% 	*IK9 3 3 04/I/It 0J 0 0,n   ..."">2222k))c;'' 	* &*%?%?',,.?& &"I{ $/63%%BKk)) c9%% 	'!#&&DdH%% 	%Y))*55F %&+ %)-)C)CK5 *D * *&	;  %[ %  5"9$$ %..t44c9-- 9+4SY+E+EDI( I01C588vu}$ 	 u9 53%<r
  current_typetuple[TypeMap, TypeMap]c                   |si i fS t          t          |          t                    ri i fS |                     |d          \  }}|r:|r8|r|t	          j        |          ind}|r|t	          j        |          ind}||fS |ri dfS di fS )a8  Takes in an expression and the current type of the expression.

        Returns a 2-tuple: The first element is a map from the expression to
        the restricted type if it were callable. The second element is a
        map from the expression to the type it would hold if it weren't
        callable.
        Fr  N)r  r   r   r  r   r  )rJ  r  r  r  r  callable_mapuncallable_maps          r  conditional_callable_type_mapz)TypeChecker.conditional_callable_type_map(  s      	r6Mol33W== 	r6M!%!;!;L\a!;!b!b	; 	 	FOYD)"6y"A"ABBUYLJU_dI$8$E$EFF[_N// 	t8ORxr
  r  tuple[Type | None, Type | None]c                   g }g }t          |          }t          |t                    r"t          |                                          }n|g}t          |          }|D ]}|rt          |t                    ru|D ]q}||j        v r|                    |           !||j	        v s|j
        s+|                    |           |                    |           \|                    |           r|                    |           |                    |           |rt          j        |          nd|rt          j        |          ndfS )z
        Narrows the type of `iterable_type` based on the type of `item_type`.
        For now, we only support narrowing unions of TypedDicts based on left operand being literal string(s).
        N)r   r  r   r   r  r   r   required_keysr  r  rO  r  )	rJ  r  r  if_types
else_typespossible_iterable_typesitem_str_literalspossible_iterable_typer  s	            r  conditional_types_for_iterablez*TypeChecker.conditional_types_for_iterableD  s     "!#
'66mY// 	6&6}7S7S7U7U&V&V##'4o#>yII&= 	: 	:"  :Z0F%V%V :, 	B 	BC4BBB (>????5;;;CYCb; (>???"))*@AAAA"))*@AAAA	B  6777!!"89999 /7@I ***D0:DI ,,,
 	
r
  c                    t          |t                    oYt          |j                  oE|j                            d           o*|j                            d           o|j        j        dk    pVt          |t                    pAt          |t                    o,t           fdt          |j
                  D                       S )N__bool____len__r  c              3  B   K   | ]}                     |          V  d S rN  )_is_truthy_typer  r  rJ  s     r  r  z.TypeChecker._is_truthy_type.<locals>.<genexpr>w  s1      SSA,,Q//SSSSSSr
  )r  r   r  rj  r  r_  r   r   r  r   r  rJ  r  s   ` r  r  zTypeChecker._is_truthy_typek  s     1h'' 9LL922:>>>9 229===9 FO'88 !\** 1i(( TSSSS9I!'9R9RSSSSS	
r
  c                    t           j        sd S t                                                   sd S d fd}d fd}t	          t
                    r=                     t          j        	                     |                                 d S t	          t                    r=                     t          j        	                     |                                 d S t	          t                    rj        j        dk    r                     dddg           \  }}                     t          j        	                     |            t#          t          |j                   j                                       d S                      t          j        	                     |                                 d S )	Nr9  r6  c                    t          j                  } t          t                    rdj         d|  S t          t
                    rj        rdj         d|  S t          t                    rit          j        t                    rdj        j         d|  S t          j        t
                    rj        j        rdj        j         d|  S d|  S d|  S )NzMember "z" has type rI  z
" returns zCall returns zExpression has type )	r?   r-  r  rt   r  r~   r_  rZ   r  )r  r  rJ  r  s    r  format_expr_typez<TypeChecker._check_for_truthy_type.<locals>.format_expr_type  s   a..C$
++ 4=$)=====D'** 	4t} 	4:4=::S:::D(++ 4dk:66 E@t{/@@3@@@W55 E$+:N EDt{3DDsDDD,s,,,3c333r
  c                 |    t           t          t          f          rd j         dS t	          j                  S )NrI  )r  rv   rt   r  r?   r-  )r  rJ  r  s   r  get_expr_namez9TypeChecker._check_for_truthy_type.<locals>.get_expr_name  sA    $: 677 4'49'''' #1dl333r
  r#  typing
Collection)r9  r6  )r   r]  r   r  r  r   rm  r   FUNCTION_ALWAYS_TRUEro  r   TYPE_ALWAYS_TRUE_UNIONTYPEr   rj  r_  rP  ITERABLE_ALWAYS_TRUEr?   r  r-  TYPE_ALWAYS_TRUE)rJ  r  r  r  r  r  r  s   ```    r  _check_for_truthy_typez"TypeChecker._check_for_truthy_type{  s   $ 	FA##A&& 	F	4 	4 	4 	4 	4 	4 	4 	4	4 	4 	4 	4 	4 	4 	4 	4 a&& 	ZII&;BB==??SSUYZZZZZ9%% 	ZII&AHHIYIYI[I[\\^bccccc8$$ 		Z<M)M)M--hlTVWWGAtII 5<<$$&&HT164J4JDL(Y(Y  	     II&7>>?O?O?Q?QRRTXYYYYYr
  r\   expr_indices	list[int]c                   dd}g }d}d}|D ]}|j         |         }t          |t                    r, ||          r!|                    |j        d                    P|                     |          }	|	h|i i fc S t          |t                    r&t          |j        t                    r|j        j	        }|	}|si i fS g }
g }|D ]m}| 
                    |                     |          ||          \  }}t          |||          \  }}|
                    |           |                    |           ndd} ||
          }|r ||          }ni }||fS )a  Narrow types based on any checks of the type ``type(x) == T``

        Args:
            node: The node that might contain the comparison
            expr_indices: The list of indices of expressions in ``node`` that are being
                compared
        r  rZ   r9  r  c                \    t          | j        d          ot          | j                  dk    S )z)Is expr a call to type with one argument?r  r   )r   r  rU  r  )r  s    r  is_type_callz8TypeChecker.find_type_equals_check.<locals>.is_type_call  s'    %dk?CC[DIZ[H[[r
  NFr   	list_mapsr=  r  c                B    i }| D ]}||                     |           |S )z2Combine all typemaps in list_maps into one typemap)update)r  
result_maprp  s      r  combine_mapsz8TypeChecker.find_type_equals_check.<locals>.combine_maps  s6    J ) )=%%a(((r
  )r  rZ   r9  r  )r  r=  r9  r  )operandsr  rZ   r  r  get_isinstance_typer~   r  r   rO  #conditional_types_with_intersectionr)  r-  )rJ  r  r  r  exprs_in_type_callstype_being_comparedrO  r  r  r  if_maps	else_mapscurrent_if_typecurrent_else_typecurrent_if_mapcurrent_else_mapr  r  r  s                      r  find_type_equals_checkz"TypeChecker.find_type_equals_check  s   	\ 	\ 	\ 	\
 136:! 	7 	7E='D$)) 7ll4.@.@ 7#**49Q<8888#77=='&2 r6MMM!$00 6Z	85T5T 6#'9#5*6''" 	r6M!##%	' 	/ 	/D151Y1Y  &&(;T2 2.O. 0Mo'80 0,N, NN>***-....	 	 	 	 g&&
  	#|I..HHHxr
  c                    |                      |          \  }}|                     |          }|                     |          }||fS )a  Find any isinstance checks (within a chain of ands).  Includes
        implicit and explicit checks for None and calls to callable.
        Also includes TypeGuard functions.

        Return value is a map of variables to their types if the condition
        is true and a map of variables to their types if the condition is false.

        If either of the values in the tuple is None, then that particular
        branch can never occur.

        May return {}, {}.
        Can return None, None in situations involving NoReturn.
        )find_isinstance_check_helperpropagate_up_typemap_info)rJ  r  r  r  
new_if_mapnew_else_maps         r  r  z!TypeChecker.find_isinstance_check  sM      <<TBB33F;;
55h??<''r
  c           
     F  . t          |          ri d fS t          |          rd i fS t          |t                    rt	          |j                  dk    rt          |j        d                   }t          |j        d          rt	          |j                  dk    ri i fS t          |          t          k    rSt          |g|                     |                     |          |                     |j        d                   |          R  S 	nt          |j        d          rLt	          |j                  dk    ri i fS t          |          t          k    r|                     ||          S 	n t          |j        d          rat	          |j                  dk    ri i fS t          |          t          k    r+|                     |          }|                     ||          S nt          |j        d          rt	          |j                  dk    ri i fS t#          |j        d         |                     |j        d                             }t          |          t          k    rE|rCt	          |          dk    r0|                     ||                     |          |d                   S nt          |j        t&                    rS|j        j        F|j        d         t,          j        k    rt1          |                     |j                            }t          |t2                    r$t5          d||d	
          }|t1          |          }t          |t6          t8          f          rz|j        d         j        d         }||j        v r5|j                            |          }t          |j        |                   }n$|                      tB          j"        |           i i fS t          |          t          k    r|tG          |j        j                  ii fS nqt          |tH                    r9d |j%        D             }	g .i }
tM          |	          D ]\  }}| '                    |          si i fc S |                     |          }.(                    |           t          |          t          k    r:tS          |          s+| *                    |          stW          |          }|||
|<   tY          |-                                |
ddh          }g }|D ]\  }}|dv r|dv rt\          }d}d	}n7d$d}d$d}|}d	}.fd|D             }t_          ta          ||                    }i }i }|r/| 1                    |	.||
2                                ||          \  }}|i k    r3|i k    r-| 3                    |	.||
2                                          \  }}|i k    r|i k    r| 4                    ||          \  }}n|dv rt	          |          dk    sJ |\  }}.|         }.|         }i i }}||
v rtk          |          rzt1          tm          |                    }|\tk          |          sMt          |t2                    r|j7        j8        dk    s(ts          ||          rtu          |          ||	|         <   ||
v r5| ;                    ||          \  }} |	|         }|d }n|||<   | d }n
| ||<   ni }i }|dv r||}}|(                    ||f           ty          d |D                       rt{          |          S t{          | >                    |          d	          S t          |t~                    ri }i }| @                    |jA                  \  }!}"|!|B                    |!           |"|B                    |"           | @                    |jC                  \  }#}$|#|B                    |#           |$|B                    |$           |!|#d n||"|$d n|fS t          |t                    rg|jE        dk    r\| @                    |jF                  \  }%}&| @                    |jG                  \  }'}(t          |%|'          t          |&|(d	          fS t          |t                    re|jE        dk    rZ| @                    |jF                  \  }%}&| @                    |jG                  \  }'}(t          |%|'          t          |&|(          fS t          |t                    r,|jE        d k    r!| @                    |jK                  \  })}*|*|)fS t          |          t          k    r| '                    |          r| L                    |                     |                    rt          |                     |          d!          s| jN        jO        r| P                    |                     |          d"d          \  }+},|+t          |+          }+nt                      }+|,t          |,          },nt                      },t          |+t                    s||+ind }t          |,t                    s||,ind }||fS |                     |          }-| T                    |-|           t          |-d#          }t          |          }t          |          } t          |t                    s||ind }t          | t                    s|| ind }||fS )%Nr   zbuiltins.isinstancer  r   zbuiltins.issubclasszbuiltins.callablezbuiltins.hasattrr  Tr  c                ,    g | ]}t          |          S r	  )collapse_walrusrb  s     r  r  z<TypeChecker.find_isinstance_check_helper.<locals>.<listcomp>U  s     BBBq**BBBr
  ==is>   r  !=r  is not>   r  r  Fr  r   r9  r  c                F    t          t          |           t                    S rN  )r  r   r   r  s    r  is_exactly_literal_typezITypeChecker.find_isinstance_check_helper.<locals>.is_exactly_literal_type  s    #-oa.@.@+#N#NNr
  c                N    t          | dd           ot          | dd           S )Nr  F)	check_allr  )r   r  s    r  has_no_custom_eq_checkszITypeChecker.find_isinstance_check_helper.<locals>.has_no_custom_eq_checks  sB    '< !8u( ( ( $ $Z&;AxSX&Y&Y&Y"YZr
  c                     g | ]
}|         S r	  r	  )r  r  operand_typess     r  r  z<TypeChecker.find_isinstance_check_helper.<locals>.<listcomp>  s    %M%M%M1mA&6%M%M%Mr
  >   innot inr  >   r  r  r  c              3  &   K   | ]}|i i fk    V  d S rN  r	  )r  r  s     r  r  z;TypeChecker.find_isinstance_check_helper.<locals>.<genexpr>  s*      <<Q1R=<<<<<<r
  use_meetrc  )coalesce_anyornotr  rd  r  r  )Vis_true_literalr   r  rZ   rU  r  r  r   r  r5   rQ   r-  r  r)  r  infer_issubclass_mapsr  r   hasattr_type_mapsr~   r~  rp  r   rG   r   r   r   r   r   r  rv  r  rm  r   TYPE_GUARD_POS_ARG_REQUIREDr   r\   r  r  r(  r  rD  is_literal_enumr6   group_comparison_operandspairwiser   r  r  %refine_identity_comparison_expressionr  refine_away_none_in_comparisonr  r   builtin_item_typerj  r_  r8   r   r  r  reduce_conditional_mapsfind_tuple_len_narrowingrU   r  r  r  r3  ry   rf  r  r  and_conditional_mapsr0  r   r  can_be_narrowed_with_lenr   r-  r]  narrow_with_lenr   r   r   r  r   )/rJ  r  r  vartyper  called_typer  r  r  r   narrowable_operand_index_to_hashr  rn  hsimplified_operator_listpartial_type_mapsoperatorr  is_valid_targetcoerce_only_in_literal_contextshould_narrow_by_identityr  r  
expr_typesr  r  
left_indexr  r  r  collection_item_typeif_type	else_typeif_assignment_mapelse_assignment_mapif_condition_mapelse_condition_mapleft_if_varsleft_else_varsright_if_varsright_else_varsr  r  yes_typeno_typeoriginal_vartyper  s/                                                 @r  r  z(TypeChecker.find_isinstance_check_helper	  s]   4   	t8OD!! 	8OdH%% p	$#di..A*=*="49Q<00D!$+/DEE >Sty>>Q&&r6M4==L008AA ,,T22D4L4LTYWX\4Z4Z\`     1 $DK1FGG 4Sty>>Q&&r6M4==L0055dDAAA 1#DK1DEE /Sty>>Q&&r6M4==L00"..t44G==dGLLL 1 $DK1CDD )Sty>>Q&&r6M/	!d>N>NtyYZ|>\>\]]4==L00T0c$ii1nn11$8H8H8N8NPTUVPWXXXDK11 #S;)5~a(EM99&5d6F6Ft{6S6S&T&T &k8<< D#. *KRV$ $ $D  $/.=d.C.C &kL*3MNN .#.#4Q#7#A!#DD#t~55&*n&:&:4&@&@'6ty~'F'F $		*:*VX\ ] ] ]')2vt}}44 !%odk6L&M&MNPRRRn-- o	$
 CBDMBBBHM/1,$X.. @ @4}}T** "r6MMM ,,T22	$$Y/// DMM\11+D11 2 0066 2 %T**A}>?8;$ (A!AD$<( ($ !#*B p= p=&,;;;"  #333BS9>64811O O O OZ Z Z Z
 +B9=6%M%M%M%M%M%M%M
47<SU_8`8`4a4a1&(F(*H0 +/+U+U$)(<AACC+:, ,( ||B+/+N+N$)(<AACC	, ,( ||B+/+F+Ft\+Z+Z(!111|,,1111.:+J -j 9I$1+$>M')2HF!%EEE.y99 Z3B 1- @ @4 40 !5 @(;<P(Q(Q !A %//CX$N$N !A )=(A(JN_(_(_$?	K_$`$` )` @Oy?Y?Yx
'; <"&FFF-1-P-P%}. .*  (4"?%)FF+2F4L$,'+HH-6HTN  F!H999'/HF!((&();<<<<
 <<*;<<<<< c./@AAA /t/L/LT/R/R]abbbbn-- D	$FH595O5OPTP[5\5\22 ,/000". 3444373M3Mdj3Y3Y00+.///!- 2333 +26F6NTZ,48J8RX`  f%% .	$$'U*:*:+/+E+Edi+P+P(L.-1-G-G
-S-S*M?
 %\=AA $NORVWWW  f%% !	$$'T//+/+E+Edi+P+P(L.-1-G-G
-S-S*M?
 $L-@@$^_EE  i(( 	$TW-=-=44TY??KD%$;DMM\))d## *--d.>.>t.D.DEE * *$*:*:4*@*@*MM	 *
 , * !% 4 4T5E5Ed5K5KSRS T THg#$X..*,,"$W--)++-7/-R-R\dH%%X\F.8/.R.R\gX\H8##  ++D11##$4d;;;12BOTTG$$w''	(27O(L(LV$RV,6y/,R,R\D)$$X\xr
  	new_typesc                    |dS i }|                                 D ]D\  }}|||<   |                     ||          }|                                 D ]\  }}||v r
|||<   E|S )a  Attempts refining parent expressions of any MemberExpr or IndexExprs in new_types.

        Specifically, this function accepts two mappings of expression to original types:
        the original mapping (existing_types), and a new mapping (new_types) intended to
        update the original.

        This function iterates through new_types and attempts to use the information to try
        refining any parent types that happen to be unions.

        For example, suppose there are two types "A = Tuple[int, int]" and "B = Tuple[str, str]".
        Next, suppose that 'new_types' specifies the expression 'foo[0]' has a refined type
        of 'int' and that 'foo' was previously deduced to be of type Union[A, B].

        Then, this function will observe that since A[0] is an int and B[0] is not, the type of
        'foo' can be further refined from Union[A, B] into just B.

        We perform this kind of "parent narrowing" for member lookup expressions and indexing
        expressions into tuples, namedtuples, and typeddicts. We repeat this narrowing
        recursively if the parent is also a "lookup expression". So for example, if we have
        the expression "foo['bar'].baz[0]", we'd potentially end up refining types for the
        expressions "foo", "foo['bar']", and "foo['bar'].baz".

        We return the newly refined map. This map is guaranteed to be a superset of 'new_types'.
        N)r  refine_parent_types)rJ  r  
output_mapr  rn  new_mappingparent_exprproposed_parent_types           r  r  z%TypeChecker.propagate_up_typemap_infoM  s    2 4
(00 	? 	?OD)(Jt 224CCK5@5F5F5H5H ? ?11 )++*>
;''? r
  rn  Mapping[Expression, Type]c                   
 i }	 t          |t                    r9t          |j                                                 }|j        d
 fd}nt          |t                    ryt          |j                                                 }                     |j                  }||S t          |          d
fd}nt          |          

d

fd	}n|S |S ||S t          |          }t          |t                    s|S g }t          |j                  D ]E} |t          |                    }	|	|c S t          |	|          r|                    |           F|s|S }t#          |          x}|<   )a  Checks if the given expr is a 'lookup operation' into a union and iteratively refines
        the parent types based on the 'expr_type'.

        For example, if 'expr' is an expression like 'a.b.c.d', we'll potentially return refined
        types for expressions 'a', 'a.b', and 'a.b.c'.

        For more details about what a 'lookup operation' is and how we use the expr_type to refine
        the parent types of lookup_expr, see the docstring in 'propagate_up_typemap_info'.
        Tnew_parent_typer   r9  r  c                    j                                         5 }t          | dddj         | d
  
        }d d d            n# 1 swxY w Y   |                                rd S |S )NF)
r  r  r  r  r  r  r  r  r  in_literal_context)r  r  r"   rG  )r  wmember_typemember_namer  rJ  s      r  replay_lookupz6TypeChecker.refine_parent_types.<locals>.replay_lookup  s    //11 Q&;!, /$/&+%*(- $*9 $/4' ' '               '')) +#t**s   AA
A
Nc                     t           t                    sd S 	 J  fdD             }n# t          $ r Y d S w xY wt          |          S )Nc                *    g | ]}j         |         S r	  r  r  r  r  s     r  r  zJTypeChecker.refine_parent_types.<locals>.replay_lookup.<locals>.<listcomp>  s!    +_+_+_3O,A#,F+_+_+_r
  )r  r   r  r   )r  member_typesstr_literalss   ` r  r  z6TypeChecker.refine_parent_types.<locals>.replay_lookup  sx    )/=II (#'4(#/#;#;#;+_+_+_+_R^+_+_+_LL' ( ( (#'44(4\BBB   . 
<<c                     t           t                    sd S 	 J  fdD             }n# t          $ r Y d S w xY wt          |          S )Nc                *    g | ]}j         |         S r	  r  r  s     r  r  zJTypeChecker.refine_parent_types.<locals>.replay_lookup.<locals>.<listcomp>  s!    /c/c/cs0Ec0J/c/c/cr
  )r  r   
IndexErrorr   )r  r  int_literalss   ` r  r  z6TypeChecker.refine_parent_types.<locals>.replay_lookup  sx    #-oy#I#I ,'+t,'3'?'?'?/c/c/c/cVb/c/c/c#- , , ,'+tt,#8#F#FFr  )r  r   r9  r  )r  rt   r  r  lookup_type_or_noner  rn   rK  r  r   r   r   r   r   r  r9   r  r   )rJ  r  rn  outputparent_typer  r  new_parent_typesr  r  r   r  r  r  s   `         @@@@r  r  zTypeChecker.refine_parent_typesx  s7    *,h	V
 $
++ @-di88"66{CC"i+ + + + + + + + +& D),, (-di88"66{CC!55djAA
%!MA*MM+C C C C C C C $Fj#Q#QL#/G G G G G G G  & " *+66Kk955   "-k.?@@ 2 2+mOD,A,ABB& "MMM'Y?? 2$++D111
 $ D.CDT.U.UUI{+Qh	Vr
  r  r  chain_indicesnarrowable_operand_indicesAbstractSet[int]r  Callable[[ProperType], bool]r  c                T   d}|r ddt          fd|D                       }d}g }	|D ]c}
|
         }|rt          |          } |t          |                    s4|rt          ||          sdi fc S |}|	                    |
           d|i i fS d	}|	D ]}
|
|vr|
}	|d	k    r|	d	         }d}t          |          }t          |t                    r?|                                st          |j        t                    r|j
        j        j        }t          |d
          g}g }|D ]m}
|
|k    r	|
|vr||
         }t          |
                   }|t          ||          }t          ||          }|                    t!          |g|R             nt#          |          S )aG  Produce conditional type maps refining expressions by an identity/equality comparison.

        The 'operands' and 'operand_types' lists should be the full list of operands used
        in the overall comparison expression. The 'chain_indices' list is the list of indices
        actually used within this identity comparison chain.

        So if we have the expression:

            a <= b is c is d <= e

        ...then 'operands' and 'operand_types' would be lists of length 5 and 'chain_indices'
        would be the list [1, 2, 3].

        The 'narrowable_operand_indices' parameter is the set of all indices we are allowed
        to refine the types of: that is, all operands that will potentially be a part of
        the output TypeMaps.

        Although this function could theoretically try setting the types of the operands
        in the chains to the meet, doing that causes too many issues in real-world code.
        Instead, we use 'is_valid_target' to identify which of the given chain types
        we could plausibly use as the refined type for the expressions in the chain.

        Similarly, 'coerce_only_in_literal_context' controls whether we should try coercing
        expressions in the chain to a Literal type. Performing this coercion is sometimes
        too aggressive of a narrowing, depending on context.
        Tr  r   r9  r  c                    t          |           } t          |           p t          | t                    o| j        j        S rN  )r   r   r  r   rj  r  r  s    r  should_coerce_innerzNTypeChecker.refine_identity_comparison_expression.<locals>.should_coerce_inner  s;    %c**+C00 sH--B#(2Br
  c              3  :   K   | ]} |                   V  d S rN  r	  )r  r  r  r,  s     r  r  zDTypeChecker.refine_identity_comparison_expression.<locals>.<genexpr>  s4      ]]! 3 3M!4D E E]]]]]]r
  NrG  Fr  r  r   r9  r  )r  r   r   r   r  r  r   is_enum_literalr3  r  r  rj  r_  r  r   conditional_typesr-  r  )rJ  r  r  r%  r&  r  r  should_coercer  possible_target_indicesr  rn  singleton_indexsum_type_nametarget_typer  r  rs  r,  s     `               @r  r  z1TypeChecker.refine_identity_comparison_expression  sY   F ) 	^     ]]]]]}]]]]]M""$ 	. 	.A%a(I 9-i88	"??9#=#=>>   l69==   RxF#**1---- >r6M
 ( 	$ 	$A222"#4 b  5b9O ((fk** 	:""$$	:(26<(F(F	: #O09M >>>? 	R 	RA O## 222A;D)-*:;;I(;I}UU	
 &i==E$$%B4%P%%P%P%PQQQQ&'8999r
  c                  	 g }|D ].}||         }t          |          s|                    |           /t          |          dk    s t          |          t          |          k    ri i fS i }|D ]M}||         	t          	          st          	fd|D                       rt	          	          |||         <   N|i fS )zProduces conditional type maps refining away None in an identity/equality chain.

        For more details about what the different arguments mean, see the
        docstring of 'refine_identity_comparison_expression' up above.
        r   c              3  8   K   | ]}t          |          V  d S rN  )r8   )r  r  rn  s     r  r  z=TypeChecker.refine_away_none_in_comparison.<locals>.<genexpr>  s.      YY.y!<<YYYYYYr
  )r   r  rU  r  r   )
rJ  r  r  r%  r&  non_optional_typesr  r  r  rn  s
            @r  r  z*TypeChecker.refine_away_none_in_comparisonz  s       	/ 	/A"C&s++ /"))#... !""a''3/A+B+Bc-FXFX+X+Xr6M+ 	A 	AA%a(I&y11 YYYYFXYYYYY A&5i&@&@x{#rzr
  c                ^   t          |t                    sdS t          |j        d          sdS t	          |j                  dk    rdS |j        d         }t          |          t          k    rdS |                     |          sdS | 	                    | 
                    |                    S )zIIs this expression a `len(x)` call where x is a tuple or union of tuples?Fzbuiltins.lenr   r   )r  rZ   r   r  rU  r  r5   rQ   r(  r  r)  )rJ  r  s     r  is_len_of_tuplezTypeChecker.is_len_of_tuple  s    $)) 	5!$+~>> 	5ty>>Q5y|4==L((5}}T"" 	5,,T-=-=d-C-CDDDr
  c                    t          |d          rdS t          |          }t          |t                    r5t	          d |j        D                       r|j        j        j        dk    S dS t          |t                    r|j        
                    d          S t          |t                    r t	           fd|j        D                       S dS )zIs this a type that can benefit from length check type restrictions?

        Currently supported types are TupleTypes, Instances of builtins.tuple, and
        unions involving such types.
        r  Fc              3  @   K   | ]}t          |t                    V  d S rN  )r  r   r,  s     r  r  z7TypeChecker.can_be_narrowed_with_len.<locals>.<genexpr>  s,      BB:a,,BBBBBBr
  r  Tc              3  B   K   | ]}                     |          V  d S rN  )r  r  s     r  r  z7TypeChecker.can_be_narrowed_with_len.<locals>.<genexpr>  s1      MMAt44Q77MMMMMMr
  )r   r   r  r   r  r  r  rj  r_  r   r  r   )rJ  r  p_typs   `  r  r  z$TypeChecker.can_be_narrowed_with_len  s     !i00 	5$$ eY'' 	BBekBBBBB P-2;?OOO4eX&& 	9:&&'7888eY'' 	NMMMMMMMMMMur
  c                
   |                      |          sdS |                     |          }t          |          }t          |          }t	          |t
                    sdS t	          |j        t                    sdS |j        S )zIs this expression an int literal, or a reference to an int constant?

        If yes, return the corresponding int value, otherwise return None.
        N)r(  r)  r   r   r  r   r3  r  )rJ  r  rn  rj  s       r  literal_int_exprzTypeChecker.literal_int_expr  s    
 }}T"" 	4$$T**	%i00	%i00+{33 	4++S11 	4  r
  list[tuple[TypeMap, TypeMap]]c                    g }t                      }|                                D ]\  }}}t          |t                    r|j        }t          |t                    r|j        }|dv r+|                    |           |                    |           m|r2|                    dt          |          f           t                      }|dv r|                    |||gf           |r$|                    dt          |          f           g }|D ]L\  }}t           fd|D                       s"t           fd|D                       s>|dv r$t                      }	g }
|D ]{} 	                    |          }||	                    |           / 
                    |          r7t          |t                    sJ |
                    |j        d                    |t          |	          dk    rdi fgc S |	                                }|t          k    r|
D ]V}                                          |          ||          \  }}|dn||i}|dn||i}|                    ||f           Wg|\  }} 
                    |          r||}}t%          j        ||          } 	                    |          }|J |t          k    rt          |t                    sJ                                           |j        d                   ||          \  }}|dn|j        d         |i}|dn|j        d         |i}|                    ||f           N|S )	a  Top-level logic to find type restrictions from a length check on tuples.

        We try to detect `if` checks like the following:
            x: tuple[int, int] | tuple[int, int, int]
            y: tuple[int, int] | tuple[int, int, int]
            if len(x) == len(y) == 2:
                a, b = x  # OK
                c, d = y  # OK

            z: tuple[int, ...]
            if 1 < len(z) < 4:
                x = z  # OK
        and report corresponding type restrictions to the binder.
        )r  r  r  >   >=<rd  r  <=r  c              3  F   K   | ]}                     |          d uV  d S rN  )rA  rp  s     r  r  z7TypeChecker.find_tuple_len_narrowing.<locals>.<genexpr>  s6      MMt,,R00<MMMMMMr
  c              3  B   K   | ]}                     |          V  d S rN  )r;  rp  s     r  r  z7TypeChecker.find_tuple_len_narrowing.<locals>.<genexpr>  s1      @@Bt++B//@@@@@@r
  Nr   r   )r=  r  r  rU   r3  rz  r  r5  r  rA  r;  rZ   r  rU  r=  r   r  r)  r   ri  )rJ  r  chained
last_grouprf  r  r  r3  r  literal_valuestuplesrq  litsizetplr  r  yes_mapno_mapr_sizes   `                   r  r  z$TypeChecker.find_tuple_len_narrowing  s   ( UU
#}} 	8 	8OBe$// "z%00 $\!!t$$$u%%%% 'NND$z*:*:#;<<<!$J???NNBu#6777 	5NND$z"2"23444 	  1	4 1	4IBMMMMuMMMMM @@@@%@@@@@  \!!!$ 2 2B//33C&**3/// ++B// 2)"h77777bgaj111~&&** "2J<'''%))++000! 8 8C(,(<(<T=M=Mc=R=RTVX\(](]%Hg&.&6ddS(OG%,_TT3.F$$gv%67777	8 $e''.. ."'%D!b"--B..u55)))222!$11111$($8$8$$TYq\22B% %!' #+"2$$1x8P!(ty|W6M  '6!23333r
  rN  c                   t          |          }t          |t                    r|                     |||          S t          |t                    r|                     |||          S t          |t                    rg }g }g }|j        D ]u}|                     |          s|	                    |           -| 
                    |||          \  }}	||	                    |           |	|	                    |	           v||z  }||z  }|rt          |          }
nd}
|rt          |          }nd}|
|fS J d            )zIDispatch tuple type narrowing logic depending on the kind of type we got.NFz"Unsupported type for len narrowing)r   r  r   refine_tuple_type_with_lenr   refine_instance_type_with_lenr   r  r  r  r  r   )rJ  r  rf  rN  	yes_typesno_typesother_typesr  ytntr  r  s               r  r  zTypeChecker.narrow_with_len,  st   c""c9%% 	?223DAAAX&& 	?55c2tDDDY'' 	?IHKY ( (44Q77 &&q)))--aT::B>$$R(((>OOB'''$I#H  0;; /99W$$>>>>>r
  c                   t          |j                  }|3t          |         } ||                                |          r|dfS d|fS |j        |         }t	          |t
                    sJ t          |j                  }t	          |t                    r|                                dz
  |j	        z   }|dv r||k    r||fS d|fS |dv r|dk    r|dz  }||k     r{|j        d|         }	|j        |dz   d         }
t          |
                    ||                                z
  dz                       }||
                    |	|gz   |
z             fS d|fS |                     |t          |         |          \  }}||fS t	          |t                    r|j        j        dk    sJ |                                dz
  }|j        d	         }|j        d|         }	|j        |dz   d         }
|dv r/||k    r%|
                    |	|g||z
  z  z   |
z             |fS d|fS |dv r|dk    r|dz  }||k     r|
                    |	|g||z
  z  z   |gz   |
z             }g }t!          ||z
            D ]5}|                    |
                    |	|g|z  z   |
z                        6t%          j        ||j        |j                  |fS d|fS |                     |t          |         |          \  }}||fS )
z-Narrow a TupleType using length restrictions.Nr   r  r  rE  rF  rF  )min_lenr  r  r   )r   r  r   r  r  r   r   rj  r   r^  r  rT  r   r   r_  r  rn  r  r   r  r  r7  )rJ  r  rf  rN  unpack_indexr  unpackr  r^  r  suffixr  r  r  	yes_itemsr  s                   r  rT  z&TypeChecker.refine_tuple_type_with_lenN  s    +3955 &b)FvcjjllD)) !Dy 9<(&*-----"6;//h 011 	) jjllQ&)99G\!!d??8OSy {""::AIDT>> Y}}5F Y|a'7'9'9:F'(>(>tcjjllGZ]^G^(>(_(_``F 1 1&8IF8R 1 S SSSSy $($C$CCQSVZ$[$[!'(( (H--\(-2HL\2\2\2\2\**,,"mA=L=)<!+--.$((v8P/PSY/Y(ZZ\___9;Tzz	~~ ++ C5D7N#;;vhFO ,   	tg~.. [ [A$$S%6%6Vseai=ORX=X%6%Y%YZZZZ +IsxLLgUU9 $ ? ?WR[RV W WHgH$$r
  c                   t          ||                     d                    }|j        d         }t          | j        j        v o|j        j        dk    }|dv rt          |g|z  |          |fS |dv r|dk    r|dz  }|r>t          | 
                    d|g                    }t          |g|z  |gz   |          }n|}|r]g }	t          |          D ]*}
|	                    t          |g|
z  |                     +t          j        |	|j        |j                  }n|}||fS |                     |t$          |         |          \  }}||fS )	z5Narrow a homogeneous tuple using length restrictions.r  r   r\  )r  r  r]  rF  r   r  )r7   lookup_typeinfor  r   r-  enable_incomplete_featurerj  r_  r   r   rk  rn  r  r   r  r  r7  rU  r   )rJ  r  rf  rN  rK  r  allow_preciser`  r  r  r  r  s               r  rU  z)TypeChecker.refine_instance_type_with_len  s    )d.B.BCS.T.TUUil  4<#II4h#33 	 C54<#>>>CC;Tzz	 #D$;$;<Lse$T$TUU'0ut|vh7NY\']']'] t E EALLC519s!C!C!CDDDD(1(<UCHcj(Y(YW$$ $ B B3PRUY Z ZHgH$$r
  )r  rR  ry  rU  rT  rV  rR  ErrorCode | Nonery  c                   d S rN  r	  )
rJ  rU  rW  r  r  rT  rV  r  rR  ry  s
             r  r.  zTypeChecker.check_subtype  s	     	r
  )r  ry  c                   d S rN  r	  )	rJ  rU  rW  r  r  rT  rV  r  ry  s	            r  r.  zTypeChecker.check_subtype  s	     	r
  str | ErrorMessagec               8   t          ||| j                  rdS t          |t                    rt	          ||          }| j                                        r|                     ||           dS |}
t          |          }|}t          |          }| j        	                    ||||||          rdS g }d}|pg }||t          |
|| j                  \  }}||                    |dz   |z              ||                    |dz   |z              t          |	p||||          }t          |t                    r&t          |t                    rt          |||          }|r.|                    dd	                    |          z   d
z             }|                     ||           |D ]$}| j                            |||j                   %|r|                     |||j                   | j                            ||||j                   t          |t                    rW|j        j        rKt          |t,          t          t.          t0          f          r#| j                            ||||j                   t          |t,                    rMt          |t                    r8t5          d||d          }|r#| j                            ||||j                   t          |t,          t8          f          rt          |t                    rk|j        j        r_d|j        j        v rQt5          d||d          }|J t          ||| j                  s#| j                            ||||j                   |                     ||||j                   dS )zBGenerate an error if the subtype is not compatible with supertype.r[  TrQ  F N z (z, )r  r  )r   r-  r  r6  r;   r  prefer_simple_messagesrm  r   &try_report_long_tuple_assignment_errorrA   r  rB   r   r>   with_additional_msgjoinr  rR  maybe_note_concatenate_pos_argsrj  ru  r   r   r   report_protocol_problemsr   	note_callr   protocol_memberscheck_possible_missing_await)rJ  rU  rW  r  r  rT  rV  r  rR  ry  orig_subtypeorig_supertype
extra_infonote_msgsubtype_strsupertype_strr  r  s                     r  r.  zTypeChecker.check_subtype  s    gy$,??? 	4c3 	/s...C8**,, 	IIc7###5!'**"#I..	8::Ym_
 
 	 5 "
$(C)?ndl* * *&K (!!-#"5"CDDD*!!/C"7-"GHHH.('9m H '8,, KIx1P1P K/w	JJ 	N))$:1F1F*F*LMMC		#w 	8 	8DHMM$chM7777 	8IIhchI77700)WSVS[0\\\y(++	Z*	Z 7\8Y$VWW	Z
 H--gy'PSPX-YYYi.. 	J:gx3P3P 	Jz7GNNND J""7D'"IIIgj9:: 	Pz)U]?^?^ 	P~) PjIN<[.[.[":y'tTTT'''!'4FFF PH&&y$ch&OOO))'9gCH)UUUur
  rJ  r.   c                J   t          t          |          t                    rdS 	 | j                            |t                      dd          }n# t          $ r Y dS w xY w|                                rdS t          t          |          t          t          f          rdS |S )zIf type implements Awaitable[X] with non-Any X, return X.

        In all other cases return None. This method must be called in context
        of local_errors.
        Nrm  T)ignore_binder)
r  r   r   r  r  r]   r  rG  r   r   )rJ  r  rJ  aw_types       r  get_precise_awaitable_typez&TypeChecker.get_precise_awaitable_type!  s     oc**K88 	4	'<<WYY$ =  GG  	 	 	44	 &&(( 	4og..+0FGG 	4s   *A 
AAc              #  H   K   d| _         	 d V  d| _         d S # d| _         w xY wr  )rE  rP  s    r  checking_await_setzTypeChecker.checking_await_set7  s>      &*#	0EEE*/D'''%D'////s    	!c                   | j         rdS |                                 5  | j                                        5 }|                     ||          }|	 ddd           ddd           dS |                     |||t          j                  s	 ddd           ddd           dS 	 ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   | j                            ||           dS )z7Check if the given type becomes a subtype when awaited.N)r  )	rE  r  r  r  r  r.  r   INCOMPATIBLE_TYPESpossible_missing_await)rJ  rU  rW  r  rR  rJ  r  s          r  rx  z(TypeChecker.check_possible_missing_await?  s    & 	F$$&& 	 	(>(>(@(@ 	L55g|LLG	 	 	 	 	 	 	 	 	 	 	 	 	 	 %%G1A1T &    	 	 	 	 	 	 	 	 	 	 	 	 	 			 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	''66666sF   C
B3C
*$B3C
'C
3B7	7C
:B7	;C

CCc                j   |                      |          }|j        }t          |t                    r(t          |j        t
                    sJ |j        j        }t          |t                    sJ t          t          j
                  }t          ||gt          |j        j                  z            S )zReturn an instance type with given name and implicit Any type args.

        For example, named_type('builtins.object') produces the 'object' type.
        )lookup_qualifiedr  r  r   r  r   rj  r   r   r   from_omitted_genericsrU  r  r  )rJ  r  rd  r  r  s        r  rl  zTypeChecker.named_typeP  s     ##D))xdI&& 	$dk844444;#D$)))))9:;;xj3ty/B+C+CCDDDr
  r  c                d    |                      |          }d |D             }t          ||          S )zReturn an instance with the given name and type arguments.

        Assume that the number of arguments is correct.  Assume that
        the name refers to a compatible generic type.
        c                ,    g | ]}t          |          S r	  )r(   )r  r  s     r  r  z2TypeChecker.named_generic_type.<locals>.<listcomp>f  s!    GGG31#66GGGr
  )re  r   )rJ  r  r  r  s       r  rk  zTypeChecker.named_generic_type_  s:     ##D))GG$GGGd###r
  r_  c                l    |                      |          }|j        }t          |t                    sJ |S rN  )r  r  r  r   )rJ  r_  rd  r  s       r  re  zTypeChecker.lookup_typeinfoj  s7    ##H--x$)))))r
  c                ,    |                      d          S )zReturn instance type 'type'.r  rl  rP  s    r  r  zTypeChecker.type_typeq  s    ///r
  c                ,    |                      d          S )zReturn instance type 'str'.rZ  r  rP  s    r  r%  zTypeChecker.str_typeu  s    ~...r
  c                &    || j         d         |<   dS )z)Store the type of a node in the type map.rG  N)r  )rJ  r  r  s      r  r2  zTypeChecker.store_typey  s    $'D!!!r
  c                ^    t          fdt          | j                  D                       S )Nc              3      K   | ]}|v V  	d S rN  r	  )r  r  r  s     r  r  z'TypeChecker.has_type.<locals>.<genexpr>~  s'      @@419@@@@@@r
  )r  r  r  r  s    `r  r(  zTypeChecker.has_type}  s0    @@@@ht&?&?@@@@@@r
  c                P    t          | j                  D ]}||v r
||         c S d S rN  )r  r  )rJ  r  r  s      r  r!  zTypeChecker.lookup_type_or_none  s;    $/** 	 	Aqyyw tr
  c                    t          | j                  D ]}|                    |          }||c S t          |          rN  )r  r  ri  r  )rJ  r  r  r  s       r  r)  zTypeChecker.lookup_type  sI    $/** 	 	AdA} tnnr
  rp  dict[Expression, Type]c                F    | j         d                             |           d S r1  )r  r  )rJ  rp  s     r  rH  zTypeChecker.store_types  s#    ""1%%%%%r
   Iterator[dict[Expression, Type]]c              #  |   K   i }| j                             |           |V  | j                                          dS )zStore inferred types into a temporary type map (returned).

        This can be used to perform type checking "experiments" without
        affecting exported types (which are used by mypyc).
        N)r  r  r=  )rJ  rS  s     r  rF  zTypeChecker.local_type_map  sI       13}---r
  c                F    | j         j        p| j         p| j        d          S )zShould we type-check the current function?

        - Yes if --check-untyped-defs is set.
        - Yes outside functions.
        - Yes in annotated functions.
        - No otherwise.
        rG  )r-  check_untyped_defsr&  rP  s    r  r  zTypeChecker.in_checked_function  s-     L+c43E/EcTM_`bMcIc	
r
  c                    || j         v r| j         |         S | j                             dd          }|r4t          |j        t                    sJ |j        j        }||v r||         S t          d|           )z?Look up a definition from the symbol table with the given name.__builtins__NzFailed lookup: )r)  ri  r  r  ru   r<  r  )rJ  r  r  tables       r  lookupzTypeChecker.lookup  s    4<<%%  66A '!!&(333335== ;&3T33444r
  c                   d|vr|                      |          S |                    d          }| j        |d                  }t          dt	          |          dz
            D ]Q}|j                            ||                   }|
J d            t          |j        t                    sJ |j        }R|d         }||j        v r|j        |         S t	          |          dk    ru|d         dv rkd
                    |          }|t          v r(d                    |d         t          |                   }nd	}t          d
                    ||                    d}	t          |	                    ||                    )NrM  r   r   z0Internal error: attempted lookup of unknown namerG  r  )builtinsr  z*, e.g. add '[{} fixtures/{}]' to your testrm  zmCould not find builtin symbol '{}' (If you are running a test case, use a fixture that defines this symbol{})z0Failed qualified lookup: '{}' (fullname = '{}').)r  splitr+  rn  rU  r<  ri  r  r  ru   rs  r<   ro  r  )
rJ  r  partsr  r  rd  lastr_  
suggestionr  s
             r  r  zTypeChecker.lookup_qualified  sw   d??;;t$$$JJsOOEU1X&A1c%jj1n--  gkk%(++(Z!#(H55555H9Dqwwt}$UqU1X1G%G%G88E??666!M!T!Ta"9("C" "JJ "$J--3VD*-E-E   Iszz$55666r
  )r  rr  r  rr  c             #  :  K   | j         r| j         d         j        p|}| j                             t          i ||                     dV  | j        j        r| p| j        j        o| j        o| j        d         }| j                                         \  }}}| j	        s|
                                D ]\  }}| j        j         p|p|o|                     |          }	|	r<t          |j        t                    r"|j        j        |st!                      |_        i|| j        vrB|s@| j                            ||| j        j                   | j                            |           |j        r+t-          |j                  }
|
|j        k    |_        |
|_        dS dS )zEnter a new scope for collecting partial types.

        Also report errors for (some) variables which still have partial
        types, i.e. we couldn't infer a complete type.
        rG  N)r'  r  r  r  r-  allow_untyped_globalsr  r&  r=  r?  r  local_partial_typesis_defined_in_base_classr  rj  r   r   r(  r  r%  r&  rz  r   ra  )rJ  r  rr  r  
permissiver'  r  r  r  
allow_nonefixeds              r  ra  zTypeChecker.enter_partial_types  s      &J4+=b+A+JZ{!!"22{H"M"MNNN
 l8I\ 
L+]0B]tGYZ\G] 	 #04466q!) "	) - 3 3 5 5 !) !)W 88 I"I GT%B%B3%G%G  )"38[99) -& .  (zzCHH$"777
788gt|Gbccc-11#666x ) 238 < <38CH3D0#(E"	) "	)!) !)r
  r   r  c                   |                      |          \  }}}|j        |r|st                      S |S |{| j        st|rW||         }|s| j        j        s@| j                            ||| j        j                   | j	        
                    |           n|                     |j        |           t          |          S )zjHandle a reference to a partial type through a var.

        (Used by checkexpr and checkmember.)
        ) find_partial_types_in_all_scopesrj  r   r?  r-  r  r  r%  r&  r(  rz  r  r  r   )rJ  r  r  r  r  in_scoper  r'  s           r  handle_partial_var_typez#TypeChecker.handle_partial_var_type  s     -1,Q,QRV,W,W)(M8  zz!
(1K( 	J+D1G 8t|'I 888 '4<+F   -11$777 55diIII%c***r
  c                    j         sdS j         j        p,t          fdj         j        dd          D                       S )NFc              3  P   K   | ] }|                     j                  d uV  !d S rN  )ri  r  )r  rK  r  s     r  r  z7TypeChecker.is_defined_in_base_class.<locals>.<genexpr>-  sG       /
 /
/3DHHSXd*/
 /
 /
 /
 /
 /
r
  r   )r  fallback_to_anyr  rI  )rJ  r  s    `r  r  z$TypeChecker.is_defined_in_base_class*  sc    x 	5x' 
3 /
 /
 /
 /
7:x|ABB7G/
 /
 /
 ,
 ,
 	
r
  dict[Var, Context] | Nonec                @    |                      |          \  }}}|r|S dS )a  Look for an active partial type scope containing variable.

        A scope is active if assignments in the current context can refine a partial
        type originally defined in the scope. This is affected by the local_partial_types
        configuration option.
        N)r  )rJ  r  r  r  r'  s        r  r+  zTypeChecker.find_partial_types1  s1     &*%J%J3%O%O"!] 	!  tr
  ,tuple[bool, bool, dict[Var, Context] | None]c                   t          | j                  D ]u}||j        v rj| j        j        }t          |j        t                    r|j        j        	|j        rd}| p|j	        | j        d         j	        k    }||j	        |j        fc S vdS )zLook for partial type scope containing variable.

        Return tuple (is the scope active, is the scope a local scope, scope).
        NTrG  )FFN)
r  r'  r  r-  r  r  rj  r   r  r  )rJ  r  r#  disallow_other_scopesscope_actives        r  r  z,TypeChecker.find_partial_types_in_all_scopes=  s     d011 	? 	?Eei )-(H%ch44 19RWZW_9R -1) .-b4CUVXCYCb1b  $U^UY>>>>   "!r
  r   c                $    t          ||          S )z3Create a temporary node with the given, fixed type.rJ  r  )rJ  r  r  s      r  r  zTypeChecker.temp_nodeV  s    7++++r
  rQ  c                   t          |t                    r)| j                            |j        ||j                   dS | j                            |||           dS )zProduce an error message.rQ  N)r  r;   r  rm  r3  rR  )rJ  r  r  rR  s       r  rm  zTypeChecker.failZ  sY     c<(( 	HMM#)W38M<<<Fc7.....r
  offsetc                   t          |t                    r)| j                            |j        ||j                   dS | j                            ||||           dS )zProduce a note.rQ  N)r  rR  )r  r;   r  r  r3  rR  )rJ  r  r  r  rR  s        r  r  zTypeChecker.notec  s[     c<(( 	HMM#)W38M<<<Fc76=====r
  rq  /Instance | CallableType | TypeType | Overloadedc                   t          |t                    rTt          ||                     d                    }|j        d         }t          t          |          t                    s|S |                     ||          d         S )Nr#  r   r   )r  r   r7   re  r  r   r   r  )rJ  rq  r  r  r  s        r  r  zTypeChecker.iterable_item_typeq  s}     b(## 	!0T5I5IJ[5\5\]]H a(Ioi88'BB ! ! AA"gNNqQQr
  c                H    t          ||                     d                    S )Nr  )r   rl  )rJ  r  s     r  r   zTypeChecker.function_type~  s    T4??3F#G#GHHHr
  c                    || j                                          d S |                                D ] \  }}| j                             ||           !d S rN  )r  r  r  put)rJ  rK  r  rj  s       r  r  zTypeChecker.push_type_map  sb    K##%%%%%&nn.. , ,
dd++++, ,r
  rZ   c                
   |                      |          }|                     |j        d                   }t          |t                    r|j        }t          |          }t          |t                    r^g }t          |j	                  D ]7}t          |t                    r|                    |j                   2i i fc S t          |          }net          |t                    r|j        }nHt          |t                    r/|j                                        r|                     d          }ni i fS |                     |||          \  }}t%          |||          \  }	}
t'          t(          |	|
f          \  }	}
|	|
fS )z=Infer type restrictions for an expression in issubclass call.r   r  )r)  r  r  r  r   r  r   r   r   r  r   r  r  r   rj  r  rl  r  r-  r  convert_to_typetype)rJ  r  r  r  rj  
union_listr  r  r  rP  rQ  s              r  r  z!TypeChecker.infer_issubclass_maps  s   ""4((''	!55g{++ 	*)G!'**gy)) 	J%gm44 " "a** "%%af---- r6MMM
++GG** 	lGG** 	w|/H/H/J/J 	oo&788GG r6M DDWdTXYY'7hPP1GV3DEEr
  type_rangeslist[TypeRange] | Noner  c                    d S rN  r	  rJ  rn  r  r  r  s        r  r  z/TypeChecker.conditional_types_with_intersection  s	     	r
  c                    d S rN  r	  r  s        r  r  z/TypeChecker.conditional_types_with_intersection  s	     	r
  c                6   t          |||          }|d         }|d         }t          t          |          t                    r|||fS t          |          }t          |t                    r"t          |                                          }	n|g}	g }
|D ]M}t          |j                  }t          |t                    r|j	        r||fc S |

                    |           Ng }g }|	D ]R}t          |t                    s||fc S |
D ]2}|                     ||f|          }||
                    |           3S|sH|                                 r$|D ]!\  }}| j                            |||           "t                      |fS t          |          }||fS )Nr   r   )r1  r  r   r   r   r   r  r  r   r  r  rs  re  r  impossible_intersectionr   )rJ  rn  r  r  r  initial_typesr  r  rj  possible_expr_typespossible_target_typesr  r  outr  r  r  intersectionrs  reasonnew_yes_types                        r  r  z/TypeChecker.conditional_types_with_intersection  s    *)['JJ !.a 0,Q//(33_EE 	%I\W$$ &i00k9-- 	0"2;3M3M3O3O"P"P#.- " 	/ 	/B"27++DdH-- )1B )((((!((....(*$ 	) 	)Aa** )((((* ) )#77AGG'

<((((	)
  	04466 I%+ I IME6H44UFCHHHH"$$i//,S11Y&&r
  c                    t          |t                    r|j        r	|j        sdS dS t          |t                    r7|j        r0|j        d         }t          |t                    sJ |j        j        S dS )z!Check if an attribute is writableFTr   )r  r   r  r[  rz   r  r_   r  )rJ  r  
first_items      r  r^  z!TypeChecker.is_writable_attribute  s    dC   	7 (A u4/00 	7T5E 	7AJj)44444>66ur
  c                   t          |t                    rJ|j        dk    r?|                     |j                  }|                     |j                  }||d S ||z   S t          t          |                     |                              }g }|D ]V}t          |t                    rX|
                                rDt          |j        d         j                  }|                    t          |d                     pt          |t                     r*|                    t          |j        d                     t          |t$                    rV|j        j        dk    rFt%          |j        j        d         g           }|                    t          |d                     t          |t,                    r&|                    t          |d                     U d S |sd S |S )Nr\  r   Fr.  Tr  rG  )r  ry   rf  r  r  r  r   flatten_typesr)  r   r  r'   r  r  r  r  r   r  r   rj  r_  rI  r   )	rJ  r  r  r  r  rs  r  erased_typer  s	            r  r  zTypeChecker.get_isinstance_type  s   dF## 	 3++DI66D,,TZ88E|u}t%<$]43C3CD3I3I%J%JKK	!# 	 	C#|,, 1B1B  -SYq\-BCCY{5IIIJJJJC** 
 YsxEEEFFFFC** sx/@O/S/S&sx|B'7<<Y{4HHHIIIIC)) Ys5AAABBBBtt 	 4r
  c                   t          |t                    rt          |j        t                    sdS |                     |j                  }|                     |          }||dS t          |          }t          t          |                    }t          |t                    rt          |t                    sdS |	                                sdS |
                                o!|j        j        |                                k    S )a  Returns true if this expression (with the given type context) is an Enum literal.

        For example, if we had an enum:

            class Foo(Enum):
                A = 1
                B = 2

        ...and if the expression 'Foo' referred to that enum within the current type context,
        then the expression 'Foo.A' would be a literal enum. However, if we did 'a = Foo.A',
        then the variable 'a' would *not* be a literal enum.

        We occasionally special-case expressions like 'Foo.A' and treat them as a single primitive
        unit for the same reasons we sometimes treat 'True', 'False', or 'None' as a single
        primitive unit.
        F)r  rt   r  rv   r!  r   r   r   r   r  r0  r  rj  r  )rJ  r  r#  r  s       r  r  zTypeChecker.is_literal_enum  s   " !Z(( 	
1680L0L 	5..qv66..q11+"55%k22%&7&D&DEE+|44 	J{T_<`<` 	5&&(( 	5 '')) G$)[-D-D-F-FF	
r
  c                    |}t          |          }t          t          j                  }t	          |t
                    r-|                    |          }|j        J d|j        _        |S t	          |t                    r1|j
                            |          }|                    |          S t	          |t                    r1|j                            |          }|                    |          S t	          |t                    rGt	          |j        t
                    r-t          j                             |j                            S t	          |t$                    rB|                                         |j                   fd|j        D                       S t	          |t*                    r8                     |          \  }}t/          | fd|D             z             S |S )z8Inject an extra attribute with Any type using fallbacks.Nrd  c                <    g | ]}                     |          S r	  add_any_attribute_to_type)r  r  r  rJ  s     r  r  z9TypeChecker.add_any_attribute_to_type.<locals>.<listcomp>U  s)    TTTA66q$??TTTr
  )r  rf  c                <    g | ]}                     |          S r	  r  )r  r  r  rJ  s     r  r  z9TypeChecker.add_any_attribute_to_type.<locals>.<listcomp>Z  s)    ___3T;;CFF___r
  )r   r   r   r  r  r   copy_with_extra_attrrm  mod_namer   r  r  r   r  r   r  rt  r  r   r  rf  r   partition_union_by_attrr   )	rJ  r  r  orig_typr  r>  r  	with_attrwithout_attrs	   ` `      r  r  z%TypeChecker.add_any_attribute_to_type=  s   c""9011c8$$ 	--dH==F %111*.F'Mc9%% 	8+@@xPPH$$h$777c<(( 	8|88xHHH$$h$777c8$$ 	\CHh)G)G 	\+D,J,J38UY,Z,Z[[[c;'' 	$$ ::3?DQQTTTTTTTT %    c9%% 	&*&B&B3&M&M#I|(_____R^____   r
  source_typec                V   |                      ||          r||ii fS t          |          }t          |t                    rD|                     ||          \  }}||                     ||          i}||t          |          ifS |                     ||          }||k    r||ii fS i i fS )af  Simple support for hasattr() checks.

        Essentially the logic is following:
            * In the if branch, keep types that already has a valid attribute as is,
              for other inject an attribute with `Any` type.
            * In the else branch, remove types that already have a valid attribute,
              while keeping the rest.
        )has_valid_attributer   r  r   r  r  r   )rJ  r  r  r  r  r  rP  type_with_attrs           r  r  zTypeChecker.hasattr_type_maps^  s     ##K66 	++&**%k22k9-- 	H"::;MMOA|T;;KNNOGT#8#F#FGGG77TJJ[((.)2--2vr
  c                    g }g }|j         D ]C}|                     ||          r|                    |           .|                    |           D||fS rN  )r  r  r  )rJ  r  r  r  r  r  s         r  r  z#TypeChecker.partition_union_by_attrw  sq     	% 	* 	*D''d33 *  &&&&##D)))),&&r
  c                   t          |          }t          |t                    rdS t          |t                    r |j        r|j        j        r|j        j        }nd }| j        	                                5 }t          ||t          t          t          j                            ddd| j        || d|           d d d            n# 1 swxY w Y   |                                 S )NFT)r  r  no_deferralmodule_symbol_table)r   r  r   r   rm  r  rj  r<  r  r  r"   r   r   r   rG  )rJ  r  r  r?  r  watchers         r  r  zTypeChecker.has_valid_attribute  s-   $$eW%% 	5eX&& 	'5+< 	'ARA[ 	'"'*"2"&X##%% 	!!78899! $7   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ))++++s   7ACCCrO  c                :    | j                             ||          S )Nr;  )r  rh  )rJ  r  rO  s      r  get_expression_typezTypeChecker.get_expression_type  s     ''<'HHHr
  )r  r-   r+  r*  r-  r   r4  ru   r5  r6  r3  r   r7  r8  r9  r:  )r9  rL  r9  r:  rN  )rw  rx  r9  r  )r  r  r9  r:  )r  ru   r9  r:  )r  r   r  r  r9  r:  )r  r6  r  r]   r9  r:  )r  r   r9  r:  )r  r   r  r  r  r  r9  r:  )r  rz   r9  r:  )r  r  r  r]   r9  r  )r  r   r  r  r9  r  r/  )r
  r   r  r  r9  r   )r
  r   r9  r   )r  rg   r9  r:  )NNF)
r  rh   r4  r  r  r5  r  r  r9  r:  )r9  rB  F)
r  rh   r  r   r  r5  r  r  r9  r:  )r  r   r  r  r9  r  )r  r   r9  r:  )r  rh   r  r  r9  r:  )r  r6  r9  r  )r  rh   r9  r:  )r  rg   r  r   r9  r:  )
r  rh   r  r   r  r6  r  r]   r9  r:  )r  r   r  r6  r  r   r  r   r  r6  r  r   r  r]   r9  r:  )r  r   r  r   r  r   r9  r  )r  rf   r9  r:  )r  r   r  r]   r  r6  r9  r:  )r  r   r  r]   r9  r:  )r  r   r  r   r  r]   r9  r:  )r  rh   r  r   r9  r.  )r  rA  r  rB  r  rC  r9  r:  )r  rG  r9  rB  )r  rG  rK  r   r9  rL  )r  rG  r  r6  rK  r   r9  r  )
rd  r   r  r   re  r   rf  r   r9  r   )rl  r   r9  r  )rW  r   rV  r   r  r6  ru  r6  rW  r6  rc  r  r`  r  r  r]   r9  r:  )r  rh   r9  r:  )r  r[   r9  r:  )r  r   r9  r:  )r  r[   rK  r   r9  r:  )rd  r   r9  r  )rd  r   r9  r  )
r  r6  r  r   r  r   r  r   r9  r:  )r  rm   r9  r:  )r  rk   r9  r:  )r  rj   r9  r:  )r  rl   r9  r:  )r  rW   r9  r:  )r9  r  )r  r   r9  r  r  rV   r9  r:  )TF)
r  rr   r  rb   r4  r  r&  r  r9  r:  )rP  r   r9  r  )r  rr   r  rb   rf  r6  r9  r:  )r  r~   r  r  r  rb   r9  r  )r  r~   r  r  r  rb   rK  r   rc  r   rd  rw   r9  r  )r}  r   rK  r   r9  r~  )r  r   rK  r   rd  r  r9  r  )r  r6  rd  r  r  r]   r9  r:  )r$  r  r9  rB  )r  r  r9  r:  )r  rr   r9  r:  )r  rr   r  r  r9  r  )r  r  r9  r  )T)
r  r  r  rb   r  r]   r4  r  r9  r:  )
r  r  r  r  r  r]   r  r  r9  r  )TNF)r  r  r  rb   r  r]   r4  r  r  r  r  r  r9  r:  )r  r  r  rb   rR  r   r  r]   r4  r  r9  r:  )r  r  r9  r  )r  r  r  rb   rR  r   r  r]   r  r  r4  r  r9  r:  )r  r  rR  r   r9  r   )r  r  r  r  r  r  r9  r  )rj  r   r9  r  )
r  r  rR  r   r  r]   r4  r  r9  r:  )r  rr   r9  r  )r  rr   r9  r  )
r  r   r  rr   r"  r   r  r]   r9  r:  )r  r   r  rr   r"  r   r9  r  )r  r   r9  r  )r  r   r  rr   rj  r   r9  r:  rj  r   r9  r   )r  rb   r9  r  )r  r  r  rb   r  r]   r  r;   r  r6  r  r6  r  rD  r9  r   )
rT  r   rM  r   r  rb   r  r]   r9  rN  )r  rn   r  rb   r  r]   r9  r:  )r  rn   r  rb   r9  r:  )r  r   r9  rg  )r  rc   r9  r:  )r  r   r9  r:  )r  ri   r9  r:  )r  r   r9  r:  )r  rx   r9  r:  )r  rT   r9  r:  )r  r}   r9  r:  )r  rb   r  r}   r  r  r9  r:  )r  r   r9  r:  )r  r   r  r  r9  r:  )r  rb   r  r  r9  r   )r  r  r9  r   )rs  r  r9  r   )r  r   r  rb   r9  r$  r  re   r9  r:  )r  rb   r9  r  )rj  r   r  r]   r9  r  )r  rb   r9  r  )r  r   r9  r  )
r  rb   r  r   r4  r  r  r]   r9  r:  )r  r`   r9  r:  )r  r_   r9  r:  )r  r_   r  r  r9  r:  )r  rg   r  r   r  rb   r9  r:  r  r   r9  r:  )r  r   r  rg   r9  r:  )r  rb   r  r  r4  r  r9  r   )r  rX   r9  r:  )r  r^   r9  r:  )r  rs   r9  r:  )r3  r=  r9  r>  )rK  r  r4  r>  r9  r:  )
rG  r6  rH  r6  rI  r6  r  rJ  r9  rK  )rQ  rR  r  rS  r9  rT  )r  r   rt  r   r9  r   )r  r   r9  r   )r  r   r  r  r9  r  )r  rb   r  r  r9  r  )r  r   r  r   r9  r  )r  r   r  rb   r9  r:  )r  r\   r  r  r9  r  )r  rb   r9  r  )r  r  r9  r  )r  rb   rn  r   r9  r  )r  r  r  r$  r%  r  r&  r'  r  r(  r  r  r9  r  )
r  r  r  r$  r%  r  r&  r'  r9  r  )r  rb   r9  r  )r  rb   r9  r  )r  r\   r9  rB  )r  r   rf  r6  rN  r  r9  r  )r  r   rf  r6  rN  r  r9  r  )r  r   rf  r6  rN  r  r9  r  r  )rU  r   rW  r   r  r]   r  r6  rT  r5  rV  r5  r  rD  rR  rh  ry  rC  r9  r  )rU  r   rW  r   r  r]   r  r;   rT  r5  rV  r5  r  rD  ry  rC  r9  r  )rU  r   rW  r   r  r]   r  rk  rT  r5  rV  r5  r  rD  rR  rh  ry  rC  r9  r  )r  r   rJ  r.   r9  r  )
rU  r   rW  r   r  r]   rR  rh  r9  r:  )r  r6  r9  r   )r  r6  r  r$  r9  r   )r_  r6  r9  r   )r9  r   )r  rb   r  r   r9  r:  )r  rb   r9  r  )r  rb   r9  r  )r  rb   r9  r   )rp  r  r9  r:  )r9  r  )r  r6  r9  r   )r  r  rr  r  r9  rB  )
r  r   r  r  r  r   r  r]   r9  r   )r  r   r9  r  )r  r   r9  r  )r  r   r9  r  )r  r   r  rC  r9  r   )r  rk  r  r]   rR  rh  r9  r:  r  )
r  rk  r  r]   r  r  rR  rh  r9  r:  )rq  r  r  r]   r9  r   )r  rf   r9  r   )rK  r  r9  r:  )r  rZ   r  rb   r9  r  )
rn  r   r  r  r  r]   r  r:  r9  r  )
rn  r   r  r  r  r]   r  r   r9  r  )
rn  r   r  r  r  r]   r  r  r9  r  )r  rw   r9  r  )r  rb   r9  r  r  rb   r9  r  )r  r   r  r6  r9  r   )r  rb   r  r   r  r6  r9  r  )r  r   r  r6  r9  r  )r  r   r  r6  r9  r  )r  rb   rO  r  r9  r   )r  r  r  __doc__r@  r  r>  r   r  r?  rB  r.  r/  rK  propertyrO  rT  rv  r  r}  r  r  r  rh  r  r  r  r  r  r  r	  r  r  r  r   r#  r"  r  r   r:  r;  r~  re  r|  r  r_  r\  r[  r`  r  r
  r  ra  rb  r'  r-  rX  r  r  rJ  rQ  rY  ro  r]  r>  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  re  rf  r/  r'  r%  r]  rL  r=  r?  rm  r`  rj  rk  rP  r  r#  r*  rN  r  r  r<  r  r  r  r  r  r  r  r  r  r  r  rM  rC  r.  rB  r'  r;  rA  r   ru  r  rG  rK  r^  rk  rq  ru  rs  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/  rP  rs  rz  r~  r  r  r  r  r  r  r  r  r  r  r  r  r;  r  rA  r  r  rT  rU  r   r.  r  r  rx  rl  rk  re  r  r%  r2  r(  r!  r)  rH  rF  r  r  r  ra  r  r  r+  r  r  rm  r  r  r   r  r  r  r^  r  r  r  r  r  r  r  r	  r
  r  r  r     sL          GNNN -,,, "!!!2222####MMM))))      '&&&H!I " 8<;;;;"""""  )((( NNN>T >T >T >T@ . . . X.( ( ( (** * * *Z OS$ $ $ $ $L" " " ". . . .H H H H: : : : _ _ _ _ '+'
 -1' ' ' ' ' ':2 2 2 21 1 1 1f   6hR hR hR hRj^ ^ ^ ^&$ $ $ $ 3  3  3  3D! ! ! !F# # # #3 3 3 3<' ' ' '6 6 6 6v .2! J  J  J  J  JD 2 2 2 ^2 X]`% `% `% `% `%D	   "   (! ! ! !F; ; ; ;> > > >)M )M )M )MV! ! ! !FV
 V
 V
 V
p>N >N >N >N@.S .S .S .S`M M M M4N N N N:W W W W"
 
 
 
    ! ! ! !8 #'	     ) ) ) )*&! &! &! &!Pz z z zx+Z +Z +Z +ZZI I I IJ J J JX3 3 3 32E" E" E" E"NS S S S. . . .`" " " "&   ; ; ; ;0   8   8 V  V  V  VDI I I I*   .VR VR VR VRp
 
 
 
:                        ,   "
 
 
 
   82A 2A 2A 2Ah3 3 3 3 #' i3 i3 i3 i3 i3X *@AV(WWWWW   .,' ,' ,' ,'\4 4 4 4lM M M M^+ + + +Z      := = = =(" " " " ) ) ) ^)1Q 1Q 1Q 1Qf
 
 
 
>   &   H #'RU RU RU RU RUr %)" " " " "R #'#!&4 4 4 4 4lB& B& B& B&H	 	 	 	$ #'W W W W Wr$M $M $M $ML! ! ! ! 
 
 
 
 #'= = = = =2%3 %3 %3 %3N    !< !< !< !<F& & & &P   2* * * *$6 6 6 6( ( ( (   , -M%'< #'< < < < < <|vC vC vC vCp& & & &4%/ %/ %/ %/N   "
0 
0 
0 
0" " " "
^I ^I ^I ^I@- - - -2F F F F   0
% 
% 
% 
%" " " "9 9 9 9 9*, , , ,B') ') ') ')R'0 '0 '0 '0R   4 4 4 4   "	. 	. 	. 	.	# 	# 	# 	#" " " "& & & &&   6   ,\ \ \ \   $& & & &4D 4D 4D 4D 4DlK K K K[ [ [ [#  #  #  # J; ; ; ;
 
 
 
&   # # # #   E E E EN$ $ $ $L+ + + +   ,ad ad ad adF? ? ? ?8D D D DT  T  T  T l   8%
 %
 %
 %
N
 
 
 
 +Z +Z +Z +ZZL  L  L  L \( ( ( (&B  B  B  B H
) ) ) )VvV vV vV vVpH: H: H: H:T   @E E E E   ,! ! ! ! [ [ [ [z ?  ?  ?  ?DB% B% B% B%H% % % %H  %)&* #'!%(,     X  %)&* #'(,     X( %)&*J #'!%(,J J J J J JX   , 0 0 0 ^07 7 7 7"E E E E	$ 	$ 	$ 	$   0 0 0 0/ / / /( ( ( (A A A A      & & & & 	 	 	 ^	

 

 

 

5 5 5 57 7 7 7> %*U6) 6) 6) 6) 6) ^6)p+ + + +:
 
 
 

 
 
 
" " " "2, , , , ,
 VZ/ / / / / / 	> "&> > > > > >R R R RI I I I, , , ,   <      X    X  $2' 2' 2' 2' 2'h
 
 
 
   B$
 $
 $
 $
L   B   2
' 
' 
' 
', , , ,4I I I I I I Ir
  r  c                  "    e Zd ZdZd	dZd
dZdS )r  z0Collects the non-nested argument types in a set.r9  r:  c                ,    t                      | _        d S rN  )r=  r  rP  s    r  rK  zCollectArgTypeVarTypes.__init__  s    +.55r
  r  r   c                :    | j                             |           d S rN  )r  rz  r  s     r  visit_type_varz%CollectArgTypeVarTypes.visit_type_var  s    1r
  Nr  )r  r   r9  r:  )r  r  r  r  rK  r  r	  r
  r  r  r    sB        ::1 1 1 1     r
  r  r  r   proposed_type_rangesr  r  r:  r9  r  c                    d S rN  r	  r  r  r  s      r  r1  r1    	     Cr
  r  c                    d S rN  r	  r  s      r  r1  r1    r   r
  r  c                   |rWt          |          dk    r|d         j        }t          |          }t          |t                    rO|                                st          |j        t                    r!|j        j	        j
        }t          | |          } d |D             }t          |          }t          |t                    r||fS t          d |D                       s"t          | |d          r|t!                      fS t#          | |dd          st!                      |fS t%          j        d |D                       }t)          | |          }||fS | |fS )	a  Takes in the current type and a proposed type of an expression.

    Returns a 2-tuple: The first element is the proposed type, if the expression
    can be the proposed type. The second element is the type it would hold
    if it was not the proposed type, if any. UninhabitedType means unreachable.
    None means no new information can be inferred. If default is set it is returned
    instead.r   r   c                    g | ]	}|j         
S r	  r  r  
type_ranges     r  r  z%conditional_types.<locals>.<listcomp>  s    QQQj*/QQQr
  c              3  $   K   | ]}|j         V  d S rN  r.  r  s     r  r  z$conditional_types.<locals>.<genexpr>  s6       
 
*4J%
 
 
 
 
 
r
  TrV  )prohibit_none_typevar_overlaprW  c                *    g | ]}|j         	|j        S r	  )r  r  r  s     r  r  z%conditional_types.<locals>.<listcomp>  s3       "%4O  r
  )rU  r  r   r  r   r0  r3  r  r  rj  r_  r   r   r   r  r   r   r9   r   r  r   )	r  r  r  r  	enum_nameproposed_itemsproposed_typeproposed_precise_typeremaining_types	            r  r1  r1    s     '%#$$)))!,1F$V,,F&+.. X&&((X,6v|T,J,JX #O09	>|YWWQQ<PQQQ-n==mW-- 	1 !')) 
 
8L
 
 
 
 
 	1mtTTT	1 O----%-t_c
 
 
 	1 #$$g-- %.$8 &:  % %! 3<AVWWN .00 W$$r
  r  rb   r  r  r  c                t   t          |           } g }||fD ]o}t          |          }t          |t                    r|                    d            <||                    i            T|J |                    | |i           pt          t          t          t          f         t          |                    S rN  )	r  r   r  r   r  r   r   r  r  )r  r  r  mapsr  rj  s         r  r-  r-    s     4  DD'" % %%c**k?33 	%KK KKOOOO???KKs$$$$gw&'t555r
  rK  r6  r  r   c                    | |vr| S d}| t          |          z   |v r|dz  }| t          |          z   |v | t          |          z   S )zKGenerate a name that does not appear in table by appending numbers to base.r   )r6  )rK  r  r  s      r  re  re    s\    5	A
Q-5
 
 	Q Q-5
 
 #a&&=r
  r  r  c                b    t          | d          pt          | t                    o
| j        dk    S )z>Returns true if this expression is the 'True' literal/keyword.zbuiltins.Truer   r   r  ro   r3  r  s    r  r  r  	  s0    a11\Z75K5K5\PQPW[\P\\r
  c                b    t          | d          pt          | t                    o
| j        dk    S )z?Returns true if this expression is the 'False' literal/keyword.r  r   r  r  s    r  r   r     s1    a!122]jG6L6L6]QRQX\]Q]]r
  c                B    t          | t                    o
| j        dk    S )z>Returns true if this expression is the 'None' literal/keyword.r  r  rv   r_  r  s    r  rD  rD    s    a""Dqz_'DDr
  c                B    t          | t                    o
| j        dk    S )Nzbuiltins.NotImplementedr  r  s    r  r~  r~    s    a""Nqz5N'NNr
  r  rh   c                `   | j         j         }t          |          dk    ot          |d         x}t                    or|j        du pt          |j                  oUt          |d         x}t                    o8t          |j        x}t                    o|j        du pt          |j                  S )z
    Checks whether a function's body is 'return; yield' (the yield being added only
    to promote the function into a generator function).
    r  r   Nr   )r  rU  r  r   r  rD  rc   r   )r  r  ret_stmt	expr_stmt
yield_exprs        r  r  r    s    
 9>DD		Q 	J47*xJ77	J]d"Dohm&D&D	J DG+y^<<	J Y^3zY??		J
 _$H
(H(Hr
  rl  c                   t          |           } t          | t                    rS| j        j        dv rC| j        sdS t          t          | j        d                   t                    s| j        d         S nft          | t                    rg }| j        D ]}t          |t                    rt          |j                  }t          |t                    rt          |j                  }t          |t                    r|j        j        dk    sJ |                    |j        d                    |                    |           t          d t          |          D                       rt          |          S n`t          | t                     rK| j        j        j        D ]/}|j        dk    r"t'          | j        |          j        d         c S 0J d            dS )	a  Get the item type of a builtin container.

    If 'tp' is not one of the built containers (these includes NamedTuple and TypedDict)
    or if the container is not parameterized (like List or List[Any])
    return None. This function is used to narrow optional types in situations like this:

        x: Optional[int]
        if x in (1, 2, 3):
            x + 42  # OK

    Note: this is only OK for built-in containers, where we know the behavior
    of __contains__.
    )rY  r  r)  r[  zbuiltins.frozensetz_collections_abc.dict_keysztyping.KeysViewNr   r  c              3  B   K   | ]}t          |t                     V  d S rN  )r  r   rs  s     r  r  z$builtin_item_type.<locals>.<genexpr>Y  s/      XXr:b'***XXXXXXr
  ztyping.MappingFz2No Mapping base class found for TypedDict fallback)r   r  r   rj  r_  r  r   r   r  r   r   r  r  r  r   r   r   r  rI  r7   )rl  normalized_itemsrq  r  rK  s        r  r  r  ,  s    
		B"h %K7  
 
 
 7 tobgaj997CC "wqz!	B		"	" K( 	, 	,B"j)) 	,*2733h(899 E.x/CDDHx227?}7MQa7a7a7a7a ''a(89999 ''++++XX5EFV5W5WXXXXX 	;()9:::	;	B	&	& K K$( 	L 	LD} 0000dCCHKKKK 1JJJJJ4r
  Fm1m2r  c                z   | |dS |                                 }d |D             }| D ]F}t          |          |vs(t          t          | |                   t                    r| |         ||<   G|rI| D ]F}|D ]A}t          |          t          |          k    rt          | |         ||                   ||<   BG|S )zCalculate what information we can learn from the truth of (e1 and e2)
    in terms of the information that we can learn from the truth of e1 and
    the truth of e2.
    Nc                ,    h | ]}t          |          S r	  )r6   )r  n2s     r  r8  z'and_conditional_maps.<locals>.<setcomp>t  s     ---B|B---r
  )copyr6   r  r   r   r:   )r   r!  r  r>  m2_keysn1r$  s          r  r  r  e  s     
zRZt WWYYF--"---G    7**jB9P9PRY.Z.Z*BF2J <  	< 	<B < <##|B'7'777!+BrFBrF!;!;F2J< Mr
  r  c                   | |S || S i }| D ]}}|D ]x}t          |          t          |          k    rV|r4t          t          | |                   t                    r| |         ||<   Xt	          | |         ||         g          ||<   y~|S )zCalculate what information we can learn from the truth of (e1 or e2)
    in terms of the information that we can learn from the truth of e1 and
    the truth of e2. If coalesce_any is True, consider Any a supertype when
    joining restrictions.
    )r6   r  r   r   r   )r   r!  r  r>  r'  r$  s         r  r0  r0    s     
z		z	
 &(F I I 	I 	IBB<#3#333 IJr"v/F/F$P$P I!#BF2JJ!6227G!H!HF2J	I Mr
  r3  rB  c                    t          |           dk    ri i fS t          |           dk    r| d         S | d         \  }}| dd         D ]'\  }}t          |||          }t          ||          }(||fS )a  Reduces a list containing pairs of if/else TypeMaps into a single pair.

    We "and" together all of the if TypeMaps and "or" together the else TypeMaps. So
    for example, if we had the input:

        [
            ({x: TypeIfX, shared: TypeIfShared1}, {x: TypeElseX, shared: TypeElseShared1}),
            ({y: TypeIfY, shared: TypeIfShared2}, {y: TypeElseY, shared: TypeElseShared2}),
        ]

    ...we'd return the output:

        (
            {x: TypeIfX,   y: TypeIfY,   shared: PseudoIntersection[TypeIfShared1, TypeIfShared2]},
            {shared: Union[TypeElseShared1, TypeElseShared2]},
        )

    ...where "PseudoIntersection[X, Y] == Y" because mypy actually doesn't understand intersections
    yet, so we settle for just arbitrarily picking the right expr's type.

    We only retain the shared expression in the 'else' case because we don't actually know
    whether x was refined or y was refined -- only just that one of the two was refined.
    r   r   Nr  )rU  r  r0  )r3  r  final_if_mapfinal_else_mapr  r  s         r  r  r    s    4 9~~2v	Y1		|'0|$n )!"" 	K 	KFH/fxXXXL0JJNN^++r
  rK  c                
   i }| d S |                                  D ]g\  }}|}t          |t                    r|j        }t          t	          |          t
          t          f          si c S t          j        |          ||<   h|S rN  )	r  r  r   r  r   r   r   r   rt  )rK  converted_type_mapr  r  r  s        r  r  r    s    13t^^%% A A	ca%% 	A/!,,y(.CDD 	III#+#;C#@#@4  r
  r  r  c                    t          | t          t          f          rd | j        D             S t          | t                    rt          | j                  S | gS )zAFlatten a nested sequence of tuples/lists into one list of nodes.c                6    g | ]}t          |          D ]}|S r	  )r  r  ar  s      r  r  zflatten.<locals>.<listcomp>  s+    777aGAJJ77q7777r
  )r  r   rq   r  r   r  r  r  s    r  r  r    sV    !i*++ 77177777	Ax	 	  qvs
r
  r$  c                    t          |           } t          | t                    rd | j        D             S t	          | d          r| j        d         gS | gS )z;Flatten a nested sequence of tuples into one list of nodes.c                6    g | ]}t          |          D ]}|S r	  )r  r0  s      r  r  z!flatten_types.<locals>.<listcomp>  s-    ===aM!,<,<==q====r
  r  r   )r   r  r   r  r   r  r  s    r  r  r    s_    A!Y ==17====	1.	/	/ q	{s
r
  r  r  dict[TypeVarId, Type]c                |    t          |          }|                    |           }t          |t                    sJ |S rN  )TypeTransformVisitorr  r  rh   )r  r  visitorr  s       r  r;  r;    s;    "3''G
,,t

Cc8$$$$$Jr
  c                  (     e Zd Zd	 fdZd
dZ xZS )r6  r  r4  r9  r:  c                V    t                                                       || _        d S rN  )superrK  r  )rJ  r  	__class__s     r  rK  zTypeTransformVisitor.__init__  s$    r
  rj  r   c                ,    t          || j                  S rN  )r1   r  r  s     r  rj  zTypeTransformVisitor.type  s    4***r
  )r  r4  r9  r:  r  )r  r  r  rK  rj  __classcell__r;  s   @r  r6  r6    sQ             + + + + + + + +r
  r6  r   r  c                    t          | j        |j                  }t          |                                 |                                          }||k    S )zOCan a single call match both t and s, based just on positional argument counts?)maxr  minmax_possible_positional_args)r  r  r  max_argss       r  r  r    sG    1:qz**H11133Q5S5S5U5UVVHxr
  rQ  re  class_type_varslist[TypeVarLikeType]c                    t          | |          } t          ||          }t          | |t          dd dddd	  	        pt          || t          dd dddd	  	        S )a  Check if two overloaded signatures are unsafely overlapping or partially overlapping.

    We consider two functions 's' and 't' to be unsafely overlapping if both
    of the following are true:

    1.  s's parameters are all more precise or partially overlapping with t's
    2.  s's return type is NOT a subtype of t's.

    Assumes that 'signature' appears earlier in the list of overload
    alternatives then 'other' and that their argument counts are overlapping.
    Fc                $    t          | |           S rN  is_subtype_no_promotelrs     r  <lambda>z;is_unsafe_overlapping_overload_signatures.<locals>.<lambda>.      *?1*E*E&E r
  T)r  r   is_compat_returnr  check_args_covariantlyallow_partial_overlapno_unify_nonec                $    t          ||            S rN  rH  rJ  s     r  rM  z;is_unsafe_overlapping_overload_signatures.<locals>.<lambda>8  rN  r
  )detach_callabler   6is_overlapping_types_no_promote_no_uninhabited_no_none)rQ  re  rD  s      r  r  r     s    (  	?;;IE?33E( "HEE#"
 
 
  
 HEE$"

 

 

r
  r  c                    |s| S t                      | j        | j        gz   D ]!}t          t          |                    z  "|                     t          | j                  fd|D             z             S )at  Ensures that the callable's type variables are 'detached' and independent of the context.

    A callable normally keeps track of the type variables it uses within its 'variables' field.
    However, if the callable is from a method and that method is using a class type variable,
    the callable will not keep track of that type variable since it belongs to the class.

    This function will traverse the callable and find all used type vars and add them to the
    variables field if it isn't already present.

    The caller can then unify on all type variables whether the callable is originally from
    the class or not.c                    g | ]}|v |	S r	  r	  )r  tvseen_type_varss     r  r  z#detach_callable.<locals>.<listcomp>S  s#    (^(^(^~I]I]I]I]I]r
  r@  )r=  r  r  r   r  r5  r  )r  rD  r  rY  s      @r  rT  rT  @  s      
UUN]cl^+ 0 0#mA..///s}%%(^(^(^(^o(^(^(^^    r
  c                r    t          | d | j        D                       }t          ||t          dd          S )zCheck if the 'other' method can never be matched due to 'signature'.

    This can happen if signature's parameters are all strictly broader then
    other's parameters.

    Assumes that both signatures have overlapping argument counts.
    c                8    i | ]}|j         t          |          S r	  )r2  r   )r  r4  s     r  
<dictcomp>z,overload_can_never_match.<locals>.<dictcomp>g  s%    ___4DG8>>___r
  Tr  )r1   r  r   r   )rQ  re  exp_signatures      r  r  r  W  sO      __9K^___ M "u4_c   r
  r   c                   t          | t                    r/t          |t                    rt          | |t          dd          S nt          | t                    rpt          |t                    r[t          | j                  t          |j                  k    r1t          d t          | j        |j                  D                       S dS )z#Does t have wider arguments than s?Tr  c              3  <   K   | ]\  }}t          ||          V  d S rN  )is_same_arg_prefix)r  r  itemts      r  r  z-is_more_general_arg_prefix.<locals>.<genexpr>z  sB        9E&ue44     r
  F)	r  r   r   r   r   rU  r  r  r  r  r  s     r  r  r  n  s     !\"" 
a&& 	)1 1TY]   	 
A|	$	$ a&& 	17||s17||++  ILQWVWV]I^I^      5r
  c           	     8    t          | |t          dddd          S )NT)r  r   r  rP  rx  )r   r   rb  s     r  r`  r`    s.    !		#!   r
  r  tuple[bool, str]c                    t          |           } t          j        |         }d}t          | t                    rt          | ||          }n+t          | t                    rt          | j        ||          }|d|fS d|fS )zDetermine if operator assignment on given value type is in-place, and the method name.

    For example, if operator is '+', return (True, '__iadd__') or (False, '__add__')
    depending on which method is supported by the type.
    NTF)r   r   
op_methodsr  r   _find_inplace_methodr   r  )r  r  r  existing_methods       r  r  r    s     #

C!(+FO#x   O.sFHEE	C	'	' O.s|VXNN"_$$&=r
  r  r   r  r5  c                t    |t           j        v r)d|dd          z   }| j                            |          r|S d S )N__ir  )r   ops_with_inplace_methodrj  r  )r  r  r  inplace_methods       r  rg  rg    sE    9444+9((88 	"!!4r
  r:  c                    t          |           }t          |t                    r|S t          |t                    rdS |                     t                                 S )a  Is an inferred type valid and needs no further refinement?

    Examples of invalid types include the None type (when we are not assigning
    None to a final lvalue) or List[<uninhabited>].

    When not doing strict Optional checking, all types containing None are
    invalid.  When doing strict Optional checking, only None and types that are
    incompletely defined (i.e. contain UninhabitedType) are invalid.
    F)r   r  r   r   rh  InvalidInferredTypes)r  r:  rj  s      r  rA  rA    s^     "#&&K+x(( 
 	K	1	1 uzz.001111r
  c                  <     e Zd ZdZd fdZddZdd
ZddZ xZS )rn  zFind type components that are not valid for an inferred type.

    These include <Erased> type, and any uninhabited types resulting from failed
    (ambiguous) type inference.
    r9  r:  c                T    t                                          t                     d S rN  )r:  rK  r   )rJ  r;  s    r  rK  zInvalidInferredTypes.__init__  s!    &&&&&r
  r  r   r  c                    |j         S rN  )r`  r  s     r  visit_uninhabited_typez+InvalidInferredTypes.visit_uninhabited_type  s
    {r
  r   c                    dS r  r	  r  s     r  visit_erased_typez&InvalidInferredTypes.visit_erased_type  s    tr
  r   c                4    |j                                         S rN  )r2  is_meta_varr  s     r  r  z#InvalidInferredTypes.visit_type_var  s     t!!!r
  r  )r  r   r9  r  )r  r   r9  r  )r  r   r9  r  )	r  r  r  r  rK  rr  rt  r  r=  r>  s   @r  rn  rn    s         ' ' ' ' ' '      " " " " " " " "r
  rn  c                  "    e Zd ZdZd
dZddZd	S )r=  zRReplace all ambiguous Uninhabited types with Any (to avoid spurious extra errors).r  r   r9  r   c                F    |j         rt          t          j                  S |S rN  )r`  r   r   r  r  s     r  rr  z&SetNothingToAny.visit_uninhabited_type  s"    ; 	19/000r
  r   c                R     |                      fd|j        D                       S )Nc                :    g | ]}|                               S r	  )rh  )r  r1  rJ  s     r  r  z9SetNothingToAny.visit_type_alias_type.<locals>.<listcomp>  s#    $D$D$DQXXd^^$D$D$Dr
  )r  )r  r  r  s   ` r  visit_type_alias_typez%SetNothingToAny.visit_type_alias_type  s0     $D$D$D$DQV$D$D$DEEEr
  N)r  r   r9  r   )r  r   r9  r   )r  r  r  r  rr  r{  r	  r
  r  r=  r=    sH        \\   
F F F F F Fr
  r=  r  r  rL  c                v    t          | t                    r| j        S t          | t                    r| j        S dS )z6Find out if a node describes a static function method.N)r  rg   ro  r   rt  )r  s    r  rs  rs    s>     $   ~$ $##4r
  c                  z    e Zd ZU ded<   ddZdd	Zdd
ZddZddZddZ	e
dd            Ze
dd            ZdS )r"  z$list[TypeInfo | FuncItem | MypyFile]rV  moduleru   r9  r:  c                    |g| _         d S rN  )rV  )rJ  r~  s     r  rK  zCheckerScope.__init__  s    X


r
  FuncItem | Nonec                f    t          | j                  D ]}t          |t                    r|c S d S rN  )r  rV  r  rh   rJ  r  s     r  rz  zCheckerScope.top_function  s@    $*%% 	 	A!X&& tr
  c                    t          | j                  D ]0}t          |t                    rt          |t                    s|c S 1d S rN  )r  rV  r  rh   rp   r  s     r  r  z$CheckerScope.top_non_lambda_function  sM    $*%% 	 	A!X&& z!Z/H/H tr
  r  c                `    t          | j        d         t                    r| j        d         S d S r1  )r  rV  r   rP  s    r  r  zCheckerScope.active_class  s+    djnh// 	":b>!tr
  c                    |                                  }|s
J d            | j                            |          }|s
J d            | j        |dz
           }t          |t                    r|S dS )z4Is there a class *directly* enclosing this function?z1This method must be called from inside a functionz2CheckerScope stack must always start with a moduler   N)rz  rV  r  r  r   )rJ  topr  	enclosings       r  r  zCheckerScope.enclosing_class	  s    !!GGGGGG
  %%JJJJJJJuqy)	i** 	tr
  Instance | TupleType | Nonec                    |                                  }|s(|                                 r|                                 }|rt          |          S dS )zAn instance or tuple type representing the current class.

        This returns None unless we are in class body or in a method.
        In particular, inside a function nested in method this returns None.
        N)r  rz  r  r   rJ  r  s     r  rm  zCheckerScope.active_self_type  s\       "" 	*))++ 	*''))D 	' &&&tr
  r  rh   rB  c              #  x   K   | j                             |           d V  | j                                          d S rN  rV  r  r=  )rJ  r  s     r  rl  zCheckerScope.push_function!  =      
$
r
  r  r   c              #  x   K   | j                             |           d V  | j                                          d S rN  r  r  s     r  r|  zCheckerScope.push_class'  r  r
  N)r~  ru   r9  r:  )r9  r  )r9  r  )r9  r  )r  rh   r9  rB  )r  r   r9  rB  )r  r  r  r  rK  rz  r  r  r  rm  r   rl  r|  r	  r
  r  r"  r"    s         ////            
	 	 	 	       ^
    ^  r
  r"  TKeyTValuec                  :    e Zd ZdZddZdd	ZddZddZddZdS )DisjointDictai  An variation of the union-find algorithm/data structure where instead of keeping
    track of just disjoint sets, we keep track of disjoint dicts -- keep track of multiple
    Set[Key] -> Set[Value] mappings, where each mapping's keys are guaranteed to be disjoint.

    This data structure is currently used exclusively by 'group_comparison_operands' below
    to merge chains of '==' and 'is' comparisons when two or more chains use the same expression
    in best-case O(n), where n is the number of operands.

    Specifically, the `add_mapping()` function and `items()` functions will take on average
    O(k + v) and O(n) respectively, where k and v are the number of keys and values we're adding
    for a given chain. Note that k <= n and v <= n.

    We hit these average/best-case scenarios for most user code: e.g. when the user has just
    a single chain like 'a == b == c == d == ...' or multiple disjoint chains like
    'a==b < c==d < e==f < ...'. (Note that a naive iterative merging would be O(n^2) for
    the latter case).

    In comparison, this data structure will make 'group_comparison_operands' have a worst-case
    runtime of O(n*log(n)): 'add_mapping()' and 'items()' are worst-case O(k*log(n) + v) and
    O(k*log(n)) respectively. This happens only in the rare case where the user keeps repeatedly
    making disjoint mappings before merging them in a way that persistently dodges the path
    compression optimization in '_lookup_root_id', which would end up constructing a single
    tree of height log_2(n). This makes root lookups no longer amoritized constant time when we
    finally call 'items()'.
    r9  r:  c                0    i | _         i | _        i | _        d S rN  )
_key_to_id_id_to_parent_id_root_id_to_valuesrP  s    r  rK  zDisjointDict.__init__M  s#    +-
 13 ;=r
  r  	set[TKey]rf  set[TValue]c                (    |sdS  fd|D             }|d         } j         |         }|                    |           |dd         D ]I}||k    s	| j         vr| j        |<   |                     j                             |                     JdS )a  Adds a 'Set[TKey] -> Set[TValue]' mapping. If there already exists a mapping
        containing one or more of the given keys, we merge the input mapping with the old one.

        Note that the given set of keys must be non-empty -- otherwise, nothing happens.
        Nc                :    g | ]}                     |          S r	  )_lookup_or_make_root_id)r  r  rJ  s     r  r  z,DisjointDict.add_mapping.<locals>.<listcomp>b  s'    KKKs55c::KKKr
  r   r   )r  r  r  r=  )rJ  r  rf  subtree_rootsnew_rootroot_valuessubtree_roots   `      r  add_mappingzDisjointDict.add_mappingY  s      	FKKKKdKKK #-h76""")!""- 	J 	JLx''<t?V+V+V2:D!,/t6::<HHIIII		J 	Jr
  #list[tuple[set[TKey], set[TValue]]]c                $   i }| j         D ]G}|                     |          }||vrt                      ||<   ||                             |           Hg }|                                D ]'\  }}|                    || j        |         f           (|S )z1Returns all disjoint mappings in key-value pairs.)r  _lookup_root_idr=  rz  r  r  r  )rJ  root_id_to_keysr  root_idr"  r  s         r  r  zDisjointDict.itemsm  s    02? 	. 	.C**3//Go--+.55(G$((----,2244 	D 	DMGTMM4!8!ABCCCCr
  r  r  r  c                    || j         v r|                     |          S t          | j                   }|| j         |<   || j        |<   t	                      | j        |<   |S rN  )r  r  rU  r  r=  r  )rJ  r  new_ids      r  r  z$DisjointDict._lookup_or_make_root_id|  sa    $/!!'',,,))F#)DOC ,2D!&).1eeD#F+Mr
  c                    | j         |         }|| j        |         k    r5| j        | j        |                  }|| j        |<   |}|| j        |         k    5|S rN  )r  r  )rJ  r  r  
new_parents       r  r  zDisjointDict._lookup_root_id  sf    OC 4(+++ .t/DQ/GHJ'1D!!$A 4(+++ r
  Nr  )r  r  rf  r  r9  r:  )r9  r  )r  r  r9  r  )	r  r  r  r  rK  r  r  r  r  r	  r
  r  r  r  2  s         4
= 
= 
= 
=J J J J(           r
  r  pairwise_comparisons,Iterable[tuple[str, Expression, Expression]]operand_to_literal_hashMapping[int, Key]operators_to_groupr0  list[tuple[str, list[int]]]c                   d |D             }g }d}t                      }t                      }t          |           D ]\  }\  }	}
}||	}|rk|	|k    s|	|vra|s%|                    |t          |          f           n||                             ||           |	}t                      }t                      }|                    |           |                    |dz              |	|v r[|                    |          }||                    |           |                    |dz             }||                    |           	|C|s%|                    |t          |          f           n||                             ||           |                                D ]C\  }	}|                                D ])\  }}|                    |	t          |          f           *D|                    d            |S )a  Group a series of comparison operands together chained by any operand
    in the 'operators_to_group' set. All other pairwise operands are kept in
    groups of size 2.

    For example, suppose we have the input comparison expression:

        x0 == x1 == x2 < x3 < x4 is x5 is x6 is not x7 is not x8

    If we get these expressions in a pairwise way (e.g. by calling ComparisionExpr's
    'pairwise()' method), we get the following as input:

        [('==', x0, x1), ('==', x1, x2), ('<', x2, x3), ('<', x3, x4),
         ('is', x4, x5), ('is', x5, x6), ('is not', x6, x7), ('is not', x7, x8)]

    If `operators_to_group` is the set {'==', 'is'}, this function will produce
    the following "simplified operator list":

       [("==", [0, 1, 2]), ("<", [2, 3]), ("<", [3, 4]),
        ("is", [4, 5, 6]), ("is not", [6, 7]), ("is not", [7, 8])]

    Note that (a) we yield *indices* to the operands rather then the operand
    expressions themselves and that (b) operands used in a consecutive chain
    of '==' or 'is' are grouped together.

    If two of these chains happen to contain operands with the same underlying
    literal hash (e.g. are assignable and correspond to the same expression),
    we combine those chains together. For example, if we had:

        same == x < y == same

    ...and if 'operand_to_literal_hash' contained the same values for the indices
    0 and 3, we'd produce the following output:

        [("==", [0, 1, 2, 3]), ("<", [1, 2])]

    But if the 'operand_to_literal_hash' did *not* contain an entry, we'd instead
    default to returning:

        [("==", [0, 1]), ("<", [1, 2]), ("==", [2, 3])]

    This function is currently only used to assist with type-narrowing refinements
    and is extracted out to a helper function so we can unit test it.
    c                ,    i | ]}|t                      S r	  )r  )r  rf  s     r  r\  z-group_comparison_operands.<locals>.<dictcomp>  s    0a0a0a\^^0a0a0ar
  Nr   c                    | d         d         S r_  r	  r  s    r  rM  z+group_comparison_operands.<locals>.<lambda>  s    471: r
  )r  )	r=  r  r  r  r  rz  ri  r  sort)r  r  r  groupsr  last_operatorcurrent_indicescurrent_hashesr  r  	left_expr
right_expr	left_hash
right_hashdisjoint_dictr  indicess                    r  r  r    sY   ` 1b0aN`0a0a0aF<> $M #O"uuN09:N0O0O / /,,Hi $M 		#M 9 9XM_=_=_ " S(//@W@W0XYYYY}%11./RRR$M!eeO UUN 	AAE""" )))/33A66I$""9---044QU;;J%"":...  	O$++]F?<S<S,TUUUU=!--noNNN $*<<>> I I-*0022 	I 	IMD'$++Xvg,GHHHH	I !!&=&=!>>>##r
  r  c                    t          |           } | rt          | t                    sdS t          d t	          | j        | j        gz             D                        S )NFc              3  j   K   | ].}t          |t                    o|j        t          j        k    V  /d S rN  )r  r   r  r   r  r,  s     r  r  z$is_typed_callable.<locals>.<genexpr>  sO         	1gI1=I4I#I     r
  )r   r  r   r  r   r  r  )r  s    r  r  r    ss    A Jq,// u  !!+"<==      r
  c                t   t          |           } | sdS t          | t                    rt          |            S t          | t                    r| j                            d          }|rt          |t                    r2t          |j	        j                  pt          |j
        j                  S t          |j        t                    r#t          d |j        j        D                       S t          |j                   S dS t          | t                    rt          d | j        D                       S dS )NTr  c              3  4   K   | ]}t          |          V  d S rN  r  r+  s     r  r  z'is_untyped_decorator.<locals>.<genexpr>   s+      TT$/55TTTTTTr
  Fc              3  4   K   | ]}t          |          V  d S rN  r  r+  s     r  r  z'is_untyped_decorator.<locals>.<genexpr>   s+      DD$'--DDDDDDr
  )r   r  r   r  r   rj  rQ  r_   r  r  r  r   r  r  )r  r  s     r  r  r    s:   
#

C Et	C	&	& E$S))))	C	"	" E$$Z00 	&),, +FK,<== AUJOB B  &+z22 :TT&+BSTTTTTT,V[99995	C	$	$ EDD#)DDDDDD4r
  FuncBase | Decoratorc                    t          | t                    rt          | j                  S t          | t                    r| j        S J dt          |                        )NFzUnexpected func type: )r  r_   ro  r  rf   rj  )r  s    r  ro  ro     s[    $	"" ###	D(	#	# ~774::77777r
  r   c                    t          | t                    r| j        j        S t          | t                    r>| j        r7t          | j        d         t                    r| j        d         j        j        S dS )Nr   F)r  r_   r  r  rz   r  )r  s    r  r  r      sj    $	"" %y$$$)** 2: 	2*TZ]I>> 	2:a=%115r
  r   c                    t          | t                    rt          | j                  S t          | t                    rt          | j        d         j                  S | S r  )r  r   r   r  r   r  r  s    r  rZ  rZ  )   sR    !\"" +qz***!Z   4qwqz2333Hr
  r  r  c                &    t          | |d          S )NTrV  )r   r  s     r  rI  rI  1   s    dET::::r
  c                *    t          | |ddd          S )NT)rW  ignore_uninhabitedr  )r9   r  s     r  rU  rU  5   s*    
  &*   r
  	node_namec                X    |                      d          o|                     d           S )z-Check if node is private to class definition.r  )r  endswith)r  s    r  rz  rz  C   s,    %%Fi.@.@.F.F*FFr
  c                N    t          |           }|d uot          |          dk    S )Nr   )r   rU  )r  strss     r  r*  r*  H   s)    -c22Dt.D		Q.r
  c                    t          | d          rdS t          | t                    rt          d | j        D                       S dS )z<Return True if type is 'bool' or a union with a 'bool' item.r  Tc              3  6   K   | ]}t          |d           V  dS )r  N)r   r+  s     r  r  z has_bool_item.<locals>.<genexpr>R   s-      RR$T?;;RRRRRRr
  F)r   r  r   r  r  r+  s    r  r  r  M   sP    o.. t#y!! SRR	RRRRRR5r
  r  c                >    t          | t                    r| j        S | S )zIf an expression is an AssignmentExpr, pull out the assignment target.

    We don't make any attempt to pull out all the targets in code like `x := (y := z)`.
    We could support narrowing those if that sort of code turns out to be common.
    )r  rU   r  )r  s    r  r  r  V   s"     !^$$ xHr
  rw   r  r   r  c                X    t          |          }|                     |           |j        S )zFind the highest line number of a potential assignment to variable within node.

    This supports local and global variables.

    Return -1 if no assignment was found.
    )VarAssignVisitorrh  	last_line)r  r  r7  s      r  r  r  a   s,     q!!GHHWr
  c                  l     e Zd ZddZddZddZd fdZd fdZd dZd!dZ	d"dZ
d#dZd$dZ xZS )%r  r  r   r9  r:  c                0    d| _         d| _        || _        d S )NrG  F)r  r  var_node)rJ  r  s     r  rK  zVarAssignVisitor.__init__n   s    r
  r  rV   c                `    d| _         |j        D ]}|                    |            d| _         d S r  )r  r  rh  rJ  r  r.  s      r  r/  z&VarAssignVisitor.visit_assignment_stmts   s6    ) 	 	BIIdOOOOr
  r  rv   c                v    | j         r/|j        | j        u r#t          | j        |j                  | _        d S d S d S rN  )r  r  r  r@  r  r  r  s     r  visit_name_exprz VarAssignVisitor.visit_name_expry   sC    ; 	916T]22 88DNNN	9 	922r
  rt   c                t    | j         }d| _         t                                          |           || _         d S r  )r  r:  r  rJ  r  
old_lvaluer;  s      r  r  z"VarAssignVisitor.visit_member_expr}   s4    [
!!!$$$ r
  rn   c                t    | j         }d| _         t                                          |           || _         d S r  )r  r:  visit_index_exprr  s      r  r  z!VarAssignVisitor.visit_index_expr   s4    [
  ### r
  r   c                    d| _         |j        D ]}||                    |            d| _         |j                            |            d S r  )r  r  rh  r  r  s      r  r  z VarAssignVisitor.visit_with_stmt   sP    ( 	  	 B~		$	dr
  re   c                    d| _         |j                            |            d| _         |j                            |            |j        r|j                            |            d S d S r  )r  r  rh  r  r  rt  s     r  r  zVarAssignVisitor.visit_for_stmt   sh    	t	d; 	%Kt$$$$$	% 	%r
  rU   c                    d| _         |j                            |            d| _         |j                            |            d S r  )r  r  rh  r3  r  s     r  visit_assignment_exprz&VarAssignVisitor.visit_assignment_expr   s>    		tr
  r#  r   c                    |j         |j                             |            |j        *d| _        |j                            |            d| _        d S d S r  )r1  rh  r  r  rJ  r#  s     r  visit_as_patternz!VarAssignVisitor.visit_as_pattern   sV    9 IT"""6DKFMM$DKKK r
  r   c                h    |j         *d| _        |j                             |            d| _        d S d S r  )capturer  rh  r  s     r  visit_starred_patternz&VarAssignVisitor.visit_starred_pattern   s:    9 DKIT"""DKKK ! r
  )r  r   r9  r:  r  )r  rv   r9  r:  )r  rt   r9  r:  )r  rn   r9  r:  r  r  )r  rU   r9  r:  )r#  r   r9  r:  )r#  r   r9  r:  )r  r  r  rK  r/  r  r  r  r  r  r  r  r  r=  r>  s   @r  r  r  m   s           
   9 9 9 9! ! ! ! ! !! ! ! ! ! !   % % % %                         r
  r  rN  )r  r   r  r  r  r:  r9  r  )r  r   r  r  r  r   r9  r  )r  r   r  r  r  r  r9  r  )r  rb   r  r  r  r  r9  r  )rK  r6  r  r   r9  r6  r  )r  rh   r9  r  )rl  r   r9  r  r  )r   r  r!  r  r  r  r9  r  )r   r  r!  r  r  r  r9  r  )r3  rB  r  r  r9  r  )rK  r  r9  r  )r  rb   r9  r  )r  r   r9  r$  )r  rh   r  r4  r9  rh   )r  r   r  r   r9  r  )rQ  r   re  r   rD  rE  r9  r  )r  r   rD  rE  r9  r   )rQ  r   re  r   r9  r  )r  r   r  r   r9  r  )r  r   r  r6  r9  rd  )r  r   r  r6  r  r6  r9  r5  )r  r   r:  r  r9  r  )r  r  r9  rL  )r  r  r  r  r  r0  r9  r  )r  r  r9  r  )r  r  r9  r  )r  r  r9  r  )r  r   r9  r  )r  r   r9  r   )r  r   r  r   r9  r  )r  r6  r9  r  r/  )r  r   r9  r  )r  rb   r9  rb   )r  rw   r  r   r9  r  (`  r  
__future__r   r8  collectionsr   
contextlibr   r   r  r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   typing_extensionsr   r   mypy.checkexprrG  r   r  r   r   r   mypy.binderr   r   r   mypy.checkmemberr   r    r!   r"   r#   mypy.checkpatternr$   mypy.constraintsr%   mypy.erasetyper&   r'   r(   mypy.errorcodesr)   r*   r+   r,   mypy.errorsr-   r.   r/   mypy.expandtyper0   r1   r2   mypy.literalsr3   r4   r5   r6   mypy.maptyper7   	mypy.meetr8   r9   r:   mypy.message_registryr;   mypy.messagesr<   r=   r>   r?   r@   rA   rB   rC   mypy.mrorD   rE   
mypy.nodesrF   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   rk   rl   rm   rn   ro   rp   rq   rr   rs   rt   ru   rv   rw   rx   ry   rz   r{   r|   r}   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   mypy.operatorsr   r   r   mypy.optionsr   r   mypy.patternsr   r   mypy.pluginr   r   mypy.pluginsr   r?  
mypy.scoper   mypy.semanalr   r   r   mypy.semanal_enumr   r   mypy.sharedparser   
mypy.stater   mypy.subtypesr   r   r   r   r   r   r   r   r   mypy.traverserr   r   r   mypy.treetransformr   mypy.typeanalr   r   r   mypy.typeopsr   r   r   r   r   r   r   r   r   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   r   r   r   r   mypy.types_utilsr   r   r   r   mypy.typetraverserr   mypy.typevarsr   r   r   	mypy.utilr   r   mypy.visitorr   r   r   r  r   r   r   r   r  r  r  r  r  r  r1  r-  re  r  r   rD  r~  r  r  r  r0  r  r  r  r  r;  r6  r  r  rT  r  r  r`  r  rg  rA  rn  r=  rs  r"  r  r  r  r  r  r  ro  r  rZ  rI  rU  rz  r*  r  r  r  r  r	  r
  r  <module>r	     s      " " " " " "     # # # # # # 2 2 2 2 2 2 2 2                                   $ 6 5 5 5 5 5     H H H H H H H H H H H H E E E E E E E E E E              - , , , , , ) ) ) ) ) ) X X X X X X X X X X S S S S S S S S S S S S C C C C C C C C C C R R R R R R R R R R S S S S S S S S S S S S 2 2 2 2 2 2 S S S S S S S S S S . . . . . .	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 - , , , , , , ,M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M\ ? > > > > > > > > > 5 5 5 5 5 5 5 5 3 3 3 3 3 3 3 3 6 6 6 6 6 6 6 6 : : : : : :       O O O O O O O O O O < < < < < < < < 1 1 1 1 1 1      
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Y X X X X X X X X X / / / / / / b b b b b b b b b b                                           ,& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &N c b b b b b b b b b b b 3 3 3 3 3 3 P P P P P P P P P P * * * * * * * * $ $ $ $ $ $GCLL      !"  ! ! ! !$Wj:KY%VW  W W W W*/CT0T*U  U U U U% % % % %: % % %% % % % %j % % %$ tJ$456 6 6 6 6
    
       z   ~mI ~mI ~mI ~mI ~mI+d#%; ~mI ~mI ~mIB\    1    
VZ    
 
   
 ^b1% 1% 1% 1% 1%h6 6 6 6$   ] ] ] ]
^ ^ ^ ^
E E E E
O O O O    6 6 6 6r    <    6 @E$, $, $, $, $,N             + + + + ++ + + +       = = = =@   .   .   $	 	 	 	   &   2 2 2 2 22" " " " "= " " ".F F F F Fn F F F	 	 	 	: : : : : : : :z wv			\ \ \ \ \74<( \ \ \~a$ a$ a$ a$H      28 8 8 8      ; ; ; ;   G G G G
/ / / /
      	 	 	 	>  >  >  >  > ' >  >  >  >  > r
  