
    X-Ph%2                   b
   U d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	m
Z
mZ 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 ddlmZmZ ddlZdd	lm Z!m"Z"m#Z#m$Z$m%Z% dd
l&m'Z'm(Z(m)Z) ddl*m+Z+m,Z,m-Z- ddl.m/Z/m0Z0m1Z1m2Z2m3Z3 ddl4m5Z5 ddl6m7Z7 ddl8m9Z9m:Z:m;Z; ddl<m=Z=m>Z>m?Z?m@Z@ ddlAmBZBmCZCmDZD ddlEmFZF ddlGmHZHmIZImJZJmKZK ddlLmMZM ddlNmOZOmPZPmQZQ ddlRmSZS ddlTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\m]Z] ddl^m_Z_m`Z` ddlambZbmcZcmdZdmeZemfZfmgZgmhZhmiZimjZjmkZkmlZlmmZmmnZnmoZompZpmqZqmrZrmsZsmtZtmuZumvZvmwZwmxZxmyZymzZzm{Z{m|Z|m}Z}m~Z~mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ ddlmZmZmZ ddlmZmZ ddlmZmZ ddlmZ ddlmZ ddlmZ dd lmZmZmZ dd!lmZmZ dd"lmZ dd#lmZ dd$lmZmZmZmZmZmZmZmZmZmZ dd%lmZmZmZ dd&lmZ dd'lmZmZmZ dd(lmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ dd)lmZmZmZmZmZmZmZmZmZm Z mZmZmZmZmZmZmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZmZmZmZmZ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,d0Z-d1e.d2<   d3Z/d1e.d4<   eeee|f         Z0d5e.d6<   eeeef         Z1d5e.d7<    G d8 d9e          Z2 G d: d;e          Z3ee4eef                  Z5d5e.d<<    G d= d>e          Z6 G d? d@e+d         e,          Z7 G dA dBe"          Z8e	 dddK            Z9eddM            Z9	 dddOZ9ddUZ:ddZZ;dd]Z<dd^Z=dd_Z>dd`Z?ddcZ@ddeZAdddjZBdddlZC	 dddoZDddqZEddtZFddvZGddzZH G d{ d|eݦ          ZIddZJddZK	 dddZLddZMddZNddZOddZPddZQddZR	 dddZS G d de          ZT G d de          ZUddZV ed          ZW ed          ZX G d deeWeXf                   ZYddZZddZ[ddZ\ddZ]ddZ^ddZ_ddZ`ddZaddZbddZcddZdddÄZeddĄZfddƄZgdd˄Zh G d̄ de٦          ZiddτZjd dЄZkdd҄ZlddӄZmdS (  zMypy type checker.    )annotationsN)defaultdict)IterableIteratorMappingSequenceSet)	ExitStackcontextmanager)	CallableFinalGeneric
NamedTupleOptionalTypeVarUnioncastoverload)	TypeAlias	TypeGuard)
errorcodesjoinmessage_registrynodes	operators)ConditionalTypeBinderFrameget_declaration)CheckerScopeTypeCheckerSharedApi	TypeRange)MemberContextanalyze_class_attribute_accessanalyze_instance_member_accessanalyze_member_accessis_instance_var)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_type)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append_union_noteformat_typeformat_type_bareformat_type_distinctlymake_inferred_type_note
pretty_seq)MroErrorcalculate_mro)P	ARG_NAMEDARG_POSARG_STARCONTRAVARIANT	COVARIANTFUNC_NO_INFOGDEFIMPLICITLY_ABSTRACT	INVARIANTIS_ABSTRACTLDEFLITERAL_TYPEMDEFNOT_ABSTRACTSYMBOL_FUNCBASE_TYPES
AssertStmtAssignmentExprAssignmentStmtBlock	BreakStmt	BytesExprCallExprClassDefComparisonExprContextContinueStmt	DecoratorDelStmtEllipsisExpr
ExpressionExpressionStmt	FloatExprForStmtFuncBaseFuncDefFuncItem
GlobalDeclIfStmtImport	ImportAll
ImportBase
ImportFrom	IndexExprIntExpr
LambdaExprListExprLvalue	MatchStmt
MemberExprMypyFileNameExprNodeNonlocalDeclOperatorAssignmentStmtOpExprOverloadedFuncDefPassStmtPromoteExpr	RaiseStmtRefExpr
ReturnStmtStarExpr	StatementStrExpr
SymbolNodeSymbolTableSymbolTableNodeTempNodeTryStmt	TupleExprr   TypeAliasStmtTypeInfoTypeVarExpr	UnaryExprVar	WhileStmtWithStmt	YieldExpris_final_node)flip_opsint_op_to_methodneg_ops)PRECISE_TUPLE_TYPESOptions)	AsPatternStarredPattern)Plugin)dataclasses)Scope)is_trivial_bodyrefers_to_fullnameset_callable_name)
ENUM_BASESENUM_SPECIAL_PROPS)BINARY_MAGIC_METHODS)state)
find_memberinfer_class_variances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is_literal_type_likeis_singleton_typemake_simplified_union	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type_object_type)%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__     L/var/www/html/test/jupyter/venv/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ed<   dS )PartialTypeScopedict[Var, Context]mapbool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!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d0<   d0d9Zed1d:            Zed2d<            Zd3d=Zd3d>Z	 d4d5dAZd6dDZd7dEZd8dIZd9dMZd:dPZ	 d4d&d&dQd;dYZd<d\Zd<d]Zd=dbZd<dcZd>dgZd?dhZd@djZ d@dkZ!dAdlZ"d@dmZ#dBdoZ$dBdpZ%	 	 	 dCdDduZ&dEdxZ'e(dFdz            Z)	 dGdHd|Z*dIdZ+dJdZ,dKdZ-dLdZ.dLdZ/dMdZ0dNdZ1dOdZ2dPdZ3dQdZ4dRdZ5dSdZ6dTdZ7dTdZ8dUdZ9dVdZ:	 d4dWdZ;dXdZ<dYdZ=dZdZ>d[dZ?d\dZ@d]dZAd^dZBd_dZCd`dZDd_dZEdadZFd_dZGdbdZHdcdZId_dÄZJd_dĄZKd_dńZLd`dƄZMdddǄZNdedʄZOd`d˄ZPdfd̈́ZQdgdτZRdhdфZSdidӄZTdjdքZUdkdׄZVdldلZWdmdۄZXdmd܄ZY	 	 dndodZZddhZ[ded<   dpdZ\dqdZ]drdZ^dsdZ_ddd&ddtdZ`dudZadudZbdvdZcdkdZde(dwd            ZedxdZfdydZgdzdZhd{dZi	 d|d}dZj	 d4d~dZk	 	 	 dddZlddZmddZn	 d|ddZoddZpddZqdd!Zr	 d|dd"Zs	 d4dd$Ztdd%Zudd'Zvdd(Zwdd+Zxdd,Zydd-Zzdd.Z{dd/Z|e}j~        d0d1fd&d&d&d2dd;Zdd=ZddBZddDZddFZddGZddIZddKZddMZddNZddPZddRZddTZddVZddXZdGdd[Zdd]Zdd_ZddbZddcZddfZddgZddiZddlZddmZddnZddoZddpZddsZdduZddwZ	 dddy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ZdݐddÐdZdݐ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Є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(dF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Ԑdk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ܐd4ddZd&ddd	Z	 dd&dddZߐddZddZdݐdddZddZe	 d4dd            ZeŐdd            Z	 d4ddZddZddZddZddZdd Zdd!Zdd"Zd4dd$Zdd%Zdd&Zd&d'dd)Zdd+Zdd-Z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.
    Fr0   errorsr>   msgzlist[dict[Expression, Type]]
_type_mapsr   binder mypy.checkexpr.ExpressionChecker_expr_checkerr'   pattern_checkerr   tscoper   scope
list[Type]return_typesz
list[bool]dynamic_funcszlist[PartialTypeScope]partial_typeszset[Var]partial_reportedz	list[str]widened_varsr   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treery   pathstrper_line_checking_time_nsdict[int, int]returnNonec                   || _         || _        || _        || _        || _        t          ||          | _        || _        t                      | _	        t          |          | _        t          |          | _        |j        | _        g | _        g | _        g | _        t'                      | _        i | _        g | _        g | _        i g| _        t'                      | _        d| _        d| _        |j        | _        |                    |          | _        d| _        d| _         d| _!        d| _"        d| _#        tH          j%        &                    | | j        | j        |          | _'        tQ          | | j        | j        |          | _)        dS )zSConstruct a type checker.

        Use errors to report type check errors.
        r   FNT)*r  r.  r0  r7  r8  r>   r  r6  r   r$  r   r%  r   r   namesr,  r'  r(  r)  setr*  r5  r/  r+  r  r4  pass_numcurrent_node_deferredis_stubis_typeshed_fileis_typeshed_stubr1  recurse_into_functions_is_final_defchecking_missing_awaitallow_abstract_callmypy	checkexprExpressionCheckerr"  r'   r#  )selfr  r.  r0  r7  r8  r6  r:  s           r  __init__zTypeChecker.__init__a  sl    		!&'22gg!$''
+G44z #! $55%*"| $ 5 5g > >(,% '+# # ',#
 $)  "^==$(DK)B
 
  .dDHdk7SSr  c                    | j         S N)r"  rM  s    r  expr_checkerzTypeChecker.expr_checker      !!r  list[Type | None]c                    | j         j        S rP  )r"  type_contextrQ  s    r  rV  zTypeChecker.type_context  s    !..r  c                   | j                                          | j                                         t          | j                  | _        g | j        dd<   | j        d                                          d| _        | j        	                                 g | _
        g | _        d| _        t          | 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.
           Nr   )r*  clearr4  r   r0  r   r  temp_type_maprR  resetr/  r)  r1  r   r7  r%  rQ  s    r  r[  zTypeChecker.reset  s     	##%%%   +DL99   """!!!! (,%!$),,


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%  r0  N__all__ztyping.Sequencebuiltins.strr0  )#rF  r   strict_optional_setr0  strict_optionalr  set_filer8  r7  fullnamer$  module_scopeenter_partial_typesr   top_frame_contextdefsis_unreachable should_report_unreachable_issuesis_noop_for_reachabilityr  unreachable_statementacceptrB  r,  gettyper  named_generic_type
named_typer   rC   failr   ALL_MUST_BE_SEQ_STRformat)rM  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
|                    |           t!                      5 }|rZ|                    | j                            |                     |                    | j                            |                     |                     |           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.
        TNFr]  rX  )rF  r   ra  r0  rb  r/  r  rc  r8  r7  rd  r$  re  rA  r@  addr
   enter_contextclass_scoper%  
push_classcheck_partial)rM  r|  doner  r  stacks         r  check_second_passzTypeChecker.check_second_pass  s    '+#&t|'CDD 	 	  3 	 	 	 	 	 	 	 	 K  	49-T[$, !    ))$)*<== 1 1" 3.DD#2222&(#LOEE-1 1 1)D/t||  HHTNNN" 1* X!//0G0G0X0XYYY!//
0E0Eo0V0VWWW**4000	1 1 1 1 1 1 1 1 1 1 1 1 1 1 111 1 1 1 1 1 1 1 1 1 1 1 1 1 1( 5	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	sa   F1AF1A(FA2E?	3F?FFFFF1F	F1F	 F11F58F5r  .DeferredNodeType | FineGrainedDeferredNodeTypec                   g | _         t          |t                    r|                     |           d S d| _        | j                                        5  |                     |           d d d            d S # 1 swxY w Y   d S NT)r+  
isinstancery   check_top_levelrF  r   rg  rm  rM  r  s     r  r  zTypeChecker.check_partial  s    dH%% 	"  &&&&&*.D'..00 " "D!!!" " " " " " " " " " " " " " " " " "s   A77A;>A;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)rF  rf  r   rg  rh  rm  rB  )rM  r  ru  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  )rM  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     rNt	          |t
                    r9| j                             |          }|                     ||           d| _        d S | j	        
                    ||           d S r  )r%  top_level_functionrA  	last_passr  rj   r  r  rB  r  cannot_determine_type)rM  r  r  r  r  s        r  handle_cannot_determine_typez(TypeChecker.handle_cannot_determine_type.  s    z,,..=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)rm  	Exceptionr2   r  fileliner0  )rM  r  errs      r  rm  zTypeChecker.accept>  sx    	_KK 	_ 	_ 	_!#t{'7DKQUQ]^^^^^^^^^	_s    
A,AA)exit_conditionon_enter_bodybody	else_bodyStatement | Noner  Expression | Noner  Callable[[], None] | Nonec                  | j                             dd          5  t          d | j        D                       }t	          | j                  }| j        j        }t          j	        | j        j
        v }d| j        _        | j        j
                            t          j	                   d}		 | j                             ddd          5  |
 |             |                     |           ddd           n# 1 swxY w Y   t          d	 | j        D                       }
t	          | j                  }|
|k    r| j         j        r|	d
k    r||k    s|	dk    rn|
}|}|	dz  }	|	dk    rt          d          || j        _        |r)| j        j
                            t          j	                   |s|rV| j                             ddd          5  |
 |             |                     |           ddd           n# 1 swxY w Y   |r-|                     |          \  }}|                     |           |r|                     |           ddd           dS # 1 swxY w Y   dS )zARepeatedly type check a loop body until the frame doesn't change.FT)can_skipconditional_framec              3  >   K   | ]}t          |j                  V  d S rP  lenr  .0ptss     r  	<genexpr>z*TypeChecker.accept_loop.<locals>.<genexpr>S  s*      JJs37||JJJJJJr  rX  r   )r  break_framecontinue_frameNc              3  >   K   | ]}t          |j                  V  d S rP  r  r  s     r  r  z*TypeChecker.accept_loop.<locals>.<genexpr>f  s*      "N"NC3sw<<"N"N"N"N"N"Nr        z(Too many iterations when checking a loop)r   frame_contextsumr)  r  r+  r0  warn_unreachablecodesREDUNDANT_EXPRenabled_error_codesdiscardrm  last_pop_changedRuntimeErrorr  find_isinstance_checkpush_type_map)rM  r  r  r  r  partials_oldwidened_oldr  warn_redundantiterpartials_newwidened_new_else_maps                 r  accept_loopzTypeChecker.accept_loopE  s_    [&&&NN <	' <	' JJt7IJJJJJL d/00K  $|<"1T\5UUN,1DL)L,44U5IJJJDS[..!\].^^ & &$0%KK%%%	& & & & & & & & & & & & & & &
  #"N"N4;M"N"N"NNN!$"344 "\11![9 2=AAXX$33taxx+)	2::&'QRRR1S6 -=DL) K044U5IJJJ &> &[..!\].^^ & &$0%KK%%%	& & & & & & & & & & & & & & &  -"88HH8""8,,,  'I&&&y<	' <	' <	' <	' <	' <	' <	' <	' <	' <	' <	' <	' <	' <	' <	' <	' <	' <	's\   B,I	"C7+I7C;	;I>C;	?CI"G>2I>H	IH	A	II #I defnr   c                    | j         sd S | j                            |          5  |                     |           d d d            d S # 1 swxY w Y   d S rP  )rF  r$  function_scope_visit_overloaded_func_defrM  r  s     r  visit_overloaded_func_defz%TypeChecker.visit_overloaded_func_def  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        rt          |j         d         t                    sJ |                     |j         d                    |j         d         j	        j
        r?t          |j         d         t                    sJ |                     |j         d         d           |j         d         j	        j        }t          t          j                  }t!          ||gt"          t"          gd d g||                     d                    }|r6t'          ||          s&|                     d|j         d         j                   |                     ||          }t          |t                     rt          |j                  dk    r|}||j         d         j	        _        t1          |j                   D ]\  }}t          |t                    sJ |j        rqt          |j         d         t                    sJ |j         d         j	        j
        }|r|dk    s|s-|dk    r'|                     |j        |j        j        d	           n|                     |d
           |j        j        t8          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tG          |	          |_        n|j        t          |j         d         t                    sJ |                     |j         d         j	        j        |          }t          |t                     sHt          t          j                  }t!          |gt"          gd g||                     d                    }tG          |g          |_        |j$        r| %                    |          }|j&        r0|s.|,|j$        j'        s | j(        )                    |j        |           | *                    |||j                   | +                    |           d S d S )Nr   rX  T)skip_first_itembuiltins.function)	arg_types	arg_kinds	arg_namesret_typefallbackz!Invalid property setter signaturer   r  allow_empty)r  ),itemsr  rr  r   MULTIPLE_OVERLOADS_REQUIREDis_propertyr  rb   visit_decoratorvaris_settable_propertyvisit_decorator_innerro  r   r   special_formr   rI   rq  r   funcextract_callable_typer  setter_type	enumeratecheck_func_itemr  abstract_statusrQ   rO   INCONSISTENT_ABSTRACT_OVERLOADimplrm  check_overlapping_overloadsr  r   infocheck_method_overrideis_explicit_overridefallback_to_anyr  no_overridable_method!check_explicit_override_decoratorcheck_inplace_operator_method)rM  r  num_abstractr  any_typefallback_setter_typeifdefsettable
item_typesitem	item_typevar_typefound_method_base_classess                 r  r  z&TypeChecker._visit_overloaded_func_def  s   z 	 Ftz??aII&BDIII 	<djmY77777  A///z!} 5 <!$*Q-;;;;;**4:a=$*OOO"jm/4"9#9::'3'2&0#Tl%!__-@AA( ( ($  Wz+?S'T'T WIIA4:a=CUVVV"88dKK!+|<< 7KDY@Z@Z^_@_@_"6K0;
1!- ,, 	" 	"GAtdI..... 	C!$*Q-;;;;;:a=,A [QQUU((UY(ZZZ **4T*BBBy([:M,NNN!3tz??333II&EtLLL9 	#IT""" 	/,,T222y 
 J 5 5D%dI66666 $ : :48=$ O OI ,")))444 7 *: 6 6DIY djmY7777711$*Q-2C2H$OOHh55 	"9#9::''j&i#f%!__-@AA   #H:..DI9 	5(,(B(B4(H(H%)@1@ .9 	1 :
 ..ty$???2249RTXT]^^^..t44444	5 	5r  
inner_typeType | NonectxCallableType | Nonec                .   t          |          }d}|t          |t                    rdS t          |t                    rt          |j                  }t          |t
                    r-t          | j                            |j        |                    }t          |t                    r|}nt          |t                    r<t          t          d||ddd||                     }t          |t                    r|}nt          |t                    rt          |j                  }t          |t                    rtg }|j        D ]1}|                     ||          }| nN|                    |           2t          t#          j        |                    }	t          |	t                    r|	}n|                     ||          S || j                            ||           dS t          |t*                    rdS t          |t,                    sJ |S )z,Get type as seen by an overload item caller.N__call__FT)r  typr  	is_lvalueis_superis_operatororiginal_typechk)r   r  r   r   upper_boundr   rR  analyze_type_type_calleer  r   r   r%   r   r   r  r  r  r   join_type_listr  not_callabler   r   )
rM  r  r  
outer_type
inner_call
union_typer  r  callable_itemjoined_types
             r  r  z!TypeChecker.extract_callable_type  s*   $Z00
*.
J!@!@4j/22 	A()?@@Jj(++ 	(!:::?CPP J j,// 	C#JJ
H-- 	C(%#"#" $",	 	 	 J *l33 ('

I.. 	C.z/?@@J*i00 C&, 1 1D$($>$>tS$I$IM$,LL////"1$2Ee2L2L"M"MK!+|<< 1%0
11*cBBBH!!*c2224j*-- 	4*l33333r  c           	        | j         j        j        | j         j        j        v s| j        r| j        j        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 ]H\  }}	t          |	t                    sJ |                     |	j        j
        |	          }
|
@t%          ||
          sQt'          ||
          r+| j                             |dz   ||z   dz   |	j                   |s| j                                        }|r|j        j        ng }t5          j        d          5  t9          ||
|          rT|dk    o"t9          |
||           ot'          |
|           }| j                             |dz   ||z   dz   ||j                   d d d            n# 1 swxY w Y   J|e|j        J t=          ||t>          dd 	          r|j         rdtC          |"                                |"                                dtF          
          }|%| j         $                    |dz   |j                   ;n|}|j%        r9|j        r2|&                    |j%        d         g|j%        dd          z             }t=          ||t>          dd          s#| j         '                    |dz   |j                   t?          |j(        |j(                  s=t?          |j(        |j(                  s#| j         )                    |dz   |j                   d S )NFzImpl isn't the right type__get__rX  r   Tr   c                "    t          ||           S rP  r   lrs     r  <lambda>z9TypeChecker.check_overlapping_overloads.<locals>.<lambda>  s    *Q2B2B r  )	is_compatr   is_compat_return)ignore_returnreturn_constraint_direction)r  r  r   r  )*r  r  r  ignored_filesrE  r0  test_envr  r  rj   ro  rb   r  r  r  r  r  r  are_argument_counts_overlappingoverload_can_never_match%overloaded_signature_will_never_matchr  r%  active_classr  	type_varsr   ra  )is_unsafe_overlapping_overload_signaturesoverloaded_signatures_overlapr   r   	variablesr   with_unpacked_kwargsr(   &overloaded_signatures_typevar_specificr  copy_modified"overloaded_signatures_arg_specificr  "overloaded_signatures_ret_specific)rM  r  	impl_typer  is_descriptor_getr  r  sig1jitem2sig2current_classr%  	flip_noter  s                  r  r  z'TypeChecker.check_overlapping_overloads.  s    8?48?#@@@! A&*l&; A F *.	9 
	J$)W-- :*.).

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

99999 22:tyIII I@$)y*@ ,, c	R c	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[[ 
 !Q !M(Q$($	)" )" %"!M )At(L(L$L & !HBB !Aq1uqy)TY                 $y,,, *(&+%B%B     & %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QGc	R c	Rs   	A&I;;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   rp  r   r   r  r   r  r   ro  rd  )rM  r  r6  atr  gts         r  is_generator_return_typez$TypeChecker.is_generator_return_type  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  rp  KeyErrorr   )rM  r  r  agts       r  is_async_generator_return_typez*TypeChecker.is_async_generator_return_type  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r  r  r6  rM  s     r  
<listcomp>z8TypeChecker.get_generator_yield_type.<locals>.<listcomp>  s)    aaat..t\BBaaar  r8  r   )r   r  r   r   from_another_anyr   r   r  r=  rB  
from_errorr   ro  rd  r  args)rM  rC  r6  r  s   ` ` r  rH  z$TypeChecker.get_generator_yield_type  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).rE  c                <    g | ]}                     |          S r  )get_generator_receive_typerI  s     r  rJ  z:TypeChecker.get_generator_receive_type.<locals>.<listcomp>$  s)    ccc00|DDcccr  r8  r9  r:  r  rX  r?  r   )r   r  r   r   rK  r   r   r  r=  rB  rL  r   ro  rd  r  r  rM  r   rM  rC  r6  s   ` `r  rP  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 )NrE  z-Should only be called on coroutine functions.r   )r   r  r   r   rK  r   rM  )rM  rC  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                      S )	zSGiven the declared return type of a generator (t), return the type it returns (tr).rE  c                <    g | ]}                     |          S r  )get_generator_return_typerI  s     r  rJ  z9TypeChecker.get_generator_return_type.<locals>.<listcomp>O  s)    bbb//lCCbbbr  r8  rX  r   rQ  r  r   )r   r  r   r   rK  r   r   r  r=  rL  r   ro  rd  r  rM  r   rR  s   ` `r  rW  z%TypeChecker.get_generator_return_typeG  sE   %k22k7++ 	95+NNNNY// 	(bbbbbP[Pabbb   ..{LII 	 9/000K22 	9/000&*<<<[EUAVAVZ[A[A[#A&&%)ZZZK$%%** #A&& ::r  rj   c                    | j         sd S | j                            |          5  |                     |           d d d            d S # 1 swxY w Y   d S rP  )rF  r$  r  _visit_func_defr  s     r  visit_func_defzTypeChecker.visit_func_deff  s    * 	F[''-- 	' 	'  &&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	'r  c                P   |                      ||j                   |j        rN|j        s2|j        s+|                     |          }|                     ||           |                     |           |j        r-| 	                    |          }| 
                    ||           dS dS )!Type check a function definition.r  N)r  r  r  is_overloadis_decoratedr  r  r  original_defr   check_func_def_override)rM  r  r  new_types       r  rY  zTypeChecker._visit_func_defl  s    T	2229 	5# XD,= X
 -1,F,Ft,L,L)66t=VWWW..t444 	9))$//H((x88888	9 	9r  rk   type_override
str | Noner  c                   | j                             |                                o|            | j        }|                     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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 supported__exit____post_init__)r(  r  
is_dynamicrB  rf  r   r+  r  rf  r  r   !enter_attribute_inference_contextcheck_func_defr  popcheck__exit__return_typedataclasses_pluginis_processed_dataclassr  check_post_init)rM  r  rc  r  r  enclosing_node_deferredr  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"C;/CC;C	C;C	C;;C?C?rb  r   c                   |j         J 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 t          |j                  }|j         |_        | j                            |||           |                     |||t          j        dd           d S )Nzredefinition with typezoriginal type)r`  r  rj   r   r   r  %incompatible_conditional_function_defro  r   rb   r  find_partial_typesrr  r   INCOMPATIBLE_REDEFINITIONrz   r  r  r   assign_typecheck_subtype)	rM  r  rb  old_type	orig_typeorig_defr  r)  	name_exprs	            r  ra  z#TypeChecker.check_func_def_override  s    ,,,d'11 '	))$*;<<H(33 Y>>tXxXXXXXY Y
 ).I  )[11 >)#0H!(I66 '&l&$($;$;C$@$@M$0#+)#... 10
 II.H$OOOOO$TY//	!%!2	''	8YGGG""$>,#    r  Iterator[None]c              #  <   K   | j         }i | _         d V  || _         d S rP  )r1  )rM  	old_typess     r  rj  z-TypeChecker.enter_attribute_inference_context  s-      1	(*%


(1%%%r  r   c                   |                      |j                   |                     ||          }|}|D ]\  }}| j        }t	          | j                  | _        | 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        t4                    r|	j        j        }
t7          |
          r| j                            d|
|	           t=          |	j        j                  D ]A\  }}t7          |          r-d|dz    d|	j         d}| j                            |||	           BtA          |	j        | j        | j!        | j        |		           |rm|j        r.| "                    |          r| #                    ||||           n8|d
v r| $                    |||           n|dk    r| %                    ||           t          |j        tL                    rP|j        j'        tP          k    r%|                     t&          j)        |j                   | *                    |           n;t          |j        tL                    r!|j        j+        r| *                    |           |j,        r|j-        r;| .                    |j                  s |                     t&          j/        |           n@| 0                    |j        |j1                  s |                     t&          j2        |           |j3        r|j        }|j1        }| 4                    ||          }| 5                    ||          }|r| 6                    |          }n| 7                    ||          }| 8                    d||||g          }
|9                    |
          }||_        | j:                            |j                   | j;        <                    |          5  | j;        =                                }ddd           n# 1 swxY w Y   |j>        rd}||j?        r|j        dk    rd}|t          |j                  k     rt          |j>        |j        |                   sj|                     t&          jB                            t          |j>        | j                  t          |j        |         | j                            |           t          t          |j                            D ]\}|j        |         }t          |t                    r||dk    r|j?        r|j        dk    r|jE        d         t          jG        t          jH        fvrk|jI        s|j        dk    r$t          jK        jL        M                    |          }t          ||          s#t          t          t          |                              }t          ||d          st          |t                    r|j        jR        s@t          |t                    r.t          |jS        t                    r|jS        j        jR        rd}nl|jT        |         dv rQt&          jU                            |V                    | j                  |V                    | j                            }nt&          jW        }|r|                     ||           nqt          |tL                    r\|j'        t          k    rL|j        dvrCt          |j                  s/|}|jZ        dk     r|}|                     t&          j[        |           t          |||| j8                   ^t          |j^                  }| _                    ||           ddd           n# 1 swxY w Y   | j                                        5  d}|j`        D ]}|jK        a                                D ]p\  }}t          |          }|Z| c                    ||jZ                  s?|| j        d                                }||jK        |<   |je        |         | j        je        |<   q| j        jf        r`| jg        sY|jh        D ]Q}|ji        } | j        At          | j                  }!| |!_k        | j        l                    |!| j        | j                   R| j;        <                    |          5  t          |          st          |          dk    r| j        n                                 | o                    |j^                   ddd           n# 1 swxY w Y   | j        p                                }"|| j        q                    dd           ddd           n# 1 swxY w Y   |"s|j,        st          | j:        d         d          r'| 7                    | j:        d         |j1                  }#n5|j1        r!| 6                    | j:        d                   }#n| j:        d         }#t          |#          }#|p| j        js        }| p0| o-t          |t                    o|jt        t          k     o| jg         }$|j        t          ur1|j        |j        jw        vs|j        jw        |j                 jx        rd}$t          |t                    r	|jy        rd}$|rt          t                      |#          rd}$|o?|j        t          uo1|j        jz        duo#|j        jz        j        {                    d          }%| j        j|        r| j}        st          |#t          t          f          s|$rt          |#t                     rt&          j        }nt&          j        }|r!|                    t          j                  }|                     ||           |%r |                     t&          j        |           nz|$rxt&          j        }|r!|                    t          j                  }|                     dt                      d|#||          s"|%r |                     t&          j        |           | j:                                         || _        dS )r\  )rN  __init_subclass____new__zReturn typez	Argument rX  z to ""r  )__getattr____getattribute____setattr__r:  )r  Nr   T)ignore_type_params>   clsrM  )rN  r  rh  r   Fabc.ABCMetacodezimplicitly returnsexpected)subtype_labelsubtypesupertype_label	supertyper  r  )check_typevar_defaultsr(  expand_typevarsr   r   r0  rg  expandedr  r  rj   r  r  r   r  r   r   r(  rr  r   MUST_HAVE_NONE_RETURN_TYPErt  check___new___signaturecheck_for_missing_annotationsdisallow_any_unimportedro  r   r   r  unimported_type_becomes_anyr  r  r   rE  is_reverse_op_methodcheck_reverse_op_methodcheck_getattr_methodcheck_setattr_methodr   variancerK   #RETURN_TYPE_CANNOT_BE_CONTRAVARIANTcheck_unbound_return_typevarvaluesis_generatoris_async_generatorrB  'INVALID_RETURN_TYPE_FOR_ASYNC_GENERATORr=  r6  !INVALID_RETURN_TYPE_FOR_GENERATORis_awaitable_coroutinerH  rP  rT  rW  rp  r+  r'  r%  push_functionactive_self_typetype_is	is_staticr  r   NARROWED_TYPE_NOT_SUBTYPErA   ranger  r   rJ   	ARG_STAR2is_classrJ  typesr   make_normalizedr   r*   r   r   is_protocolr  r  ERASED_SELF_TYPE_NOT_SUPERTYPEstr_with_optionsMISSING_OR_INVALID_SELF_TYPErL   
is_privater  &FUNCTION_PARAMETER_CANNOT_BE_COVARIANTr   r   r  check_default_argsframesr  r5   !is_var_redefined_in_outer_context
push_framedeclarationsallow_redefinition_newrC  	argumentsvariablerz   r  rv  _is_empty_generator_functionsuppress_unreachable_warningsrm  ri  	pop_framer   allow_empty_bodiesr  rU   rM   r?  plugin_generatedis_mypy_onlymetaclass_typehas_basewarn_no_returnrB  r   INVALID_IMPLICIT_RETURNMISSING_RETURN_STATEMENT_replacer  
EMPTY_BODYnoteEMPTY_BODY_ABSTRACTINCOMPATIBLE_RETURN_VALUE_TYPErw  rl  )&rM  r  r  r  r  r  original_typr  
old_binderr  r  idxarg_typeprefixtctytctrref_type	arg_indexr  erasedr  r  body_is_trivial	new_frameframekeynarrowed_typekey_varargvnunreachablerC  
show_errormay_be_abstracts&                                         r  rk  zTypeChecker.check_func_def  s   
 	##CM222''c22! t	% t	%ID#J/==DK..00 h? h?$$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
 ;  !I+T^+tyT]G]G]$%	 3s}#5#555jS]9%=? ?5 		,FMM +CK F F +CM),Ddl S S  !   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h? h? h? h? h? h? h? h? h? h? h? h? h? h? h?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 <6 Gt| G#~ G GL6- ( 0 0A%&AF K33AqvqvFFFZ--d33 + + 4D99 DS]]a=O=OAACCCKK	***+ + + + + + + + + + + + + + + #k88::(K))$222W+3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3Z  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!!###$DKKi	t	% t	%sp   4Qa1
S0$a10S44a17S48M-a11a5	8a5	Di!2Ahi!hi!h9i!!i%	(i%	r  r   
after_lineintc                    |j         rdS | 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.
        FT)is_finalr$  outer_functionsr  rj   find_last_var_assignment_liner  )rM  r  r  outersouters        r  r  z-TypeChecker.is_var_redefined_in_outer_context  sz     : 	5,,.. 	 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.r  builtins.objectzConsider using the upper bound z insteadr  N)r  r  r   r(  CollectArgTypeVarTypesr  rm  rr  r   UNBOUND_TYPEVARr,   r   r	  r   ro  rd  r  rA   r0  )rM  r  arg_type_visitorargtyper	  s        r  r  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 "r  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 codebaser  argumentdefault)r  r  lvalue_namervalue_namenotes)r  initializerr  rd   r  r  
startswithr0  implicit_optionalrz   rd  check_simple_assignmentro  r<   r  
ASSIGNMENT)rM  r  r  r  r  r  r  s          r  r  zTypeChecker.check_default_args9  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 rP  )r   reverse_op_methodsrM  r  s     r  is_forward_op_methodz TypeChecker.is_forward_op_method\  s    i:::r  c                    |t           j        v S rP  )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   r<  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_anyd  s8    a,, ua))Tamy?T.TTr  c              3  0   K   | ]} |           V  d S rP  r  r  r  r  s     r  r  z<TypeChecker.check_for_missing_annotations.<locals>.<genexpr>i  sK       N
 N
*+""1%%%N
 N
 N
 N
 N
 N
r  rX  r   rM  r  z1Use "-> None" if function does not return a valuer  c              3  .   K   | ]} |          V  d S rP  r  r  s     r  r  z<TypeChecker.check_for_missing_annotations.<locals>.<genexpr>  s/      JJ))!,,JJJJJJr  r  r   r<  r  )r  ro  r   anyr  r  rE  r0  warn_incomplete_stubdisallow_incomplete_defsdisallow_untyped_defsr  r  r  rr  r   RETURN_TYPE_EXPECTEDr   r  r  r  NO_UNTYPED_DEFFUNCTION_TYPE_EXPECTEDr   rW  r6  r   rT  ARGUMENT_TYPE_EXPECTED)rM  r  has_explicit_annotationshow_untypedcheck_incomplete_defsr  r  s         @r  r  z)TypeChecker.check_for_missing_annotationsb  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_metaclassrw  r  	type_typer   INVALID_NEW_TYPEr  r   r   r   r   r   rr  NON_INSTANCE_NEW_TYPErt  rA   r0  )rM  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__.Nr  )__eq____ne__r  r   r   r  r  r  r  rX  r  UnionType | Instancer  r9  r<  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 rP  )r  r   r   ro  has_readable_member)r  xr3  r  s     r  r  zSTypeChecker.check_reverse_op_method.<locals>.has_readable_member.<locals>.<genexpr>  s~          Ay))J.A.A!T.J.J Rq(++P0J0J40P0P     r  )r  r   ro  r3  allr   relevant_items)r  r  r3  s    `r  r3  z@TypeChecker.check_reverse_op_method.<locals>.has_readable_member  s    #x(( :x33D999      *#*<*<*>*>??     r  r  )r  r0  r  r9  r<  r  )(r  r   r   r   r  r   rI   rq  r   r  invalid_signaturer   r  r  r   ro  rd  r  rJ   r+  r  r  r  r   normal_from_reverse_opr   r	  r   r   r   r   r   r  r   r  r  r   rR  analyze_external_member_accesscheck_overlapping_op_methods)rM  r  r*  r+  r  method_typer  forward_nameforward_instr  opt_metaforward_baseforward_typer3  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)
rM  r*  r+  rA  r@  r<  r?  r  forward_itemr  s
             r  r:  z(TypeChecker.check_overlapping_op_methods  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  rF  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          ||	|d          S )NrX  Fr   r   r/  )partial_only)r  r  r  r   r   r+  r   rI   r   r   r%  r$  r  r%  r&  )rM  rF  r?  r*  forward_base_erasedforward_tweakedreverse_tweakedreverse_base_erasedfirstsecondr4  r%  s               r  rC  z$TypeChecker.is_unsafe_overlapping_opH  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695
 
 
 	
r  ri   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  r3  r   r   rR  r9  r  r   is_more_general_arg_prefixr  signatures_incompatible)	rM  r  methodr  r  other_methodinstancetyp2rr  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 )NrX  r  r_  r  )r  r%  r  rr  r   MODULE_LEVEL_GETATTRIBUTEr   rq  r   rI   r   r   r  r$  r   r  $invalid_signature_for_special_method)rM  r  r  r  r;  s        r  r  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 )Nr_  )NNNr  r  )r%  r$  r   r   r   r  rq  r   rI   r   r   r  rZ  )rM  r  r  r;  s       r  r  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__.r_  typing.Iterablezactual typezexpected typer  N)rq  r   rp  rw  r   INVALID_TYPE_FOR_SLOTSr  r  )rM  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              3  4   K   | ]}t          |          V  d S rP  )is_string_literalr  r  s     r  r  z/TypeChecker.check_match_args.<locals>.<genexpr>  s<       5
 5
(,d##5
 5
 5
 5
 5
 5
r  zb__match_args__ must be a tuple containing string literals for checking of match statements to workr  )r%  r$  r   r  r   r5  r  r  r  r  LITERAL_REQ)rM  r  r  r  s       r  check_match_argszTypeChecker.check_match_args  s    z&&(( 	Fc""#y)) 	 5
 5
03	5
 5
 5
 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  rJ  z/TypeChecker.expand_typevars.<locals>.<listcomp>  s     HHH5tw.HHHr  )listr(  r  r  r%  r  r   r  r  r0  mypycinspections	itertoolsproductdictexpand_funcr3   )	rM  r  r  substtvarsresultsubstitutionsmappingrn  s	           @r  r  zTypeChecker.expand_typevars  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                ,   d}|j         r*|j                             |j                  x}r	|j        rd}|sV|rV|j        sQ|j        dvrJt          |j                  s8| j                            |j        |d         j        |p|           d S d S d S d S d S d S )NFTrN  r  r   )	r  rn  r  r  r  r  r  #explicit_override_decorator_missingrd  )rM  r  r  r  r  r  s         r  r  z-TypeChecker.check_explicit_override_decorator  s     !9 	$$)--	":"::$ 	$@U 	$  $ !		)		 -		 	!888ty)) 9 H88	4Q7@'/T    		 		 		 		 		 		 9888r  'FuncDef | OverloadedFuncDef | Decoratorc                J   |j         dvoL| j        j        p|                                 o+|j         dk    p |j        j                            d          du }g }|j        j        dd         D ]5}|                     |||          }| dS |r|	                    |           6|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.
        )rN  r  r  rh  __replace__dataclass_tagNrX  )
r  r0  check_untyped_defsri  r  metadatarn  mro*check_method_or_accessor_override_for_baser  )rM  r  check_override_compatibilityr  baserx  s         r  r  z!TypeChecker.check_method_override  s     IZZ 0I8I8I4I 	]* C9%))/::dB 	% 57!IM!""% 	7 	7DDDd8 F ~tt 7)00666((r  r  r  bool | Nonec                   d}|r|j         }|j                            |          }|rit          |j                  r0t          |          s!| j                            ||j         |           |j        r| 	                    ||j        |           d}|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.
        FTNrP  r  )r  r?  rn  r   r  r  r  cant_override_finalr  $check_if_final_var_override_writable(check_method_override_for_base_with_namer   rQ  )rM  r  r  r  found_base_methodr  	base_attrrT  s           r  r  z6TypeChecker.check_method_or_accessor_override_for_base2  s    " 	$9D
t,,I ) 00 HD9I9I HH00ty$GGG= Z==dINTXYYY$(!+ $ @@tTRR  49===!DH_F
 DDT6SWXX $#t  r  c                   |                      |j        |j        |d          \  }}|                      |j        ||d          \  }}||J t          ||          s*| j                            |j        d         |||           dS dS )a&  Check override of a setter type of a mutable attribute.

        Currently, this should be only called when either base node or the current node
        is a custom settable property (i.e. where setter type is different from getter type).
        Note that this check is contravariant.
        Tr  NrX  )node_type_from_baser  r  r   r  incompatible_setter_overrider  )rM  r  r  r  r  r  s         r  check_setter_type_overridez&TypeChecker.check_setter_type_override[  s     ))$)TYRV)WWQ33DItTW[3\\q=#<#<#<--- 	[H11$*Q-mUYZZZZZ	[ 	[r  c           
        |j                             |          }|sdS t          |t                    s|}n|j        }t          |t
          t          f          r|j        p|j        }n|j        j        p|j        j        }| 	                    |j
        |j        |          \  }J |j        }d}	| 	                    |||          \  }
}|
<d}	| j        | j        k     rz| j                                        }t          |t
                    r/|                     || j                            |                     n|                     ||j                   dS t          |t
          t          f          r|                     |          }
n~t          |t                    r|                     |j                  }
nNt          |t(                    r+|j        t-          |j                  }
nt/                      }
nt/                      }
d}t1          |          r\t1          |          st          |t(                    r8t3          |          st3          |          r|	rd}|                     ||           t          |t
          t          f          r|j        p|j        }n-t          |t                    r|j        }|j        p|j        }nd}t-                    t-          |
          }
t7          |          rt          |t(                    rk|j        sd|j        r|j        rVt          |t                    rAt          |
t:                    s,| j                            d|j        t@          j!                   t          |
t:                    st          t:                    rnt          |
tD                    rt          tD                    r| #                    |
|j
        ||j
        |||          }|r|rt@          j$        | j%        j&        v r| '                    |          rr|sptQ          |
d          s^tS          |
| j%                  \  }}tT          j+        ,                    d|j
         d	| d
| d          }|                     ||           n*t          |
tZ                    rt]          fd|
j/        D                       r|rt@          j$        | j%        j&        v ru| '                    |          r`|s^tS          |
| j%                  \  }}tT          j+        ,                    d|j
         d	| d
| d          }|                     ||           neta          |
          rnT|r(| '                    |          r|rtQ          |
          rn*| j        1                    |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.
        FNTz;Cannot override writeable attribute with read-only propertyr  ignore_pos_arg_namesr`  z (base class "z" defined the type as z, override has type )c              3  <   K   | ]}t          |d           V  dS )Tr  Nr  )r  orig_typr  s     r  r  zGTypeChecker.check_method_override_for_base_with_name.<locals>.<genexpr>  sF       :
 :
 sH4@@@:
 :
 :
 :
 :
 :
r  originaloverride)2r?  rn  r  rb   r  rj   r   r  r  r  r  r  r  rA  r  r%  r  r  r  r   r   ro  r   r   r  is_custom_settable_propertyr  r  r  r   r  rr  r  OVERRIDEr   check_overrideMUTABLE_OVERRIDEr0  r  is_writable_attributer   rC   r   'COVARIANT_OVERRIDE_OF_MUTABLE_ATTRIBUTEwith_additional_msgr   r  r  r   %signature_incompatible_with_supertype)rM  r  r  r  r  r  override_class_or_staticr  original_nodesupertype_readyr  	top_levelalways_allow_covariantoriginal_class_or_staticr  okbase_stroverride_strr  r  s                      @r  r  z4TypeChecker.check_method_override_for_base_with_namej  s    JNN4((	 	5
 $	** 	 GGiG dW&7899 	Q'+}'F$$'+y'9'PTY=P$))$)TYEEQ! 33D$EEq #O}t~-- !J99;;	i11 5OOItz/I/I)/T/TUUUU OOD$)444tMG5F+GHH + $ 2 2= A AM955 + $ 2 2=3E F FM3// + !%1$3M4F$G$GMM$,JJMM !)

!&%% 		@ //		@3=mS3Q3Q		@ +400 @5PQ^5_5_ @ # @-1*33D$???mg/@%ABB 	-'4'='XAX$$y11 	- %D'+}'F$$',$c""'66 	=#..	 "*	 #.		 3@2T		
 4++	 mW55 QI	     mW-- B	C1I1I B	|44 @	C9V9V @	$$		((	 	B (!( *dl.NNN..}== O. O #=#DQQQ O
 *@!3* * *&, 'Nbb:TY : :h : :*6: : :  		#w'''y11 #	c :
 :
 :
 :
)/:
 :
 :
 7
 7
 #	 (*dl.NNN..}== O. O
 *@!3* * *&, 'Nbb:TY : :h : :*6: : :  		#w'''=#.. 	 
	//>>
	BX
	 3..
	 H::	4GmVY ;    ur  tpc                b    t          |t                    rI|j        r@|j        d         t          k    r*t	          |j        d         d |j        D                       S d S t          |t                    r2 fd|j        D             }d |D             }|rt          |          S d S J d            )Nr   c                    h | ]	}|j         
S r  rk  r  r  s     r  	<setcomp>z2TypeChecker.get_op_other_domain.<locals>.<setcomp>	  s    7S7S7S7S7S7Sr  c                :    g | ]}                     |          S r  )get_op_other_domainr  itrM  s     r  rJ  z3TypeChecker.get_op_other_domain.<locals>.<listcomp>	  s'    III"11"55IIIr  c                    g | ]}||S r  r  r  r  s     r  rJ  z3TypeChecker.get_op_other_domain.<locals>.<listcomp>	  s    222Br2R222r  Fz,Need to check all FunctionLike subtypes here)
r  r   r  rI   r*   r  r(  r   r  r   )rM  r  	raw_itemsr  s   `   r  r  zTypeChecker.get_op_other_domain	  s    b,'' 	I| UQ7 : : &bl1o7S7Sbl7S7S7STTT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  r  r  name_in_superr  r  r  c	                
   d}	d}
t          ||d          sd}	nlt          |t                    rW|                     |          rB|                     |          }|                     |          }|r|rt          ||          sd}	d}
t          |t
                    rQ|r|sd}	nJt          |t                    r5t          |t                    r |j        	|j        d}	|j        	|j        d}	t          |          rd}	|	r	d}t          |t                    o|j
        }t          |t          t          f          r|                                }t          |t          t          f          r|                                }t          |t                    rat          |t                    rKt          |j                  t          |j                  k    r |j        |j        k    r|                                d}t          |j        t"                    r|j        j        j        }dfd}t)          t+          |j        |j                            D ]P\  }\  }}|                                r/|                                r|j        |         }|j        |         }nw|                                rb|                                rN|sL|j        |         }||j        v r5|j        |         }|j                            |          }|j        |         }nt          | ||                    st|}t          |t"                    r6|j        s/|j        |t          |j                  z            }|j        d	k    r|}| j                             |d
z   |||||||           d}Rt           ||j!                  |j!                  s+| j        "                    ||||j!        |j!        |           d}nt          |t                    rt          |t                    rg }|j#        D ]C}t)          |j#                  D ],\  }}t          ||          r|$                    |            n-Dt          |          t          |j#                  k    r2|tK          |          k    r| j        &                    ||||           d}|s | j        '                    ||||||           |
r"| (                    d|tR          j*                   |	 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 overriding method.
                                    Used primarily for generating error messages.
          name_in_super:            The name of the overridden in the superclass.
                                    Used for generating error messages only.
          supertype:                The name of the supertype.
          original_class_or_static: Indicates whether the original method (from the superclass)
                                    is either a class method or a static method.
          override_class_or_static: Indicates whether the overriding method (from the subclass)
                                    is either a class method or a static method.
          node:                     Context node.
        FTr  Nr  r   r<  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  rX  )secondary_contextr  zHOverloaded operator methods can't have wider argument types in overridesr  )r  r   r<  r   )+r   r  r   r  r  r   r   
type_guardr  r  unpack_kwargsr)  r  r  min_argstype_var_ids
definitionrj   r  r  r  zipr  is_positionalis_namedr  indexr  r  
bound_argsr  r  $argument_incompatible_with_supertyper  'return_type_incompatible_with_supertyper  r  sorted.overload_signature_incompatible_with_supertyper  r  r  r  )rM  r  r  r  r  r  r  r  r  rr  op_method_wider_noteoriginal_domainoverride_domainemitted_msgoffset_arguments	type_namer  r  sub_kind
super_kindoverride_arg_typeoriginal_arg_typearg_name
original_ir  arg_nodeorderchild_variantparent_variantr  s                                @r  r  zTypeChecker.check_override&	  s   8 $(H4HHH 	,DD*-- 	,$2K2KD2Q2Q 	, #66x@@O"66x@@O,#, #?ODD,
 '+$h-- 	 '  0H  Hl33  
8\8Z8Z  &2x7J7RD#/H4D4LDd 	D h	K)(LAA\hF\(\:$>?? ;#88::(\:$>?? ;#88:: 8\22R'x66R' *++s83E/F/FFF%):::  (4466 	h17;; > ( 3 8 =IH H H H H H 2;*H,>??2 2  +  +-A-*  --// !J4L4L4N4N !,4,>q,A),4,>q,A))!**,, 	!1D1D1F1F 	!O_ 	!#+#5a#8#x'999080B10E-)1);)A)A()K)KJ080B:0N--$ %&7HY9Z9Z[[ ++/%dG44 3T=M 3'+~a#h>Q:R:R6R'SH'}22*2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^ ?    $ 		^    
 x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  exprrz   rd  )r  rets     r  r  z7TypeChecker.check__exit__return_type.<locals>.<genexpr>	  sR       
 
 sx**Tsx/@DT/T
 
 
 
 
 
r  )
ro  r  r   r   r  has_bool_itemr   r5  r  incorrect__exit__return)rM  r  r  r#  s       r  rm  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           
     h   |j         }|j        dd         D ]A}|j        r8|                     t          j                            |j                  |           B| j        	                    |j                   5  | 
                    d          5  | j        }t          | j                  | _        | 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        rt9          |j         | j                  }t=          |j                  D ]}t?          |t@                    rt?          |j!        tD                    r2| j#                            |          }| $                    ||          }d}	t?          |tJ                    r	|j&        pd}	| j'        }
d| _'        | j#        (                    ||gtR          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+        j-        |j.        D ]}t_          |j0        |j1        j+        j,                  D ]z\  }}t?          |td                    r`|j3        th          k    rPt?          |td                    r;|j3        |j3        k    r+|                     d|j         d|tj          j6        	           {|j+        j,        r| 7                    |j+        j,                   |j8        r!|j+        j,        r| 9                    |           |j        s!|j         j        r| :                    |           tw          |j                    dS )
zType check a class definition.rX  NTr  r  )callable_namezVariance of TypeVar "z+" incompatible with variance in parent type)r  r  )<r  r  r  rr  r   CANNOT_INHERIT_FROM_FINALrt  r  r$  r  rf  r   r   r0  rg  r%  r  rm  rh  typeddict_type
tuple_typeis_enumcheck_init_subclasshas_incompatible_baseclasscheck_multiple_inheritancecheck_metaclass_compatibilitycheck_final_deletable
decoratorsr   rq  reversedr  r]   analyzedr   rR  	temp_noder   rd  rI  
check_callr   rI   r  r%  	type_argsbasesr  rM  ro  r   r  rP   r  r,   r  r  check_protocol_variance
check_enumr   )rM  r  r  r  r  sig	decoratordectemprd  old_allow_abstract_callr  	base_inst	base_tvarbase_decl_tvars                  r  visit_class_defzTypeChecker.visit_class_def	  sZ   iGABBK 	^ 	^D} ^		*DKKDIVVX\]]][$$TY// '	G '	G1I1ISW1I1X1X '	G '	GJ/==DK..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 	#("4"< Y  	14NIN$7$A2 2  -I~ #9k::%.);;&~{CC <*3y7III		;IN ; ; ;$(!&	 "    8 	<''(:;;;? 	/sx1 	/((.... 	"493D 	"OOD!!!di(((((s   8K:K 	 D')D	D'DD'DD'K 'D++K .D+/FK 4K K	KK	KKKc                    |j         D ]i}|j                            |          }|rKt          |j        t
                    r1|j        j        r%|                     t          j	        |j                   jd S rP  )
deletable_attributesr?  rn  r  r  r   r  rr  r   CANNOT_MAKE_DELETABLE_FINAL)rM  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.typer  NrX  r  	metaclassTallow_none_returnalways_allow_any)r  r  ro  rd  r  r?  rz   r  r  rx   ro  keywordsr  keysr  rl  rH   r  r]   r  rf  end_linerR  rm  )
rM  r  r  r{  calleerM  r  r  r  	call_exprs
             r  r  zTypeChecker.check_init_subclass4
  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  rw  Sequence[TypeVarLikeType]c                @   |D ]t          t                    r                                s,t          j        j                  s|                     d           j        r6t          fdj        D                       s|                     d           d S )Nz3TypeVar default must be a subtype of the bound typec              3  .   K   | ]}j         |k    V  d S rP  )r  )r  rm  tvs     r  r  z5TypeChecker.check_typevar_defaults.<locals>.<genexpr>j
  s*      $P$PURZ5%8$P$P$P$P$P$Pr  z3TypeVar default must be one of the constraint types)	r  r   has_defaultr   r  r	  rr  r  r  )rM  rw  r  s     @r  r  z"TypeChecker.check_typevar_defaultsd
  s     	U 	UBr;// BNN4D4D bj".99 U		OQSTTTy U$P$P$P$Pbi$P$P$P!P!P U		OQSTTT	U 	Ur  c                   |j         j        sJ |j         j        t          vrkd|j         j        v r]|j         j        d         }t          |j        t                    r1|j        j        r%| 	                    t          j        |j                   |j         j        dd         D ]-}|j        r$|j        t          vr|                     ||           .| j        rT| j        j        dvrF|j         j        s:| 	                    d|j         j         d|           |                     d|           |                     |           |                     |           d S )N__members__rX  r  >   enum	_typeshedzDetected enum "z" in a type stub with zero members. There is a chance this is due to a recent change in the semantics of enum membership. If so, use `member = value` to mark an enum member, instead of `member: type`zLSee https://typing.readthedocs.io/en/latest/spec/enums.html#defining-members)r  r  rd  r   r?  r  r  r   has_explicit_valuerr  r   $ENUM_MEMBERS_ATTR_WILL_BE_OVERRIDDENr  check_final_enumrC  r7  enum_membersr  check_enum_basescheck_enum_new)rM  r  symr  s       r  r  zTypeChecker.check_enumm
  ss   y    9Z//MTY_4T4T)/-0C#(C(( [SX-H [ 		*OQTQYZZZIM!B$' 	2 	2D| 2Z ? ?%%dD111< 	DI.6KKK9) 		0di&8 0 0 0    		b  
 	d###D!!!!!r  c                V    |j         r!|                     d|j         d|           d S d S )Nz+Cannot extend enum with existing members: "r  )r"  rr  r  )rM  r  r  s      r  r!  zTypeChecker.check_final_enum
  sC     	XIIPDIPPPRVWWWWW	X 	Xr  r%  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  ri   rb   r   r  r  r   r   r   ro  r   rC  r  rM  r%  s     r  is_final_enum_valuez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 "r  )r  r  ro  r  rr  r  r0  )rM  r  	enum_baser  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   r<  r  c                x    |                      d          }t          |o|j        o|j        j        dk              S )Nr  zbuiltins.object.__new__)rn  r  r  rd  )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 rP  )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  rX  r  zLOnly a single data type mixin is allowed for Enum subtypes, found extra "{}"T)r  r   r<  r  )
r  r  ro  r  r  r  rr  rt  r  r0  )rM  r  has_newr  	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                V  
 |j         dS |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.
        Nr  c                X    g | ]&\  }}|k    rnt          t          j                  'S r  )r   r   r  )r  r1  r  r  object_types      r  rJ  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  r1  r  r  s      r  rJ  z7TypeChecker.check_protocol_variance.<locals>.<listcomp>
  sK     % % %Aq &'!VV!!!9O1P1P% % %r  T)ignore_declared_variance)r  r  r   r  r  r%  r  r  r   r   rL   rK   rP   r  r  bad_proto_variancer  )rM  r  r  rw  rn  up_args	down_argsupdownr  r  r6  s             @@r  r  z#TypeChecker.check_protocol_variance
  sk    >%F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         }d |D             }t          ||j                                        z
            D ]wt                    rt          fdt          |          D                       \  }}||dz   d         D ],}|j        v r!||j        vr| 	                    |||           -xdS )z.Check for multiple inheritance related errors.rX  Nc                &    h | ]}|j         D ]}|S r  r?  )r  r  r  s      r  r  z9TypeChecker.check_multiple_inheritance.<locals>.<setcomp>
  s'    AAAddjAAdTAAAAr  c              3  8   K   | ]\  }}|j         v ||fV  d S rP  r@  )r  r  r  r  s      r  r  z9TypeChecker.check_multiple_inheritance.<locals>.<genexpr>  s7      YYDddjFXFXAt9FXFXFXFXYYr  )
r  r  r  r  r?  r  r  nextr  check_compatibility)rM  r  r  	all_namesr  r  base2r  s          @r  r  z&TypeChecker.check_multiple_inheritance  s   sy>>QFgabbkAAcAAA	9sy~~'7'7788 	E 	ED $  YYYYIcNNYYYYYGAtQUWW E E5;&&5+@+@,,T4DDDE	E 	E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  )ro  r  r  rV   r   r   r  rR  typeddict_callabler   rq  r   r   r   r  r   r  filter_errorsalias_type_in_runtime_contextr(  s     r  determine_type_of_memberz$TypeChecker.determine_type_of_member  sp   88Och 566 	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?base1rE  c           	     T   |dv rdS |j         |         }|j         |         }|                     ||||          \  }}|                     ||||          \  }	}t          |          }
t          |	          }t          |
t                    rt          |t                    r|
                                re|                                rQt          t          |
                                          t          |                                                    }nQ|r|	sJ t          ||	d          }n7|r|	r|j	        +| 
                    |j	                  st          ||	          }nt          ||	          }|r|j	        r|j	        r| 
                    |j	                  r~t          |j	                  rjt          |j	        t                    rPt          |t                    s;| j                            d| d|j         d	|j         d
|t$          j                   nH|!| j                            ||j        |           |	!| j                            ||j        |           d}t+          |j	                  r0t-          |          s!| j                            ||j        |           t+          |j	                  r|                     ||j	        |           t          |j	        t2                    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
        )rN  r  r  N)r4  leftrightTr  z%Cannot override writeable attribute "z" in base "z#" with read-only property in base "r  r  )r?  r  r   r  r   is_type_objr   r   type_objectr  r  r   r  rb   r   r  rr  r  r  r  cannot_determine_type_in_baser   r  r  r  r   allow_incompatible_override#base_class_definitions_incompatible)rM  r  rK  rE  r  rM  rN  
first_typer  second_typep_first_typep_second_typer  s                r  rC  zTypeChecker.check_compatibility0  s   . ???FD!T"00ucQT0UU
A11$sRU1VVQ 'z22'44lL11 $	jP\6]6] $	'')) 	Tm.G.G.I.I 	T  /0H0H0J0JKK01J1J1L1LMM  
 "1k111
KdSSS 	K 	{&t/I/I&+/V/V&
K88":{;; J 226;?? $EJ//	
 #5:y99 '}g>> HMMK K KQVQ[ K K=BZK K K"^	 "    !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  rJ  z=TypeChecker.check_metaclass_compatibility.<locals>.<listcomp>  sK     
 
 
#
 &e&:OLL	
 
 
 
r  rX  r  c              3  B   K   | ]}t          j        |          V  d S rP  )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$  r  is_named_tupler  r  r  r  r5  rr  )rM  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  rq   c                    |j         D ]<\  }}| j                            |          x}|                     |j        |           =|                     |           d S rP  )r?  r,  rn  warn_deprecatedr  check_import)rM  r  r  r  r%  s        r  visit_import_fromzTypeChecker.visit_import_from  se    z 	5 	5GD!|''---:$$SXt444$r  ro   c                0    |                      |           d S rP  rb  r  s     r  visit_import_allzTypeChecker.visit_import_all      $r  rn   c                0    |                      |           d S rP  re  r  s     r  visit_importzTypeChecker.visit_import  rg  r  rp   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  rvaluerz   r   INCOMPATIBLE_IMPORT_OFrt  r  r  )rM  r  assignlvaluelvalue_typer  rP  messages           r  rb  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  r1  rZ   c                N   |j         r| j                                         d S |j        D ]z}| j                                         rJ|                                 s d S |                     |          s| j                            |            d S e|                     |           {d S rP  )	ri  r   r  r  rj  rk  r  rl  rm  )rM  r1  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 rP  )in_checked_functionr0  r  rB  r   !is_unreachable_warning_suppressedrQ  s    r  rj  z,TypeChecker.should_report_unreachable_issues  sO    $$&& D-D..D KAACCC		
r  ru  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  rW   is_false_literalr  r   r   rf   rd   r]   rR  r  rH  r   rm  r   )rM  ru  r  s      r  rk  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rY   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).
        r  NzA type on this linezType of variabler  rX  )&is_alias_defrC  enter_final_contextis_final_defcheck_assignmentrl  rn  ro  
new_syntaxcheck_type_alias_rvaluer0  r  r   r  r   r  r  r   r   r  r   rE  r  has_typerR  rm  r  lookup_typecheck_finalr   r%  r$  rr  r   DEPENDENT_FINAL_IN_CLASS_BODYunanalyzed_typerx   annotation_in_unchecked_function)rM  ru  rn  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                                         5  | j                            |j                  }d d d            n# 1 swxY w Y   |                     |j        d         |           d S Nr  )r  rH  rR  rm  rn  
store_typerl  )rM  ru  
alias_types      r  r  z#TypeChecker.check_type_alias_rvalue1  s    X##%% 	< 	<*11!(;;J	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	<	"z22222s    AA
A
Trq  rv   rn  re   infer_lvalue_typer  c                L   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.                    r+|j	        dk    r |                     t*          j        |           |rt          |t2                    r|j        | j
                            |          }t          t7          |          t8                    rdS |j        }t=          || j        |j                   ru| !                    |          }|\| j"        sDtG          |          }tI          |t9                      g          }| %                    |||           nd|_        ||= |j        }n| &                    |j        ||           ntO          |          rAt          |t                    r,t          |j        tP                    r|j        j)        r|s|}nt          |t.                    rF|j*        ?| j
                            |j+                  }| ,                    |||||	          \  }}}n$| j        j-        stO          |          rt          |t                    r|j*        t\          k    rt          |j        tP                    r|j        j        r|j        | j/        v rt          t7          |          t`                    so| j/        |j                 }d
}tc          | j2        j3                  D ]}|j4        |v r	|j5        } n|r+tm          |          }| %                    |j        ||           | 7                    |||||          \  }}d}t7          |          }t7          |          }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 |r|rt          |t2                    st          |t                    r|jB        s~| j2        C                    |||           t          |t                    rMt          |j        tP                    r3|j        jD        r'|j        jE        r|tG          |          |j        _        nD| j        j-        r|| j2        C                    |||           n|r| F                    |||           |r| G                    ||          }| j
                            ||          }|j         s6|jE        s/t          |t                    r|j	        dk    stG          |          }| H                    ||||           | I                    |           t          |t                    r8t          |t                    r|j	        dk    s| K                    ||           dS dS dS )z0Type check a single assignment: lvalue = rvalue.=)r  r  r  r  	__slots____match_args__Nrh  )is_lvalue_finalr  F)r  inferredrq  rV  )Lr  r   ru   $check_assignment_to_multiple_lvaluesr  .try_infer_partial_generic_type_from_assignmentrm  rz   r  r  rR  rm  r  r  ra  rg  r%  r$  rn  ro  rr  r   &DATACLASS_POST_INIT_MUST_BE_A_FUNCTIONrx   CANNOT_MODIFY_MATCH_ARGSr   ro  r   r   r  is_valid_inferred_typer0  r  rt  rB  r+   r   set_inferred_typeinfer_partial_typeis_literal_noner   is_initialized_in_classkindr  check_member_assignmentr  rR   r5  r   r  r   r  rl  r  r   r  r   rP  rQ  is_abstractr  r   r  r   r  concrete_only_assignis_special_formrv  is_inferredis_index_varcheck_indexed_assignmentget_variable_type_contextinfer_variable_typecheck_assignment_to_slotsr   check_compatibility_all_supers)rM  rq  rn  r  r  rr  index_lvaluer  r  	signaturer  r$  rvalue_typer  r)  inferred_typeinstance_typedecl_frame_maphas_if_ancestorr  p_rvalue_typep_lvalue_typerV  s                          r  r  zTypeChecker.check_assignment6  se    fy(344 l	D55ff.?     ??PSTTT262C2CFF2S2S/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&*-- M&+AQ2Q2Q		*CVLLL tLk;77 M$K<L<T"&"3":":6"B"BK!/+">">II  %/C-#T\3<   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U#F++2$"68442$ #6;442$ ;	2$
 '2$ #.KKvz22)$7={7J$($5$<$<V[$I$IMBFB^B^{FF C_ C C?K.?.? !L?U+F33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/3/K/K#VVhW] 0L 0 0,K
  $H !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 N#4 NZU`=a=a N&vx88 	^V=S 	^//[QQQ&vx88^ *6; < <^ !' 7^ !' 8	^
 !, 7/PQ\/]/]FK,\8 N[=TK++FKMMM L--lFFKKK 	P#==hOO"/66vL6YY%Q,Q #6844Q :@HX9X9X"CK"P"PK((6;OOO**6222&'** D68,,D17@P1P1P
 33FFCCCCCD D1P1Pr  )builtins.list+)builtins.set|r   partial_type_augmented_opsr  c           
     6   g }|j         r|j         j        dd          D ]}|j        |j        vr|j        |j                 j        }|                     |j        ||t          |          p$t          |t                    ot          |                     \  }}|rFt          |t                    r|j
        s*t          |t                    s|                    |           |sd S |d         }|D ](}	t          |	|          r|	}t          ||	          s d S )|S )NrX  r  r   )r  r  r  r?  r  r  	is_methodr  r   r&   invalid_partial_typer   r  r   r   )
rM  r  rn  type_contextsr  	base_node	base_typer  r3  others
             r  r  z%TypeChecker.get_variable_type_context  sc   = 	4 )!""- 4 4=
22 !Jx}5:	#77M&y11 7!)S11 7+I666  8    	1 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                    r5|j        |j        k    r!t          || j                  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'
        Nr  )r  rz   r  r   ro  r   rx   rR  get_partial_self_varrd  r  rt  rm  r   r   r  r0  r   r   )rM  rq  rn  r  r  r  r)  r  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5 5/  +CH%c***	 0///
 K11 '1#(;;!#&&&/ ?*' 'r  r   c           	     D   |j         }t          |t                    rx|j        |j        k    s|j        rd|j        s^|j        t          d fv rIt          |j	        j
                  dk    r1|j	        j        dd          D ]_}|j                            |j                  }|<|                     |||j                   s n|                     |||j                   s n`|j	                                        }|r|d         nd }d }|j        r5|j        s.| j                            ||j                  }	|j        }|	|_        |                     |j        |j	        |          \  }
}|j        r|j        s||_        |
sd S |j	        j        dd          D ]	}|j        r|j        dk    r|j        dk    s!t1          |j                  r6|                     |j        ||          \  }}t3          |          }t          |t4                    rd }|r|J |                     |
|||||          s d S |
rW|rU|                     |j        ||d          \  }}|J t9          ||
          s | j                            ||
||            d S ||u r d S d S d S d S d S d S d S )	Nr   rX  r  r  r  )r  Tr  )r  r  r   r  r  explicit_self_typer  rT   r  r  r  r  r?  rn  r  "check_compatibility_classvar_supercheck_compatibility_final_superdirect_base_classesrR  rm  ro  r  rS  rd  r  r  r   check_compatibility_superr   r  r  )rM  rq  rn  lvalue_noder  tnodedirect_baseslast_immediate_baseactual_lvalue_typer  rr  r  r  r  custom_setters                  r  r  z*TypeChecker.check_compatibility_all_supersB  ss   k {C((W	  FK//* 0#6 0 d|++K$*++a//#(,QRR0 	 	
{'788$BB;PTV[V`aa ??TSXS]^^ &+??AAL6B"L,r"2"2
 "&& /{/M /"/66v{?OPP%0%5"#. !55k6FHXZ`aaNK& 6{/M 6#5  #(,QRR0 - - : $33IZ8Z8Zk.// '+'?'?@PRVX^'_'_$	9 ;I F Fi55 % $I $00099#!!/< :   
 " #} #'+'?'?',dF (@ ( (	1
  )444))[AA # HAA &Y   #FF222 oW	 W	 ,+	 0///
 0/>- -r  compare_typer  r  r{   r  c           
        |                      |||t          j        dd|j         d          }|r\t          j        | j        j        v rD|                     |          r/|s-|                      |||t          j	        d|j         dd          }|S )Nzexpression has typezbase class "z" defined the type as)
rw  r    INCOMPATIBLE_TYPES_IN_ASSIGNMENTr  r  r  r0  r  r  r  )rM  r  rn  r  r  r  r  r  s           r  r  z%TypeChecker.check_compatibility_super  s     =!;49;;;
 
 	&$,*JJJ**955 K* K ## H?ty???% B 	r  )r  r  r4  r  r  r4  %tuple[Type | None, SymbolNode | None]c          	        |j                             |          }|rNt          |j        t          t
          f          r|j        r&t          |j        t                    r|j        j        dS || j        	                                }nt          |          }|
J d            t          |t                    rt          |          }	n|}	t          |dt          j                            |          ||| d          }
| j                            d          5  |r6|	j        j        p|
                    d          }t+          |	||
||	          }nt-          ||	|
|          }ddd           n# 1 swxY w Y   ||j        fS )
a  Find a type for a name in base class.

        Return the type found and the corresponding node defining the name or None
        for both if the name is not defined in base or the node type is not known (yet).
        The type returned is already properly mapped/bound to the subclass.
        If setter_type is True, return setter types for settable properties (otherwise the
        getter type is returned).
        NNNz)Internal error: base lookup outside classFT)r  r  r  r  r  r  suppress_errorsfilter_deprecatedr  )mcs_fallbackoverride_info)r?  rn  r  r  r   rb   ro  r   r%  current_self_typer   r   r   r"   rJ  rK  is_operator_methodr  rH  r  rq  r#   r$   )rM  r  r  r  r  r  r4  r  r(  rV  mxr  r  s                r  r  zTypeChecker.node_type_from_base  s   $ JNN4((	 	).3	*:;;	 N	 ).+66		
 #/: 
4466II%m44I$$&Q$$$i++ 	!%i00HH H!99$??# 
 
 
 X##d#;; 	U 	U U#=7Y2==;Y;Y:dBXT  		 ;42tTT		U 	U 	U 	U 	U 	U 	U 	U 	U 	U 	U 	U 	U 	U 	U ).((s   AE00E47E4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_classvarrr  r   CANNOT_OVERRIDE_INSTANCE_VARrt  r  CANNOT_OVERRIDE_CLASS_VARrM  r  r  r  s       r  r  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   ri   rb   r  r  r  r  r  rd  r   r   r  r  s       r  r  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)rM  r  r  r  writables        r  r  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.rG  rQ  s    r  get_final_contextzTypeChecker.get_final_context;  rS  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  )rM  r  old_ctxs      r  r~  zTypeChecker.enter_final_context?  sH       $)	)EEE!(DD((((s    	(8AssignmentStmt | OperatorAssignmentStmt | AssignmentExprc                   t          |t                    r|                     |j                  }n&t          |t                    r	|j        g}n|j        g}t          |t                    r|j        nd}|r| j        	                                x}r|d         }t          |t                    sJ |j        xt          |j        t                    sJ |j        j        rP|j        j        sD| j        s=t          |t                    r(|j        !|j        s| 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   NrX  )r  rY   flatten_lvaluesrl  rX   targetrq  r  r%  r$  r   r  r   final_unset_in_classfinal_set_in_initrC  ro  r^  r  final_without_valuer  r  r?  rn  r  cant_assign_to_finalr  )
rM  ru  lvsis_final_declr$  r  r  r  r  r%  s
             r  r  zTypeChecker.check_finalI  s`    a(( 	&&qy11CC>** 	8*CC8*C*4Q*G*GRU 	4dj.E.E.G.GGl 	4QBb'*****w"!"'3/////G04G54 !L4 q.114 * )7 +
 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 )Nr  r  )r  rx   r   rR  rm  r  r   ro  slotsr  r  r?  rn  is_assignable_slotrr  r   NAME_NOT_IN_SLOTSrt  rd  )rM  rq  inst	base_infor  s        r  r  z%TypeChecker.check_assignment_to_slots  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 rP  )r  )r  urq  rM  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   ro  rn  r   r   r5  r  )rM  rq  r  s   `` r  r  zTypeChecker.is_assignable_slot  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   rR  rm  r  r   r  r   r   ro  r   r	  r   rd  )rM  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  rl  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 )NrX  r   c                .    g | ]}t                    S r  r   )r  r  iterable_types     r  rJ  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 rP  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   ru   r  r
  r  r   r   rR  rm  r  type_is_iterabler   iterable_item_typerr  r   CONTIGUOUS_ITERABLE_EXPECTEDr  ITERABLE_TYPE_EXPECTEDrt  r  r   check_rvalue_count_in_assignmentrB  r   split_around_starro  r  set_lineextendr  check_multi_assignment)rM  rl  rn  r  r  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 rP  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 rP  r  r  s      r  r  z?TypeChecker.check_rvalue_count_in_assignment.<locals>.<genexpr>2  s7      "a"aB
SUW_H`H`"a1"a"a"a"a"a"ar  rX  Tc              3  @   K   | ]}t          |t                    V  d S rP  r  )r  rq  s     r  r  z?TypeChecker.check_rvalue_count_in_assignment.<locals>.<genexpr>?  s,      BBz&(++BBBBBBr  )	r  rr  r  r   $TOO_MANY_TARGETS_FOR_VARIADIC_UNPACKrB  r  r  wrong_number_values_to_unpack)
rM  rl  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.rX  r   NrE  r_  )r   rR  rm  r  r   r	  r   r6  r  r   r   r  r3   r   r   r  r  r   rK  r  !check_multi_assignment_from_tuple!check_multi_assignment_from_unionr   ro  rd  r  unpacking_strings_disallowed$check_multi_assignment_from_iterable)rM  rl  rn  r  r  r<  r=  r  r6  r  r  s              r  r  z"TypeChecker.check_multi_assignmentH  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  r  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'          |                               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 rP  r  )r  r  s     r  r  z@TypeChecker.check_multi_assignment_from_union.<locals>.<genexpr>  s"      2]2]!22]2]2]2]2]2]r  )r  r<  r=  r   Nc              3  4   K   | ]}t          |          V  d S rP  )r   )r  cols     r  r  z@TypeChecker.check_multi_assignment_from_union.<locals>.<genexpr>  s+      MM31#66MMMMMMr  F)r2  tupler  r   accumulate_type_assignmentsr  r  r  r  r  rl  r   r   r  r  r   r  rv  r   ro  rm  r  r  )rM  rl  rn  r  r  r  
transposedrk  r  r  r  union_typesr  r  clean_itemsro  declared_typer  declared_typesunion_1_2r  s                          r  r@  z-TypeChecker.check_multi_assignment_from_union~  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 rP  )	r  r   ru   r  r  r  r   r  r  )rM  rl  resr  s       r  r  zTypeChecker.flatten_lvalues  s~     " 	 	B"y(344 ;

4//99:::"h'' WJJrNNNN
r  r   c                    t          |j                  }                     |t          |j                  |          rt	          d t          |          D             t          |                    }|d |         }	|t          |          k    rt          t          ||                   nd }
||dz   d          }|s                     ||          }t           j
                            ||                    }t          |t                    r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|
rMt1           fd|D                       }|                                                    |
j        ||           t+          ||          D ]0\  }}                     |                     |          |           1d S |r |D ]}t          |t6                    rJt          |j        t:                    r0|j        j        $t%          t>          j                   |j        _        at          |t                    rt          |j        t6                    rmt          |j        j        t:                    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 rP  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  rX  r   c                    g | ]U}t          |t                    s                    |          n't                              |j                            VS r  )r  r   r  r   ro  )r  r<  r  rM  s     r  rJ  zATypeChecker.check_multi_assignment_from_tuple.<locals>.<listcomp>  si        $ $.gz#B#BQDNN7G<<<!)$..w*O*O!P!P	  r  r  )"r   r  r  r  rB  r  r   r   lvalue_type_for_inferencer   rR  rm  r  r   r	  r   r6  r@  r   r   r  r  r  r  ru   r  r  rz   r  r   ro  r   rL  rp  )rM  rl  rn  r  r  r=  r  r.  r$  r%  r&  r'  rr  reinferred_rvalue_typer6  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 
 
 Q	 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* *& 4oFF a-<=S=_-`-`*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 rP  r  r  s      r  r  z8TypeChecker.lvalue_type_for_inference.<locals>.<genexpr>+  s5      KK51b*R2J2JKQKKKKKKr  rX  r  r  rv_typesr&  r<  r=  c                    t          | |          D ]`\  }}                    |          \  }}}|r+t          |t                    s                    |           K                    |           ad S rP  )r  rm  r  r   r  )	r  r^  r  r<  sub_lvalue_type
index_exprr  rM  type_parameterss	          r  append_types_for_inferencezITypeChecker.lvalue_type_for_inference.<locals>.append_types_for_inference6  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&  r<  r=  )rB  r  r  r   r   r  r  rm  r  r  r   r  r   rq  )rM  rl  r  r$  r%  r&  r'  rX  rY  rZ  rc  r`  ra  r  rb  s   `             @r  rV  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])
        rX  r   N)r  )	rM  r  r$  re  nr_right_of_starright_indexrN  starrO  s	            r  r  zTypeChecker.split_around_starO  sj     "J.2+;q+@+@'''c%jj[j[!Z+,kll#T5  r  ro  c           	         t          |          }t          |t                    r|                                r|j        }t          ||                     dt          t          j	                  g                    S )Nr]  )
r   r  r   rP  r  r   rp  r   r   r  rM  ro  s     r  r  zTypeChecker.type_is_iterable_  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 )Nr  )r   r  r  r   r   r   r   r  r   rp  r  r  r  r  type_not_iterable)rM  rl  r  r  r  r  r  
items_types           r  rB  z0TypeChecker.check_multi_assignment_from_iterableg  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	        }nt          |t                    r|}nkt          |t
                    r4 j                            |d|          }                     ||           n"t          |t                    rl j                            |d          } j        j        r-t          |j        t                    r|j        j        r|j        }                     ||           nt          |t"          t$          f          r7 fd|j        D             }t)          |                     d                    }nNt          |t,                    r                     |j                  \  }}}n j                            |          }|||fS )NT)rq  c                b    g | ]+}                     |          d          pt                      ,S r   )rm  r   )r  sub_exprrM  s     r  rJ  z,TypeChecker.check_lvalue.<locals>.<listcomp>  sN       
 	 !!(++A. "  !!	  r  r  )is_definitionr  rz   r  r   rx   rR  rm  r  def_varrr   analyze_ordinary_member_accessr  analyze_ref_exprr0  r  r  r   ru   r  r   rq  r   rm  )rM  rq  rn  rr  r  r  r  r  s   `       r  rm  zTypeChecker.check_lvalue  sN    f%% $	;68,,$	;0:6;0L0L$	; &(++ *!&+s33333!;!&*55555!((555!>	** 	;!LL
++ 	;+JJ6SWY_``KOOFK0000)) 	;+<<VD<QQK3'v{C00' K+'
 ";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  rz   is_inferred_defr  r   ro  rx   )rM  ru  r  s      r  ru  zTypeChecker.is_definition  sq    a"" 	%  t 6D$$$ )yD(():&& 	%$$ur  	init_typec           	     p   t          |t                    r| j                            ||           dS t	          || j        |j        t          |t                              sl| j        se|j	        dk    rV| 
                    |||          sA| j                            ||| j        j                   |                     |||           dS 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+          |          }|                     |||           | j        j        r| j                            |||           dS dS )z>Infer the type of initialized variables from initializer type.)r  is_lvalue_memberr  N)r  r   r  deleted_as_rvaluer  r0  r  rx   r2  r  r  need_annotation_for_varpython_version!set_inference_error_fallback_typer1  rv  r   r   r   rL  ro  r   r  r  r   rv  )rM  r  rq  r{  r  s        r  r  zTypeChecker.infer_variable_type  s    i-- $	FH&&y':::::& $!+FJ!?!?	  "	F )"	F yC(?(?fi(X(X00w@[\\\66tVYOOOOO   vz**	F-9 :$"??? !>v~!NPYZZ @ H,,T7DL<WXXX	 455DIII
 #9--I""4;;;|2 F''	9EEEEEF Fr  c                   t          |          }t          |t                    r3t          |t                    s| j        j        s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 )Nr  r  builtins.dictcollections.OrderedDictc              3  N   K   | ] }t          |t          t          f          V  !d S rP  )r  r   r   r  r  s     r  r  z1TypeChecker.infer_partial_type.<locals>.<genexpr>  sE         q8_"=>>     r  collections.defaultdictr   rX  Fr  T)r   r  r   rx   r0  r  r   r   ro  rd  r   r5  r   rM  r   'is_valid_defaultdict_partial_value_typer)   r  r)  r  )	rM  r  rq  r{  partial_typerd  is_refarg0arg1s	            r  r  zTypeChecker.infer_partial_type  s   #I..	i** %	vz**%	26,2U%	
 '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   TrX  )
r  r   r  rM  r   r0  old_type_inferencer   r   r   )rM  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	        <   | 
                    ||           t          |          }t          |t                    r!t          |j                  rd|_        dS dS 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  rk  )r  r  s     r  r  z0TypeChecker.set_inferred_type.<locals>.<setcomp>:  s    ,V,V,V%UX,V,V,Vr  N)rB  ro  r  r5  r   r  r  rx   r1  rv  r  r   r   is_node_staticr  is_staticmethod)rM  r  rq  ro  p_types        r  r  zTypeChecker.set_inferred_type.  s      	+t1 	+CH"CO$...,V,V4;CU,V,V,V$S)&*-- I$2O2[>-DHD1&.AOOFD)))$T**F&,// +N6CT4U4U +&*###	+ 	+ 	+ 	++ + + +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 it 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_typer  )rM  r  rq  ro  r  s        r  r  z-TypeChecker.set_inference_error_fallback_typeE  s5     55d;;sFH55555r  c                b    |                     t                                }t          |          S rP  )rm  SetNothingToAnyr*   )rM  ro  r  s      r  r  z)TypeChecker.inference_error_fallback_typeS  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 rP  r(  re  s     r  r  z,TypeChecker.simple_rvalue.<locals>.<genexpr>i  s$      "H"Hd4>"H"H"H"H"H"Hr  F)r  rs   r   r\   rg   r   r]   r  r  rV   ro  r   r(  r   r  r  )rM  rn  r  s      r  simple_rvaluezTypeChecker.simple_rvalueY  s     fwIwOPP 	4fh'' 	I&-11 Ij"$97 7 I m(-c<00 I"},,Z00 I""H"Hci"H"H"HHHHHur  r  
expression)r  rq  r  rr  r<   r  r  r  list[str] | None
Var | Nonetuple[Type, Type | None]c          	        | j         r0t          |t                    rt          t          j                  |fS |d uo"t          t          |          t                     }
|	t          |          r|}nd }| j        	                    |||
          }||t          || j                  s{| j        	                    |||
          }t          || j                  sI|	G| j                            |	|| j        j                   |	                    t                                }t          |t                     r|	|	j        |	j        st'          |          }t)          |	j        |          s|                     |          st-          |	j        |g          }t)          ||	j                  st          |	j        t.                    sq| j                            |	j                   |                     |	||           | j                            ||           t=          |          }||| j        j        |<   t          t          |          t@                    rtC          |          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   |%                                s^t          t          |          t                    s<t          || j                  r'tM          ||          r|}| '                    |           t          |tP                    r| j        )                    ||           t          |tP                    r| j        *                    ||           n$|r"| +                    ||||| d| d|           ||fS )N)rV  r  )r  z	 has type)r  ),rC  r  rd   r   r   r  r   is_typeddict_type_contextrR  rm  r  r0  r  r  r  r  rz   ro  r  r+   r   refers_to_different_scoper   r   r+  r  r  r  r   putr7   r  r   r   r  rH  local_type_maphas_new_errorsr   store_typesr   r~  deleted_as_lvaluerw  )rM  rr  rn  r  r  r  r  r  rq  r  r  rV  r  new_inferredlitlocal_errorstype_mapalt_rvalue_types                     r  r  z#TypeChecker.check_simple_assignmentl  s    < ^	,Jv|<< ^	,9122K??*$6  z,,g@ @ < #<[#I#I*#+22\DT 3  K ' (.{DLII )
 #/66GW 7   .k4<HH HXMaH44 '4<+F   #."4"4_5F5F"G"GK 68,,L(M- ) .  AMM#HM<@@ L  99&AA L&;X]L<Y&Z&Z+KGG LPZ$M;Q Q L !-44X]CCC 228V[QQQ KOOFK@@@ #/v"6"6C"@K 8 =?;77CC/ -[99/ **622	/ X++-- t?R?R?T?T X`&*&7&>&>7G '? ' 'O                             
 %3355/ ''G'GQQ/ /MM	/
 */;GG/ #2K$$X...+{33 A**;@@@+{33 **;@@@@ 
"""---"--- # 	 	 	 ++s6   K10KK1K	K1!K	"K11K58K5rz   c                    |j         t          k    rdS | j                                        dS |j         t          k    r0|j                            d          d         | j        j        k    rdS dS )NFT.r   )r  rR   r%  r  rN   rd  
rpartitionr7  )rM  r  s     r  r  z%TypeChecker.refers_to_different_scope  sh    95Z**,,84Y$4=#;#;C#@#@#CtyGY#Y#Y4ur  rx   r  attribute_typetuple[Type, Type, bool]c                   t          |          }t          |t                    r|                                st          |t                    r|                     |||          \  }}||dfS | j                            d          5  | j        	                    |d          }ddd           n# 1 swxY w Y   |                     |||          \  }}t          ||          ot          ||          }	|	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.
        Tr  F)r  N)r   r  r   rP  r   r  r  rH  rR  rw  r   )
rM  rq  r  r  rn  r  r  r  get_lvalue_typeinfers
             r  r  z#TypeChecker.check_member_assignment  se     (66}l33 	58Q8Q8S8S 	5Xb8Y
 Y
 	5 "99.&RYZZNK44X##d#;; 	 	"/NN% O  O	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 55nfgVVQ;88 
Z^>
 >
 $7{{NNs   B11B58B5rr   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   rR  rm  r  r9  r;  check_method_callr  r   rI   r  r   	ambiguousr   r  )rM  rq  rn  r  basetyper;  res_typer  s           r  r  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  r  c                    ||_         ||= | j        j        r9t          |j                  }||_        | j                            |||           dS dS )z8Replace the partial type of var with a non-partial type.N)ro  r0  r  rz   r  r  r   rv  )rM  r  rb  r)  r  s        r  replace_partial_typez TypeChecker.replace_partial_type&  sb     #<. 	; ""AAFK##Ax:::::	; 	;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          || j                  r}t          || j                  rl| j        sg|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  r  r   r  r   rx   rR  r  ro  r   rt  rd  rm  r  r  r0  rB  
value_typer   rp  r  )
rM  rq  rn  r  r%  r)  typenamekey_typer  rb  s
             r  r  z:TypeChecker.try_infer_partial_type_from_indexed_assignment3  s    fk7++ 	F
6;;KS0Q0Q 	F+"CCZ00 	F#88EECc3 	P#(K00 PHM	$F $ 7 7 < < (F$-//#<<<#<<<  $077EEH!%!2!9!9&!A!AJ.xFFP2:t|LLP !% :P
 %(AAA # 3 ?$1*ch>Q$R$R !@ $(#:#:8hPZE[#\#\11#xOOOOO9	P 	PP P P P =<
P P P P !@ ?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   ro  rd  r.   rn  r-   )rM  r  proper_types      r  type_requires_usagezTypeChecker.type_requires_usageZ  sl     &c**k8,, 	G (,>>>35EFF##K00<35EFFtr  rf   c                F   | j                             |j        dd          }|                     |          }|rg|\  }}|                     t
          j                            t          || j	                            ||           | 
                    |||           d S d S )NTr  r  )rR  rm  r  r  rr  r   TYPE_MUST_BE_USEDrt  rA   r0  r  )rM  ru  	expr_typeerror_note_and_code
error_noter  s         r  visit_expression_stmtz!TypeChecker.visit_expression_stmtk  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  rM  ru  s     r  visit_return_stmtzTypeChecker.visit_return_stmtw  s0    q!!!!!!!!r  c           	        | j                                         }|-|j        r'|                     | j        d         |j                  }n5|j        r!|                     | j        d                   }n| j        d         }t          |          }t          |t                    }t          |t                    r+|s)|j        s"|                     t          j        |           d S |j        rt          |t                     }t          |t"                    }|p|p|}t          | j                            |j        ||                    }t          |t(                    r)|j        j        dk    rt#          t.          j                  }|j        r"|                     t          j        |           d S t          |t"                    r| j        j        r| j        st=          t#          t.          j                  |          sd|j        t@          v rtC          |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 )Nr  r  builtins._NotImplementedTyper  gotr  )r  r  r  r  r  outer_contextr  ))r%  current_functionr  rW  r'  r6  rT  r   r  rt   r   r  rr  r   NO_RETURN_EXPECTEDr  r   r   rR  rm  r   ro  rd  r   r  r  RETURN_IN_ASYNC_GENERATORr0  warn_return_anyrB  r   r  r   is_literal_not_implementedr  incorrectly_returning_anyNO_RETURN_VALUE_EXPECTEDrw  r  rx  RETURN_VALUE_EXPECTED)	rM  ru  r  rC  	is_lambdadeclared_none_returndeclared_any_returnallow_none_func_callr  s	            r  r  zTypeChecker.check_return_stmt|  s   z**,,  4"<<%b)4+<  " 4"<<T=Nr=RSS"/3)+66K"444I+77   )> II.A1EEEFv QI'1+x'H'H$&0g&F&F# (1'_4H'_L_$ &%,,?S -    sH--:)-KKK!)"899C* 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HG DI Ir  rm   c                (   | j                             ddd          5  t          |j        |j                  D ]\  }}t          | j                            |                    }t          |t                    r| j
                            ||           |                     |          \  }}| j                             dd          5  |                     |d           |                     |           ddd           n# 1 swxY w Y   |                     |d           | 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  from_assignmentN)r   r  r  r  r  r   rR  rm  r  r   r  r~  r  r  r  )rM  ru  r2  r1  r  if_mapr  s          r  visit_if_stmtzTypeChecker.visit_if_stmt  sO    [&&\]&^^ 	- 	-AFAF++ D D1#D$5$<$<Q$?$?@@a-- 5H..q!444#'#=#=a#@#@  [..1.MM # #&&vu&EEEKKNNN# # # # # # # # # # # # # # #
 ""8U"CCCC**E*JJ - -; -KK,,,- - - - - - - - - - - - - - -#	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-sZ   B+F	-D6FDF	D
7F"E/#F/E3	3F6E3	7FFFr   c                    t          |j        g|j        gd          }|                    |           |                     ||j        |j                   dS )zType check a while statement.N)r  )rm   r  r  r  r  r  )rM  ru  if_stmts      r  visit_while_stmtzTypeChecker.visit_while_stmt  sT    !&AF8T22!+afEEEEEr  r}   c                :   |                      |j        |j        |j                   t	          |j        t
                    r!| j                            |j        d          }n| j                            |j                  }t          ||j                  \  }}|r| j        
                    |||j        |          \  }}t	          t          |          x}t                    rt	          |j                            |          x}t                    rn|j        D ]f}	t	          |	t"                    rOt	          |	j        j        x}
t&                    r.t)          |
          |k    r|                     |	j        |           gt-          ||          s | j                            |j        |           nSt3          |j        |j        |j                  }|                    |           |                     |j        |dd           |                     |           dS )z9Type check an operator assignment statement, e.g. x += 1.TF)rq  rn  r  r  N)r  rq  rn  r  r  rx   rR  visit_member_exprrm   infer_operator_assignment_methodcheck_opr   r   ro  
get_methodr   r  rb   r  r   r   ra  r   r   incompatible_operator_assignmentr~   r  r  r  )rM  ru  rr  inplacerT  r  r;  r  r  r  r  r  s               r  visit_operator_assignment_stmtz*TypeChecker.visit_operator_assignment_stmt  s   ;;AHahPQPTUUUah
++ 	= +==ahMMKK+2218<<K:;MM 	'+'8'A'A&+WXW_ab'c'c$K/+">">>$II 	;j	,,V4446GO O 	; !J ; ;D"433;&din'<slKK; 's^^{::,,TY:::k;77 C99!$BBB !$!(33DMM!!!xQV "    	r  rW   c                   | j                             |j                   t          |j        t                    r=t          |j        j                  dk    r |                     t          j	        |           | 
                    |j                  \  }}|j        #| j                             ||j        d d           |                     |           d S )Nr   F)suppress_unreachable_errors)rR  rm  r  r  r   r  r  rr  r   MALFORMED_ASSERTr  r  analyze_cond_branchr  )rM  ru  true_mapr  s       r  visit_assert_stmtzTypeChecker.visit_assert_stmt$  s      (((afi(( 	<S->->-B-BII&7;;; "77??(511!%5 2    	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  r  s     r  visit_raise_stmtzTypeChecker.visit_raise_stmt2  sf    6 	-!!!&!,,,; 	A!!!+q4!@@@!!!!!r  r2  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 |           t          |t$                    rE|j        j        dk    r7|                     t          j                            d          |           d S d S d S )Nbuiltins.BaseExceptionr  z%; did you mean "NotImplementedError"?)r   rR  rm  r  r   r  r~  rq  r   r  r   rw  r   
make_unionr   INVALID_EXCEPTIONr   r  r   ro  rd  rr  r  )rM  r2  ru  r  r  exc_typeexpected_type_itemss          r  r  zTypeChecker.type_check_raise:  sh   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c8$$ 	):>\)\)\II 2FF;  	    	 	)\)\r  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_bodyrm  ri  r  s     r  visit_try_stmtzTypeChecker.visit_try_stmtW  s	   
 [&&A&FF 	, 	, **1Q^8L8L*MMM~ ,AN+++	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, > 	, ;--// ,AN+++++	, 	,, ,s   AA44A8;A8r  c                   | 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
        |         }|r|j        }t          |j        t"                    rDt%          |	          }||j        _        | j        j        r| j                             |||           | j        j        s| 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  rm  r  r  r  handlersr  check_except_handler_testis_starvarsrz  r  r  r  r  r  r   r   ro  r0  r  rv  cleanser  )	rM  ru  r  r  r  r  r  r  rb  s	            r  r	  z%TypeChecker.visit_try_without_finallyx  sP    [&&AQZ&[[ !	) !	)**ET`a*bb 9 9[..AY].^^ ( (KK'''( ( ( ( ( ( ( ( ( ( ( ( ( ( (s1:// 9 9A22Dq2QQ 9 9gaj S $ > >sAI N NA"#&)C" S 7; 3 $ 5 5c4>>!S;Q;Q R R RAJqM222fQi 9 &)XF)#(C88 U+6f+E+E+E08#'<#F !U$(K$;$;C8$T$T$T#'<#F 9 $ 3 3C 8 8 819 9 9 9 9 9 9 9 9 9 9 9 9 9 99	9 9 9 9 9 9 9 9 9 9 9 9 9 9 9< { )AK(((C!	) !	) !	) !	) !	) !	) !	) !	) !	) !	) !	) !	) !	) !	) !	) !	) !	) !	)s~   IHB5HBHB	AHD%G=	1H=HHHHIH	IH	$IIIr  r  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)rR  rm  get_types_from_except_handlerr   r  r   r  r   r  rP  rr  r   INVALID_EXCEPTION_TYPEdefault_exception_typer*   r  r   r  r   rq  r   INVALID_EXCEPTION_GROUPr   rL  wrap_exception_groupr   )
rM  r  r  r  	all_types
test_typesttyper  r  new_all_typess
             r  r  z%TypeChecker.check_except_handler_test  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   rL  rp  )rM  r  r  s      r  r  z"TypeChecker.default_exception_type  s9    9/00 	R**+DxjQQQr  r  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   rq  rp  )rM  r  r  r  s       r  r  z TypeChecker.wrap_exception_group  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  rM  s      r  rJ  z=TypeChecker.get_types_from_except_handler.<locals>.<listcomp>  sP       !%!C!CD!!L!L      r  r  r   )r   r  r   r  r   r6  r   rM  )rM  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  rh   c                     j         r                     j                  \  }n                     j                  \  }_        |_                             j        j         fd           dS )zType check a for statement.c                 L                         j         j        d u           S rP  )analyze_index_variablesr  
index_type)r  ru  rM  s   r  r  z,TypeChecker.visit_for_stmt.<locals>.<lambda>  s)    $">">ALD$8!# # r  )r  N)	is_async analyze_async_iterable_item_typer  analyze_iterable_item_typeinferred_item_typeinferred_iterator_typer  r  r  )rM  ru  iterator_typer  s   `` @r  visit_for_stmtzTypeChecker.visit_for_stmt  s    : 	O'+'L'LQV'T'T$M99'+'F'Fqv'N'N$M9(#0 FK      	 	
 	
 	
 	
 	
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__)rR  rm  check_method_call_by_namecheck_awaitable_exprr   INCOMPATIBLE_TYPES_IN_ASYNC_FOR)rM  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.r  )-analyze_iterable_item_type_without_expressionrR  rm  analyze_range_native_int_type)rM  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__)
rR  r   r6  r  r   partial_fallbackro  rd  r   rM  )rM  ro  r  r9  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.rangerX  r  c              3  ,   K   | ]}|t           k    V  d S rP  )rI   )r  r  s     r  r  z<TypeChecker.analyze_range_native_int_type.<locals>.<genexpr>5  s&      ??DGO??????r  NTF)r  r]   r  r   rd  r  rM  r5  r  r   r  r   ro  r   )rM  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.ContainerrX  r   N)r   r  r   r  analyze_container_item_typer  r  r   ro  r  rq  r8   r  rM  r   r   )rM  r  r  r  c_typer  super_instances          r  rJ  z'TypeChecker.analyze_container_item_typeD  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  )rM  r  r  r  r  s        r  r)  z#TypeChecker.analyze_index_variablesZ  s1     	eT^^Iw%G%GIZ[[[[[r  rc   c                d   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 ]S}t          |t                    r<| j                            |t#          |j                  t'          |                     Td S )N__delitem__Tr  r  )r  r  rr   rx   r  r  rf  r]   r  r   rI   rR  rm  flattenrz   r   rv  r   r  r   )rM  ru  r2  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;O   r  rb   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   rd  r   r   r  r  ro  is_readyr  )rM  r2  ru  s      r  r  zTypeChecker.visit_decoratorr  sr     	 	A!W%% :!777!()?!@!@AEJ%)AENFF""1%%%%%r  r  c           	        | j         r^| j                            |j                  5  |                     |j        |j        j        |           d d d            n# 1 swxY w Y   |                     |j                  }d}t          |r|j        dd          n|j                  D ]W}t          |d          rt          |t                    r#|s |                     t          j        |           Ld}| j                            |          }|                     ||          }d }	t#          |t$                    r	|j        pd }	d }
|	it#          |t(                    rT|                     |j                  r:|                     |j                  }
| j                            |
|j                  }	|                     |j        ||           | j                            ||gt6          j        g||	|
          \  }}Y|r|                     ||j                   t=          ||j                  }||j        _         d|j        _!        |j        j"        rgt#          |tF                    r=tI          d |j%        D                       dk    r| j&                            d	|           | '                    |           |s|rd S |j        j(        r|j)        s{| *                    |          }|j        j+        r?|s=|;|j        j(        j,        s*| j&        -                    |j        j        |j                   | .                    |j        |           |j        j(        rc|j        j        d
v rU|j         rNt#          t_          |j                   t`          tb          f          s |                     t          j2        |           |j        j3        r2t#          |t`                    r| 4                    |j        |           d S d S d S )Nr  FrX  zabc.abstractmethodTr  )r  r6  c                :    g | ]}|                                 |S r  )is_required)r  ks     r  rJ  z5TypeChecker.visit_decorator_inner.<locals>.<listcomp>  s%    DDDaAMMOODDDDr  zToo many arguments for propertyr  )5rF  r$  r  r  r  r  r   r  r  r   r   rr  r   r  rR  rm  r  r  r   rd  rx   r  r  r  method_fullnamecheck_for_untyped_decoratorr  r   rI   check_untyped_after_decoratorr   r  ro  rU  r  r   r  r  r  $check_incompatible_property_overrider  r^  r  r  r  r  r  r   r   r   BAD_CONSTRUCTOR_TYPEr`  ra  )rM  r2  r  r  r  non_trivial_decoratorru  r   r  rd  r6  t2r  s                r  r  z!TypeChecker.visit_decorator_inner{  sZ    & 	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.. %oO!,qrr**1<PP 	 	A!!%9::  !!^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 ! 	<..sAF;;;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q} 	V(,(B(B1(E(E%+D1D .9 3 :
 ..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6 	6:c<#@#@ 	6((55555	6 	6 	6 	6s   (AA"Ar  dec_typedec_exprc                    | j         j        rLt          |j                  r:t	          |          r-| j        s(| j                            |j        |           d S d S d S d S d S rP  )	r0  disallow_untyped_decoratorsis_typed_callablero  is_untyped_decoratorrB  r   typed_function_untyped_decoratorr  )rM  r  ra  rb  s       r  r[  z'TypeChecker.check_for_untyped_decorator  s     L4	K!$),,	K %X..	K .		K H55diJJJJJ	K 	K 	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 NrX  r   )r  r  r  r  r  r  r?  rn  r  r  r   r  r   rb   r  rr  r   'READ_ONLY_PROPERTY_OVERRIDES_READ_WRITE)rM  r2  r  r  r  s        r  r]  z0TypeChecker.check_incompatible_property_override  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   ro  rd  r   rb  r   r  rm  r  )rM  ru  exceptions_maybe_suppressedr  r  exit_ret_types         r  visit_with_stmtzTypeChecker.visit_with_stmt  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 rP  )r0  disallow_any_decoratedrC  rJ  rK  has_any_typer  untyped_decorated_function)rM  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  )rR  rm  r6  r7  r   'INCOMPATIBLE_TYPES_IN_ASYNC_WITH_AENTERr  r  r   r   r  r   rI   &INCOMPATIBLE_TYPES_IN_ASYNC_WITH_AEXIT
rM  r  r  r  r9  r  objr  rR  r  s
             r  rm  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   rg  r  )
rR  rm  r6  r  r  r   r   r  r   rI   r{  s
             r  rn  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  r[   c                8    | j                                          d S rP  )r   handle_breakr  s     r  visit_break_stmtzTypeChecker.visit_break_stmt(  s      """""r  ra   c                8    | j                                          d S rP  )r   handle_continuer  s     r  visit_continue_stmtzTypeChecker.visit_continue_stmt+  s    ##%%%r  rw   c           
     	    t          |j        t                    ru|j        ft          |j        j        t
                    r|j        j        j        nd}d|z   }t          |          }t          |          |_        ||j        _	        |j        }n|j        } 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
                            dd	          5  |j        s't          t          |j                  t6                    r                     d d
           i }nt;          ||j        |j                  \  }}                     |          }                     |          }                      |j!        |                                |d
           |rE|"                                D ]0\  }}                      #                    ||          d
           1                     |j!        d
           |
e j
                            dd          5  t           j                            |
                    }t          |t                    r j                            ||            $                    |
          \  }}tK          ||          }t          |	tL                    rp|	j'        p|	j(        }t          |t                    rM||fD ]H}|stS          |          D ]3}t          |t                    r|j        |j        k    s(||         ||<   4I                     |d
                                |           d d d            n# 1 swxY w Y   n                     |           d d d            n# 1 swxY w Y                        |d
           , 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 )N zdummy-match-Fr   r  c                F    g | ]}j                             |          S r  )r#  rm  )r  prM  subject_types     r  rJ  z0TypeChecker.visit_match_stmt.<locals>.<listcomp>J  s,    ^^^aT188LII^^^r  c                    g | ]	}|j         
S r  )capturesr  s     r  rJ  z0TypeChecker.visit_match_stmt.<locals>.<listcomp>K  s    'J'J'Jq
'J'J'Jr  Tr   r  r  )*r  subjectr]   subject_dummyr  r   rd  r   rz   r  r   r  r   rR  rm  r   r  r~  patterns#infer_variable_types_from_type_mapsr  guardsbodiesnarrow_type_from_binderr#  ri  ro  r   r  conditional_types_to_typemaps	rest_typepropagate_up_typemap_inforemove_capture_conflictsr  r  _get_recursive_sub_patterns_mapr  or_conditional_mapsr   patternr  ro  )rM  ru  rl  r  r  named_subjectpattern_types	type_mapsinferred_typesr  gr1  current_subject_typepattern_typer  pattern_mapr  r  r<  	guard_mapguard_else_mapcase_targetr  r  s   `                      @r  visit_match_stmtzTypeChecker.visit_match_stmt/  s+   ai** 	& &2<QY=Mw2W2W_QY%..]_%*II"*4..'($OMMIM[&&A&FF L	 L	*4+<+C+CAI+N+NOOL,44 <**<;;; _^^^^STS]^^^M'J'JM'J'J'JI!EEiPPN qz18QX>> 7D 7D1a'+'8'P'P!<( ($  $3::1>RSS[..1.MM 1' 1'' Y:'(9::O, , Y **4*GGG,.0M)<+<l>T1 1-X '+&D&D[&Q&Q#'#A#A(#K#K55l6K^\\\**;*NNN& "-8->->-@-@ " "	c $ 2 2$($H$Hs$S$S49 !3 !" !" !" !" **<+@RW*XXX}![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 U./i.A16#-k8#D#D 
!U5>4I 	%U 	%U/7 )5,448NN )U )UD0:40J0J-948M[EY4Y4Y08FNtnH],C,C)U !..y%.PPP KKNNN3+ + + + + + + + + + + + + + +6 Ac1' 1' 1' 1' 1' 1' 1' 1' 1' 1' 1' 1' 1' 1' 1'd ""8U"CCCC **E*JJ                WL	 L	 L	 L	 L	 L	 L	 L	 L	 L	 L	 L	 L	 L	 L	 L	 L	 L	s   5DS EQ/D1Q	6Q/QQ/	Q
Q/#S/Q33S6Q378S/R=1S=S	SS	SSSdict[Expression, Type]c                p   i }t          |          }t          |t                    rt          |t                    rzt	          |j                  t	          |j                  k    sJ t          |j        |j                  D ]3\  }}|||<   |                    |                     ||                     4|S rP  )	r   r  r   r   r  r  r  updater  )rM  r  r  sub_patterns_maptyp_	item_expritem_typs          r  r  z+TypeChecker._get_recursive_sub_patterns_map  s     46s##dI&& 	c:dI+F+F 	ctz??c$*oo5555'*4:tz'B'B c c#	8.6 + ''(L(LYX`(a(abbbbr  r  list[TypeMap]dict[SymbolNode, Type]c           
        t          t                    }|D ][}|W|                                D ]B\  }}t          |t                    r(|j        }|J ||                             ||f           C\i }|                                D ]\  }}	d}
g }|	D ]a\  }}|                    |           |                     |          \  }}}|,d}
|                     |||t          j
        dd          r|||<   b|
sSt          j        |          }|	d         \  }}t          |t                    sJ |||<   |                     ||||           |S )NFTzpattern captures typezvariable has type)r  r  r  r   )r   ro  r  r  rz   r  r  rm  rw  r   INCOMPATIBLE_TYPES_IN_CAPTUREr   r  r   r  )rM  r  all_capturestmr  r  r  r  r  r  already_existsr  previous_typer  rb  first_occurrences                   r  r  z/TypeChecker.infer_variable_types_from_type_maps  s   
 GRRVFWFW 	? 	?B~!# ? ?ID#!$11 ?#y#///$T*114+>>>13)//11 	\ 	\MC"N "E% < <	cS!!!&*&7&7&=&=#q! ,%)N))%,J&=(; *   < /<s+! \$/66&.qk# !!#s+++++&.s#((.>JZ[[[r  r  r  r  c                    |r`t          |                                          D ]@\  }}t          |t                    r$|j        }||vst          |||                   s||= ?d S d S rP  )ro  r  r  rz   r  r   )rM  r  r  r  r  r  s         r  r  z$TypeChecker.remove_capture_conflicts  s      	+!(.."2"233 + +	cdH-- +9D>11CX\I]9^9^1$TN	+ 	++ +r  or   c                    |j         r|                     |j         j                   | j                                        5  | j                            |j                   d d d            d S # 1 swxY w Y   d S rP  )
alias_noder  alias_tvarsr  rH  rR  rm  rm  rM  r  s     r  visit_type_alias_stmtz!TypeChecker.visit_type_alias_stmt  s    < 	B''(@AAAX##%% 	. 	.$$QW---	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.s     A--A14A1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 )Nr  )
r^   rZ   rd  r   r   r  r  rG   calculate_metaclass_typer  )rM  r  r  r  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                  \  }} j                                        5 }                     |           ddd           n# 1 swxY w Y   |                                ro ||ddd                   } ||| j                  \  }} 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 the first time you call it.
        So this means calling `self.intersect_intersection([inst_1, inst_2], ctx)`
        twice will return the same subclass of inst_1 and inst_2.

        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_r  r<  r  c                    g }| D ]8}|j         j        r|j         j        }n|g}|D ]}|                    |           9|S rP  )ro  is_intersectionr  r  )r  base_classes_r  r  expanded_insts        r  _get_base_classesz:TypeChecker.intersect_instances.<locals>._get_base_classes  sf    M" 8 89, &#yHH $vH%- 8 8M!((77778  r  r  curr_module_ry   r0  r   tuple[TypeInfo, str]c                   fd| D             }dt          |d           d}|j                            |          x}%t          |j        t
                    sJ |j        |fS                     |j        |||           \  }}||fS )Nc                4    g | ]}t          |d           S )r   )r0  	verbosity)rB   )r  r4  r0  s     r  rJ  z^TypeChecker.intersect_instances.<locals>._make_fake_typeinfo_and_full_name.<locals>.<listcomp>   s)    ^^^1%aAFFF^^^r  z<subclass of and>)rE   r?  rn  r  r  r   r  rd  )	r  r  r0  r?  r  symbolr  info_rM  s	     `     r  !_make_fake_typeinfo_and_full_namezJTypeChecker.intersect_instances.<locals>._make_fake_typeinfo_and_full_name  s     _^^^P]^^^E>:eU#;#;>>>D&,00666C!&+x88888{D((11,2GtUbccKD%$;r  T)r0  barer  r  z
" is finalNr  z/would have inconsistent method resolution orderz)would have incompatible method signaturesrX  extra_attrs)r  r  r<  r  )r  r  r  ry   r0  r   r<  r  )r%  r  r  ry   r   rE   rC   r0  ro  r  r  r  r  r  rH  r  r  r  rF   r   rN   r?  r   r  )rM  r  r  curr_modulerN  rO  r  r  base_classespretty_names_list
new_errorsr  r  	full_namer  s   `              r  intersect_instanceszTypeChecker.intersect_instances  sm   * 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	??k4< OD) '')) 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"C +t|# #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[   .G D/#G /D33G 6D37AG F2&G 2F66G 9F6:
G !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 r  r  z	.__call__r  )r%  r  r  ry   gen_unique_namero  r  r?  rB   r0  r  rd  rj   rZ   	_fullnamer  r   rT   rN   r   r  )	rM  r  r  
cur_modulegen_name
short_namer  r  func_defs	            r  intersect_instance_callablez'TypeChecker.intersect_instance_callableP  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)	rq  r   r   r   explicitr   rJ   r  r  )rM  r  r  r  s       r  make_fake_callablezTypeChecker.make_fake_callablel  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   ro  r  r  r  )rM  r  r  	callablesuncallablesr  subcallablessubuncallablesityprT  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  )rM  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   r6  r   r   required_keysr  r  r  r  )	rM  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_iterable  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 rP  )_is_truthy_typer  r  rM  s     r  r  z.TypeChecker._is_truthy_type.<locals>.<genexpr>   s1      SSA,,Q//SSSSSSr  )r  r   r  ro  r3  rd  r   r   r5  r   r  rM  r  s   ` r  r  zTypeChecker._is_truthy_type  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 )
z
        Check if a type can have a truthy value.

        Used in checks like::

            if x: # <---

            not x  # <---
        Nr<  r9  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 r  z
" returns zCall returns zExpression has type )	rA   r0  r  rx   r  r   rd  r]   r  )r  r  rM  r  s    r  format_expr_typez;TypeChecker.check_for_truthy_type.<locals>.format_expr_type5  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 )Nr  )r  rz   rx   r  rA   r0  )r  rM  r  s   r  get_expr_namez8TypeChecker.check_for_truthy_type.<locals>.get_expr_nameD  sA    $: 677 4'49'''' #1dl333r  r]  typing
Collection)r<  r9  )r   rb  r   r  r  r   rr  r   FUNCTION_ALWAYS_TRUErt  r   TYPE_ALWAYS_TRUE_UNIONTYPEr   ro  rd  r  ITERABLE_ALWAYS_TRUErA   rM  r0  TYPE_ALWAYS_TRUE)rM  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  r]   r<  r  c                \    t          | j        d          ot          | j                  dk    S )z)Is expr a call to type with one argument?r  rX  )r   r  r  rM  )r  s    r  is_type_callz8TypeChecker.find_type_equals_check.<locals>.is_type_calle  s'    %dk?CC[DIZ[H[[r  NFr   	list_mapsr  r  c                x    t          d | D                       rdS i }| D ]}||                    |           |S )z2Combine all typemaps in list_maps into one typemapc              3     K   | ]}|d u V  	d S rP  r  r  rQ  s     r  r  zKTypeChecker.find_type_equals_check.<locals>.combine_maps.<locals>.<genexpr>  s&      0019000000r  N)r5  r  )r%  
result_mapru  s      r  combine_mapsz8TypeChecker.find_type_equals_check.<locals>.combine_maps  sZ    00i00000 tJ ) )=%%a(((r  )r  r]   r<  r  )r%  r  r<  r  )operandsr  r]   r  rM  get_isinstance_typer   r  r   r  #conditional_types_with_intersectionr  r  )rM  r  r   r$  exprs_in_type_callstype_being_comparedr  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_checkZ  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  in_boolean_contextr8  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 and TypeIs 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.

        If `in_boolean_context=True` is passed, it means that we handle
        a walrus expression. We treat rhs values
        in expressions like `(a := A())` specially:
        for example, some errors are suppressed.

        May return {}, {}.
        Can return None, None in situations involving NoReturn.
        r7  )find_isinstance_check_helperr  )rM  r  r8  r  r  
new_if_mapnew_else_maps          r  r  z!TypeChecker.find_isinstance_check  s\    *  <<%7 = 
 
 33F;;
55h??<''r  c          
     4   t          |          ri d fS t          |          rd i fS t          |t                    rzt	          |j                  dk    rat          |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 nt          |j        d          rat	          |j                  dk    ri i fS t          |          t          k    r+|                     |          }|                     ||          S n t          |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 nWt          |j        t&                    r|j        j        |j        j        |j        d         t.          j        k    rt3          |                     |j                            }t          |t4                    r$t7          d||d	
          }|t3          |          }t          |t8          t:          f          r|j        d         j        d         }||j        v r5|j                             |          }	t          |j        |	                   }nG|j        j        dnd}
| !                    tD          j#        $                    |
          |           i i fS t          |          t          k    r|j        j        |tK          |j        j                  ii fS |j        j        J t          |g|                     |                     |          tM          |j        j        d          g|          R  S nNt          |tN                    r| (                    |          S t          |tR                    ri }i }| *                    |j+                  \  }}||,                    |           ||,                    |           | *                    |j-        d          \  }}||,                    |           ||,                    |           ||d n|||d n|fS t          |t\                    rg|j/        dk    r\| *                    |j0                  \  }}| *                    |j1                  \  }}te          ||          tg          ||d	          fS t          |t\                    re|j/        dk    rZ| *                    |j0                  \  }}| *                    |j1                  \  }}tg          ||          te          ||          fS t          |th                    r,|j/        dk    r!| *                    |j5                  \  }}||fS t          |          t          k    r| 6                    |          r| 7                    |                     |                    rtq          |                     |          d          s| j9        j:        r| ;                    |                     |          dd          \  }}|ty          |          }nt{                      }|t}          |          }nt{                      }t          |tz                    s||ind }t          |tz                    s||ind }||fS |                     |          }|r| ?                    ||           t          |d          }ty          |          }t}          |          }t          |tz                    s||ind }t          |tz                    s||ind }||fS )Nr   zbuiltins.isinstancer   rX  zbuiltins.issubclasszbuiltins.callablezbuiltins.hasattrr  Tr  guardnarrowerFis_upper_boundr7  r  )coalesce_anyornotr  r  rl  )Ais_true_literalr{  r  r]   r  rM  collapse_walrusr   r  r6   rS   r  r-  r  r,  infer_issubclass_mapsr  r   hasattr_type_mapsr   r  r  r  r   rI   r   r   r   r   r   r  r  r  rr  r   TYPE_GUARD_POS_ARG_REQUIREDrt  r   r!   r_    comparison_type_narrowing_helperrX   r  r  r  rm  r~   r  rN  rO  and_conditional_mapsr  r   r  r  can_be_narrowed_with_lenr   r0  rb  narrow_with_lenr   r   r   r  r   )rM  r  r8  r  vartyper
  called_typecallr  r  r  r  r  if_assignment_mapelse_assignment_mapif_condition_mapelse_condition_mapleft_if_varsleft_else_varsright_if_varsright_else_varsrN  rO  yes_typeno_typeoriginal_vartypeif_type	else_types                               r  r:  z(TypeChecker.find_isinstance_check_helper  s    4   	t8OD!! 	8OdH%% [	$#di..A*=*="49Q<00D!$+/DEE Nty>>Q&&r6M4==L008AA ,,T22D4L4LTYWX\4Z4Z\`     1 $DK1FGG Dty>>Q&&r6M4==L0055dDAAA 1#DK1DEE ?ty>>Q&&r6M4==L00"..t44G==dGLLL 1 $DK1CDD 9ty>>Q&&r6M/	!d>N>NtyYZ|>\>\]]4==L00T0c$ii1nn11$8H8H8N8NPTUVPWXXXDK11 3;)59L9X~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 04{/E/QGGWa !% !%		$4$P$W$WX\$]$]_c!" !" !" (*2vt}}44  ;1=$(/$+:P*Q*Q#RTV#VV#';#6#B#B#B#@ $$!%!I!I$($4$4T$:$:%.t{/BSX%Y%Y%Y$Z$("" ""$ $ $  n-- J	$88>>>n-- H	$ FH595O5OPTP[5\5\22 ,/000". 3444373M3M
u 4N 4 4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 	? &&'7>>>12BOTTG$$w''	(27O(L(LV$RV,6y/,R,R\D)$$X\xr  c           	        d |j         D             }g }i }t          |          D ]\  }}|                     |          si i fc S |                     |          }|                    |           t          |          t          k    r:t          |          s+|                     |          st          |          }||||<   t          |                                |ddh          }	g }
|	D ]D\  }}|dv r|                     ||||||          \  }}n|dv rt          |          dk    sJ |\  }}||         }||         }i i }}||v rt          |          rzt          t!          |                    }|\t          |          sMt#          |t$                    r|j        j        dk    s(t+          ||          rt-          |          |||         <   ||v r5|                     ||          \  }}||         }|d}n|||<   |d}n
|||<   ni }i }|d	v r||}}|
                    ||f           Ft1          d
 |
D                       rt3          |
          S t3          |                     |          d          S )z2Infer type narrowing from a comparison expression.c                ,    g | ]}t          |          S r  )rG  )r  r4  s     r  rJ  z@TypeChecker.comparison_type_narrowing_helper.<locals>.<listcomp>  s     >>>1OA&&>>>r  N==is>   rb  !=ra  is not>   innot inr   r  >   rc  rd  rf  c              3  &   K   | ]}|i i fk    V  d S rP  r  r(  s     r  r  z?TypeChecker.comparison_type_narrowing_helper.<locals>.<genexpr>  s*      88qRH}888888r  Tuse_meet)r+  r  r  r  r  r6   rS   r  is_literal_enumr7   group_comparison_operandspairwiseequality_type_narrowing_helperr  r   r   builtin_item_typer  r   ro  rd  r9   r   r  r  reduce_conditional_mapsfind_tuple_len_narrowing)rM  r  r+  operand_types narrowable_operand_index_to_hashr  r  r  hsimplified_operator_listpartial_type_mapsoperatorr   r  r  
left_indexri  r  r  collection_item_typer]  r^  s                         r  rK  z,TypeChecker.comparison_type_narrowing_helperz  sC    ?>>>>+-( ** 	< 	<GAt==&& 2v((..I  +++ --'-- .,,T22 . !&&=:;4Q7$ $=MMOO=d|$
 $
  &> 6	9 6	9"Hl777#'#F#F! 4$ $  ---<((A----*6'
K)*5	 -k :#%r!AAA*955 V/>?PQ^?_?_/`/`,0<$78L$M$M = !++? J J = %9$=$FJ[$[$[ ;IG[ \ \ %\ <K9;U;UF8J#78"BBB)-)L)L!=* *&GY $K0D!%'.t (#')2 555#+V$$fh%78888
 88&788888 	_*+<=== +4+H+H+N+NY]^^^^r  rv  r+  rq  rr  dict[int, tuple[Key, ...]]c                   |dv rt           }d}d}	nGdd}
dd	}|
}d}fd
|D             }t          t          ||                    ot          |           }	i }i }|	r/|                     |||                                ||          \  }}|i k    r3|i k    r-|                     |||                                          \  }}|i k    r|i k    r|                     ||          \  }}||fS )z@Calculate type maps for '==', '!=', 'is' or 'is not' expression.>   rb  rd  FTr  r   r<  r  c                F    t          t          |           t                    S rP  )r  r   r   r  s    r  is_exactly_literal_typezKTypeChecker.equality_type_narrowing_helper.<locals>.is_exactly_literal_type  s    !/!"4"4kBBBr  c                N    t          | dd           ot          | dd           S )Nr-  F)	check_allr.  )r   r  s    r  has_no_custom_eq_checkszKTypeChecker.equality_type_narrowing_helper.<locals>.has_no_custom_eq_checks  sA    0x5    N/8uMMMMNr  c                     g | ]
}|         S r  r  )r  r  rq  s     r  rJ  z>TypeChecker.equality_type_narrowing_helper.<locals>.<listcomp>  s    AAAq-*AAAr  r  )r   r5  r  is_ambiguous_mix_of_enums%refine_identity_comparison_expressionr  refine_away_none_in_comparisonr6  )rM  r  rv  r+  rq  r   rr  is_valid_targetcoerce_only_in_literal_contextshould_narrow_by_identityr|  r  
expr_typesr  r  s       `          r  rm  z*TypeChecker.equality_type_narrowing_helper  su   4 '''6GO-2*(,%%C C C CN N N N
 6O-1*AAAALAAAJ(++Z88) ) )</
;;; & $ 	#II05577.   FH R<<HNN#BB-7W7\7\7^7^   FH R<<HNN#::4NNFH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)rM  r  
output_mapr  r  new_mappingparent_exprproposed_parent_types           r  r  z%TypeChecker.propagate_up_typemap_info4  s    2 4
(00 	? 	?OD)(Jt 224CCK5@5F5F5H5H ? ?11 )++*>
;''? r  r  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   r<  r  c                    j                                         5 }t          | ddd| 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  in_literal_context)r  rH  r%   r  )r  wmember_typemember_namer  rM  s      r  replay_lookupz6TypeChecker.refine_parent_types.<locals>.replay_lookupw  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  rJ  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  rJ  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   r<  r  )r  rx   rG  r  lookup_type_or_noner  rr   r  r  r   r   r   r   r   r  r:   r  r   )rM  r  r  outputparent_typer  r*  new_parent_typesr  r  r  r  r  r  s   `         @@@@r  r  zTypeChecker.refine_parent_types_  s6    *,g	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{+Og	V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   r<  r  c                    t          |           } t          |           p t          | t                    o| j        j        S rP  )r   r   r  r   ro  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 rP  r  )r  r  rq  r  s     r  r  zDTypeChecker.refine_identity_comparison_expression.<locals>.<genexpr>  s4      ]]! 3 3M!4D E E]]]]]]r  Nr  FrA  r  r   r<  r  )r  r   r   r   r  r  r   is_enum_literalrm  r  r  ro  rd  r!   r   conditional_typesr  ro  )rM  r+  rq  r  r  r  r  should_coercer  possible_target_indicesr  r  singleton_indexsum_type_nametarget_typeru  r  r  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|                    |           /i i }	}|r t          |          t          |          k    r|D ]M}||         
t          
          st          
fd|D                       rt	          
          |||         <   Nt          d |D                       r4|D ]1}||         
t          
          rt	          
          |	||         <   2||	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.
        c              3  8   K   | ]}t          |          V  d S rP  )r9   )r  r  r  s     r  r  z=TypeChecker.refine_away_none_in_comparison.<locals>.<genexpr>}  s.      ]]Q29a@@]]]]]]r  c              3  Z   K   | ]&}t          t          |          t                    V  'd S rP  )r  r   r   )r  ots     r  r  z=TypeChecker.refine_away_none_in_comparison.<locals>.<genexpr>  s4      UU:ob118<<UUUUUUr  )r   r  r  r  r   )rM  r+  rq  r  r  non_optional_typesr  r  r  r  r  s             @r  r  z*TypeChecker.refine_away_none_in_comparison`  sT      	/ 	/A"C&s++ /"))#...r! 	Kc*<&=&=]ASAS&S&S 0 E E)!,	*955 ]]]]J\]]]]] E*9)*D*DF8A;'
 UU}UUUUU K3 K KA -a 0I*955 K0?	0J0J!-x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.lenrX  r   )r  r]   r   r  r  rM  r6   rS   r  rM  r  )rM  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 rP  )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 rP  )rM  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  rD  ro  rd  r   r  r   )rM  r  p_typs   `  r  rM  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   rm  r  )rM  r  r  r  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.
        )rb  ra  ra  >   >=<r  rc  <=rd  c              3  F   K   | ]}                     |          d uV  d S rP  )r  r  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 rP  )r  r  s     r  r  z7TypeChecker.find_tuple_len_narrowing.<locals>.<genexpr>  s1      @@Bt++B//@@@@@@r  Nr   rX  )r@  rl  r  rX   rm  r  r  ro  r  r  r  r]   rM  r  rl  r   rN  r  r   rn  )rM  r  chained
last_groupr  rN  rO  r  r  literal_valuestuplesr  r  sizetplrZ  r[  yes_mapno_mapr_sizes   `                   r  rp  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  r  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  rM  r  rN  r   )rM  r  r  r  	yes_typesno_typesother_typesr  ytntrZ  r[  s               r  rN  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.NrX  ra  rb  r  r  r  )min_lenr  r  r   )r   r  r   re  r  r   r   ro  r   r  r+  r  r   r   rd  rM  r  r  r   r  r  rf  )rM  r  r  r  unpack_indexrT  unpackr	  r  r  suffixrZ  r[  r  	yes_itemsr  s                   r  r  z&TypeChecker.refine_tuple_type_with_len?  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  r  rX  r  )r8   lookup_typeinforM  r   r0  enable_incomplete_featurero  rd  r   r   rp  r  r  r   r  r  rf  r  r   )rM  r  r  r  r  r  allow_preciser  r[  r  r  rZ  s               r  r  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  r  r  r  r  r  r  ErrorCode | Noner  c                   d S rP  r  )
rM  r  r  r  r  r  r  r  r  r  s
             r  rw  zTypeChecker.check_subtype  s	     sr  )r  r  c                   d S rP  r  )	rM  r  r  r  r  r  r  r  r  s	            r  rw  zTypeChecker.check_subtype  s	     sr  str | ErrorMessagec                  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          |||          }t          |t                    r,t          |t                    rt!          |||| j                  }|r.|                    dd	                    |          z   d
z             }|                     ||           |D ]$}| j                            |||j                   %|r|                     |||j                   | j                            ||||j                   t          |t                    rW|j        j        rKt          |t0          t          t2          t4          f          r#| j                            ||||j                   t          |t0                    rMt          |t                    r8t9          d||d          }|r#| j                            ||||j                   t          |t0          t<          f          rt          |t                    rk|j        j        r_d|j        j        v rQt9          d||d          }|J t          ||| j                  s#| j                            ||||j                   |                      ||||j                   dS )zBGenerate an error if the subtype is not compatible with supertype.r`  Tr  Fr  N z (z, r  r  r>  )!r   r0  r  r9  r<   r  prefer_simple_messagesrr  r   &try_report_long_tuple_assignment_errorrC   r  rD   r   r?   r   r@   r  r   r  r  maybe_note_concatenate_pos_argsro  r  r   r   r   report_protocol_problemsr   	note_callr   protocol_memberscheck_possible_missing_await)rM  r  r  r  r  r  r  r  r  r  orig_subtypeorig_supertype
extra_infonote_msgsubtype_strsupertype_strr  rQ  s                     r  rw  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'9-- S*Y	2R2R S)%)T\RR 	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  r  r1   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.
        Nr  T)ignore_binder)
r  r   r   rR  r7  r`   r@  r  r   r   )rM  r  r  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  )rH  rQ  s    r  checking_await_setzTypeChecker.checking_await_set(  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  )	rH  r  r  rH  r   rw  r   INCOMPATIBLE_TYPESpossible_missing_await)rM  r  r  r  r  r  r  s          r  r  z(TypeChecker.check_possible_missing_await0  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   ro  r   r   r   from_omitted_genericsr  r  r%  )rM  r  r%  r  r  s        r  rq  zTypeChecker.named_typeA  s     ##D))xdI&& 	$dk844444;#D$)))))9:;;xj3ty/B+C+CCDDDr  rM  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  rJ  z2TypeChecker.named_generic_type.<locals>.<listcomp>W  s!    GGG31#66GGGr  )r  r   )rM  r  rM  r  s       r  rp  zTypeChecker.named_generic_typeP  s:     ##D))GG$GGGd###r  rd  c                l    |                      |          }|j        }t          |t                    sJ |S rP  )r  r  r  r   )rM  rd  r%  r  s       r  r  zTypeChecker.lookup_typeinfo[  s7    ##H--x$)))))r  c                ,    |                      d          S )zReturn instance type 'type'.r  rq  rQ  s    r  r%  zTypeChecker.type_typeb  s    ///r  c                ,    |                      d          S )zReturn instance type 'str'.r_  r  rQ  s    r  r_  zTypeChecker.str_typef  s    ~...r  c                &    || j         d         |<   dS )z)Store the type of a node in the type map.r  N)r  )rM  r  r  s      r  r  zTypeChecker.store_typej  s    $'D!!!r  c                ^    t          fdt          | j                  D                       S )Nc              3      K   | ]}|v V  	d S rP  r  )r  rQ  r  s     r  r  z'TypeChecker.has_type.<locals>.<genexpr>o  s'      @@419@@@@@@r  )r  r  r  r  s    `r  r  zTypeChecker.has_typen  s0    @@@@ht&?&?@@@@@@r  c                P    t          | j                  D ]}||v r
||         c S d S rP  )r  r  )rM  r  rQ  s      r  r  zTypeChecker.lookup_type_or_noneq  s;    $/** 	 	Aqyyw tr  c                    t          | j                  D ]}|                    |          }||c S t          |          rP  )r  r  rn  r@  )rM  r  rQ  r  s       r  r  zTypeChecker.lookup_typew  sI    $/** 	 	AdA} tnnr  ru  c                F    | j         d                             |           d S r  )r  r  )rM  ru  s     r  r  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  rl  )rM  rZ  s     r  r  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.
        r  )r0  r  r(  rQ  s    r  rx  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,  rn  r  r  ry   r?  r@  )rM  r  r1  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          |	                    ||                    )Nr  r   rX  z0Internal error: attempted lookup of unknown namer  r   )builtinsr  z*, e.g. add '[{} fixtures/{}]' to your testr  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.  r  r  r?  rn  r  r  ry   r   r=   rt  r@  )
rM  r  partsr  r  r%  lastrd  
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  r  r  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.
        r  N)r)  r  r  r  r0  allow_untyped_globalsr  r(  rl  rB  r  local_partial_typesis_defined_in_base_classr  ro  r   r   r*  r  r  r  r  r   r  )rM  r  r  r  
permissiver)  r  r  r  
allow_nonefixeds              r  rf  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_scopesro  r   rB  r0  r#  r  r  r  r*  r  r  r  r   )rM  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 rP  )rn  r  )r  r  r  s     r  r  z7TypeChecker.is_defined_in_base_class.<locals>.<genexpr>  sG       /
 /
/3DHHSXd*/
 /
 /
 /
 /
 /
r  rX  )r  r  r  r  )rM  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*  )rM  r  r+  r  r)  s        r  rt  zTypeChecker.find_partial_types"  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).
        NTr  )FFN)
r  r)  r  r0  r$  r  ro  r   r  r  )rM  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.r  r  )rM  r  r  s      r  r  zTypeChecker.temp_nodeG  s    7++++r  r  c                   t          |t                    r)| j                            |j        ||j                   dS | j                            |||           dS )zProduce an error message.r  N)r  r<   r  rr  rm  r  )rM  r  r  r  s       r  rr  zTypeChecker.failK  sY     c<(( 	HMM#)W38M<<<Fc7.....r  offsetc                   t          |t                    r)| j                            |j        ||j                   dS | j                            ||||           dS )zProduce a note.r  N)r7  r  )r  r<   r  r  rm  r  )rM  r  r  r7  r  s        r  r  zTypeChecker.noteT  s[     c<(( 	HMM#)W38M<<<Fc76=====r  r  /Instance | CallableType | TypeType | Overloadedc                   t          |t                    rTt          ||                     d                    }|j        d         }t          t          |          t                    s|S |                     ||          d         S )Nr]  r   rX  )r  r   r8   r  rM  r   r   r>  )rM  r  r  r:  r  s        r  r  zTypeChecker.iterable_item_typeb  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   rq  )rM  r  s     r  r   zTypeChecker.function_typeo  s    T4??3F#G#GHHHr  r  r  c                   || j                                          d S |                                D ]"\  }}| j                             |||           #d S )Nr  )r   r  r  r  )rM  r  r  r  ro  s        r  r  zTypeChecker.push_type_mapr  sj    K##%%%%%&nn.. M M
ddOLLLLM Mr  r]   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.rX  r  )r  r,  rM  r  r   r	  r   r   r   r  r   r  r  r   ro  r$  rq  r-  r  r  convert_to_typetype)rM  r  r  rO  ro  
union_listr  rZ  r[  r  r  s              r  rH  z!TypeChecker.infer_issubclass_mapsy  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 rP  r  rM  r  r@  r  r  s        r  r-  z/TypeChecker.conditional_types_with_intersection  s	     +.#r  c                    d S rP  r  rC  s        r  r-  z/TypeChecker.conditional_types_with_intersection  s	      Cr  c                   t          |||          }|d         }|d         }t          t          |          t                    r|||fS t          |          }t          |t                    r"t          |                                          }	n|g}	g }
|D ]G}t          |j                  }t          |t          t          f          r|

                    |           H|
s||fS g }g }|	D ]}t          |t                    s||fc S |
D ]m}t          |t                    r&|
                    d|j        j         ddf           =|                     ||f|          }|X|
                    |           n|sH|                                 r$|D ]!\  }}| j                            |||           "t                      |fS t#          |          }||fS )Nr   rX  r  z" and "NoneType"z"NoneType" is final)r  r  r   r   r   r   r6  r  r   r   r  ro  r  r  rj  r  impossible_intersectionr   )rM  r  r@  r  r  initial_typesrZ  r[  r  possible_expr_typespossible_target_typesr  r  outr  r  r  intersectionr  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#.- " 	3 	3B"27++D$8 455 3%,,T222$ 	%W$$(*$ 
	) 
	)Aa** )((((* ) )a** MM#Dqv{#D#D#DF["\]]]#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  r   r  rb   r  )rM  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                    r|j        dk    r|                     |j                  }|2t          |j                  rt          t                      d          g}|                     |j                  }|2t          |j                  rt          t                      d          g}||d S ||z   S t          t          |                     |                              }g }|D ]}t          |t                    rX|                                rDt          |j        d         j                  }|                    t          |d                     pt          |t$                    rHd}t          |j        t                    rd}|                    t          |j        |                     t          |t(                    rV|j        j        dk    rFt)          |j        j        d         g           }	|                    t          |	d                     8t          |t(                    rO|j        j        dk    r?|j        r8|                    t          t3          |j                  d                     t          |t4                    r&|                    t          |d                      d S |sd S |S )	Nr  FrA  r   Tr  r  ztypes.UnionType)r  r~   r  r,  rN  r  r!   r   rO  r   flatten_typesr  r   rP  r*   r  r  r  r   r  r   ro  rd  r  rM  r   r   )
rM  r  rN  rO  r  r  r  erased_typerB  r6  s
             r  r,  zTypeChecker.get_isinstance_type  s   dF## 		 3++DI66D|	 : :|!(**UCCCD,,TZ88E}!<!<}"8::eDDDE|u}t%<$]43C3CD3I3I%J%JKK	!# 	 	C#|,, 1B1B  -SYq\-BCCY{5IIIJJJJC**  "&ch11 +%*NYsxOOOPPPPC** sx/@O/S/S&sx|B'7<<Y{4HHHIIIIC** sx/@DU/U/UZ]Zb/UYy':':5QQQRRRR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  rx   r  rz   r  r   r   r   r   rP  r  r  ro  rQ  )rM  r  r  r  s       r  rj  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.Nr  c                <    g | ]}                     |          S r  add_any_attribute_to_type)r  r  r  rM  s     r  rJ  z9TypeChecker.add_any_attribute_to_type.<locals>.<listcomp>R  s)    TTTA66q$??TTTr  )r	  r  c                <    g | ]}                     |          S r  rV  )r  r  r  rM  s     r  rJ  z9TypeChecker.add_any_attribute_to_type.<locals>.<listcomp>W  s)    ___3T;;CFF___r  )r   r   r   r  r  r   copy_with_extra_attrr  mod_namer   rD  r+  r   r  r   r  r  rW  r   r	  r  r   partition_union_by_attrr   )	rM  r  r  r  r  rx  r  	with_attrwithout_attrs	   ` `      r  rW  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[  rW  r   )rM  r  r^  r  r  r]  r  type_with_attrs           r  rI  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 rP  )r  r`  r  )rM  r^  r  r\  r]  r  s         r  r[  z#TypeChecker.partition_union_by_attrt  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|| d|
  
         d d d            n# 1 swxY w Y   |                                 S )NFT)r  r  r  r  r  no_deferralmodule_symbol_table)r   r  r   r   r  rZ  ro  r?  r  rH  r%   r   r   r  r  )rM  r  r  r  re  watchers         r  r`  zTypeChecker.has_valid_attribute  s(   $$eW%% 	5eX&& 	'5+< 	'ARA[ 	'"'*"2"&X##%% 	!!78899!! $7   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ))++++s   7>CCCrV  c                :    | j                             ||          S )Nr  )rR  rm  )rM  r  rV  s      r  get_expression_typezTypeChecker.get_expression_type  s     ''<'HHHr  c                T   t          t                    rj        t          t          t          t
          f          rfj        a| j        j        D ]:}t          |t                    r#t          fd|j        D                       r dS ;|                     |           dS dS dS )zEWarn if deprecated and not directly imported with a `from` statement.Nc              3  :   K   | ]}j         |d          k    V  dS )r   Nr]  )r  r  r  s     r  r  z/TypeChecker.check_deprecated.<locals>.<genexpr>  s.      6\6\QtyAaD7H6\6\6\6\6\6\r  )r  rb   r  rj   r   r   
deprecatedr7  importsrq   r  r?  ra  )rM  r  r  imps    `  r  check_deprecatedzTypeChecker.check_deprecated  s    dI&& 	9DdW&7BCC 	4O'y( 4 4c:.. 36\6\6\6\RUR[6\6\6\3\3\ EE$$T733333	4 	4''r  c                v   t          t                    rj        t          t          t          t
          f          rsj        x}l| j        sgt          fd| j	        j
        D                       sD| j	        j        r| j        j        n| j        j        } |||t          j                   dS dS dS dS dS )zWarn if deprecated.Nc              3  h   K   | ],}j         |k    pj                             | d           V  -dS )r  N)rd  r   )r  r  r  s     r  r  z.TypeChecker.warn_deprecated.<locals>.<genexpr>  sY         "Gdm&>&>!www&G&G     r  r  )r  rb   r  rj   r   r   rk  rE  r  r0  deprecated_calls_excludereport_deprecated_as_noter  r  rr  r  
DEPRECATED)rM  r  r  rk  warns    `   r  ra  zTypeChecker.warn_deprecated  s    dI&& 	9Dtg'8(CDD
	= $/*<) =    >     = %)L$J]48==PTPXP]DDW5+;<<<<<<
	= 
	=<<<<<<r  )selftyperu  c               t   t          |          }t          |t                    rt          |t                    r||j        D ]v}t          |t
                    r[t          |j        j        x}t                    r:||j        st          ||          }||k    r| 
                    |j        |           sdS dS dS )zLWarn if the overload item corresponding to the given callable is deprecated.N)r   r  r   r   r  rb   r  ro  r  r   ra  )rM  r  r  r  ru  r  r3  s          r  warn_deprecated_overload_itemz)TypeChecker.warn_deprecated_overload_item  s     !((d-.. 	A:fl3S3S 	A
 A AdI.. A:!%/I4 4 A  +DN+$-i$B$B	 F**,,TY@@@	A 	A 	A 	AA Ar  r   c                   d S rP  r  r  s     r  visit_pass_stmtzTypeChecker.visit_pass_stmt      tr  r|   c                   d S rP  r  r  s     r  visit_nonlocal_declzTypeChecker.visit_nonlocal_decl  rz  r  rl   c                   d S rP  r  r  s     r  visit_global_declzTypeChecker.visit_global_decl  rz  r  )r  r0   r.  r-  r0  r   r7  ry   r8  r9  r6  r   r:  r;  r<  r=  )r<  r!  )r<  rT  r<  r=  rP  )r|  r}  r<  r  )r  r  r<  r=  )r  ry   r<  r=  )r  r  r  r  r<  r=  )r  r9  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   r6  r  r<  r  r  )rC  r   r6  r  r<  r   )rC  r   r<  r   )r  rj   r<  r=  )NNF)
r  rk   rc  r   r  rd  r  r  r<  r=  )r  rj   rb  r   r<  r=  )r<  r|  F)
r  rk   r  r   r  rd  r  r  r<  r=  )r  r   r  r  r<  r  )r  r   r<  r=  )r  rk   r  r  r<  r=  )r  r9  r<  r  )r  rk   r<  r=  )r  rj   r  r   r<  r=  )
r  rk   r*  r   r+  r9  r  r`   r<  r=  )r*  r   r+  r9  rA  r   r@  r   r<  r9  r?  r   r  r`   r<  r=  )rF  r   r?  r   r*  r   r<  r  )r  ri   r<  r=  )r  r   r  r`   r  r9  r<  r=  )r  r   r  r`   r<  r=  )r  r   r  r   r  r`   r<  r=  )r  rk   r  r   r<  rh  )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  r9  r  r   r<  r  )r  r   r<  r  )r  r   r  r   r  r9  r  r9  r  r9  r  r  r  r  r  r`   r<  r  )r  rk   r<  r=  )r  r^   r<  r=  )r  r   r<  r=  )rw  r  r<  r=  )r  r^   r  r   r<  r=  )r%  r   r<  r  )r%  r   r<  r  )
r  r9  rK  r   rE  r   r  r   r<  r=  )r  rq   r<  r=  )r  ro   r<  r=  )r  rn   r<  r=  )r  rp   r<  r=  )r1  rZ   r<  r=  )r<  r  )ru  r   r<  r  ru  rY   r<  r=  )TF)
rq  rv   rn  re   r  r  r  r  r<  r=  )r  r   rn  re   r<  r  )rq  rv   rn  re   r  r9  r<  r=  )rq  r   rn  re   r<  r=  )r  r   rn  re   r  r   r  r   r  r{   r  r  r<  r  )r  r9  r  r   r  r`   r  r  r  r  r4  r  r<  r  )r  r   r  r   r  r  r<  r  )r  r9  r  r  r  r`   r<  r=  )r  r  r<  r|  )ru  r  r<  r=  )rq  rv   r<  r=  )rq  rv   r  r  r<  r  )r  r  r<  r  T)
rl  r  rn  re   r  r`   r  r  r<  r=  )
rl  r  r-  r  r  r`   r.  r/  r<  r  )TNF)rl  r  rn  re   r  r`   r  r  r<  r  r=  r  r<  r=  )rl  r  rn  re   r  r   r  r`   r  r  r<  r=  )rl  r  r<  r  )rl  r  rn  re   r  r   r  r`   r=  r  r  r  r<  r=  )rl  r  r  r   r<  r   )r  rd  r$  r  re  r  r<  rf  )ro  r   r<  r  )
rl  r  r  r   r  r`   r  r  r<  r=  )rq  rv   rn  r  r<  rp  )ru  rv   r<  r  )
r  r   rq  rv   r{  r   r  r`   r<  r=  )r  r   rq  rv   r{  r   r<  r  )r  r   r<  r  )r  r   rq  rv   ro  r   r<  r=  ro  r   r<  r   )rn  re   r<  r  )rr  r  rn  re   r  r`   r  r<   r  r9  r  r9  r  r  rq  r  r  r  r<  r  )r  rz   r<  r  )rq  rx   r  r   r  r   rn  re   r  r`   r<  r  )rq  rr   rn  re   r  r`   r<  r=  )r  r   rb  r   r)  r  r<  r=  )rq  rr   rn  re   r<  r=  )r  r   r<  r  )ru  rf   r<  r=  )ru  r   r<  r=  )ru  rm   r<  r=  )ru  r   r<  r=  )ru  r}   r<  r=  )ru  rW   r<  r=  )ru  r   r<  r=  )r2  re   ru  r   r  r  r<  r=  )ru  r   r<  r=  )ru  r   r  r  r<  r=  )r  re   r  r  r<  r   )r  r  r<  r   )r  r"  r<  r   )r  r   r  re   r<  r&  ru  rh   r<  r=  )r  re   r<  r2  )ro  r   r  r`   r<  r2  )r  re   r<  r  )r  r   r<  r  )
r  re   r  r   r  r  r  r`   r<  r=  )ru  rc   r<  r=  )r2  rb   r<  r=  FF)r2  rb   r  r  r  r  r<  r=  )r  rj   ra  r   rb  re   r<  r=  ru  r   r<  r=  )r  r   r  rj   r<  r=  )r  re   r  r  r  r  r<  r   )ru  r[   r<  r=  )ru  ra   r<  r=  )ru  rw   r<  r=  )r  re   r  r   r<  r  )r  r  r<  r  )r  r  r  r  r<  r=  )r  r   r<  r=  )
r  r9  r  r9  r  r9  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  re   r  r  r<  r  )r  r   r  r   r<  r  )r  r   r  re   r<  r=  )r  r_   r   r!  r<  r  )r  re   r8  r  r<  r  )r  r_   r<  r  )r  r_   rv  r9  r+  r  rq  r&  r   r!  rr  ry  r<  r  )r  r  r<  r  )r  re   r  r   r<  r  )r+  r  rq  r&  r  r!  r  r  r  r  r  r  r<  r  )
r+  r  rq  r&  r  r!  r  r  r<  r  )r  re   r<  r  )r  re   r<  r/  )r  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  r<  r  r  )r  r   r  r   r  r`   r  r9  r  rd  r  rd  r  r  r  r  r  r}  r<  r  )r  r   r  r   r  r`   r  r<   r  rd  r  rd  r  r  r  r}  r<  r  )r  r   r  r   r  r`   r  r  r  rd  r  rd  r  r  r  r  r  r}  r<  r  )r  r   r  r1   r<  r  )
r  r   r  r   r  r`   r  r  r<  r=  )r  r9  r<  r   )r  r9  rM  r&  r<  r   )rd  r9  r<  r   )r<  r   )r  re   r  r   r<  r=  )r  re   r<  r  )r  re   r<  r  )r  re   r<  r   )ru  r  r<  r=  )r<  r  )r  r9  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<  r1  )r  r   r  r}  r<  r   )r  r  r  r`   r  r  r<  r=  rs  )
r  r  r  r`   r7  r  r  r  r<  r=  )r  r9  r  r`   r<  r   )r  ri   r<  r   )r  r  r  r  r<  r=  )r  r]   r  re   r<  r  )
r  r   r@  rA  r  r`   r  r=  r<  r  )
r  r   r@  rA  r  r`   r  r   r<  r2  )
r  r   r@  rA  r  r`   r  r  r<  r  )r  r{   r<  r  )r  re   r<  rA  r  re   r<  r  )r  r   r  r9  r<  r   )r  re   r^  r   r  r9  r<  r  )r^  r   r  r9  r<  r  )r  r   r  r9  r<  r  )r  re   rV  r  r<  r   )r  r  r  r`   r<  r=  )
r  r  r  r`   r  r   ru  r  r<  r=  )r  r   r<  r=  )r  r|   r<  r=  )r  rl   r<  r=  )r
  r  r  __doc__rC  r  rA  r   r  rB  rE  r1  r2  rN  propertyrR  rV  r[  r{  r  r  r  r  r  rm  r  r  r  r  r  r=  rB  rH  rP  rT  rW  rZ  rY  r  ra  r   rj  rk  r  r  r  r  r  r  r  r  r:  rC  r  r  r  ra  rg  r  r  r  r  r  r  r  r  rm  r  r  r  r  r  r!  r)  r#  r$  r  r  rJ  rC  r  rc  rf  ri  rb  rv  rj  rk  r  r  r  r  r  r  r  r  r  r  r  r  r  r~  r  r  r  r
  r  r  r  r@  r  r?  rV  r  r  rB  rm  ru  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  r1  r,  r-  r>  r?  rJ  r)  rS  r  r  r[  r]  rq  r\  rm  rn  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r6  r  r:  rK  rm  r  r  r  r  r  rM  r  rp  rN  r  r  r   rw  r   r  r  rq  rp  r  r%  r_  r  r  r  r  r  r  rx  r  r  rf  r,  r%  rt  r*  r  rr  r  r  r   r  rH  r-  r  r,  rj  rW  rI  r[  r`  rh  rn  ra  rw  ry  r|  r~  r  r  r  r  r    s@          GNNN -,,, "!!!3333####MMM))))       '&&&H!I " 8<;;;;"""""  )((( NNN?T ?T ?T ?TB " " " X" / / / X/- - - -&* * * *Z OS" " " " "H" " " ". . . .H H H H: : : : _ _ _ _ '+G'
 -137G' G' G' G' G' G'Z2 2 2 2]5 ]5 ]5 ]5~5 5 5 5nR R R RX^ ^ ^ ^&$ $ $ $ 3  3  3  3D! ! ! !F# # # #   >' ' ' '9 9 9 9( .2!!J !J !J !J !JF) ) ) )V 2 2 2 ^2 X]|% |% |% |% |%|	   *   (! ! ! !F; ; ; ;> > > >)M )M )M )MV! ! ! !FV
 V
 V
 V
p>N >N >N >N@2
 2
 2
 2
hM M M M4N N N N:W W W W"
 
 
 
    ! ! ! !8 #'	    .) ) ) )@'! '! '! '!R[ [ [ [j j j jXI I I I a a a aF3 3 3 32H) H) H) H)TS S S S. . . .`U U U U" " " ":X X X X; ; ; ;0   8   8#V #V #V #VJE E E E,   .VR VR VR VRp
 
 
 
:                        ,   "
 
 
 
   83A 3A 3A 3Aj3 3 3 3 #' tD tD tD tD tDn *@AV(WWWWW! ! ! !F.' .' .' .'`Z Z Z Zx! ! ! !R ")-:) :) :) :) :) :)x      := = = =(" " " " ) ) ) ^)4Q 4Q 4Q 4Ql
 
 
 
>   &   H #'RU RU RU RU RUr %)" " " " "R #'#!&4 4 4 4 4lA& A& A& A&F	 	 	 	$ #'[ [ [ [ [z$M $M $M $ML! ! ! ! 
 
 
 
 #'= = = = =4 ;?-3 -3 -3 -3 -3^    (F (F (F (FT* * * *X   2+ + + +.6 6 6 6( ( ( (   0 -M%'k, #'$(#k, k, k, k, k, k,Z
 
 
 
%O %O %O %ON& & & &4; ; ; ;%P %P %P %PN   "
0 
0 
0 
0" " " "
eI eI eI eIN- - - -2F F F F       D% % % %" " " "    :, , , ,B,) ,) ,) ,)\'0 '0 '0 '0R   4 4 4 4   "
 
 
 
"	# 	# 	# 	#" " " "& & & &&   6   ,\ \ \ \   $& & & & PUE6 E6 E6 E6 E6N	K 	K 	K 	K[ [ [ [#  #  #  # J; ; ; ;
 
 
 
&   # # # #   \ \ \ \|       * * * *X+ + + +. . . .   ,`d `d `d `dD? ? ? ?8D D D DT  T  T  T l   8%
 %
 %
 %
N
 
 
 
 4Z 4Z 4Z 4ZlN  N  N  N b ?C( ( ( ( ( (: ?Cr  r  r  r  r  r hq_ q_ q_ q_fE  E  E  E N) ) ) )VuV uV uV uVnH: H: H: H:T)  )  )  ) VE E E E   ,! ! ! ! [ [ [ [z ?  ?  ?  ?DB% B% B% B%H% % % %H  %)&* #'!%(,     X  %)&* #'(,     X& %)&*L #'!%(,L L L L L L\   , 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 KO M M M M M M   <  . . . . X.       X   $6' 6' 6' 6' 6'p
 
 
 
) ) ) )V$
 $
 $
 $
L   B   2
' 
' 
' 
', , , ,2I I I I I4 4 4 4= = = =" ]aA A A A A A"           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.r<  r=  c                ,    t                      | _        d S rP  )r@  r  rQ  s    r  rN  zCollectArgTypeVarTypes.__init__  s    +.55r  r  r   c                :    | j                             |           d S rP  )r  r  r  s     r  visit_type_varz%CollectArgTypeVarTypes.visit_type_var  s    1r  Nr  )r  r   r<  r=  )r
  r  r  r  rN  r  r  r  r  r  r    sB        ::1 1 1 1     r  r  r  r   proposed_type_rangesrA  r  r=  r<  r  c                    d S rP  r  r  r  r  s      r  r  r    s	     '*cr  r2  c                    d S rP  r  r  s      r  r  r    s	     r  r  c                   |rVt          |          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          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.rX  r   c                    g | ]	}|j         
S r  r  r  
type_ranges     r  rJ  z%conditional_types.<locals>.<listcomp>  s    QQQj*/QQQr  c              3  $   K   | ]}|j         V  d S rP  rA  r  s     r  r  z$conditional_types.<locals>.<genexpr>  s6       
 
*4J%
 
 
 
 
 
r  Tr  c                *    g | ]}|j         	|j        S r  )rB  r  r  s     r  rJ  z%conditional_types.<locals>.<listcomp>  s3       "%4O  r  )r  r  r   r  r   r  rm  r  r  ro  rd  r   r   r   r  r   r   r:   r   r  r   )	r  r  r  r  	enum_nameproposed_itemsproposed_typeproposed_precise_typeremaining_types	            r  r  r    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----%lMUYZZZ 	1"$$g-- %.$8 &:  % %! 3<AVWWN .00 W$$r  r  re   rZ  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 rP  )rG  r   r  r   r  r   rG  r  )r  rZ  r[  mapsr  r  s         r  r  r    s     4  DD'" % %%c**k?33 	%KK KKOOOO???KKs$$$$gw&'t555r  r  r9  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.rX  )r9  )r  r  r  s      r  r  r  .  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  rs   rm  r  s    r  rF  rF  8  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  rz   rd  r  s    r  r  r  B  s    a""Dqz_'DDr  c                B    t          | t                    o
| j        dk    S )Nzbuiltins.NotImplementedr  r  s    r  r  r  G  s    a""Nqz5N'NNr  r  rk   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   NrX  )r  r  r  r   r  r  rf   r   )r  r  ret_stmt	expr_stmt
yield_exprs        r  r  r  K  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  r  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__.
    )r  r  r  r  zbuiltins.frozensetz_collections_abc.dict_keysztyping.KeysViewNr   r  c              3  B   K   | ]}t          |t                     V  d S rP  )r  r   r  s     r  r  z$builtin_item_type.<locals>.<genexpr>  s/      XXr:b'***XXXXXXr  ztyping.MappingFz2No Mapping base class found for TypedDict fallback)r   r  r   ro  rd  rM  r   r   r  r   r   r	  r  r5  r   r   r   r  r  r8   )r  normalized_itemsr  r	  r  s        r  rn  rn  [  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m2ri  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  )r7   )r  n2s     r  r  z'and_conditional_maps.<locals>.<setcomp>  s     ---B|B---r  )copyr7   r  r   r   r;   )r  r  ri  rx  m2_keysn1r  s          r  rL  rL    s     
zRZt WWYYF--"---G    7**jB9P9PRY.Z.Z*BF2J <  	< 	<B < <##|B'7'777!+BrFBrF!;!;F2J< Mr  rC  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.
    )r7   r  r   r   r   )r  r  rC  rx  r  r  s         r  r  r    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  r  r  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   rX  Nrh  )r  rL  r  )r  ri  final_if_mapfinal_else_mapr  r  s         r  ro  ro    s    4 9~~2v	Y1		|'0|$n )!"" 	K 	KFH/fxXXXL0JJNN^++r  r  c                   i }| d S |                                  D ]m\  }}|}t          |t                    r|j        }t          t	          |          t
          t          t          f          si c S t          j	        |          ||<   n|S rP  )
r  r  r   r	  r   r   r   r   r   r  )r  converted_type_mapr  r  r  s        r  r>  r>    s    13t^^%% A A	ca%% 	A/!,,y(H.MNN 	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  )rP  r  ar1  s      r  rJ  zflatten.<locals>.<listcomp>   s+    777aGAJJ77q7777r  )r  r   ru   r  r   rP  r  r  s    r  rP  rP     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  )rQ  r  s      r  rJ  z!flatten_types.<locals>.<listcomp>   s-    ===aM!,<,<==q====r  r  r   )r   r  r   r  r   rM  r  s    r  rQ  rQ     s_    A!Y ==17====	1.	/	/ q	{s
r  r  r  dict[TypeVarId, Type]c                |    t          |          }|                    |           }t          |t                    sJ |S rP  )TypeTransformVisitorr  r  rk   )r  r  visitorr  s       r  ru  ru     s;    "3''G
,,t

Cc8$$$$$Jr  c                  (     e Zd Zd	 fdZd
dZ xZS )r  r  r  r<  r=  c                V    t                                                       || _        d S rP  )superrN  r  )rM  r  	__class__s     r  rN  zTypeTransformVisitor.__init__    s$    r  ro  r   c                ,    t          || j                  S rP  )r3   r  rl  s     r  ro  zTypeTransformVisitor.type$   s    4***r  )r  r  r<  r=  r  )r
  r  r  rN  ro  __classcell__r  s   @r  r  r     sQ             + + + + + + + +r  r  r   ru  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  ru  r  max_argss       r  r!  r!  (   sG    1:qz**H11133Q5S5S5U5UVVHxr  r  list[CallableType]c                   | j         D ]\}|j                                        rAt          | |j        |j        i                              d | j         D                       }  n]|                                 s| gS g }| j         D ]T}t          |t                    r"|j	        r|
                    |j	                   9|
                    |j        g           Ug }t          j        | D ]X}d t          | j         |          D             }|
                    t          | |                              g                      Y|S )zRExpand a generic callable using all combinations of type variables' values/bounds.c                D    g | ]}|j                                         |S r  )rl  is_selfr  s     r  rJ  z,expand_callable_variants.<locals>.<listcomp>6   s'    HHHH1HHHr  r  c                $    i | ]\  }}|j         |S r  rk  )r  r  rv  s      r  
<dictcomp>z,expand_callable_variants.<locals>.<dictcomp>G   s     RRR[b%BE5RRRr  )r(  rl  r  r3   r	  r+  
is_genericr  r   r  r  rr  rs  r  )r  r  tvar_valuesrn  variantscombinationtvar_maps          r  expand_callable_variantsr  /   sf   k   5==?? 	Ar~677EEHHakHHH F  A E		 <<>> s
K 3 3dK(( 	3T[ 	3t{++++ 012222H (+6 N NRRC[4Q4QRRRAx00>>>LLMMMMOr  Tr  r  class_type_varslist[TypeVarLikeType]rH  c                x   t          | |          } t          ||          }t          |           D ]}t          |          D ]w}t          |j        |j                  rt	          ||t
          ddd d          st	          ||t
          ddd d          sV|rt	          ||t          dddd          s  dS xdS )aS  Check if two overloaded signatures are unsafely overlapping or partially overlapping.

    We consider two functions 's' and 't' to be unsafely overlapping if three
    conditions hold:

    1.  s's parameters are partially overlapping with t's. i.e. there are calls that are
        valid for both signatures.
    2.  for these common calls, some of t's parameters types are wider that s's.
    3.  s's return type is NOT a subset of t's.

    Note that we use subset rather than subtype relationship in these checks because:
    * Overload selection happens at runtime, not statically.
    * This results in more lenient behavior.
    This can cause false negatives (e.g. if overloaded function returns an externally
    visible attribute with invariant type), but such situations are rare. In general,
    overloads in Python are generally unsafe, so we intentionally try to avoid giving
    non-actionable errors (see more details in comments below).

    Assumes that 'signature' appears earlier in the list of overload
    alternatives then 'other' and that their argument counts are overlapping.
    Fc                $    t          | |           S rP  is_subset_no_promoter  s     r  r  z;is_unsafe_overlapping_overload_signatures.<locals>.<lambda>}       6J1a6P6P2P r  T)r  check_args_covariantlyr   r  allow_partial_overlapc                $    t          ||            S rP  r  r  s     r  r  z;is_unsafe_overlapping_overload_signatures.<locals>.<lambda>   r  r  )r  r  r   r  r  )detach_callabler  r  r  r   !is_overlapping_types_for_overload)r  r  r  rH  sig_variantother_variants         r  r&  r&  L   s'   <  	?;;IE?33E
 0	:: ' '5e<< &	 &	M $K$8-:PQQ &!?+0&+%P%P*.   *!?+/&+%P%P*.  (    	'=.'+"'"&*( ( ( 	 ttt	;&	N 5r  r  c                `    |s| S |                      t          | j                  |z             S )aw  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.
    r  )r+  ro  r(  )r  r  s     r  r  r     s7      
tCM':':_'LMMMr  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  )rl  r   )r  rn  s     r  r  z,overload_can_never_match.<locals>.<dictcomp>   s%    ___4DG8>>___r  Tr  )r3   r(  r   r   )r  r  exp_signatures      r  r"  r"     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 rP  )is_same_arg_prefix)r  r  itemts      r  r  z-is_more_general_arg_prefix.<locals>.<genexpr>   sB        9E&ue44     r  F)	r  r   r   r   r   r  r  r5  r  r  ru  s     r  rR  rR     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  r  r  )r   r   r  s     r  r  r     s.    !		#!   r  rv  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  rv  rT  existing_methods       r  r  r     s     #

C!(+FO#x   O.sFHEE	C	'	' O.s|VXNN"_$$&=r  r  r   rT  rd  c                t    |t           j        v r)d|dd          z   }| j                            |          r|S d S )N__ir   )r   ops_with_inplace_methodro  r3  )r  rT  rv  inplace_methods       r  r  r     sE    9444+9((88 	"!!4r  r0  r   r  r}  c                    t          |           }t          |t                    r|p	| o|j        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  r   rm  InvalidInferredTypes)r  r0  r  r}  r  s        r  r  r     so     "#&&K+x(( 
 ['7#7#ZG<Z[	K	1	1 uzz.001111r  c                  D     e Zd ZdZd fdZddZdd
ZddZddZ xZ	S )r	  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.
    r<  r=  c                T    t                                          t                     d S rP  )r  rN  r   )rM  r  s    r  rN  zInvalidInferredTypes.__init__!  s!    &&&&&r  r  r   r  c                    |j         S rP  )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 rP  )rl  is_meta_varr  s     r  r  z#InvalidInferredTypes.visit_type_var$!  s     t!!!r  r   c               6    |                      |j                  S rP  )query_typesr  r  s     r  visit_tuple_typez%InvalidInferredTypes.visit_tuple_type)!  s    (((r  r  )r  r   r<  r  )r  r   r<  r  )r  r   r<  r  )r  r   r<  r  )
r
  r  r  r  rN  r	  r	  r  r	  r  r  s   @r  r	  r	  !  s         ' ' ' ' ' '      " " " "
) ) ) ) ) ) ) )r  r	  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   r<  r   c                F    |j         rt          t          j                  S |S rP  )r  r   r   rL  r  s     r  r	  z&SetNothingToAny.visit_uninhabited_type1!  s"    ; 	19/000r  r   c                R     |                      fd|j        D                       S )Nc                :    g | ]}|                               S r  )rm  )r  r  rM  s     r  rJ  z9SetNothingToAny.visit_type_alias_type.<locals>.<listcomp>9!  s#    $D$D$DQXXd^^$D$D$Dr  )rM  )r+  rM  r  s   ` r  visit_type_alias_typez%SetNothingToAny.visit_type_alias_type6!  s0     $D$D$D$DQV$D$D$DEEEr  N)r  r   r<  r   )r  r   r<  r   )r
  r  r  r  r	  r	  r  r  r  r  r  .!  sH        \\   
F F F F F Fr  r  r  r  r  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  rj   r  r   r  r  s    r  r  r  <!  s>     $   ~$ $##4r  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()'.
    r<  r=  c                0    i | _         i | _        i | _        d S rP  )
_key_to_id_id_to_parent_id_root_id_to_valuesrQ  s    r  rN  zDisjointDict.__init__g!  s#    +-
 13 ;=r  r  	set[TKey]r  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  rM  s     r  rJ  z,DisjointDict.add_mapping.<locals>.<listcomp>|!  s'    KKKs55c::KKKr  r   rX  )r	  r  r	  rl  )rM  r  r  subtree_rootsnew_rootroot_valuessubtree_roots   `      r  add_mappingzDisjointDict.add_mappings!  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@  r  r  r  r	  )rM  root_id_to_keysr  root_idr  r  s         r  r  zDisjointDict.items!  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 rP  )r	  r(	  r  r	  r@  r	  )rM  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 rP  )r	  r	  )rM  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	  r  r	  r<  r=  )r<  r&	  )r  r	  r<  r  )	r
  r  r  r  rN  r%	  r  r 	  r(	  r  r  r  r	  r	  L!  s         4
= 
= 
= 
=J J J J(           r  r	  pairwise_comparisons,Iterable[tuple[str, Expression, Expression]]operand_to_literal_hashMapping[int, Key]operators_to_groupr3  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 ComparisonExpr'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  r  s     r  r  z-group_comparison_operands.<locals>.<dictcomp>!  s    0a0a0a\^^0a0a0ar  NrX  c                    | d         d         S ri  r  r  s    r  r  z+group_comparison_operands.<locals>.<lambda>"  s    471: r  )r  )	r@  r  r  r  r%	  r  rn  r  sort)r/	  r1	  r3	  groupsrt  last_operatorcurrent_indicescurrent_hashesr  rv  	left_expr
right_expr	left_hash
right_hashdisjoint_dictr  indicess                    r  rk  rk  !  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  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 rP  )r  r   r  r   r  r  s     r  r  z$is_typed_callable.<locals>.<genexpr>"  sO         	1gI1=I4I#I     r  )r   r  r   r5  r   r  r  )r  s    r  re  re  "  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 rP  rf  re  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 rP  rG	  re  s     r  r  z'is_untyped_decorator.<locals>.<genexpr>."  s+      DD$'--DDDDDDr  )r   r  r   re  r   ro  r  rb   rf  r  r  r   r  r  )r  rT  s     r  rf  rf  "  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  rb   r  r  ri   ro  )r  s    r  r  r  2"  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  rb   r  r  r   r  r  s    r  r  r  :"  sj    $	"" %y$$$)** 2: 	2*TZ]I>> 	2:a=%115r  SymbolNode | NoneTypeGuard[OverloadedFuncDef]c                    t          | t                    r>| j        r7t          | j        d         t                    r| j        d         j        j        S dS rL	  )r  r   r  rb   r  r  rM	  s    r  r  r  C"  sM    $)** 2: 	2*TZ]I>> 	2:a=%115r  c                   | dS t          |           sdS | j        d         }t          |t                    sJ |j        j         sdS |j        }|j        !|j        t          |j        t                    rdS |j        j        d         }t          t          |          t                    rdS t          t          t          |j                            |           S )zCheck if a node is a settable property with a non-trivial setter type.

    By non-trivial here we mean that it is known (i.e. definition was already type
    checked), it is not Any, and it is different from the property getter type.
    NFr   rX  )r  r  r  rb   r  ro  r  r   r  r   r   r   get_property_type)r  rO  r  r  s       r  r  r  J"  s     |u%% uAJj),,,,,>. u
.C
x3?2j;6W6W2u/+A.K/+..88 u-och.G.GHH+VVVVr  r   c                    t          | t                    rt          | j                  S t          | t                    rt          | j        d         j                  S | S )Nr   )r  r   r   r  r   r  r  s    r  rR	  rR	  b"  sR    !\"" +qz***!Z   4qwqz2333Hr  rN  rO  c                (    t          | |dd          S )NT)r  always_covariantr  rM  s     r  r  r  j"  s    dETDQQQQr  c                *    t          | |ddd          S )NT)r  prohibit_none_typevar_overlapoverlap_for_overloads)r:   rM  s     r  r  r  n"  s*      &*"   r  	node_namec                X    |                      d          o|                     d           S )z-Check if node is private to class definition.rP  )r   endswith)rY	  s    r  r  r  "  s,    %%Fi.@.@.F.F*FFr  c                N    t          |           }|d uot          |          dk    S )NrX  )r   r  )r  strss     r  rd  rd  "  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.rl  Tc              3  6   K   | ]}t          |d           V  dS )rl  N)r   re  s     r  r  z has_bool_item.<locals>.<genexpr>"  s-      RR$T?;;RRRRRRr  F)r   r  r   r  r  r  s    r  r  r  "  sP    o.. t#y!! SRR	RRRRRR5r  r2  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  rX   r  )r2  s    r  rG  rG  "  s"     !^$$ xHr  r{   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.
    )VarAssignVisitorrm  	last_line)r  r  r  s      r  r  r  "  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 )%rb	  r  r   r<  r=  c                0    d| _         d| _        || _        d S )Nr  F)rc	  rq  var_node)rM  r  s     r  rN  zVarAssignVisitor.__init__"  s    r  ru  rY   c                `    d| _         |j        D ]}|                    |            d| _         d S r  )rq  rl  rm  rM  ru  r  s      r  r  z&VarAssignVisitor.visit_assignment_stmt"  s6    ) 	 	BIIdOOOOr  r2  rz   c                v    | j         r/|j        | j        u r#t          | j        |j                  | _        d S d S d S rP  )rq  r  rf	  r  rc	  r  rM  r2  s     r  visit_name_exprz VarAssignVisitor.visit_name_expr"  sC    ; 	916T]22 88DNNN	9 	922r  rx   c                t    | j         }d| _         t                                          |           || _         d S r  )rq  r  r  rM  r2  
old_lvaluer  s      r  r  z"VarAssignVisitor.visit_member_expr"  s4    [
!!!$$$ r  rr   c                t    | j         }d| _         t                                          |           || _         d S r  )rq  r  visit_index_exprrm	  s      r  rp	  z!VarAssignVisitor.visit_index_expr"  s4    [
  ### r  r   c                    d| _         |j        D ]}||                    |            d| _         |j                            |            d S r  )rq  r  rm  r  rh	  s      r  rq  z VarAssignVisitor.visit_with_stmt"  sP    ( 	  	 B~		$	dr  rh   c                    d| _         |j                            |            d| _         |j                            |            |j        r|j                            |            d S d S r  )rq  r  rm  r  r  r  s     r  r1  zVarAssignVisitor.visit_for_stmt"  sh    	t	d; 	%Kt$$$$$	% 	%r  rX   c                    d| _         |j                            |            d| _         |j                            |            d S r  )rq  r  rm  rm  rj	  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  )r  rm  r  rq  rM  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  )capturerq  rm  rv	  s     r  visit_starred_patternz&VarAssignVisitor.visit_starred_pattern"  s:    9 DKIT"""DKKK ! r  )r  r   r<  r=  r  )r2  rz   r<  r=  )r2  rx   r<  r=  )r2  rr   r<  r=  r  r  )r2  rX   r<  r=  )r  r   r<  r=  )r  r   r<  r=  )r
  r  r  rN  r  rk	  r  rp	  rq  r1  rt	  rw	  rz	  r  r  s   @r  rb	  rb	  "  s           
   9 9 9 9! ! ! ! ! !! ! ! ! ! !   % % % %                         r  rb	  r  c                B    t          t          |                     dk    S )ar  Do types have IntEnum/StrEnum types that are potentially overlapping with other types?

    If True, we shouldn't attempt type narrowing based on enum values, as it gets
    too ambiguous.

    For example, return True if there's an 'int' type together with an IntEnum literal.
    However, IntEnum together with a literal of the same IntEnum type is not ambiguous.
    rX  )r  _ambiguous_enum_variants)r  s    r  r  r  "  s      '..//!33r  c                   t                      }| D ]q}t          |          }t          |t                    r(|                    t          |j                             Ot          |t                    r|j        r)|                    t          |j        g                     |j	        j
        rCt          d |j	        j        D                       r |                    |j	        j                   |j	        j
        s|                    d           t          |t                    r*|                    t          |j        g                     Et          |t"                    r\|                    d           s|S )Nc              3  (   K   | ]}|j         d v V  dS ))zenum.IntEnumzenum.StrEnumN)rd  )r  r  s     r  r  z+_ambiguous_enum_variants.<locals>.<genexpr>#  s;       ( (FJ!AA( ( ( ( ( (r  z<other>)r@  r   r  r   r  r|	  r  r   last_known_valuero  r  r  r  r  rd  r   r  r   )r  rx  r  s      r  r|	  r|	  "  sv   UUF " "Aa## 	"MM217;;<<<<8$$ 	"! 	&68J7KLLMMMM &C ( (NOfj( ( ( % % & 

16?++++V^ & 

9%%%;'' 	"MM2AJ<@@AAAA8$$ 	"JJy!!!!Mr  rr  c                R    | dS t          |           }t          |t                    S r  )r   r  r   )rr  lvalue_propers     r  r  r  #  s*    u#K00Mm]333r  c                    t          | t                    r| j         S t          | t                    r| j        j         S t          | t
                    S rP  )r  r   r  rb   r  rj   r	  s    r  r  r  #  sQ    $)** $###$	"" (8'''dG$$$r  rP  )r  r   r  rA  r  r=  r<  r  )r  r   r  rA  r  r   r<  r2  )r  r   r  rA  r  r  r<  r  )r  re   rZ  r  r[  r  r<  r  )r  r9  r  r   r<  r9  r  )r  rk   r<  r  )r  r   r<  r  r  )r  r  r  r  ri  r  r<  r  )r  r  r  r  rC  r  r<  r  )r  r  ri  r  r<  r  )r  r  r<  r  )r  re   r<  r  )r  r   r<  r&  )r  rk   r  r  r<  rk   )r  r   ru  r   r<  r  )r  r   r<  r  r  )
r  r   r  r   r  r  rH  r  r<  r  )r  r   r  r  r<  r   )r  r   r  r   r<  r  )r  r   ru  r   r<  r  )r  r   rv  r9  r<  r  )r  r   rT  r9  rv  r9  r<  rd  r  )
r  r   r0  r   r  r  r}  r  r<  r  )r  r  r<  r  )r/	  r0	  r1	  r2	  r3	  r3  r<  r4	  )r  r  r<  r  )r  r  r<  r  )r  rI	  r<  r  )r  r   r<  r  )r  rN	  r<  rO	  )r  rN	  r<  r  )r  r   r<  r   )rN  r   rO  r   r<  r  )rY	  r9  r<  r  r  )r  r   r<  r  )r2  re   r<  re   )r  r{   r  r   r<  r  )r  r&  r<  r  )r  r&  r<  r3  )rr  r  r<  r  )r  rN	  r<  r  (n  r  
__future__r   rr  collectionsr   collections.abcr   r   r   r   r	   AbstractSet
contextlibr
   r   r  r   r   r   r   r   r   r   r   r   typing_extensionsr   r  r   mypy.checkexprrJ  r   r  r   r   r   r   mypy.binderr   r   r   mypy.checker_sharedr   r    r!   mypy.checkmemberr"   r#   r$   r%   r&   mypy.checkpatternr'   mypy.constraintsr(   mypy.erasetyper)   r*   r+   mypy.errorcodesr,   r-   r.   r/   mypy.errorsr0   r1   r2   mypy.expandtyper3   mypy.literalsr4   r5   r6   r7   mypy.maptyper8   	mypy.meetr9   r:   r;   mypy.message_registryr<   mypy.messagesr=   r>   r?   r@   rA   rB   rC   rD   rE   mypy.mrorF   rG   
mypy.nodesrH   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   r   r   r   r   r   r   mypy.operatorsr   r   r   mypy.optionsr   r   mypy.patternsr   r   mypy.pluginr   mypy.pluginsr   rn  
mypy.scoper   mypy.semanalr   r   r   mypy.semanal_enumr   r   mypy.sharedparser   
mypy.stater   mypy.subtypesr   r   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   
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  rt  r  r  r  r  r  r  r  rF  r{  r  r  r  rn  rL  r  ro  r>  rP  rQ  ru  r  r!  r  r&  r  r"  rR  r  r  r  r  r	  r  r  r	  r	  r	  rk  re  rf  r  r  r  r  rR	  r  r  r  rd  r  rG  r  rb	  r  r|	  r  r  r  r  r  <module>r	     sR      " " " " " "     # # # # # # U U U U U U U U U U U U U U 0 0 0 0 0 0 0 0 a a a a a a a a a a a a a a a a a a a a a a @ @ @ @ @ @ @ @     N N N N N N N N N N N N N N E E E E E E E E E E M M M M M M M M M M              - , , , , , ) ) ) ) ) ) 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 ' ' ' ' ' ' 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 . . . . . .
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 - , , , , , , ,Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Qd ? > > > > > > > > > 5 5 5 5 5 5 5 5 3 3 3 3 3 3 3 3       : : : : : :       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      !"  ! ! ! !$W.?%JK  K K K K*/CT0T*U  U U U U% % % % %: % % %% % % % %j % % %$ tJ$456 6 6 6 6    z   Bw Bw Bw Bw Bw+d#%9 Bw Bw BwJn    1    
VZ* * * * 
*
 
   
 ^b/% /% /% /% /%d6 6 6 6$   ] ] ] ]
^ ^ ^ ^
E E E E
O O O O    6 6 6 6r    <    6 @E$, $, $, $, $,N             + + + + ++ + + +          B 	L L L L L^
N 
N 
N 
N   .   $	 	 	 	   &    Z_2 2 2 2 26) ) ) ) )= ) ) )6F F F F Fn F F F	 	 	 	 wv			\ \ \ \ \74<( \ \ \~a$ a$ a$ a$H      28 8 8 8      W W W W0   R R R R   "G G G G
/ / / /
      	 	 	 	>  >  >  >  > ' >  >  > B4 4 4 4$   44 4 4 4% % % % % %r  