
    %^g                   
   U d Z ddlmZ ddlZddlmZ ddlmZmZ ddl	m
Z
mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ ddlmZ ddlZddlmZ m!Z!m"Z"m#Z#m$Z$ dd	l%m&Z&m'Z'm(Z( dd
l)m*Z*m+Z+m,Z,m-Z-m.Z. ddl/m0Z0 ddl1m2Z2 ddl3m4Z4m5Z5m6Z6 ddl7m8Z8m9Z9m:Z:m;Z; ddl<m=Z=m>Z>m?Z? ddl@mAZAmBZBmCZC ddlDmEZEmFZFmGZGmHZH ddlImJZJ ddlKmLZLmMZMmNZN ddlOmPZP ddlQmRZRmSZSmTZTmUZUmVZVmWZWmXZXmYZYmZZZ ddl[m\Z\m]Z] ddl^m_Z_m`Z`maZambZbmcZcmdZdmeZemfZfmgZgmhZhmiZimjZjmkZkmlZlmmZmmnZnmoZompZpmqZqmrZrmsZsmtZtmuZumvZvmwZwmxZxmyZymzZzm{Z{m|Z|m}Z}m~Z~mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ ddlmZmZmZ ddlmZmZ ddlmZmZ ddlmZmZ ddlmZ ddlmZ ddlmZmZmZ ddlmZmZ dd lmZ dd!lmZ dd"lmZmZmZmZmZmZmZmZmZmZ dd#lmZmZmZ dd$lmZ dd%lmZmZmZ dd&lmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ dd'lmZmZmZmZmZmZmZmZmZmZmZmZmZmZ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'd.Z(d/e)d0<   d1Z*d/e)d2<   eeeeexf   Z+d3e)d4<   eeeef   Z,d3e)d5<    G d6 d7e      Z- G d8 d9e      Z.eee{ef      Z/d3e)d:<    G d; d<e      Z0 G d= d>e      Z1 G d? d@e&d   e      Z2 G dA dBe      Z3e	 d}	 	 	 	 	 	 	 d~dC       Z4e	 	 	 	 	 	 	 	 ddD       Z4	 d}	 	 	 	 	 	 	 ddEZ4	 	 	 	 	 	 	 	 ddFZ5ddGZ6ddHZ7ddIZ8ddJZ9ddKZ:ddLZ;ddMZ<dddNZ=dddOZ>	 d	 	 	 	 	 ddPZ?ddQZ@ddRZAddSZBddTZC G dU dVeث      ZDddWZEddXZF	 d	 	 	 	 	 	 	 	 	 ddYZGddZZHdd[ZIdd\ZJdd]ZKdd^ZLdd_ZMddd`ZN G da dbe      ZO G dc dde      ZPddeZQ G df dg      ZR edh      ZS edi      ZT G dj dkeeSeTf         ZU	 	 	 	 	 	 	 	 ddlZVddmZWddnZXddoZYddpZZddqZ[ddrZ\ddsZ]ddtZ^dduZ_ddvZ`ddwZaddxZb G dy dzeԫ      Zcdd{Zddd|Zey)zMypy type checker.    )annotationsN)defaultdict)	ExitStackcontextmanager)AbstractSetCallableDictFinalGenericIterableIteratorMapping
NamedTupleOptionalSequenceTupleTypeVarUnioncastoverload)	TypeAlias)
errorcodesjoinmessage_registrynodes	operators)ConditionalTypeBinderFrameget_declaration)MemberContext$analyze_decorator_or_funcbase_accessanalyze_descriptor_accessanalyze_member_accesstype_object_type)PatternChecker)SUPERTYPE_OF)
erase_typeerase_typevars!remove_instance_last_known_values)TYPE_VARUNUSED_AWAITABLEUNUSED_COROUTINE	ErrorCode)ErrorsErrorWatcherreport_internal_error)expand_self_typeexpand_typeexpand_type_by_instance)Keyextract_var_from_literal_hashliteralliteral_hash)map_instance_to_supertype)is_overlapping_erased_typesis_overlapping_types
meet_types)ErrorMessage)	SUGGESTED_TEST_FIXTURESMessageBuilderappend_invariance_notesappend_union_noteformat_typeformat_type_bareformat_type_distinctlymake_inferred_type_note
pretty_seq)MroErrorcalculate_mro)M	ARG_NAMEDARG_POSARG_STARCONTRAVARIANT	COVARIANTFUNC_NO_INFOGDEFIMPLICITLY_ABSTRACT	INVARIANTIS_ABSTRACTLDEFLITERAL_TYPEMDEFNOT_ABSTRACT
AssertStmtAssignmentExprAssignmentStmtBlock	BreakStmt	BytesExprCallExprClassDefComparisonExprContextContinueStmt	DecoratorDelStmtEllipsisExpr
ExpressionExpressionStmt	FloatExprForStmtFuncBaseFuncDefFuncItemIfStmtImport	ImportAll
ImportBase
ImportFrom	IndexExprIntExpr
LambdaExprListExprLvalue	MatchStmt
MemberExprMypyFileNameExprNodeOperatorAssignmentStmtOpExprOverloadedFuncDefPassStmtPromoteExpr	RaiseStmtRefExpr
ReturnStmtStarExpr	StatementStrExpr
SymbolNodeSymbolTableSymbolTableNodeTempNodeTryStmt	TupleExprr   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)CheckerPluginInterface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map_type_from_supertype	true_onlytry_expanding_sum_type_to_union"try_getting_int_literals_from_typetry_getting_str_literals"try_getting_str_literals_from_typetuple_fallback)%ANY_STRATEGYMYPYC_NATIVE_INT_NAMESOVERLOAD_NAMESAnyTypeBoolTypeQueryCallableTypeDeletedType
ErasedTypeFunctionLikeInstanceLiteralTypeNoneType
OverloadedPartialType
ProperType	TupleTypeTypeTypeAliasTypeTypedDictTypeTypeGuardedType	TypeOfAnyTypeTranslatorTypeType	TypeVarIdTypeVarLikeTypeTypeVarTupleTypeTypeVarTypeUnboundTypeUninhabitedType	UnionType
UnpackTypefind_unpack_in_listflatten_nested_unionsget_proper_typeget_proper_typesis_literal_typeis_named_instance)is_overlapping_noneremove_optionalstore_argument_type
strip_type)TypeTraverserVisitor)fill_typevarsfill_typevars_with_anyhas_no_typevars)	is_dunder	is_sunder)NodeVisitorT   r
   DEFAULT_LAST_PASS   MAX_PRECISE_TUPLE_SIZE
_TypeAliasDeferredNodeTypeFineGrainedDeferredNodeTypec                  "    e Zd ZU ded<   ded<   y)DeferredNoder   nodeTypeInfo | Noneactive_typeinfoN__name__
__module____qualname____annotations__     ,lib/python3.12/site-packages/mypy/checker.pyr  r     s    
$$r  r  c                  "    e Zd ZU ded<   ded<   y)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<   y)	TypeRanger   itemboolis_upper_boundNr  r  r  r  r  r    s    
Jr  r  c                  ,    e Zd ZU ded<   ded<   ded<   y)PartialTypeScopezdict[Var, Context]mapr  is_functionis_localNr  r  r  r  r  r    s    	Nr  r  c                  b   e Zd ZU dZdZded<   ded<   ded<   d	ed
<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded <   d!ZeZdZ	dZ
d"ed#<   d$Zd%ed&<   dZd'ed(<   d)ed*<   d+ed,<   d-ed.<   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd/Zedd0       Zdd1Zdd2Z	 d 	 	 	 d!d3Zd"d4Zd#d5Zd$d6Zd%d7Zd&d8Z	 d d$d9	 	 	 	 	 	 	 d'd:Zd(d;Zd(d<Zd)d=Zd(d>Zd*d?Zd+d@Zd,dAZd,dBZ d-dCZ!d,dDZ"d.dEZ#d.dFZ$	 	 	 d/	 	 	 	 	 	 	 	 	 d0dGZ%d1dHZ&e'd2dI       Z(	 d3	 	 	 	 	 	 	 	 	 d4dJZ)d5dKZ*d6dLZ+d7dMZ,d8dNZ-d8dOZ.d9dPZ/d:dQZ0	 	 	 	 	 	 	 	 	 	 d;dRZ1	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d<dSZ2	 	 	 	 	 	 	 	 d=dTZ3d>dUZ4d?dVZ5d@dWZ6d@dXZ7dAdYZ8	 	 	 	 	 	 dBdZZ9	 d 	 	 	 	 	 	 	 dCd[Z:	 	 	 	 dDd\Z;	 	 	 	 	 	 	 	 dEd]Z<	 	 	 	 	 	 	 	 dFd^Z=	 	 	 	 	 	 	 	 	 	 dGd_Z>dHd`Z?	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dIdaZ@dJdbZAdKdcZBdLddZCdKdeZDdMdfZEdKdgZFdNdhZGdOdiZHdKdjZIdKdkZJdKdlZKdLdmZLdPdnZM	 	 	 	 	 	 	 	 	 	 dQdoZNdLdpZOdRdqZPdSdrZQdTdsZRdUdtZSdVduZTdWdvZUdXdwZVdYdxZWdYdyZX	 	 dZ	 	 	 	 	 	 	 	 	 d[d{ZYd|d}hZZd~ed<   d\dZ[	 	 	 	 	 	 	 	 d]dZ\	 	 	 	 	 	 	 	 d^dZ]	 	 	 	 	 	 	 	 	 	 	 	 	 	 d_dZ^	 	 	 	 	 	 d`dZ_	 	 	 	 	 	 	 	 dadZ`	 	 	 	 	 	 	 	 dadZa	 	 	 	 	 	 	 	 dbdZbdWdZce'dcd       ZddddZededZfdfdZgdgdZh	 dh	 	 	 	 	 	 	 	 	 didZi	 d 	 	 	 	 	 	 	 	 	 djdZj	 	 	 dk	 	 	 	 	 	 	 	 	 	 	 	 	 dldZk	 	 	 	 	 	 	 	 	 	 	 	 dmdZldndZm	 dh	 	 	 	 	 	 	 	 	 	 	 	 	 dodZndpdZo	 	 	 	 	 	 	 	 dqdZpdrdZq	 dh	 	 	 	 	 	 	 	 	 dsdZrdtdZsdudZt	 	 	 	 	 	 	 	 	 	 dvdZudwdZvdxdZwdydZxdydZydzdZzd{dZ{e|j                  ddfd$d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d|dZ~	 	 	 	 	 	 	 	 	 	 d}dZ	 	 	 	 	 	 	 	 d~dZ	 	 	 	 	 	 ddZddZddZddZddZddZddZddZddZddZd3ddZddZddZddZddZddZddZddZddZddZ	 	 	 	 	 	 ddZddZddZ	 	 	 	 	 	 	 	 	 	 ddZddZddZd3ddZ	 	 	 	 	 	 	 	 ddZddÄZddĄZddńZ	 	 	 	 	 	 	 	 ddƄZ	 	 	 	 	 	 	 	 ddǄZddȄZddɄZddʄZ	 	 	 	 	 	 dd˄Zdd̄Zdd̈́Zdd΄Z	 	 	 	 	 	 	 	 	 	 ddτZ	 	 	 	 	 	 ddЄZddфZdd҄Z	 	 	 	 	 	 ddӄZ	 	 	 	 	 	 ddԄZ	 	 	 	 	 	 ddՄZdxdքZddׄZ	 	 	 	 	 	 dd؄Zdzdٜ	 	 	 	 	 ddڄZdzdٜ	 	 	 	 	 ddۄZdd܄Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd݄ZddބZdd߄Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 ddZddZd+dZddZddZddZ	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 ddZe	 	 dd$d$d$d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Ze	 	 dd$d$d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Z	 	 dd$d$d$d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZÐddZe'd2d       Z	 	 	 	 	 	 	 	 	 	 ddZƐddZǐddZȐddZɐddZʐddZːddZ̐ddZ͐ddZΐddZϐddZe'dd       ZѐdWdZҐddZӐddZe'ddd	 	 	 	 	 dʐd        Z	 	 	 	 	 	 	 	 	 	 dːdZ֐d̐dZאd͐dZ	 	 	 	 dΐdZِd dϐdZd$d	 	 	 	 	 	 	 dАdZ	 dd$d	 	 	 	 	 	 	 	 	 dҐdZ	 	 	 	 	 	 dӐd	ZݐdԐd
ZdzddՐdZߐd֐dZe	 d 	 	 	 	 	 	 	 	 	 dאd       Ze	 	 	 	 	 	 	 	 	 	 dؐd       Z	 d 	 	 	 	 	 	 	 	 	 dِdZdڐdZdېdZdܐdZdݐdZ	 	 	 	 	 	 	 	 dސdZ	 	 	 	 	 	 dߐdZddZd ddZddZddZd$d	 	 	 	 	 	 	 	 	 ddZy$(  TypeCheckerzMypy type checker.

    Type check mypy source files that have been semantically analyzed.

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

        Use errors to report type check errors.
        r   FNT))r  r.  r0  treepathr>   r  r6  r   r$  r%  r&  r   r!  namesr,  r(  r)  r*  setr+  r5  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  r8  r9  r6  per_line_checking_time_nss           r  __init__zTypeChecker.__init__q  sP    		!&'2g!$'
+-zz #! $5%*"|| $ 5 5g >(,% '+# # ',#
 $)  !NN<<$((DKK)B
  .dDHHdkk7Sr  c                .    | j                   j                  S N)r"  type_contextrH  s    r  rM  zTypeChecker.type_context  s      ---r  c                   | j                   j                          | j                  j                          t               | _        g | j
                  dd | j
                  d   j                          d| _        | j                  j                          | j                  J | j                  g k(  sJ | j                  g k(  sJ t        | j                  j                        dk(  sJ | j                  g k(  sJ y)zCleanup stale state that might be left over from a typechecking run.

        This allows us to reuse TypeChecker objects in fine-grained
        incremental mode.
        r   Nr   )r+  clearr4  r   r!  r   temp_type_mapr"  resetr1  r*  r/  lenr&  stackrN  s    r  rR  zTypeChecker.reset  s     	##% +-   "!!,,444!!R'''""b(((4::##$)))!!R'''r  c           	        d| _         t        j                  | j                  j                        5  | j
                  j                  | j                  | j                  j                  | j                  | j                         | j                  j                  | j                  j                        5  | j                         5  | j                  j                         5  | j                  j                  D ]n  }| j                  j!                         rA| j#                         s nB| j%                  |      rA| j&                  j)                  |        n| j+                  |       p ddd       ddd       | j,                  rJ | j.                  j1                  d      }||j2                  |j4                  }|J | j7                  d| j9                  d      g      }t;        |j2                  |      sUt=        ||j2                  | j                        \  }}| j?                  t@        jB                  jE                  ||      |       ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   *xY w# 1 sw Y   yxY w)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  )#rA  r   strict_optional_setr0  strict_optionalr  set_filer9  r8  fullnamer$  module_scopeenter_partial_typesr!  top_frame_contextdefsis_unreachable should_report_unreachable_issuesis_noop_for_reachabilityr  unreachable_statementacceptr=  r,  gettyper  named_generic_type
named_typer   rC   failr   ALL_MUST_BE_SEQ_STRformat)rH  dall_all_nodeseq_str	str_seq_sall_ss          r  check_first_passzTypeChecker.check_first_pass  s    '+#&&t||'C'CDKK  		499--T[[$,, !  ))$))*<*<=--/1N1N1P!YY^^;;557#'#H#H#J %#'#@#@#C $ > >q A % KKN , 2Q/  5555||''	2#		(=#yyH#///"55)DOON,K+LG &dii9+A#TYY,(	5 		,@@GG	SXY[c3 >	 ED
 2Q1P// >=	 EDs\   BI?2I3I'AI	60I	&I'.CI3
I?I$ I''I0,I33I<	8I??Jc           	        d| _         t        j                  | j                  j                        5  |s| j
                  s
	 ddd       y| j                  j                  | 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|j                  |       t!               5 }|rT|j#                  | j                  j%                  |             |j#                  | j&                  j)                  |             | j+                  |       ddd        	 ddd       	 ddd       y# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   yxY w)zRun second or following pass of type checking.

        This goes through deferred nodes, returning True if there were any.
        TNFrV  r   )rA  r   rZ  r0  r[  r/  r  r\  r9  r8  r]  r$  r^  r<  r;  addr   enter_contextclass_scoper&  
push_classcheck_partial)rH  tododoner  r  rT  s         r  check_second_passzTypeChecker.check_second_pass  sx    '+#&&t||'C'CD 3 3 ED KK  		499--T[[$,, !  ))$))*<*<="..D#2222&(#LOE-1)D/t|  HHTN"*!//0G0G0XY!//

0E0Eo0VW**40	 % .2 >( 5 ED* % >= EDsD   G
B G
A,G6A(F;	
G)	G;G GG	GGc                .   t        |t              r| j                  |       y d| _        | j                  j                         5  t        |t              r| j                  j                  |       n| j                  |       d d d        y # 1 sw Y   y xY wNT)	
isinstancerw   check_top_levelrA  r!  r`  rr   r"  rf  rH  r  s     r  rz  zTypeChecker.check_partial  sh    dH%  &*.D'..0dJ/%%,,T2KK%	 100s   >BBc                   d| _         | j                         5  | j                  j                         5  |j                  D ]  }|j                  |         	 ddd       ddd       | j                  rJ y# 1 sw Y    xY w# 1 sw Y   $xY w)zDCheck only the top-level of a module, skipping function definitions.FN)rA  r_  r!  r`  ra  rf  r=  )rH  r  rn  s      r  r  zTypeChecker.check_top_level%  so    &+#%%'..0AHHTN # 1 (
 -----	 10 ('s"   B#A6B6A?	;BBc                N    | j                   j                  t        ||             y)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  )rH  r  enclosing_classs      r  
defer_nodezTypeChecker.defer_node0  s     	""<o#FGr  c                *   | j                   j                         }| j                  | j                  k  rDt	        |t
              r4| j                   j                         }| j                  ||       d| _        y | j                  j                  ||       y r  )r&  top_non_lambda_functionr<  	last_passr  ri   r  r  r=  r  cannot_determine_type)rH  namecontextr  r  s        r  handle_cannot_determine_typez(TypeChecker.handle_cannot_determine_type=  sm    zz113==4>>)jw.G
 #jj88:OOOD/2 *.D&HH**49r  c                    	 |j                  |        y# t        $ rK}t        || j                  j                  |j
                  | j                  | j                         Y d}~yd}~ww xY w)z,Type check a node in the given type context.N)rf  	Exceptionr0   r  fileliner0  )rH  stmterrs      r  rf  zTypeChecker.acceptM  sR    	_KK 	_!#t{{'7'7DKKQUQ]Q]^^	_s    	A(AA##A(exit_conditionc                  | j                   j                  dd      5  	 | j                   j                  ddd      5  | j                  |       ddd       | j                   j                  snP|r%| j	                  |      \  }}| j                  |       |r| j                  |       ddd       y# 1 sw Y   dxY w# 1 sw Y   yxY w)zRepeatedly type check a loop body until the frame doesn't change.
        If exit_condition is set, assume it must be False on exit from the loop.

        Then check the else_body.
        FT)can_skipconditional_frame   r   )r  break_framecontinue_frameN)r!  frame_contextrf  last_pop_changedfind_isinstance_checkpush_type_map)rH  body	else_bodyr  _else_maps         r  accept_loopzTypeChecker.accept_loopT  s     [[&&&N[[..!\].^KK% _{{33	 
 "88H8""8,I& ON^^ ONs#    B?B3AB?3B<	8B??Cc                    | j                   sy | j                  j                  |      5  | j                  |       d d d        y # 1 sw Y   y xY wrL  )rA  r$  function_scope_visit_overloaded_func_defrH  defns     r  visit_overloaded_func_defz%TypeChecker.visit_overloaded_func_defq  s:    **[[''-++D1 .--   AAc                V   d}|j                   sy t        |j                         dk(  r | j                  t        j                  |       |j
                  r=t        |j                   d   t              sJ | j                  |j                   d          |j                   D ]  }t        |t              sJ |j
                  r3| j                  |j                  |j                  j                  d       n| j                  |d       |j                  j                  t        t        fv s|dz  } |dt        |j                         fvr | j                  t        j                   |       |j"                  r|j"                  j%                  |        |j
                  s| j'                  |       |j(                  qg }|j                   D ]N  }t        |t              sJ | j+                  |j,                  j(                  |      }|>|j/                  |       P |rt1        |      |_        |j2                  rv| j5                  |      }|j6                  r*|s(|&| j8                  j;                  |j                  |       | j=                  |||j"                         | j?                  |       y y )Nr   r   Tr  allow_empty)r  ) itemsrS  rk  r   MULTIPLE_OVERLOADS_REQUIREDis_propertyr  ra   visit_decoratorcheck_func_itemfuncr  visit_decorator_innerabstract_statusrQ   rO   INCONSISTENT_ABSTRACT_OVERLOADimplrf  check_overlapping_overloadsrh  extract_callable_typevarr  r   infocheck_method_overrideis_explicit_overrider  no_overridable_method!check_explicit_override_decoratorcheck_inplace_operator_method)rH  r  num_abstractfdef
item_typesr  	item_typefound_method_base_classess           r  r  z&TypeChecker._visit_overloaded_func_defw  s&   zz tzz?aII&BBDIdjjmY777  A/JJDdI...$$TYYTYY^^QU$V **4T*Byy(([:M,NN!  3tzz?33II&EEtL99IIT",,T2yy 
 JJD%dI666 $ : :488==$ OI ,")))4	 '
  *: 6DI99(,(B(B4(H%))1-9..tyy$?2249RTXT]T]^..t4 r  c                j   t        |      }d}|t        |t              st        |t              rt        |j                        }t        |t
              r/t        | j                  j                  |j                  |            }t        |t              r|}nt        |t              r:t        t        d||ddd| j                  || 	            }t        |t              r|}nt        |t              rt        |j                        }t        |t              rkg }|j                  D ])  }| j!                  ||      }| nV|j#                  |       + t        t%        j&                  |            }	t        |	t              r|	}n| j!                  ||      S || j                  j)                  ||       |S )z,Get type as seen by an overload item caller.N__call__FT)	r  typr  	is_lvalueis_superis_operatorr  original_typechk)r   r  r   r   upper_boundr   r"  analyze_type_type_calleer  r   r   r#   r  r   r   r  r  r  r   join_type_listnot_callable)
rH  
inner_typectx
outer_type
inner_call
union_typer  r  callable_itemjoined_types
             r  r  z!TypeChecker.extract_callable_type  s   $Z0
*.
!*Z*I*o6,Z-C-CD
*h/,%%>>zPST
 *l3'
J1,)'& #"'!&$( HH&0 

 j,7!+JJ	22:3C3CD
j)4E * 0 0(,(B(B4(M(0!]3	 !1 '6d6I6I%6P&Q%k<@)4J55j#FF!%%j#6r  c           	        | j                   j                  j                  | j                   j                  j                  v s"| j                  r| j
                  j                  ry d }|j                  rt        |j                  t              r|j                  j                  }nBt        |j                  t              r!|j                  j                  j                  }nJ d       | j                  ||j                        }|j                  xr |j                  dk(  }t!        |j"                        D ]'  \  }}t        |t              sJ | j                  |j                  j                  |      }|Bt!        |j"                  |dz   d        D ]B  \  }}	t        |	t              sJ | j                  |	j                  j                  |	      }
|
Bt%        ||
      sOt'        ||
      r1| j                   j)                  |dz   ||z   dz   |	j*                         |r| j,                  j/                         }|r|j0                  j2                  ng }t5        j6                  d      5  t9        ||
|      rU|dk(  xr t9        |
||       xr t'        |
|       }| j                   j;                  |dz   ||z   dz   ||j*                         d d d        E ||j                  J t=        ||t>        dd 	      r|j@                  r\tC        |jE                         |jE                         dtF        
      }|-| j                   jI                  |dz   |j                         4|}|jJ                  r<|j                  r0|jM                  |jJ                  d   g|jJ                  dd  z         }t=        ||t>        dd      s)| j                   jO                  |dz   |j                         t?        |jP                  |jP                        rt?        |jP                  |jP                        r| j                   jS                  |dz   |j                         * y # 1 sw Y   xY w)NFzImpl isn't the right type__get__r   r  Tr   c                    t        ||       S rL  r   lrs     r  <lambda>z9TypeChecker.check_overlapping_overloads.<locals>.<lambda>.  s    *Q2Br  )	is_compatr   is_compat_return)ignore_returnreturn_constraint_direction)	arg_typesr  r   r  )*r  r  r  ignored_filesr@  r0  test_envr  r  ri   rh  ra   r  r  r  r  	enumerater  are_argument_counts_overlappingoverload_can_never_match%overloaded_signature_will_never_matchr  r&  active_classr  	type_varsr   rZ  )is_unsafe_overlapping_overload_signaturesoverloaded_signatures_overlapr   r   	variablesr   with_unpacked_kwargsr&   &overloaded_signatures_typevar_specificr  copy_modified"overloaded_signatures_arg_specificret_type"overloaded_signatures_ret_specific)rH  r  	impl_typer  is_descriptor_getir  sig1jitem2sig2current_classr  	flip_noter  s                  r  r  z'TypeChecker.check_overlapping_overloads  s    88??488??#@#@@!!dll&;&;  *.	99$))W-*.))..
DIIy1!YY]]//
999u 22:tyyII II@$))y*@ ,GAtdI...--dhhmmTBD|%djjQ&9:5!%33311%))..%H<6tTB+D$7HHBB1q5!a%RS)UZU_U_`* %)JJ$;$;$=M@M 2 2 < <SUI2248DT4QZ[ !Q !M(Q$($	)" %"!M )At(L$L & !HHBB !Aq1uqy)TYY 987 ;P $yy,,, *(&+%B  &&0F!668113&+4@1D |GGAtyyY $D >>dii--9J8Kdnn]^]_N`8`-aD .$*]a HH??AtyyQ t}}dmm<!$--?HH??AtyyQG -B 98s   (A#QQ)c                V   t        |      }|r7| j                  dt        t        j                        g      }t        ||      r<yt        t        j                        }| j                  d|||g      }t        ||      ryt        |t              xr |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   ri  r   r   special_formr   r  r   rh  r]  )rH  r  is_coroutineatany_typegts         r  is_generator_return_typez$TypeChecker.is_generator_return_type  s     c"(();giF\F\>]=^_B"c"y556H(();hRZ=[\B"c"#x(]SXX->->B]-]]r  c                    	 t        t        j                        }| j                  d||g      }t        ||      S # t        $ r Y yw xY w)zoIs `typ` a valid type for an async generator?

        True if `typ` is a supertype of AsyncGenerator.
        typing.AsyncGeneratorF)r   r   r  ri  KeyErrorr   )rH  r  r
  agts       r  is_async_generator_return_typez*TypeChecker.is_async_generator_return_type  sR    
	y556H))*AHhCWXC #s##  		s   -; 	AAc           	        t        |      }t        |t              rt        t        j                  |      S t        |t
              r3t        |j                  D cg c]  }| j                  ||       c}      S | j                  ||      s*| j                  |      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 c c}w )zRGiven the declared return type of a generator (t), return the type it yields (ty).
source_anyr  r   )r   r  r   r   from_another_anyr   r   r  get_generator_yield_typer  r  
from_errorr   rh  r]  r  args)rH  return_typer  r  r  s        r  r  z$TypeChecker.get_generator_yield_type  s   %k2k7+955+NNY/(OZO`O`aO`t..t\BO`a  ..
55kB 9//00K29//00&&*<<91122"''*HO 911221 bs   Ec           	     >   t        |      }t        |t              rt        t        j                  |      S t        |t
              r3t        |j                  D cg c]  }| j                  ||       c}      S | j                  ||      s*| j                  |      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 c c}w )zTGiven a declared generator return type (t), return the type its yield receives (tc).r  r  r  r     r   r  r  )r   r  r   r   r  r   r   r  get_generator_receive_typer  r  r  r   rh  r]  r  rS  r  r   rH  r  r  r  s       r  r  z&TypeChecker.get_generator_receive_type  sb   %k2k7+955+NNY/(Q\QbQbcQb00|DQbc  ..
55kB 9//00K29//00&&*<<91122%%)ZZK$$%* ##A&&&&*AAc+JZJZF[_`F`##A&& :3 ds   Fc                    t        |      }t        |t              rt        t        j                  |      S t        |t
              sJ d       |j                  d   S )Nr  z-Should only be called on coroutine functions.r  )r   r  r   r   r  r   r  )rH  r  s     r  get_coroutine_return_typez%TypeChecker.get_coroutine_return_type  sP    %k2k7+955+NN+x0a2aa0""r  c           	        t        |      }t        |t              rt        t        j                  |      S t        |t
              r3t        |j                  D cg c]  }| j                  ||       c}      S | j                  ||      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 c c}w )zSGiven the declared return type of a generator (t), return the type it returns (tr).r  r  r   r   r  r  r  )r   r  r   r   r  r   r   r  get_generator_return_typer  r  r   rh  r]  rS  r  r   r  s       r  r"  z%TypeChecker.get_generator_return_type  s+   %k2k7+955+NNY/(P[PaPabPa//lCPab  ..{LI 9//00K29//00&&*<<[EUEUAVZ[A[##A&&%%)ZZK$$%* ##A&& :+ cs   Ec                    | j                   sy | j                  j                  |      5  | j                  |       d d d        y # 1 sw Y   y xY wrL  )rA  r$  r  _visit_func_defr  s     r  visit_func_defzTypeChecker.visit_func_def  s:    **[[''-  & .--r  c                N   | j                  ||j                         |j                  rL|j                  s/|j                  s#| j                  |      }| j                  ||       | j                  |       |j                  r$| j                  |      }| j                  ||       yy)!Type check a function definition.r  N)r  r  r  is_overloadis_decoratedr  r  r  original_defr   check_func_def_override)rH  r  r  new_types       r  r$  zTypeChecker._visit_func_def  s    T		299##D,=,=
 -1,F,Ft,L)66t=VW..t4))$/H((x8 r  c                   | j                   j                  |j                         xr |        | j                  d      5  | j	                  |      }|r'|j                  |j                  |j                        }t        |t              r.| j                         5  | j                  ||||       ddd       nt        d      ddd       | j                   j                          d| _        |dk(  r| j                  |       |dk(  rBt!        j"                  |j$                        r"t!        j&                  | ||j$                         yyy# 1 sw Y   xY w# 1 sw Y   xY w)	zcType check a function.

        If type_override is provided, use it as the function type.
        T)r  )r  columnNzNot supportedF__exit____post_init__)r)  r  
is_dynamicr_  r   r  r  r/  r  r   !enter_attribute_inference_contextcheck_func_defRuntimeErrorpopr=  check__exit__return_typedataclasses_pluginis_processed_dataclassr  check_post_init)rH  r  type_overrider  r  r  s         r  r  zTypeChecker.check_func_item)  s-    	!!$//"3"IM8IJ%%$%7$$T*C#11sxx

1S#|,;;=''c4E >= #?33 8 	 %*":))$/ ?"!88C"224tyyI D # >= 87s%   AEE1EE	EE!c                   |j                   J t        |j                   t              rF| j                  |j                         }t	        ||      s| j
                  j                  |||       y y |j                   j                  }|y t        |t              rw|j                  I|j                   }t        |t              r|j                  }n|}| j                  |      }|,||_        ||= y | j                  t        j                  |       y y t        |j                         }|j                   |_        | j$                  j'                  |||       | j)                  |||t        j                  dd       y )Nzredefinition with typezoriginal type)r+  r  ri   r   r   r  %incompatible_conditional_function_defrh  r   ra   r  find_partial_typesrk  r   INCOMPATIBLE_REDEFINITIONrx   r  r  r!  assign_typecheck_subtype)	rH  r  r-  old_type	orig_typeorig_defr  r*  	name_exprs	            r  r,  z#TypeChecker.check_func_def_overrideK  sN     ,,,d''1))$*;*;<H(3>>tXxX 4
 ))..I  )[1>>)#00H!(I6&ll&$($;$;C$@M$0#+)#. II.HH$O 1 %TYY/	!%!2!2	''	8YG""$>>,#r  c              #  H   K   | j                   }i | _         d  || _         y wrL  )r1  )rH  	old_typess     r  r3  z-TypeChecker.enter_attribute_inference_contextv  s&     11	(*%
(1%    "c                   | j                  |j                         | j                  ||      }|}|D ]k  \  }}| j                  }t	               | _        | j                  j                         5  |j                  j                  |       t        |t              r|}	|	j                  r|	j                  dv rqt        t        |j                        t        t        f      sH| j                   d   s9| j#                  t$        j&                  j)                  |	j                        |       |	j                  r!|	j                  dk(  r| j+                  |	|       | j-                  |	       | j.                  j0                  r|	j2                  rt        |	j2                  t4              r|	j2                  j                  }
t7        |
      r| j8                  j;                  d|
|	       t=        |	j2                  j>                        D ]D  \  }}t7        |      sd|dz    d|	j                   d}| j8                  j;                  |||	       F tA        |	j2                  | j.                  | jB                  | j8                  |		       |ra|j                  r&| jE                  |      r| jG                  ||||       n/|d
v r| jI                  |||       n|dk(  r| jK                  ||       t        |j                  tL              rY|j                  jN                  tP        k(  r*| j#                  t$        jR                  |j                         | jU                  |       nAt        |j                  tL              r'|j                  jV                  r| jU                  |       |jX                  r|jZ                  r<| j]                  |j                        sg| j#                  t$        j^                  |       nF| ja                  |j                  |jb                        s | j#                  t$        jd                  |       |jf                  r|j                  }|jb                  }| ji                  ||      }| jk                  ||      }|r| jm                  |      }n| jo                  ||      }| jq                  d||||g      }
|js                  |
      }||_        | jt                  j                  |j                         | jv                  jy                  |      5  | jv                  j{                         }ddd       |j|                  rd}|j~                  r|j                  dk(  rd}|t        |j>                        k  rt        |j|                  |j>                  |         so| j#                  t$        j                  j)                  t        |j|                  | j.                        t        |j>                  |   | j.                              |       t        t        |j>                              D ]  }|j>                  |   }t        |t              r|dk(  r|j~                  r|j                  dk(  r|j                  d   t        j                  t        j                  fvry|j                  s|j                  dk(  r)t        j                  j                  j                  |      }t        ||      st        t        t        |                  }t        ||d      sut        |t              r|j2                  j                  sJt        |t              r=t        |j                  t              r#|j                  j2                  j                  rd}nt|j                  |   dv rSt$        j                  j)                  |j                  | j.                        |j                  | j.                              }nt$        j                  }|r| j#                  ||       nyt        |tL              ri|jN                  t        k(  rV|j                  dvrHt        |j                        s3|}|j                  dk  r|}| j#                  t$        j                  |       t        |||| jp                          t        |j                        }| j                  ||       ddd       | j                  j                         5  d}|j                  D ]  }|j                  j                         D ]  \  }}t        |      }|| j                  ||j                        r1|| j                  j                         }||j                  |<   |j                  |   | j                  j                  |<     | jv                  jy                  |      5  t        |      st        |      dk\  r| j                  j                          | j                  |j                         ddd       | j                  j                         }|| j                  j                  dd       ddd       s"|jX                  st        | jt                  d   d      r*| jo                  | jt                  d   |jb                        } n:|jb                  r| jm                  | jt                  d         } n| jt                  d   } t        |       } |xs | j.                  j                  } xs: | xr5 t        |t              xr |j                  t        k7   xr | j                   }!|j                  t        urQ|j                  |j                  j                  vs-|j                  j                  |j                     j                  rd}!t        |t              r|j                  rd}!|rt        t               |       rd}!|xr] |j                  t        uxrI |j                  j                  duxr/ |j                  j                  j2                  j                  d      }"| j.                  j                  r| j                  st        | t        t        f      s|!rt        | t              rt$        j                  }nt$        j                  }|r |j                  t        j                        }| j#                  ||       |"r| j                  t$        j                  |       nw|!rut$        j                   }|r |j                  t        j                        }| j                  dt               d| ||      s"|"r | j                  t$        j                  |       | jt                  j                          || _        n y# 1 sw Y   }xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   |xY w)r'  )rJ  __init_subclass____new__zReturn typez	Argument r   z to ""r  )__getattr____getattribute____setattr__r  )r  Nr   T)ignore_type_params>   clsrH  )rJ  rL  r1  r  Fabc.ABCMetacodezimplicitly returnsexpected)subtype_labelsubtypesupertype_label	supertyper  r  )check_typevar_defaultsr  expand_typevarsr!  r   r`  expandedr  r  ri   r  r  r   r  r   r   r)  rk  r   MUST_HAVE_NONE_RETURN_TYPErm  check___new___signaturecheck_for_missing_annotationsr0  disallow_any_unimportedrh  r   r   r  unimported_type_becomes_anyr  r  r   r@  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_generatorr  'INVALID_RETURN_TYPE_FOR_ASYNC_GENERATORr  r  !INVALID_RETURN_TYPE_FOR_GENERATORis_awaitable_coroutiner  r  r   r"  ri  r  r(  r&  push_functionactive_self_typetype_is	is_staticrS  r   NARROWED_TYPE_NOT_SUBTYPErA   range	arg_kindsr   rJ   	ARG_STAR2is_classrE  typesr   make_normalizedr   r(   r   r   is_protocolr  	arg_names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_is_empty_generator_functionsuppress_unreachable_warningsrf  rb  	pop_framer   allow_empty_bodiesr  rU   r>  rM   r:  plugin_generatedis_mypy_onlymetaclass_typehas_basewarn_no_returnr=  r   INVALID_IMPLICIT_RETURNMISSING_RETURN_STATEMENT_replacecodes
EMPTY_BODYnoteEMPTY_BODY_ABSTRACTINCOMPATIBLE_RETURN_VALUE_TYPErA  r6  )#rH  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unreachabler  
show_errormay_be_abstracts#                                      r  r4  zTypeChecker.check_func_def}  s   
 	##CMM2''c2!ID#J/1DK..0$$T*
 dG,D 		 II)JJ *+CLL9Ho;V! !% 2 22 6		,GGNNtyyY[_
 yyTYY)%;44T3?66t<||;;99DII|)L'+yy'9'9H;HE $ D D]T\^b c1:499;N;N1OX#?#I/8q	tyykQR-SF$(HH$H$HQY[_$` 2P +		4<<1F1FZ^ yyT%>%>t%D44T3dK!DD11#tTB.11#t< cllK8||,,=		,PPRUR^R^ 55c:|44kB|G\G\GcGc 55lC $$..#BB3<<P II 0 X XZ]  $<<S\\4K\K\] II&6&X&XZ]^ .. A))A66q!<B88A>B!;;A>!;;AqA#663b"b!_ H ++X+>C #DI !!((6ZZ--d3 -1JJ,G,G,IH 4
 ;; !I+T^^tyyT]G]$%	 3s}}#55jS]]9%=? 		,FFMM +CKK F +CMM),Ddll S ! s3==12A"}}Q/H"41$0F!%499	3IMM!,U^^U__4UU==DII,B'+zz':':'J'J8'TH+Hh? &5^NS[D\5]%^F#-hSW#X$.vx$@(.(?(?'1&('C(26;;(I(.(8(8(D(D
 +/C%(]]1%5%H*:*Y*Y*`*`(.(?(?(M(0(A(A$,,(O+&C
 +;*W*WC#&$(IIc4$8#Hk: %--: $		1Y Y$.tyy$9+3C"xx!|&) II&6&]&]_bc'ad6M6MNc 3h #2$))"<''o>Q 1V ..0 +/	'..E.3kk.?.?.A*]"?"D".t7]7]#TYY8  )0,0KK,B,B,D	3@IOOC0<F<S<STW<XDKK44S9 /B / ZZ--d3 4D9S]a=OAACKK		* 4 #kk88:(KK))$2A 1D $$(9%%b)+F) #'"@"@))"-t/@/@#K &&"&"@"@ARARSUAV"WK"&"3"3B"7K-k:)LT\\-L-L (' 	 ( -&tW5^$:N:NR^:^- !%,  99L0IITYY__4				8R8c8c!&J dG,1B1B!&J #z(*k'J!&J $ N		5N		00<N 		0055>>}M	   <<.. 66 *;78K L& &k?C"2"J"JC"2"K"KC*"%,,E4D4D,"EC		#t,* II&6&J&JDQ*IIC&!ll0@0@lA !..*>$,J,6&1$( # /  ,		"2"F"FM!!#$DKS	 "@ 43{ 10v 43! 10s`   .F"v/K;v/v"'N*v/3?w	3w	A0w	 Av<A w	"v,'v//v9	<ww		w	c                    | j                   j                         }|sy|D ].  }t        |t              st	        |j
                  |      |k\  s. y y)zCan the variable be assigned to at module top level or outer function?

        Note that this doesn't do a full CFG analysis but uses a line number based
        heuristic that isn't correct in some (rare) cases.
        TF)r$  outer_functionsr  ri   find_last_var_assignment_liner  )rH  v
after_lineoutersouters        r  r  z-TypeChecker.is_var_redefined_in_outer_context  sM     ,,. E%)0Q?:M  r  c                ~   t        |j                  t              r"|j                  |j                  v rt	               }|j
                  D ]  }|j                  |        |j                  |j
                  vr| j                  t        j                  |j                  t               t        |j                  j                        }t        |t              r|j                  j                  dk(  sJ| j!                  dt#        |j                  j                  | j$                         d|j                         yyyyy)z:Fails when the return typevar is not defined in arguments.rU  builtins.objectzConsider using the upper bound z insteadrN  N)r  r  r   r  CollectArgTypeVarTypesr  rf  rk  r   UNBOUND_TYPEVARr*   r   r  r   rh  r]  r  rA   r0  )rH  r  arg_type_visitorargtyper  s        r  rj  z(TypeChecker.check_unbound_return_typevar  s    cllK0S\\S]]5R57==/0 ) ||#3#=#==		*::CLLx	X-cll.F.FG{H5#((115FFII9&s||'?'?NOxY #   G > 6S0r  c           
     L   |j                   D ]  }|j                  |rt        |j                  t              r.|j                  j
                  }d}|j                  d      r|d|dd   z  }n	|d| dz  }| j                  j                  s8t        |j                  t              r|j                  j                  dk(  rdd	g}nd }| j                  |j                  j                  |j                  |j                  t        |t        j                  
      dd|        y )NzIncompatible default for __tuple_arg_ztuple argument    z
argument "rM  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 codebaserU  argumentdefault)r  r  lvalue_namervalue_namenotes)	argumentsinitializerr  rc   variabler  
startswithr0  implicit_optionalrx   r]  check_simple_assignmentrh  r<   r  
ASSIGNMENT)rH  r  r  argr  r  r  s          r  r  zTypeChecker.check_default_args  s   >>C&:coo|#L<<$$D-C~.bc44D6++LL22s9OO,,?], ((!! 5+;+;<&% ) 1 "r  c                &    |t         j                  v S rL  )r   reverse_op_methodsrH  method_names     r  is_forward_op_methodz TypeChecker.is_forward_op_method  s    i::::r  c                &    |t         j                  v S rL  )r   reverse_op_method_setr  s     r  rd  z TypeChecker.is_reverse_op_method  s    i====r  c                   d
dt        |j                  t              xr@ t        fd|j                  j                  |j                  j
                  gz   D              }| j                   xs | j                  j                  }| j                  j                  xr |}|rQ| j                  j                  s|r7|j                  | j                  j                  r|j                  r<t        |j                        dk(  r~|j                  d   dk(  s|j                  d   dk(  rZ| j                  t        j                   |       t#        |      sQ|j$                  sD| j'                  d|t(        j*                         y | j                  t        j,                  |       y y y t        |j                  t              r4t/        |j                  j
                        } |      r!| j                  t        j                   |       n|j$                  rC | j1                  ||j2                              rt| j                  t        j                   |       nS|j2                  rGt        |t4              r7 | j7                  |            r | j                  t        j                   |       t        fd	|j                  j                  D              r!| j                  t        j8                  |       y y y y y )Nc                    t        | t              syt        | t              xr | j                  t        j
                  k(  S NF)r  r   r   type_of_anyr   unannotatedr  s    r  is_unannotated_anyzETypeChecker.check_for_missing_annotations.<locals>.is_unannotated_any   s0    a,a)Tammy?T?T.TTr  c              3  0   K   | ]  } |         y wrL  r  .0r  r  s     r  	<genexpr>z<TypeChecker.check_for_missing_annotations.<locals>.<genexpr>  s       N
/Y!"1%%/Ys   r   r   rH  rS  z1Use "-> None" if function does not return a valuerU  c              3  .   K   | ]  } |        y wrL  r  r  s     r  r  z<TypeChecker.check_for_missing_annotations.<locals>.<genexpr>&  s     J6I)!,6Is   r  r   returnr  )r  rh  r   anyr  r  r@  r0  warn_incomplete_stubdisallow_incomplete_defsdisallow_untyped_defsr  rS  r}  rk  r   RETURN_TYPE_EXPECTEDr   rl  r  r  NO_UNTYPED_DEFFUNCTION_TYPE_EXPECTEDr   r"  r  r   r   ARGUMENT_TYPE_EXPECTED)rH  r  has_explicit_annotationshow_untypedcheck_incomplete_defsr  r  s         @r  ra  z)TypeChecker.check_for_missing_annotations  sI   	U
 #-TYY"E #
# N
/3yy/B/BdiiFXFXEY/YN
 K
  000UDLL4U4U $ E E aJaT\\??CXyy T\\%G%G~~'1,*f4q8IU8RII.CCTJ/5d>O>O		O !&!5!5 "  II.EEtL ?P5 DII|4*499+=+=>%h/II.CCTJ&&)66xARARS 		"2"G"GN&&:h+I)$*H*H*RS		"2"G"GNJdii6I6IJJII.EEtL K 5 DY<r  c                V   t        |j                        }t        ||d      }|j                  j                         r=| j	                  |j
                  | j                         |t        j                  dd       y t        t        |j
                        t        t        t        t        f      sN| j                  t        j                   j#                  t%        |j
                  | j&                              |       y | j	                  |j
                  ||t        j                  dd       y )NTis_classmethodreturnszbut must return a subtype of)r   r  r   is_metaclassrA  r  	type_typer   INVALID_NEW_TYPEr  r   r   r   r   r   rk  NON_INSTANCE_NEW_TYPErm  rA   r0  )rH  r  r  	self_type
bound_types        r  r`  z#TypeChecker.check___new___signature)  s    *4995	sIdC
 99!!###  11. J//07HiQ`2a
 II 66==
 3 3T\\B 	 ## 11.r  c           	        |j                   sJ t        t        t        j                        t        t        j                        gt
        j                  t
        j                  gddgt        t        j                        | j                  d            }t        ||      s| j                  j                  ||       y|dv ryt        |j                        }t        |t              ryt        |t              r|j                  j                   dk(  ry|j"                  d   t$        k(  r<|j'                  |j(                  d   gdz  t        gdz  |j*                  d   dg      }t-        |j(                        dk\  sJ t.        j0                  |   }t        |j(                  d	         }t        |t2              rt        |j4                        }nCt        |t6              rt9        |      }n't        |t:        t<        t>        f      r|j@                  }t        |tB              r6|jD                  }	t        |	t              r|	j                  jF                  }
|
|
}dfd
t        |t        tH        f      r	 ||      sy|j(                  d	   }| jJ                  jM                  |||      }| jO                  |||j                   ||||       y)z1Check a reverse operator method such as __radd__.Nbuiltins.function)__eq____ne__r  r   r  r  r  rw  r}  r   c                    t        | t              r| j                  j                        S t	        fdt        | j                               D              S )Nc              3     K   | ]N  }t        |t              xr	  |      xs- t        |t              xr |j                  j	                         P y wrL  )r  r   r   rh  has_readable_member)r  xr  r  s     r  r  zSTypeChecker.check_reverse_op_method.<locals>.has_readable_member.<locals>.<genexpr>  sZ       @A Ay)J.A!T.J Rq(+P0J0J40PR?s   AA)r  r   rh  r  allr   relevant_items)r  r  r  s    `r  r  z@TypeChecker.check_reverse_op_method.<locals>.has_readable_member  sL    #x(xx33D99  *#*<*<*>?  r  rN  )r  zUnionType | Instancer  strr  r  )(r  r   r   r   r  r   rI   rj  r   r  invalid_signaturer   r  r  r   rh  r]  rw  rJ   r  r  r}  rS  r   normal_from_reverse_opr   r  r   r   r   r   r   fallbackr   r  r  r   r"  analyze_external_member_accesscheck_overlapping_op_methods)rH  r  reverse_typereverse_namer  method_typer  forward_nameforward_instr  opt_metaforward_baseforward_typer  s                @r  re  z#TypeChecker.check_reverse_op_methodL  s|    yyy #Y++,gi6L6L.MN]]EMM*4LI**+OO/0
 ,4HH&&|W=// #<#8#89h(h)}}%%)::!!!$0'55'11!459")a-'11!4c: 6 L
 <))*a/// 77E&|'='=a'@AlK0*<+C+CDLi0),7L|]K&PQ'00LlH-$$D$)9933'#+L	 |h	%:;#L,?#--a0((GG, H 
 	))II 	* 	
r  c           	        t        |g      D ]  }t        |      }t        |t              r4| j	                  |||      s2| j
                  j                  |||||       Rt        |t              rE|j                  D ]5  }	| j	                  |	||      s| j
                  j                  |||||       7 t        |t              r| j
                  j                  ||        y)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)
rH  r  r	  reverse_classr  r  r  r  forward_itemr  s
             r  r  z(TypeChecker.check_overlapping_op_methods  s    ` 2<.AL*<8L,500|\ZHH??%|\<QX L*5(..D44T<VCC)<|U\ /
  g666|WM Br  c                   t        |j                        dk  ry|}t        |t              rt	        |      }|j                  ||j                  d   gt        j                  gdz  d gdz        }|j                  |j                  d   |j                  d   gt        j                  gdz  d gdz        }|j                  d   }t        |t              rt	        |      }t        ||      ryt        ||      r|}|}	n|}|}	| j                  j                         }
|
r|
j                  j                  ng }t        ||	|d      S )Nr   Fr   r  r  )partial_only)rS  r  r  r   r   r  r   rI   r   r   r&  r  r  r  r  )rH  r  r  r  forward_base_erasedforward_tweakedreverse_tweakedreverse_base_erasedfirstsecondr  r  s               r  r  z$TypeChecker.is_unsafe_overlapping_op  sZ    |%%&*
 +lK0"0"> '44*L,B,B1,EF}}o)fqj 5 

 '44#--a0,2H2H2KL}}o)fqj 5 
 +44Q7);7"01D"E+-@A+-@A#E$F#E$F

//14AM&&00r	8695
 	
r  c                   |j                   }|t        j                  vryt        | j	                  |            }|j
                  }d|dd z   }|j                  |      rut        |      }t        | j                  j                  |||            }d}t        |t              rt        ||      sd}nd}|r| j                  j                  |||       yyy)zrCheck an inplace operator method such as __iadd__.

        They cannot arbitrarily overlap with __add__.
        N__r  FT)r  r   inplace_operator_methodsr   r   r  r  r   r   r"  r  r  r   is_more_general_arg_prefixr  signatures_incompatible)	rH  r  methodr  rS  other_methodinstancetyp2rk  s	            r  r  z)TypeChecker.check_inplace_operator_method  s    
 ;;;**401iifQRj(""<0$S)H"!!@@xY]^D D$-1#t<D 00tL  1r  c           	        t        | j                  j                        dk(  r||dk(  r!| j                  t        j
                  |       y t        | j                  d      gt        j                  gd gt        t        j                        | j                  d            }n| j                  j                         r~t        t        t        j                        | j                  d      gt        j                  t        j                  gd d gt        t        j                        | j                  d            }ny t        ||      s| j                  j!                  |||       y y )Nr   rP  rX  r  )rS  r&  rT  rk  r   MODULE_LEVEL_GETATTRIBUTEr   rj  r   rI   r   r   r  r  r   r  $invalid_signature_for_special_method)rH  r  r  r  r
  s        r  rf  z TypeChecker.check_getattr_method2  s   tzz A%))		*DDgN '01	../ 34K ZZ$$&&//0$//.2QR.t	../ 34K #{+HH99#wM ,r  c           	        | j                   j                         sy t        t        t        j
                        | j                  d      t        t        j
                        gt        j                  t        j                  t        j                  gg dt               | j                  d            }t        ||      s| j                  j                  ||d       y y )NrX  )NNNr  rQ  )r&  r  r   r   r   r  rj  r   rI   r   r   r  r)  )rH  r  r  r
  s       r  rg  z TypeChecker.check_setattr_methodO  s    zz&&("	..//	../
 ]]EMM5==9JOO/0

 #{+HH99#wV ,r  c           	         | j                  d      }t        || j                  d|g      g      }| j                  |||t        j
                  ddt        j                         y)zCheck the type of __slots__.rX  typing.Iterablezactual typezexpected typerU  N)rj  r   ri  rA  r   INVALID_TYPE_FOR_SLOTSr  r  )rH  r  r  str_typeexpected_types        r  check_slots_definitionz"TypeChecker.check_slots_definition`  sh    ??>2!t../@8*MN
 	33!! 	 	
r  c                   | j                   j                         syt        |      }t        |t              rt        d |j                  D              s-| j                  j                  d|t        j                         yy)z2Check that __match_args__ contains literal stringsNc              3  2   K   | ]  }t        |        y wrL  )is_string_literalr  r  s     r  r  z/TypeChecker.check_match_args.<locals>.<genexpr>u  s      5
09d#	   zb__match_args__ must be a tuple containing string literals for checking of match statements to workrU  )r&  r  r   r  r   r   r  r  r  r  LITERAL_REQ)rH  r  r  r  s       r  check_match_argszTypeChecker.check_match_argsp  sn    zz&&(c"#y) 5
03		5
 2
 HHMM.&&	  2
r  c                \   g }t        |j                        xs g }|j                  r'||j                  j                  j                  xs g z  }|D ]U  }t        |t              s|j                  s!|j                  |j                  D cg c]  }|j                  |f c}       W |ry| j                  j                  sc| j                  j                  sMg }t        j                  | D ]4  }t        |      }	|j                  t!        ||	      t#        ||	      f       6 |S ||fgS c c}w rL  )listr  r  r  r  r  r   rk  r  idr0  mypycinspections	itertoolsproductdictexpand_funcr2   )
rH  r  r  substtvarstvarvalueresultsubstitutionsmappings
             r  r]  zTypeChecker.expand_typevars  s     57S]]#)r99TYY^^--33ED$,DKKHK5tww.KHI  $,,,,0H0H:<F!*!2!2E!:}-{49;sG;TUV "; M3K=  Is   D)
c                j   d}|j                   r5|j                   j                  |j                        x}r|j                  rd}|sn|rk|j                  s^|j                  dvrOt        |j                        s9| j                  j                  |j                  |d   j                  |xs |       y y y y y y )NFTrJ  rL  r   )	r  rg  r  r  r  r  r  #explicit_override_decorator_missingr]  )rH  r  r  r  r  r  s         r  r  z-TypeChecker.check_explicit_override_decorator  s     !99$))--		"::$:@U@U  $ !)--		!88tyy)HH88		4Q7@@'/T * 9 . * !r  c                   |j                   dvxr) | j                  j                  xs |j                          }g }|j                  j
                  dd D ]-  }| j                  |||      }| y|s|j                  |       / |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.
        )rJ  rL  rJ  r1  r   N)r  r0  check_untyped_defsr2  r  mro*check_method_or_accessor_override_for_baser  )rH  r  check_override_compatibilityr  baserE  s         r  r  z!TypeChecker.check_method_override  s     (,yy 9
 (
 (I
 ||..Gdoo6G2G 	% 57!IIMM!"%DDDd8F ~)006 & )(r  c                   d}|r|j                   }|j                  j                  |      }|rrt        |j                        r2t        |      s'| j                  j                  ||j                   |       |j                  r| j                  ||j                  |       d}|rB| j                  |||      ry|t        j                  v rd|dd z   }| j                  |||      ry|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.
        FTNr  r  )r  r:  rg  r   r  r  r  cant_override_finalis_final$check_if_final_var_override_writable(check_method_override_for_base_with_namer   r   )rH  r  rP  rO  found_base_methodr  	base_attrr#  s           r  rN  z6TypeChecker.check_method_or_accessor_override_for_base  s     "99D

t,I 0D9IHH00tyy$G====dINNTXY$(!+ @@tTR9===!DH_F
 DDT6SWX#  r  c           
     J   |j                   j                  |      }|rt        |t              s|}n|j                  }t        |t
        t        f      r8| j                  |      |j                  xs |j                  }|j                  }n|j                  j                  sJ |j                  j                  J |j                  j                  |j                  j                  xs |j                  j                  }|j                  j                  }t              t        t              r1t        |      s&t        | j                   j#                         |      t        |j                        }|j$                  }	|t        |t&              r| j(                  | j*                  k  r| j-                  ||j.                         yt        |	t
        t        f      r| j                  |	      }nst        |	t              r| j                  |	j                        }nGt        |	t0              r-|	j                  t        |	j                        }nt3               }n
t3               }t        |	t
        t        f      r|	j                  xs |	j                  }
n9t        |	t              r'|	j                  }|j                  xs |j                  }
nd}
t        |t              r6| j5                  |||j.                  |      }|	rt7        |	      rt9        |      }t7        |      rt        t              r}n| j;                  |      }||t9              t        |	t0              rz|	j<                  sn|	j6                  r|	j>                  rVt        |t              rFt        |t@              s6| jB                  jE                  d|j                  tF        jH                         t        |t@              st        t@              r	 yt        |t              rt        t              r| jK                  ||jL                  ||jL                  |
||      }|r|	rtF        jN                  | jP                  jR                  v r| jU                  |	      rntW        |d      s`tY        || jP                        \  }}tZ        j\                  j_                  d|jL                   d	| d
| d      }| jE                  ||       yt        |t`              rtc        fd|jd                  D              r|	rtF        jN                  | jP                  jR                  v rq| jU                  |	      r`tY        || jP                        \  }}tZ        j\                  j_                  d|jL                   d	| d
| d      }| jE                  ||       ytg        |      r	 y|	r| jU                  |	      stW        |      r	 y| jB                  ji                  |jL                  ||jL                  ||       y)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.
        r  TFz;Cannot override writeable attribute with read-only propertyrU  ignore_pos_arg_namesrY  z (base class "z" defined the type as z, override has type )c              3  :   K   | ]  }t        |d         yw)TrY  Nr  )r  orig_typr  s     r  r  zGTypeChecker.check_method_override_for_base_with_name.<locals>.<genexpr>u  s%      > 3H 3tDD 3s   originaloverride)5r:  rg  r  ra   r  ri   r|   r   ry  rt  r  is_readyrh  r   r   r   r&  rr  r  r   r<  r  r  r  r   r   bind_and_map_methodr  get_property_typer  rS  is_settable_propertyr   r  rk  r  OVERRIDEcheck_overrider  MUTABLE_OVERRIDEr0  enabled_error_codesis_writable_attributer   rC   r   'COVARIANT_OVERRIDE_OF_MUTABLE_ATTRIBUTEwith_additional_msgr   r  r  r   %signature_incompatible_with_supertype)rH  r  r  rP  rW  r  override_class_or_staticoverride_classr  original_nodeoriginal_class_or_staticr  innerokbase_stroverride_strr  r  s                    @r  rU  z4TypeChecker.check_method_override_for_base_with_name  s?    JJNN4(	
 dI.)) $*; <= ..t4+/==+JDNN(!%xx((((xx}}000hhmm+/99+=+=+TATAT(!%!3!3!#&C#|,Yw5GTZZ%@%@%BSab ,INN;M%NNM $
=+(N==4>>1 OOD$))49J/KL$($6$6}$EMy9$($6$6}7I7I$JMs3 %))5(78J8J(K(0
 %-JM-'3D)EF+8+A+A+\]E\E\(M95$))+/==+JDNN(+0(-6 $ 8 8MSWS\S\^b c [%?$5m$DM4 c<0E 66sGDE$C+C0C"=#6 - 6 6!.!:!:m>`>`&tY7  *-A HHMM ] $		%*^^	 *  -1ZW5M@  M<8Z\=Z((!IIII,,	 %..$,,2R2RR22=A&}cPTU-C%sDLL.*Hl +RRff(3I( T..:^1>C IIc7+H G M95# > - 3 3> ; "..$,,2R2RR22=A .D%sDLL.*Hl +RRff(3I( T..:^1>C IIc7+" ! }c2   22=AsM2 
  >>IItTYY-Z] ?  r  c                   t        |j                  t        t        t        f      rAt        |j                        s+t        |j                  t              r!|j                  j                  j                  }n|j                  j                  }t        t        t        |||            }| j                  j                         }t        |t              r|rg }|j                  D ]h  }	|	j                  s|j!                  |	       |	j                  d   }
t        |
t"              r|
j$                  }
t'        ||
      sX|j!                  |	       j |rt        |      }t)        |||      S t        t        t        |||            S )a*  Bind self-type and map type variables for a method.

        Arguments:
            sym: a symbol that points to method definition
            typ: method type on the definition
            sub_info: class where the method is used
            super_info: class where the method was defined
        r   )r  r  ri   r|   ra   rt  r  ry  r   r   r   r&  rr  r   r  r  r  r   r  r   r   )rH  symr  sub_info
super_infois_class_method
mapped_typrr  filtered_itemsr  item_args              r  rb  zTypeChecker.bind_and_map_method  s1    chh*;Y GHQZHHR
 #((I."%((--"8"8"%(("3"3l,CCS],^_J#zz::<*j16F "$&,,D>>&--d3#~~a0H!(K8#+#7#7!"2H=&--d3 - "!+N!;JZ)9?KK&=c8Z&XYYr  c                   t        |t              r^|j                  rQ|j                  d   t        k(  r;t	        |j
                  d   |j                  D ch c]  }|j                   c}      S y t        |t              rH|j                  D cg c]  }| j                  |       }}|D cg c]  }|s|	 }}|rt        |      S y J d       c c}w c c}w c c}w )Nr   z,Need to check all FunctionLike subtypes here)r  r   rw  rI   r(   r  r  r:  r   r  get_op_other_domainr   )rH  tpr  it	raw_itemsr  s         r  r~  zTypeChecker.get_op_other_domain  s    b,'||Q7 : &bll1obll7Sll7STTJ'@BI"11"5II"+2)BrR)E2,U33HHH5 8T J2s   C
C+C3Cc	                	   d}	d}
t        ||d      sd}	nWt        |t              rG| j                  |      r6| j	                  |      }| j	                  |      }|r|rt        ||      sd}	d}
t        |t
              r[|r|sd}	nTt        |t              rDt        |t              r4|j                  |j                  d}	|j                  |j                  d}	t        |      rd}	|	rd}t        |t              xr |j                  }t        |t        t        f      r|j                         }t        |t        t        f      r|j                         }t        |t              rt        |t              rtt        |j                        t        |j                        k(  rH|j                  |j                  k(  r.|j                         d}t        |j                   t"              r |j                   j$                  j&                  }dfd}t)        t+        |j,                  |j,                              D ]R  \  }\  }}|j/                         r/|j/                         r|j                  |   }|j                  |   }n{|j1                         rj|j1                         rZ|sX|j2                  |   }||j2                  v r:|j                  |   }|j2                  j5                  |      }|j                  |   }nÌt        | ||            r|}t        |t"              rB|j6                  s6|j8                  |t        |j:                        z      }|j<                  dk7  r|}| j>                  jA                  |dz   |||||||       d}U t         ||jB                        |jB                        s| j>                  jE                  ||||jB                  |jB                  |       d}nt        |t              rt        |t              rg }|jF                  D ]>  }t)        |jF                        D ]$  \  }}t        ||      s|jI                  |        > @ t        |      t        |jF                        k(  r.|tK        |      k7  r | j>                  jM                  ||||       d}|s!| j>                  jO                  ||||||	       |
r"| jQ                  d
|tR        jT                         |	 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.
        FTrY  Nc                    t        |       S )N)ids_to_erase)r(   )r  override_idss    r  erase_overridez2TypeChecker.check_override.<locals>.erase_override.	  s    )!,GGr  rK  r   )secondary_contextr^  zHOverloaded operator methods can't have wider argument types in overridesrU  )r  r   r  r   )+r   r  r   r  r~  r   r   
type_guardrs  r  unpack_kwargsr  rS  r  min_argstype_var_ids
definitionri   r  r  r  ziprw  is_positionalis_namedr}  indexr  r  
bound_argsr  r  $argument_incompatible_with_supertyper  'return_type_incompatible_with_supertyper  r  sorted.overload_signature_incompatible_with_supertyperl  r  r  re  )rH  r`  r_  r  name_in_superr[  rp  rm  r  rk  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  rf  zTypeChecker.check_override  s}   8 $(H4HD*-$2K2KD2Q #66x@O"66x@O#"?OD'+$h-'0HHl3
8\8Z&&2x7J7J7RD##/H4D4D4LDdDK)(LA\hF\F\(\:$>?#88:(\:$>?#88: 8\2x6**+s83E3E/FF%%):)::  (446 	h117; ( 3 3 8 8 = =IH 2;**H,>,>?2-A-*  --/J4L4L4N,4,>,>q,A),4,>,>q,A)!**,1D1D1FO_#+#5#5a#8#x'9'99080B0B10E-)1););)A)A()KJ080B0B:0N-$ %&7HY9Z[+/%dG4T=M=M'+~~a#h>Q>Q:R6R'SH'}}2*2EEE %)-%#.2 F 	 '+A2D ".1B1B"CXEVEVWHHDDmY8I8I8K\K\^b #'KHj1j:6V %-^^M-6x~~-F)>%m^D!LLO! .G &4 u:X^^!44&-9OHHKKmY #'K>>-D8V^ ?  $		^  
 xr  c                    |j                   rt        |j                   t              syt        |j                   j                        }t        |      syt        |      }|syt        d |D              r| j                  j                  |       yy)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     K   | ]9  }t        |j                  t              xr |j                  j                  d k(   ; yw)builtins.FalseN)r  exprrx   r]  )r  rets     r  r  z7TypeChecker.check__exit__return_type.<locals>.<genexpr>	  s;      
 sxx*Tsxx/@/@DT/TTs   ?A)
rh  r  r   r   r  has_bool_itemr   r   r  incorrect__exit__return)rH  r  r  r  s       r  r7  z$TypeChecker.check__exit__return_typez	  sx     yy
499l C"499#5#56X&'- 

 
 HH,,T2	
r  c           
     	   |j                   }|j                  dd D ]H  }|j                  s| j                  t        j
                  j                  |j                        |       J | j                  j                  |j                         5  | j                  d      5  | j                  }t               | _        | j                  j                         5  | j                  j                  |j                         5  | j!                  |j"                         ddd       ddd       || _        |j                   j$                  s=|j                   j&                  s'|j                   j(                  s| j+                  |       |j,                  s| j/                  |       | j1                  |       | j3                  |       |j4                  rt7        |j                   | j8                        }t;        |j4                        D ]  }t=        |t>              rt=        |j@                  tB              r.| jD                  j!                  |      }| jG                  ||      }d}	t=        |tH              r|jJ                  xs d}	| jL                  }
d| _&        | jD                  jO                  ||gtP        jR                  g||	      \  }}|
| _&         ddd       ddd       |jT                  jV                  r|jT                  jX                  |jZ                  D ]  }t]        |j^                  |j`                  jT                  jV                        D ]  \  }}t=        |tb              s|jd                  tf        k7  s+t=        |tb              s<|jd                  |jd                  k7  sV| j                  d|j                   d|th        jj                  	         |jT                  jV                  r%| jm                  |jT                  jV                         |jn                  r'|jT                  jV                  r| jq                  |       |j,                  s'|j                   j(                  r| js                  |       tu        |j                          y# 1 sw Y   zxY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w)
zType check a class definition.r   NT)ry  rN  )callable_namezVariance of TypeVar "z+" incompatible with variance in parent type)r  rV  );r  rM  rS  rk  r   CANNOT_INHERIT_FROM_FINALrm  r  r$  rx  r_  r!  r   r`  r&  ry  rf  ra  typeddict_type
tuple_typeis_enumcheck_init_subclasshas_incompatible_baseclasscheck_multiple_inheritancecheck_metaclass_compatibilitycheck_final_deletable
decoratorsr$   rj  reversedr  r\   analyzedr~   r"  	temp_noder   r]  rD  
check_callr   rI   r  r  	type_argsbasesr  r  rh  r   rh  rP   r  r*   r\  r|  check_protocol_variance
check_enumr   )rH  r  r  rP  r  sig	decoratordectempr]  old_allow_abstract_callr  	base_inst	base_tvarbase_decl_tvars                  r  visit_class_defzTypeChecker.visit_class_def	  s   iiGGABKD}}		*DDKKDIIVX\]   [[$$TYY/1I1ISW1I1XJ/1DK..0ZZ**4995KK		* 6 1 %DKII,,		0D0D		HYHY((.22//4..s3&&s+,TYYH!)$//!:I!)X6:!**K< !++229=C>>#y>AD#H!)W5#,#5#5#= /3.F.F+/3D,!..99dVemm_d( : FC 0GD,) ";' 2Y/T 88#(("4"4"< YY	14NNINN$7$7$A$A2-I~ #9k:%..);&~{C*33y7I7II		3INN3C D; ;$(!&	 " 2 '  88''(:(:;??sxx11((...4993D3DOOD!dii(} 65 10 2Y1X//sU   R66R)&R;R	RF0R)R6RRR&!R))R3	.R66S c                   |j                   D ]|  }|j                  j                  |      }|s!t        |j                  t
              s<|j                  j                  sS| j                  t        j                  |j                         ~ y rL  )
deletable_attributesr:  rg  r  r  r   rS  rk  r   CANNOT_MAKE_DELETABLE_FINAL)rH  r  attrr  s       r  r  z!TypeChecker.check_final_deletable	  s[     ,,D99==&D
499c2tyy7I7I		*FF		R -r  c                   |j                   j                  r-|j                   j                  j                  j                  dvry|j                   j                  dd D ])  }d|j
                  vrt        |j                        }||_        t        |d      }t        |j                  j                               }t        |j                  j                               }d|v r3|j                  d      }|j                  |       |j                  |       t         gt#        |      z  }t%        ||||      }	|j&                  |	_        |j(                  |	_        |j*                  |	_        | j,                  j/                  |	dd        y y)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.typerT  Nr   rJ  	metaclassTallow_none_returnalways_allow_any)r  r  rh  r]  rM  r:  rx   r  r  rv   r9  keywordsrk  keysr  r6  rH   rS  r\   r  r/  end_liner"  rf  )
rH  r  rP  rE  calleer  r}  r  rw  	call_exprs
             r  r  zTypeChecker.check_init_subclass	  sF    99##		(@(@(E(E(N(N W
 )
  IIMM!"%D"$**4 +I!IN	+>?F,,./D*.t}}/A/A/C*DI i'ook2c""c$i/I y)DI!YYIN#{{I!%I$$Y$Y]$^ - &r  c                6   |D ]  t        t              rj                         s$t        j                  j
                        s| j                  d       j                  sct        fdj                  D              r| j                  d        y )Nz3TypeVar default must be a subtype of the bound typec              3  <   K   | ]  }j                   |k(    y wrL  )r  )r  rD  tvs     r  r  z5TypeChecker.check_typevar_defaults.<locals>.<genexpr>
  s     $PiURZZ5%8is   z3TypeVar default must be one of the constraint types)	r  r   has_defaultr   r  r  rk  rk  r  )rH  rB  r  s     @r  r\  z"TypeChecker.check_typevar_defaults
  sm    Br;/BNN4Dbjj"..9		OQSTyy$Pbii$P!P		OQST r  c                R   |j                   j                  sJ |j                   j                  t        vrd|j                   j                  v rs|j                   j                  d   }t        |j                  t              r@|j                  j                  r*| j                  t        j                  |j                         |j                   j                  dd D ]4  }|j                  s|j                  t        vs#| j                  ||       6 | j                  rj| j                  j                  dvrR|j                   j                   s<| j                  d|j                   j                   d|       | j#                  d|       | j%                  |       | j'                  |       y )N__members__r   rK  >   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  r]  r   r:  r  r  r   has_explicit_valuerk  r   #ENUM_MEMBERS_ATTR_WILL_BE_OVERRIDENrM  check_final_enumr>  r8  enum_membersr  check_enum_basescheck_enum_new)rH  r  rv  rP  s       r  r  zTypeChecker.check_enum
  sB   yy    99Z/MTYY__4T))//-0C#((C(SXX-H-H 		*NNPSPXPXYIIMM!B'D||Z ?%%dD1 ( <<DII..6KK99))		%dii&8&8%9 :0 0  		b
 	d#D!r  c                    |j                   j                         D ]5  }| j                  |      s| j                  d|j                   d|        y  y )Nz+Cannot extend enum with existing members: "rM  )r:  rk  is_final_enum_valuerk  r  )rH  r  rP  rv  s       r  r  zTypeChecker.check_final_enum;
  sH    ::$$&C'',		G		{RSTVZ[ 'r  c                   t        |j                  t        t        f      ryt        |j                  t              syt        |j                  j                        skt        |j                  j                        sLt        |j                  j                        s-t        t        |j                  j                        t              ry| j                  xs |j                  j                  S )NFT)r  r  rh   ra   r   r  r  r   r   r   rh  r   r>  r  rH  rv  s     r  r  zTypeChecker.is_final_enum_valueA
  s    chh9 56#((C( sxx}}%'' /#((--8,G||:sxx:::r  c                   d}|j                   j                  D ]g  }||j                  j                  r|}|!|j                  j                  r8| j	                  d|j                  | j                         d|        y y)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 "rM  )r  r  rh  r  rk  r  r0  )rH  r  	enum_baserP  s       r  r  zTypeChecker.check_enum_basesY
  s{      &*	IIOOD TYY%6%6 	&tyy/@/@		CID^D^_c_k_kDlCmmno  $r  c                |   ddd}|j                   j                  D ]  }d}|j                  j                  r,t	        fd|j                  j
                  dd D              }n |j                        }|r=|r;| j                  dj                  |j                  | j                              |       |sd} y )	Nc                    | j                  d      }t        |xr' |j                  xr |j                  j                  dk7        S )NrL  zbuiltins.object.__new__)rg  r  r  r]  )r  
new_methods     r  has_new_methodz2TypeChecker.check_enum_new.<locals>.has_new_methodv
  sF    ),J JOOJOO,,0II r  Fc              3  L   K   | ]  }|j                    xr  |        y wrL  )r  )r  br  s     r  r  z-TypeChecker.check_enum_new.<locals>.<genexpr>
  s'     aM`AII C.2C CM`s   !$r   rK  zLOnly a single data type mixin is allowed for Enum subtypes, found extra "{}"T)r  r   r  r  )
r  r  rh  r  r  rM  rk  rm  r  r0  )rH  r  has_newrP  	candidater  s        @r  r  zTypeChecker.check_enum_newu
  s    	 IIOODIyy  aTYY]][\]_M`aa	*4995	W		''-vd.C.CDLL.Q'R
 ! $r  c                   |j                   y|j                  }t        |j                  d   g       }|j                  j
                  }t        |      D ]  \  }}t        |t              st        |      D cg c]%  \  }}||k(  r|nt        t        j                        ' }	}}t        |      D cg c]-  \  }}||k(  r
t               nt        t        j                        / }
}}t        ||	      t        ||
      }}t        ||d      rt        }nt        ||d      rt        }nt         }||j"                  k7  s| j$                  j'                  |j"                  |j(                  ||       " yc c}}w c c}}w )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.
        NrK  T)ignore_declared_variance)r  r  r   rM  r  r  r  r  r   r   r   r  r   r   rL   rK   rP   rh  r  bad_proto_variancer  )rH  r  r  object_typerB  r  rC  r  r  up_args	down_argsupdownrW  s                 r  r  z#TypeChecker.check_protocol_variance
  sY    >>%yytxx|R0		## 'GAtdK0 &e,#,DAq  !Av793I3I+JJ,  # &e,%,DAq &'!V!9O9O1PP,  %  g.y0IB$TB$BtD($4==(++DMM499hPTU+ (#%s   :*E852E>c           	        t        |j                        dk  ry|j                  dd }t        |      D ]  \  }}|j                  j                         |j                  j                         z
  }|D ]M  }t        |      r||dz   d D ]4  }||j                  v s||j                  vs!| j                  ||||       6 O  y)z.Check for multiple inheritance related errors.r   N)rS  r  rM  r  r:  r  r  check_compatibility)rH  r  rM  r  rP  non_overridden_attrsr  base2s           r  r  z&TypeChecker.check_multiple_inheritance
  s    syy>Qggabk ~GAt $(::??#4syy~~7G#G ,d# Q\E u{{*uDHH/D00tUCH * -	 &r  c                   |j                   |j                   S t        |j                  t              r| j	                  |j                        S t        |j                  t
              r[|j                  j                  r%| j                  j                  |j                        S t        |j                  | j                        S t        |j                  t              rt        t        j                        S t        |j                  t              rU| j                   j#                         5  | j                  j%                  |j                  |j                        cd d d        S y # 1 sw Y   y xY w)N)r  )rh  r  r  rh   r   r   r  r"  typeddict_callabler$   rj  r   r   r   r  r   r  filter_errorsalias_type_in_runtime_contextr  s     r  determine_type_of_memberz$TypeChecker.determine_type_of_member
  s    8888Ochh)%%chh//chh)xx&&((;;CHHEE'$//BBchh, 91122chh	*'') ((FFsxxUXU]U]F^ *)
  *
 s   *1E&&E/c                   |dv ry|j                   |   }|j                   |   }t        | j                  |            }t        | j                  |            }d}	t        |t              rt        d||d      }	|	r4t        |t              r$| j                  ||||      }
t        |	|
d      }nt        |t              rt        |t              r|j                         rM|j                         r=t        t        |j                               t        |j                                     }nV| j                  ||||      }| j                  ||||      }
t        ||
d      }n|r|rt        |j                  t              r t        |j                  |t        |            }t        |j                  t              r t        |j                  |t        |            }t!        ||      }|s||   j                  }t        |t              ry|wt#        |      rlt%        |      }t        ||      }nT|'| j&                  j)                  ||j*                  |       |'| j&                  j)                  ||j*                  |       d}t-        |j                        r2t/        |      s'| j&                  j1                  ||j*                  |       t-        |j                        r| j3                  ||j                  |       t        |j                  t              r|j                  j4                  rd}|s| j&                  j7                  ||||       yy)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
        )rJ  rL  rJ  Nr  Tr  rY  leftright)r:  r   r  r  r   r   r   rb  r   is_type_objr   type_objectr  r   r1   r   r   r  rc  r  cannot_determine_type_in_baser  r   r  rR  rT  allow_incompatible_override#base_class_definitions_incompatible)rH  r  base1r  r  r  r  
first_typesecond_typecall
second_sigrr  	first_sigsecond_nodes                 r  r  zTypeChecker.check_compatibility
  s   . ??D!T"$T%B%B5%IJ
%d&C&CF&KL j(+z:ztTDJ{L911&+sERJD*4HB
L1jl6[%%'K,C,C,E  /
0F0F0HI01H1H1JK !44UJUS	!55fk3PUV
	:DQK%**c*-ejj*mTWFXY
&++s+.v{{KWZI[\z;7B#Dk..{L9#/#K0"3K"@K#J<B!66tUZZM"66tUZZMB %j.>HH((uzz3?$55dFKKM fkk3'FKK,S,SBHH88ueSQ r  c                   j                         s0j                  s$j                  sj                  sj                  yj
                  dd D cg c]0  }|j                  r"t        |j                  d      s|j                  2 }}|syj                  t        fd|D              ry| j                  d       yc c}w )z<Ensures that metaclasses of all parent types are compatible.Nr   rK  r  c              3  J   K   | ]  }t        j                  |        y wrL  )r   r  )r  metar  s     r  r  z<TypeChecker.check_metaclass_compatibility.<locals>.<genexpr>M  s"      2
=HTJs))40[s    #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  rM  r  r   r   rk  )rH  r  entrymetaclassess    `  r  r  z)TypeChecker.check_metaclass_compatibility:  s     !!{{!!- 2
&##%e&:&:OL   & 	 
 )c 2
=H2
 /
 		J	

s   5Cc                    |j                   D ]?  \  }}| j                  j                  |      x}$| j                  |j                  |       A | j                  |       y rL  )r:  r,  rg  warn_deprecatedr  check_import)rH  r  r  r  rv  s        r  visit_import_fromzTypeChecker.visit_import_fromW  sR    zzGD!||''--:$$SXXt4 " 	$r  c                &    | j                  |       y rL  r   r  s     r  visit_import_allzTypeChecker.visit_import_all]      $r  c                &    | j                  |       y rL  r#  r  s     r  visit_importzTypeChecker.visit_import`  r%  r  c           	        |j                   D ]  }|j                  d   }| j                  |      \  }}}|t        t        j
                        }t        |j                  t              sJ t        j                  j                  |j                  j                        }| j                  ||j                  ||dd        y )Nr   z
local namezimported name)r  r  r  )assignmentslvaluescheck_lvaluer   r   r  r  rvaluerx   r   INCOMPATIBLE_IMPORT_OFrm  r  r  )rH  r  assignlvaluelvalue_typer  r  messages           r  r   zTypeChecker.check_importc  s    &&F^^A&F!%!2!26!:KB"%i&<&<=fmmX666&==DDV]]EWEWXG(((+ )  'r  c                L   |j                   r| j                  j                          y |j                  D ]n  }| j                  j                         rA| j	                         s y | j                  |      rA| j                  j                  |        y | j                  |       p y rL  )	rb  r!  r  r  rc  rd  r  re  rf  )rH  r  ss      r  visit_blockzTypeChecker.visit_blocky  s{     KK##%A{{))+<<>44Q7HH2215A r  c                    | j                         xrB | j                  j                  xr* | j                   xr | j                  j                          S rL  )in_checked_functionr0  warn_unreachabler=  r!  !is_unreachable_warning_suppressedrN  s    r  rc  z,TypeChecker.should_report_unreachable_issues  sS    $$& D--D...D KKAACC		
r  c                   t        |t              rt        |j                        ryt        |t        t
        f      ryt        |t              rt        |j                  t              ryt        |j                  t              ro| j                  j                  j                         5  t        | j                  j                  |j                  dd            }ddd       t        t              ryy# 1 sw Y   xY w)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  rV   is_false_literalr  r   r}   re   rc   r\   r"  r  r  r   rf  r   )rH  r3  r  s      r  rd  z$TypeChecker.is_noop_for_reachability  s     a$)9!&&)AIx01>*!&&,/AFFH-&&**88:)))00FFdT 1 C ; c?3 ;:s   '2C33C<c                T   |j                   r| j                  se| j                  |j                        5  | j	                  |j
                  d   |j                  |j                  du |j                         ddd       |j                   r| j                  |       |j                  | j                  j                  rt        |j                        ryt        |j
                  d   t              r5| j                  j!                  dt#        t$        j&                        |       n'| j                  j!                  d|j                  |       t)        |j                  | j                  | j*                  | j                  |       t-        |j
                        dkD  r| j/                  |j                        s%| j0                  j3                  |j                         | j5                  | j7                  |j                        |      }|j
                  dd D ]E  }| j                  |j                        5  | j	                  |||j                  du        ddd       G | j9                  |       |j                  r[|j                  rOt;        |j                        s:| j<                  j?                          | jA                  tB        jD                  |       |jF                  r.| jI                         s| j                  jK                  |       yyy# 1 sw Y   xY w# 1 sw Y   xY w)z|Type check an assignment statement.

        Handle all kinds of assignment statements (simple, indexed, multiple).
        rK  NzA type on this linezType of variablerN  r   )&is_alias_defr>  enter_final_contextis_final_defcheck_assignmentr*  r,  rh  
new_syntaxcheck_type_alias_rvaluer0  rb  r   r  r   r  rc  r   r   r  r   r@  rS  has_typer"  rf  r  lookup_typecheck_finalr   r&  r  rk  r   DEPENDENT_FINAL_IN_CLASS_BODYunanalyzed_typer6   annotation_in_unchecked_function)rH  r3  r,  lvs       r  visit_assignment_stmtz!TypeChecker.visit_assignment_stmt  sA    4<<))!..9%%aiimQXXqvv~q||\ : >>((+ FF44,QVV4!))B-3 44)793I3I+JA 445GQRSqvvt||T5J5JDHH^_`qyy>A ==*!!((2^^D$4$4QXX$>BFiin--ann=))"faffnE >= % 	NN#AFF+

'')5II&DDaHT%=%=%?HH55a5@ &@Q :98 >=s   AL3 LLL'	c                    | j                   j                         5  | j                  j                  |j                        }d d d        | j                  |j                  d          y # 1 sw Y   )xY wNrK  )r  r  r"  rf  r,  
store_typer*  )rH  r3  
alias_types      r  rA  z#TypeChecker.check_type_alias_rvalue  sP    XX##%**11!((;J &		"z2 &%s   &A))A2Tc                :   t        |t        t        f      r| j                  |j                  |||       y| j                  ||d       | j                  |      \  }}}t        |t              r5|j                  r(|j                  j                  }|dv rM|r|}	n| j                  j                  |      }	|	r+|dk(  r| j                  |	|       n| j                  |	||       |dk(  r1|xs | j                  j                  |      }
| j                  |
|       |dk(  r0|.| j                  j                  |      }
| j                  ||
|       |dk(  rQ| j                   j#                         }|r5t%        j&                  |      r | j)                  t*        j,                  |       t        |t.              rOt        |t0              r|j2                  3t        |t              r|j                  dk(  s| j5                  |||      ryt        |t6              r/|j                  dk(  r | j)                  t*        j8                  |       |rt        |t0              r|j2                  | j                  j                  |      }t        t;        |      t<              ry|j>                  }tA        ||jB                        rj| jE                  |      }|t| jF                  s4tI        |      }tK        |t=               g      }| jM                  |||       nd|_        ||= |j2                  }n| jO                  |j>                  ||       t        |t.              r| j5                  |||      rytQ        |      rFt        |t              r6t        |j                  tR              r|j                  jT                  r|s|}nt        |t6              rL|jV                  @| j                  j                  |jX                        }| j[                  ||||	      \  }}}n)tQ        |      r	t        |t              r|jV                  t\        k(  rt        |j                  tR              r|j                  j2                  r|j                  | j^                  v rt        t;        |      t`              s| j^                  |j                     }d
}tc        | jd                  jf                        D ]  }|jh                  |v s|jj                  } n |r(tm        |      }| jM                  |j                  ||       | jo                  |||	      }t;        |      }t;        |      }t        |tp              r|js                         r|ju                         jv                  s|ju                         jx                  rt        |tz              rwt        |j|                  t~              r]|j|                  j2                  jv                  s |j|                  j2                  jx                  r| j                  j                  ||       y|r|rt        |t0              st        |t              r|j                  s| jd                  j                  |||d
       t        |t              rxt        |j                  tR              r^|j                  j                  rH|j                  j                  r2|0tI        |      |j                  _        n|r| j                  |||       |r| j                  |      }| j                  j                  ||      }|jB                  s6|j                  s*t        |t              r|j                  dk(  stI        |      }| j                  ||||       | j                  |       y)z0Type check a single assignment: lvalue = rvalue.=)rQ  rP  rO  rQ  	__slots____match_args__Nr1  is_lvalue_finalrN  FrM  )Jr  r   rs   $check_assignment_to_multiple_lvaluesr  .try_infer_partial_generic_type_from_assignmentr+  rx   r  r  r"  rf  rg  rf  r0  r7  r&  r  r8  r9  rk  r   &DATACLASS_POST_INIT_MUST_BE_A_FUNCTIONr   r   rh  check_compatibility_all_supersrv   CANNOT_MODIFY_MATCH_ARGSr   r   r  is_valid_inferred_typerS  r>  r=  r)   r   set_inferred_typeinfer_partial_typeis_literal_noner   is_initialized_in_classkindr  check_member_assignmentrR   r5  r   r  r!  r  r:  r  r   r  r   r  r  is_abstractr|  r   r  r   r  concrete_only_assignis_special_formr@  is_inferredis_index_varcheck_indexed_assignmentget_variable_type_contextinfer_variable_typecheck_assignment_to_slots)rH  r/  r,  infer_lvalue_typer@  r0  index_lvalueinferredr  	signaturer  r  rvalue_typer  r*  inferred_typeinstance_typedecl_frame_maphas_if_ancestorr  p_rvalue_typep_lvalue_typerM  s                          r  r?  zTypeChecker.check_assignment  s*    fy(3455ff.? ??PST262C2CF2K/Kx &(+{{''MM"$/	$($5$5$<$<V$D	 =0 55iH 55iN;&%I):):)A)A&)IC//V<++0D++226:C))(C@?*#'::#:#:#<L#(:(Q(QR^(_		"2"Y"Y[ab 67+#K=+BRBRBZ#FH5&++IY:Y66v{FS&*-&++AQ2Q		*CCVLk;7K<L<L<T"&"3"3":":6"BK!/+">I %//C-k3<<X(,(?(?(D(4#'#=#=.OP[.\0E{T\T^F_0` $ 6 6sFM R+/ -c 2*-((K //U!&'2t7Z7ZV8 #F+"684"6;;4;;& #.Kvz2v{{7J$($5$5$<$<V[[$IMBFB^B^%{FF C_ C?K.? (/&vx8"KK4/&v{{C8"KK,,"KK4+?+?? *?;+G Q)-)=)=fkk)J +0%-dkk.@.@%AE$xx>9272I2I % &B +*<[*IK 226;;T"&">">{F\b">"cK !0 < / <}l;%113%113??(446BB"=(;"=#5#5x@%**//;;}?Q?Q?V?V?b?b HH11-H#4ZU`=a&vx8V=S=S//[RWX&vx8 *6;; < & 7 7 & 8 8 + 7/PQ\/]FKK,--lFFK#==hG"//66vL6Y%%,,"684HX9X"CK"PK((6;O**62r  )builtins.list+)builtins.set|r
   partial_type_augmented_opsc                d   g }|j                   ru|j                   j                  dd  D ]Y  }| j                  ||      \  }}|st        |t              r|j
                  r8t        |t              rI|j                  |       [ |sy |d   }|D ]  }t        ||      r|}t        ||      r y  |S Nr   r   )
r  rM  lvalue_type_from_baser  r   invalid_partial_typer   r  r   r   )rH  rl  type_contextsrP  	base_type	base_noder  others           r  rg  z%TypeChecker.get_variable_type_context  s    == ))!"-'+'A'A(D'Q$	9'	37I<Z<Z&y+>!((3 . !!$	"E 	2!		51 # r  c                   d}t        |t              rKt        |j                  t              r1t        |j                  j                  t
              r|j                  }n+t        |t              r| j                  j                  |      }||j                  }t        |t
              sJ |j                  y|dk7  r%|j                  j                  |f| j                  vry| j                  |      }|y| j                  j                  |      }t        |      }t        |t              r1|j                  |j                  k(  rt        |      r||_        ||= yyyt        |t               rt#        |j                        |_        ||= yyy)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'
        NrO  )r  rx   r  r   rh  r   rv   r"  get_partial_self_varr]  ry  r>  rf  r   r   rZ  r   r   )rH  r/  r,  opr  r  r*  rn  s           r  rV  z:TypeChecker.try_infer_partial_generic_type_from_assignment  sT    vx(6;;,6;;++[9++C
+##88@C?((Cc;///xxSychh//4D<[<[[ !33C8M$++226:K)+6K+x0##sxx/4J;4W*CH%c* 5X/ K11#((;!#& 2' r  c           	     R   |j                   }t        |t              r|j                  t        d fv rut        |j                  j                        dkD  rR|j                  j                  dd  D ]h  }|j                  j                  |j                        }|+| j                  |||j                         s n!| j                  |||j                         rh n |j                  j                         }|r|d   nd }|j                  j                  dd  D ]  }|j                  r|j                  dk(  r|j                   dk(  s.t#        |j                        rD| j%                  ||      \  }	}
t        |	t&              rd }	|	sn|
J | j)                  |||||	|
      s y||u s y y)Nr   r   rK  rP  r  TF)r  r  r   r_  rT   rS  r  r  rM  r:  rg  r  "check_compatibility_classvar_supercheck_compatibility_final_superdirect_base_classesr  r]  r  r|  r   check_compatibility_super)rH  r/  r0  r,  lvalue_noderP  tnodedirect_baseslast_immediate_baser  r  s              r  rX  z*TypeChecker.check_compatibility_all_supers  s    kk {C(d|+K$$**+a/#((,,QR0

{'7'78$BB;PTV[V`V`a??TSXS]S]^ 1 '++??AL6B,r"2#((,,QR0 ::$$3IZ8Zk../'+'A'A+t'T$	9i5 $I$00099VT9i
  $22 9 18 r  c           
        |j                   }t        |t              sJ d }|r|}	|j                   }nT| j                  j	                  ||      }	t        |t
              r(|j                   }t        |t              r|j                  }t        |      }t        |	      }	|	rSt        |t              rt        |	t              rt        |      }
t        |      }|!|	j                  rt        |	j                        }|
du rL|du rHt        || j                  j                               }t        |	| j                  j                               }	|
r	|rd|_        | j!                  |	||t"        j$                  dd|j&                   d      }|rit(        j*                  | j,                  j.                  v rC| j1                  |      r2| j!                  ||	|t"        j2                  d|j&                   dd      }|S y)NFTzexpression has typezbase class "z" defined the type as)r  r  r   r"  rf  rx   ra   r  r   r   is_node_staticr  r   r&  rr  is_staticmethodrA  r    INCOMPATIBLE_TYPES_IN_ASSIGNMENTr  r  rg  r0  rh  ri  rj  )rH  r/  r0  r,  rP  r  r  r  compare_nodecompare_typebase_staticcompare_staticrr  s                r  r  z%TypeChecker.check_compatibility_super  s    kk+s+++ &L!;;L,,33FIFL&(+%{{lI6#/#4#4L#I.	&|4)\2z,P\7],Y7!/!= ")l.E.E%3L4K4K%LN %'Ne,C !*)TZZ5P5P5R SI#,\4::;V;V;X#YL >26K/## AA%tyyk)>?B **dll.N.NN..y9'' $LL"499+-BC) Ir  c                2   |j                   }|j                  j                  |      }|rm|j                  }|j                  }t        |t              r"| t        ||t        |j                              }t        |t              r|j                  }|j                  }|rt        |      sY| j                  j                         }|J d       t        |t              rt!        |      }n|}t#        ||      }	t%        ||	      }t'        |      }t        |t(              r1t        |t*              r!|j,                  rt'        |j.                        }t        |t0              r5t        |t2              r%|j,                  r|j4                  d   j.                  }||fS y)zFor a NameExpr that is part of a class, walk all base classes and try
        to find the first class that defines a Type for the same name.z)Internal error: base lookup outside classr   NN)r  r:  rg  r  rh  r  r   r1   r   r  ra   r  r   r&  rr  r   r   r8   r3   r   r   ri   r  r  r   r|   r  )
rH  	expr_noderP  	expr_namebase_varr  r  r  r%  itypes
             r  r|  z!TypeChecker.lvalue_type_from_basei  sV   
 NN	::>>), I I)S)i.C,Y	=QZQ_Q_C`a	)Y/%NN	%NN	&y1 $

 ; ; =I$0]2]]0!)Y7#1)#<#,5hEE 7	5 II+I6	i6:iQX;Y !,,$3I4F4F$G	i6:0< !,,$-OOA$6$?$?	 )++r  c                n   t        |t              sy|j                  rF|j                  s:| j                  t        j
                  j                  |j                        |       y|j                  sF|j                  r:| j                  t        j                  j                  |j                        |       yyNTF)	r  r   is_classvarrk  r   CANNOT_OVERRIDE_INSTANCE_VARrm  r  CANNOT_OVERRIDE_CLASS_VARrH  r  rP  r  s       r  r  z.TypeChecker.check_compatibility_classvar_super  s     )S)I$9$9II&CCJJ499UW[\!!i&;&;II&@@GG		RTXYr  c                   t        |t        t        t        f      syt	        |j
                        ry|j                  rN|j                  st        |t              s2| j                  j                  |j
                  |j
                  |       y|j                  rB|j                  t        v s|j
                  t        v ry| j                  |j
                  ||       y)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   rh   ra   r  r  rS  r  rR  r]  r   r   rT  r  s       r  r  z+TypeChecker.check_compatibility_final_super  s     )c8Y%?@dii 4==
9c8R
 HH((DIItD==}}
*dii;M.M55diiDQr  c                l    d}|r| j                  |      }|r| j                  j                  ||       yy)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)ri  r  final_cant_override_writable)rH  r  r  r  writables        r  rT  z0TypeChecker.check_if_final_var_override_writable  s8     11)<HHH11$< r  c                    | j                   S )z:Check whether we a currently checking a final declaration.rB  rN  s    r  get_final_contextzTypeChecker.get_final_context  s    !!!r  c              #  b   K   | j                   }|| _         	 d || _         y# || _         w xY ww)zDStore whether the current checked assignment is a final declaration.Nr  )rH  r>  old_ctxs      r  r=  zTypeChecker.enter_final_context  s3      $$)	)!(DDs   /# /	,/c                $   t        |t              r| j                  |j                        }n+t        |t              r|j
                  g}n|j                  g}t        |t              r|j                  nd}|r| j                  j                         r|d   }t        |t              sJ |j                  t        |j                  t              sJ |j                  j                  rY|j                  j                  sC| j                  s7t        |t              r'|j                   | j"                  j%                  |       |D ]K  }t        |t              st        |j                  t              s0|j                  j&                  }| j                  j                         }||j(                  dd D ]  }|j*                  j-                  |      }|s!t        |j                  t              s<|j                  j.                  sS|rV| j"                  j1                  ||j                  j2                  du |        n |j                  j.                  s|r| j"                  j1                  ||j                  j2                  du |       N y)a  Check if this assignment does not assign to a final attribute.

        This function performs the check only for name assignments at module
        and class scope. The assignments to `obj.attr` and `Cls.attr` are checked
        in checkmember.py.
        Fr   Nr   )r  rX   flatten_lvaluesr*  rW   targetr/  r>  r&  r  r   r  r   final_unset_in_classfinal_set_in_initr>  rh  r  final_without_valuer  rM  r:  rg  rS  cant_assign_to_finalr  )	rH  r3  lvsis_final_declrH  r  rS  rP  rv  s	            r  rD  zTypeChecker.check_final  s    a(&&qyy1C>*88*C88*C*4Q*GUTZZ446QBb'***ww"!"''3///GG00GG55 LL q.1*HH003B"g&:bggs+Cww||jj--/? !$"jjnnT2 :chh#<"xx00 $ = =dCHHMMUYDY[\ ] % !, 77##MHH11$8LaP' r  c                   t        |t              sy t        | j                  j	                  |j
                              }t        |t              sy |j                  j                  y |j                  |j                  j                  v ry |j                  j                  d d D ]  }|j                  j                  d       y  |j                  j                  |j                        }|y | j                  ||j                        ry | j                  t        j                   j#                  |j                  |j                  j$                        |       y )NrK  rQ  )r  rv   r   r"  rf  r  r   rh  slotsr  rM  r:  rg  is_assignable_slotrk  r   NAME_NOT_IN_SLOTSrm  r]  )rH  r/  inst	base_infor  s        r  ri  z%TypeChecker.check_assignment_to_slots  s   &*-t0077DE$)99??";;$))//)s+I""=1=  , YY]]6;;/
 ""6:??;		..55fkk499CUCUVX^	
r  c                :    t        dd       ryt        |      }|t        |t              ryt        |t              r|j
                  j                  d      d uS t        |t              ryt        |t              rt         fd|j                  D              S y)Nr  FT__set__c              3  B   K   | ]  }j                  |        y wrL  )r  )r  ur/  rH  s     r  r  z1TypeChecker.is_assignable_slot.<locals>.<genexpr>D  s     M9at..vq99   )getattrr   r  r   r   rh  rg  r   r   r   r  )rH  r/  r  s   `` r  r  zTypeChecker.is_assignable_slot4  s    664(c";*S'2c8$
 88<<	*$66c<(c9%M399MMMr  c           	     |   g }|D ]3  }t        |t              s|j                  |       &t        | j                  j                  |j                              }t        |t              s|j                  |       v|j                  D ]  }t        |t              s|j                  t        |             .t        |j                        }t        |t              r|j                  }n-t        |t              r|j                  j                  dk(  sJ |}|j                  t        t        |                    6 |S )zFlatten expression list by expanding those * items that have tuple type.

        For each regular type item in the tuple type use a TempNode(), for an Unpack
        item use a corresponding StarExpr(TempNode()).
        builtins.tuple)r  r   r  r   r"  rf  r  r   r  r   r   rh  r   r  r   r]  )rH  rvaluesnew_rvaluesrvr  r  unpackedr  s           r  flatten_rvalueszTypeChecker.flatten_rvaluesG  s    Bb(+""2&!$"3"3":":277"CDCc9-""2&YY!!Z0&&x{3.qvv6H!(,<=#+#7#7 'x: ( 6 6:J JK $,&&x0B'CD  , r  c                   t        |t        t        f      rWg }d }d }t        | j	                  |j
                              D ]1  \  }}	t        |	t              r	t        | j                  j                  |	j                              }
| j                  |
      rt        |
t              r|6|| j                  |
|      k7  r!| j                  t        j                   |       ||dz   |k(  r&|j#                  |	       |}| j                  |
|      }| j                  t        j                   |       | j                  t        j$                  j'                  |
      |       !|j#                  |	       4 d }d }t        |      D ]m  \  }}	t        |	t              st        | j                  j                  |	j                              }
| j                  |
      sWt        |
t              sh||}|}o |\|Z|X||z
  dz   }t)        |      t)        |      |z
  z
  }|dkD  r1|d| t+        |      D cg c]  }t-        |       c}z   ||dz   d  z   }| j/                  |t)        |      |      rt1        d t        |      D        t)        |            }|d | }|t)        |      k7  rt3        t        ||         nd }||dz   d  }| j5                  ||t)        |            \  }}}t7        t9        ||            }|r9t        |      }|j;                  |       |j#                  |j                  |f       |j=                  t9        ||             |D ]  \  }}| j?                  |||        y y | jA                  ||||       y c c}w )Nr   r   c              3  H   K   | ]  \  }}t        |t              s|  y wrL  r  r   r  r  rH  s      r  r  zCTypeChecker.check_assignment_to_multiple_lvalues.<locals>.<genexpr>  s      S$651b*R:RQ$6   "")!r  r   rs   r  r  r  r   r   r"  rf  r  type_is_iterabler   iterable_item_typerk  r   CONTIGUOUS_ITERABLE_EXPECTEDr  ITERABLE_TYPE_EXPECTEDrm  rS  rv  r    check_rvalue_count_in_assignmentnextr   split_around_starr9  r  set_lineextendr?  check_multi_assignment)rH  r*  r,  r  rj  r  iterable_type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_listrH  r  s                              r  rU  z0TypeChecker.check_assignment_to_multiple_lvaluesf  sg    fy(34
 )+G)-M#'H"+D,@,@,N"O$dH-*4+<+<+C+CDII+NOD,,T2z$7Q(4$JaJa &K : !II&6&S&SU\]'/8a<83K 't 4+3040G0Gf0U $		*:*W*WY` a		"2"I"I"P"PQU"VX_`NN4(% #P& *.N'+L$W-4dH-*4+<+<+C+CDII+NOD,,T2z$7Q)1-.N'( . * ,!-+n<q@ #GG|0K L 1$.1<A-<PQ<Pq8M2<PQR!,"2"456  44Wc'lGT!SIg$6SUXY`Ua
 #;J/;EW;UD7:#67[_  $JN$45	040F0FZW1-(I  Hh 78&x0G$$V,OOW\\7$;<Iy 9:&FB))"b2CD '- U2 ''BST; Rs   4Nc                |   |t        d |D              s| j                  d|       yt        |      |kD  r!| j                  t        j                  |       yt        d t        |      D              }|}t        |      |z
  dz
  }|}||z
  dz
  }	||	kD  s||kD  r | j                  t        j                  |       yt        d |D              r<t        |      dz
  |kD  r*| j                  j                  |t        |      dz
  |       yy|t        |      k7  r'| j                  j                  |t        |      |       yy)Nc              3  <   K   | ]  }t        |t                y wrL  r  )r  es     r  r  z?TypeChecker.check_rvalue_count_in_assignment.<locals>.<genexpr>  s     @1z!X.   z/Variadic tuple unpacking requires a star targetFc              3  H   K   | ]  \  }}t        |t              s|  y wrL  r  r  s      r  r  z?TypeChecker.check_rvalue_count_in_assignment.<locals>.<genexpr>  s"     "a2DB
SUW_H`12Dr  r   Tc              3  <   K   | ]  }t        |t                y wrL  r  )r  r/  s     r  r  z?TypeChecker.check_rvalue_count_in_assignment.<locals>.<genexpr>  s     B'z&(+'r  )	r  rk  rS  r   $TOO_MANY_TARGETS_FOR_VARIADIC_UNPACKr  r  r  wrong_number_values_to_unpack)
rH  r*  rvalue_countr  rvalue_unpackleft_star_indexleft_prefixleft_suffixright_prefixright_suffixs
             r  r  z,TypeChecker.check_rvalue_count_in_assignment  s0    $@@@		KWU7|l*		*OOQXY""a)G2D"aaO)Kg,81<K(L'-7!;L\)[<-G 		*OOQXYB'BB7|a,.66|S\TUEUW^_  S\)HH22<WwWr  c                   t        |xs | j                  j                  |            }t        |t              rt        |j
                        }t        |t              r,|j                         }t        |      dk(  rt        |d         }t        |t              r!t        |j                        t        |i       }t        |t              rb|D ]\  }	t        |	t              r|	j                  }	| j!                  t        t"        j$                  |      |      }
| j'                  |	|
|       ^ yt        |t              r| j)                  ||||||       yt        |t              r| j+                  |||||       yt        |t,              r5|j.                  j0                  dk(  r| j2                  j5                  |       y| j7                  ||||       y)z:Check the assignment of one rvalue to a number of lvalues.r   r   Nr  rX  )r   r"  rf  r  r   r  r   r  rS  r   r   r  r2   r   r   r  r  r   r  r?  !check_multi_assignment_from_tuple!check_multi_assignment_from_unionr   rh  r]  r  unpacking_strings_disallowed$check_multi_assignment_from_iterable)rH  r*  r,  r  rj  rv_typeundefined_rvaluern  r  rH  r  s              r  r  z"TypeChecker.check_multi_assignment  s    &g&Q1B1B1I1I&1QRk?3)+*A*ABKk9-(779N>"a'-nQ.?@ {I.#K$5$56B &k26Kk7+b(+B NNI66;OQX	 %%b)5FG  Y/22g7GIZ Y/22g7H X.;3C3C3L3LP^3^HH11':55g/@r  c                   d| _         t        d | j                  |      D              }| j                  j	                         5 }|j
                  D ]  }| j                  |||||d       t        || j                  |            D ]J  \  }	}
|	j                  | j                  d   j                  |
t        t        j                                     L  	 ddd       t        d |D              }j                         D ]  \  }}t        |t              r|j                   }g }|D ]  \  }}|J |j                  ||f        t        | \  }}| j                  j#                  |t%        t'        |            t%        t'        |            d        t        || j                  |            D ]B  \  }}
| j)                  |
      \  }}}|r| j+                  ||
|       1| j-                  |
|       D d| _         y# 1 sw Y   (xY w)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   y wrL  r  )r  r  s     r  r  z@TypeChecker.check_multi_assignment_from_union.<locals>.<genexpr>)  s     2]?\!2?\s   )rj  r  r  r   Nc              3  2   K   | ]  }t        |        y wrL  )r   )r  cols     r  r  z@TypeChecker.check_multi_assignment_from_union.<locals>.<genexpr>;  s     M*31#6*r5  F)r2  tupler  r!  accumulate_type_assignmentsr  r  r  r  r   r6  r   r   r  r  r   r  r@  r   r9  r+  r[  rL  )rH  r*  r,  rn  r  rj  
transposedr)  r  r  rH  union_typesr  r  clean_itemsrh  declared_typerz  declared_typesunion_1_2rl  s                          r  r  z-TypeChecker.check_multi_assignment_from_union  s   ( !%-22]t?S?ST[?\2]-]
[[446+#)) ++&7 %) ,  !T-A-A'-JKEAr HHT__Q/33B	@V@V8WXY L * 7  M*MM&,,.KD%$)yy 46K',#m$000""D-#89 (- %($5!E>KK##%d5k2%d>&:;	 /& [$*>*>w*GHIE2#004BH&&xU;E* I !&W 76s   BG;;Hc                    g }|D ]o  }t        |t        t        f      r*|j                  | j	                  |j
                               t        |t              r|j                  }|j                  |       q |S rL  )	r  r   rs   r  r  r  r   r  r  )rH  r*  resrH  s       r  r  zTypeChecker.flatten_lvaluesX  s`     "B"y(34

4//9:"h'WWJJrN  
r  c                   t        |j                        }| j                  |t        |j                        ||      rat	        d t        |      D        t        |            }|d | }	|t        |      k7  rt        t        ||         nd }
||dz   d  }|s| j                  ||      }t        | j                  j                  ||            }t        |t              rt        |j                        }t        |t              r,|j!                         }t        |      dk(  rt        |d         }t        |t              r| j#                  |||||       y t        |t$              ry t        |t&              sJ |}| j)                  |j                  |t        |            \  }}}t+        |	|      D ](  \  }}| j-                  || j/                  ||      |       * |
rt1        |D cg c]I  }t        |t2              s| j/                  ||      n$t        | j/                  |j4                  |            K c}      }|j7                  |       | j-                  |
j8                  ||       t+        ||      D ](  \  }}| j-                  || j/                  ||      |       * y |r(|D ]!  }t        |t:              rYt        |j<                  t>              r?|j<                  j4                  )t%        t@        jB                        |j<                  _        mt        |t              s~t        |j8                  t:              st        |j8                  j<                  t>              s|j8                  j<                  j4                  | jE                  dt%        t@        jB                        g      |j8                  j<                  _        $ y y c c}w )N)r  c              3  H   K   | ]  \  }}t        |t              s|  y wrL  r  r  s      r  r  z@TypeChecker.check_multi_assignment_from_tuple.<locals>.<genexpr>q  s      O 2uq"jX6N 2r  r   r   ru  )#r   r  r  rS  r  r  r   r   lvalue_type_for_inferencer   r"  rf  r  r   r  r   r  r  r   r   r  r  r?  r  rs   r   rh  r  r  rx   r  r   r   r  ri  )rH  r*  r,  rn  r  r  rj  r  r  r  r  r  r0  reinferred_rvalue_typer  left_rv_typesstar_rv_typesright_rv_typesrH  r  	list_exprs                        r  r  z-TypeChecker.check_multi_assignment_from_tuplec  sR    ,K,=,=>00S**+WM 1 
 O	' 2OQTU\Q]J {
+H=G3w<=Wd8WZ%89]aG
Q 01I#"<<WkR)8%%,,V[A*& 4oF-<=S=_=_-`*4i@%;%J%J%LN>*a/1@PQAR1S.4i@::)?J[ 4g> !"8)DDD4;?;Q;Q!!:s7|<8M=.  #8];G%%b$..'*JL]^  <$ (5 (5G $.gz#B !NN7G<!)$..w*O!PQ (5		 ""7+%%gllI?PQ"9n=G%%b$..'*JL]^  >
 !!B"2x0&rww4GGLL0'.y/C/C'D#B1&rww9 *277<< = " 1 1 9040G0G /'):N:N2O1P1BGGLL- " !!s   %AOc                6    t        d t        |      D        t        |            }|d | }|t        |      k7  rt        t        ||         nd }||dz   d  } j                  |j                  |t        |            \  }}}	g d fd}
 |
||       |ra j                  |j                        \  }}}|r/t        |t              sj                  |gt        |      z         nj                  |        |
||	       t         j                  d            S )Nc              3  H   K   | ]  \  }}t        |t              s|  y wrL  r  r  s      r  r  z8TypeChecker.lvalue_type_for_inference.<locals>.<genexpr>  s      K.51b*R2JQ.r  r   c                    t        | |      D ]O  \  }}j                  |      \  }}}|r"t        |t              sj	                  |       ?j	                  |       Q y rL  )r  r+  r  r   r  )	r  rv_typesrH  r  sub_lvalue_type
index_exprrl  rH  type_parameterss	          r  append_types_for_inferencezITypeChecker.lvalue_type_for_inference.<locals>.append_types_for_inference  sY    "31G8<8I8I"8M5X":o{+S#**?; $**73  2r  r  )r  list[Expression]r  r'  r  None)r  r  rS  r   r   r  r  r+  r  r  r   r  r   rj  )rH  r*  rn  r  r  r  r  r  r  r  r  r  r  rl  r  s   `             @r  r  z%TypeChecker.lvalue_type_for_inference  s   KIg.KSQX\

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

        star_index = 2, length = 5 (i.e., [a,b,*,c,d]), items = [1,2,3,4,5,6,7]
        returns in: ([1,2], [3,4,5], [6,7])
        r   r   N)rS  )	rH  r  r  lengthnr_right_of_starright_indexr
  starr  s	            r  r  zTypeChecker.split_around_star  s[     "J.2+;q+@''c%j[j!Z,kl#T5  r  c           	         t        |      }t        |t              r|j                         r|j                  }t        || j                  dt        t        j                        g            S )Nr,  )
r   r  r   r  r  r   ri  r   r   r  rH  rh  s     r  r  zTypeChecker.type_is_iterable  sX    t$dL)d.>.>.@==D$))*;giF\F\>]=^_
 	
r  c                   t        |      }| j                  |      rt        |t        t        t
        t        f      r| j                  ||      }|D ]v  }t        |t              rA| j                  d|g      }| j                  |j                  | j                  ||      |       T| j                  || j                  ||      |       x y | j                  j                  ||       y )Nru  )r   r  r  r   r   r   r   r  r   ri  r?  r  r  r  type_not_iterable)rH  r*  rn  r  rj  r  rH  
items_types           r  r  z0TypeChecker.check_multi_assignment_from_iterable  s     &k2  -*(L(JG3
 //WEIb(+!%!8!89+!VJ))
G!DFW ))DNN9g>@Q  HH&&{G<r  c                $   d }d }d }| j                  |      rt        |t              rt        |j                  t              rt        |t              r*t        |j                  t              sJ |j                  }nt        |t
              sJ | j                  j                  |j                         |j                  }nGt        |t              r|}n3t        |t
              r/| j                  j                  |d      }| j                  ||       nt        |t              r0| j                  j                  |d      }| j                  ||       nt        |t        t        f      rS|j                   D cg c]"  }| j#                  |      d   xs
 t%               $ }}t'        || j)                  d            }nKt        |t*              r | j#                  |j                        \  }}}n| j                  j                  |      }|||fS c c}w )NT)r/  r   r  )is_definitionr  rx   r  r   rv   r"  rf  r  def_varrp   analyze_ordinary_member_accessrL  analyze_ref_exprr   rs   r  r+  r   r   rj  r   )rH  r/  r0  rk  rl  sub_exprrz  r  s           r  r+  zTypeChecker.check_lvalue  s   f%68,
6;;0L&(+!&++s333!;;!&*555!!((5!>>	*!L
+++JJ6SWXKOOFK0)++<<VD<QKOOFK0H 56 !'
 !-H	 !!(+A. "  !" !-   $E4??;K+LMK) $ 1 1&++ >KA++226:KL(22s   8'Hc                    t        |t              r8|j                  ry|j                  }t        |t              r|j
                  d u S yt        |t              r|j                  S yr  )r  rx   is_inferred_defr  r   rh  rv   )rH  r3  r  s      r  r+  zTypeChecker.is_definition>  s\    a"   66D$$yyD((  :&$$$r  c                   t        |t              r| j                  j                  ||       yt	        ||j
                        se| j                  sY| j                  |||      sE| j                  j                  ||| j                  j                         | j                  |||       yyt        |t              r| j                  |j                  r|j                  | j                  v rst        | j                  |j                     |      sP| j                  j                  ||| j                  j                         t!        t"        j$                        |_        yt)        |      }| j+                  |||       y)z>Infer the type of initialized variables from initializer type.rR  N)r  r   r  deleted_as_rvaluerZ  rS  r2  r\  need_annotation_for_varr0  python_version!set_inference_error_fallback_typerv   r1  r,  r   r   r   r  rh  r   r[  )rH  r  r/  	init_typer  s        r  rh  zTypeChecker.infer_variable_typeN  s"    i-HH&&y':&y$--P)) **4C00w@[@[\66tVYO D vz*--9$"?"?? !>!>v~~!NPYZ HH,,T7DLL<W<WX	 4 45DI
 #9-I""4;r  c                   t        |      }t        |t              rt        d |      }nt        |t              r|j
                  j                  }t        |t              }|rP|dk(  s|dk(  s
|dk(  s|dk(  r<t        d t        |j                        D              rt        |j
                  |      }n|r|dk(  rt        |j                  d         }t        |j                  d         }t        |t        t        f      rF| j                  |      r5t        |      }t        |t              sJ t        |j
                  ||      }ny	y	y	| j                  |||       || j                  d
   j                   |<   y)Nru  rw  builtins.dictcollections.OrderedDictc              3  H   K   | ]  }t        |t        t        f        y wrL  )r  r   r   r  r  s     r  r  z1TypeChecker.infer_partial_type.<locals>.<genexpr>  s$      = q8_"=>=rH  collections.defaultdictr   r   FrK  T)r   r  r   r   r   rh  r]  r   r   r   r  r   'is_valid_defaultdict_partial_value_typer'   r[  r*  r  )	rH  r  r/  r7  partial_typer]  is_refarg0arg1s	            r  r\  zTypeChecker.infer_partial_typeq  sQ   #I.	i*&tT2L	8, ~~..H0F/>1?2#<< -inn= 
  +9>>4@H(AA&y~~a'89&y~~a'898_5BB4H%d+D%dH555#.y~~tT#JL tV\:+12""4(r  c                N   t        |t              syt        |j                        dk(  ryt        |j                        dk(  rct	        |j                  d         }| j
                  j                  rt        |t        t        t        f      }nt        |t        t        f      }|ryy)a  Check if t can be used as the basis for a partial defaultdict value type.

        Examples:

          * t is 'int' --> True
          * t is 'list[Never]' --> True
          * t is 'dict[...]' --> False (only generic types with a single type
            argument supported)
        Fr   Tr   )
r  r   rS  r  r   r0  old_type_inferencer   r   r   )rH  r  r  alloweds       r  r>  z3TypeChecker.is_valid_defaultdict_partial_value_type  s     !X&qvv;!qvv;!!!&&),C||..$S?Hk*RS$S?H*EFr  c                   |r| j                   s||_        d|_        || j                  vr;| j                  j
                  D ch c]  }|j                   c}| j                  |<   t        |t              r1| j                  %|j                  || j                  |j                  <   | j                  ||       yyyc c}w )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.
        TN)r=  rh  rd  r5  r!  r  r:  r  rv   r1  r,  rL  )rH  r  r/  rh  r  s        r  r[  zTypeChecker.set_inferred_type  s     t11CH"CO$...CG;;CUCU,VCU%UXXCU,V$$S)&*-$2O2O2[>>-DHD11&..AOOFD) 23
 -Ws   B<c                L    | j                  |      }| j                  |||       y)a  Store best known type for variable if type inference failed.

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

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

        We implement this here by giving x a valid type (replacing inferred Never with Any).
        N)inference_error_fallback_typer[  )rH  r  r/  rh  r  s        r  r6  z-TypeChecker.set_inference_error_fallback_type  s&     55d;sFH5r  c                J    |j                  t                     }t        |      S rL  )rf  SetNothingToAnyr(   )rH  rh  r  s      r  rH  z)TypeChecker.inference_error_fallback_type  s     ;;01 h''r  c                   t        |t        t        t        t        t
        f      ryt        |t              rt        |j                  t
              rt        |j                  j                  t              rj|j                  j                  j                  }t        |t              r|j                   S t        |t              rt        d |j                  D               S y)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  4   K   | ]  }|j                     y wrL  r  r4  s     r  r  z,TypeChecker.simple_rvalue.<locals>.<genexpr>  s     "Hid4>>i   F)r  rq   r   r[   rf   r   r\   r  r  rh   rh  r   r  r   r  r  )rH  r,  r  s      r  simple_rvaluezTypeChecker.simple_rvalue  s     fwIwOPfh'&--1jASASU]6^mm((--c<0"}},,Z0""Hcii"HHHHr  r  
expressionr  c          	        | j                   r)t        |t              rt        t        j
                        S |d uxr t        t        |      t               }| j                  j                  |||      }	t        t        |      t              rt        t        |	      t              s| j                  |      s| j                  j                         5 }
| j                         5 }| j                  j                  |d |      }d d d        d d d        
j                         sCt        t              t              s*t!        |      rt#        ||	      r|}	| j%                         t        |	t&              r| j                  j)                  |	|       t        |t&              r| j                  j+                  ||       |	S |r| j-                  |	|||| d| d|       |	S # 1 sw Y   xY w# 1 sw Y   xY w)N)r  z	 has typerQ  )r>  r  rc   r   r   r  r   r"  rf  r   r   rO  r  r  local_type_maphas_new_errorsrZ  r   store_typesr   r3  deleted_as_lvaluerA  )rH  r0  r,  r  r  r  r  r  r  rn  local_errorstype_mapalt_rvalue_types                r  r  z#TypeChecker.check_simple_assignment  s    <<Jv|<91122*$6  z,g@ < ++226F 3 K ?;7C"?;#?M**62 XX++-t?R?R?TX`&*&7&7&>&>7G '? 'O @U-
 %335&'GQ.?)/;G"1K$$X.+{3**;@+{3**;@  """m9-"m9- # 	 9 @U?T--s$   G,"G G, G)	%G,,G5c           
        t        |      }t        |      }t        |t              r|j                         st        |t              r| j                  |||      }||dfS t        |t              s| j                  |||      }||dfS t        ddd||d| j                  |       }t        ||d      }|j                  j                  d      s| j                  |||      }||dfS |j                  j                  d      }|d| j                  t        j                  j!                  |j#                  | j$                              |       t'        t(        j*                        |dfS t-        |||j                  |d|      }	t/        ||j0                        }
t3        |	|
      }| j4                  j7                  |d      }| j4                  j9                  ||t;        ||      |gt<        j>                  t<        j>                  g||	      }|}| j                  jA                         5  | j4                  jC                  |t;        ||      |gt<        j>                  t<        j>                  g|||
      \  }}ddd       t              }t        |tD              r$t;        t'        t(        jF                        |      }| j4                  jC                  |t;        ||      |gt<        j>                  t<        j>                  g|||
       |jH                  jK                  ||jL                         |jH                  jO                  ||jL                  ||       t        |tD              rtQ        |jR                        dk  rt'        t(        j*                        |dfS |jR                  d   }| j                  |||      }tU        ||      xr tU        ||      }|r|||fS |||fS # 1 sw Y   pxY w)a  Type member assignment.

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

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

        Note: this method exists here and not in checkmember.py, because we need to take
        care about interaction between binder and __set__().
        TFN)r  r  r  r  r  r  r  r  )
assignmentr  )r  r  r  r  r  mxrN  )r  )r  r  )r  selftyper  r   )+r   r  r   r  r   r  r   r    r  r"   rh  r  
get_methodrk  r   DESCRIPTOR_SET_NOT_CALLABLErm  r  r0  r   r   r  r!   r8   r  r3   r"  method_fullnametransform_callee_typer   r   rI   r  r  r   r  r  check_deprecatedr  warn_deprecated_overload_itemrS  r  r   )rH  rp  attribute_typer,  r  rn  r\  get_type
dunder_setbound_methodr  dunder_set_typer  rM  r  inferred_dunder_set_typeset_typeinfers                     r  r`  z#TypeChecker.check_member_assignment'  s    (6(8}l38Q8Q8SXb8Y
 66~vwWK44.(366~vwWK44'	
 -^RDQ""66yA
 66xQK$..#((33I>
II <<CC"33DLLA 	 9//0(EAA; $$$
 (
H1,D))99.)T++AAmW5v>]]EMM*& B 
 XX##%*.*;*;*F*F-9<H.*+ +G +'A' & $33K#L .=#GI,B,B$CWUL$$mW5|D]]EMM*&' 	% 	
 	
BJJ7
,,

+Cn 	- 	

 3\B(223a79//0(EAA+55a8
 228VWM;1Tj86T#{8UBB8UBBW &%s   'AO%%O/c                   | j                  ||       t        | j                  j                  |j                              }| j                  j                  d||      }||_        | j                  j                  d|||j                  |gt        j                  t        j                  g|      \  }}t        |      }t        |t              r(|j                  s| j                  j                          yyy)zxType check indexed assignment base[index] = rvalue.

        The lvalue argument is the base[index] expression.
        __setitem__N).try_infer_partial_type_from_indexed_assignmentr   r"  rf  rP  r  r
  check_method_callr  r   rI   r  r   	ambiguousr!  r  )rH  r/  r,  r  basetyper
  res_typer  s           r  rf  z$TypeChecker.check_indexed_assignment  s     	;;FFK"4#4#4#;#;FKK#HI''FF8V
 )''99\\6"]]EMM*
! #8,h09K9KKK##% :L0r  c                   d }t        |j                  t              r;t        |j                  j                  t              r|j                  j                  }n?t        |j                  t
              r%| j                  j                  |j                        }t        |t              r#t        |j                  t              r|j                  j                  }|y | j                  |      }|y |j                  }|dk(  s
|dk(  s|dk(  r| j                  j                  |j                        }| j                  j                  |      }t        |      rrt        |      rf| j                  sY|dk(  r6|j                  j                    t#        ||j                  j                         r| j%                  |||g      |_        ||= y y y y y y y y )Nr9  r:  r=  )r  rP  r   r  r   rv   r"  r  rh  r   r>  r]  rf  r  rZ  r=  
value_typer   ri  )	rH  r/  r,  r  r  r*  typenamekey_typert  s	            r  rn  z:TypeChecker.try_infer_partial_type_from_indexed_assignment  s    fkk7+
6;;;K;KS0Q++""CZ0##88ECc3#((K0HHMM	$ $ 7 7 < ($--/#<<#<<  $0077EH!%!2!2!9!9&!AJ.x82:> $ : :$(AA # 3 3 ?$1*chh>Q>Q$R $(#:#:8hPZE[#\)#.	 %S	 !; ? 9 = 1  r  c                    t        |      }t        |t              rD|j                  j                  dk(  rdt
        fS |j                  j                  d      dt        fS y)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   rh  r]  r,   rg  r+   )rH  r  proper_types      r  type_requires_usagezTypeChecker.type_requires_usage  sa     &c*k8, ((,>>35EFF##K0<35EFFr  c                8   | j                   j                  |j                  dd      }| j                  |      }|r_|\  }}| j	                  t
        j                  j                  t        || j                              ||       | j                  |||       y y )NTr  rU  )r"  rf  r  rz  rk  r   TYPE_MUST_BE_USEDrm  rA   r0  r  )rH  r3  	expr_typeerror_note_and_code
error_noterV  s         r  visit_expression_stmtz!TypeChecker.visit_expression_stmt  s    %%,,QVVt^b,c	"66yA2JII 2299+iQUQ]Q]:^_  
 IIj!$I/ r  c                Z    | j                  |       | j                  j                          y)zType check a return statement.N)check_return_stmtr!  r  rH  r3  s     r  visit_return_stmtzTypeChecker.visit_return_stmt  s     q!!r  c           	        | j                   j                         }|V|j                  r*| j                  | j                  d   |j
                        }n:|j
                  r| j                  | j                  d         }n| j                  d   }t        |      }t        | j                   j                         t              }t        |t              r/|s-|j                  s!| j                  t        j                  |       y |j                  rt        |t               }t        |t"              }|xs |xs |}t        | j$                  j'                  |j                  ||            }t        |t(              r2|j*                  j,                  dk(  rt#        t.        j0                        }|j2                  r!| j                  t        j4                  |       y t        |t"              r| j6                  j8                  r| j:                  st=        t#        t.        j0                        |      sn|j>                  t@        v rtC        |j                        sGt        |t(              r|j*                  j,                  dk(  s|s| jD                  jG                  ||       y |r4|st        |t               ry | j                  t        jH                  |       y | jK                  d|d||j                  |t        jL                         y |j                  r|j
                  st        |t"              ry t        |t         t"        f      ry | jO                         r!| j                  t        jP                  |       y y y )NrK  r  builtins._NotImplementedTyper  gotrW  )rX  rY  rZ  r[  r  outer_contextr  ))r&  top_functionrl  r"  r(  r  r   r   r  rr   r   rp  rk  r   NO_RETURN_EXPECTEDr  r   r   r"  rf  r   rh  r]  r   r  rm  RETURN_IN_ASYNC_GENERATORr0  warn_return_anyr=  r   r  r   is_literal_not_implementedr  incorrectly_returning_anyNO_RETURN_VALUE_EXPECTEDrA  r  r6  RETURN_VALUE_EXPECTED)	rH  r3  r  r  	is_lambdadeclared_none_returndeclared_any_returnallow_none_func_callr  s	            r  r  zTypeChecker.check_return_stmt  s   zz&&(  "<<%%b)4+<+< """<<T=N=Nr=RS"//3)+6K"4::#:#:#<jII+7 )>)>II.AA1Evv'1+x'H$&0g&F# (1'_4H'_L_$ &%%,,?S -  sH-))-KK!)"8"89C**II.HH!Lc7+ 44 $ : : 1'):P:P2QS^ _ II)== :166 B '{H= + 0 0 9 9=N N )::;J ( !JsH$=II.GGK&&&+ #(2"- !&',KK '  %% --";8kHg+>?++-II.DDaH .E r  c                   | j                   j                  ddd      5  t        |j                  |j                        D ]  \  }}t        | j                  j                  |            }t        |t              r| j                  j                  ||       | j                  |      \  }}| j                   j                  dd      5  | j                  |d       | j                  |       ddd       | j                  |d        | j                   j                  dd      5  |j                  r| j                  |j                         ddd       ddd       y# 1 sw Y   txY w# 1 sw Y   xY w# 1 sw Y   yxY w)	zType check an if statement.FTr   r  r  fall_throughr  r  r  from_assignmentN)r!  r  r  r  r  r   r"  rf  r  r   r  r3  r  r  r  )rH  r3  r  r  r  if_mapr  s          r  visit_if_stmtzTypeChecker.visit_if_stmto  s8    [[&&\]&^AFFAFF+1#D$5$5$<$<Q$?@a-HH..q!4#'#=#=a#@  [[..1.M&&vu&EKKN N
 ""8U"C ,  **E*J;;KK, K# _^ NM KJ# _^s<   B(E7%E,:E7&(E+E7E($E7+E4	0E77F c                    t        |j                  g|j                  gd      }|j                  |       | j	                  ||j
                  |j                         y)zType check a while statement.Nr  )rk   r  r  r  r  r  )rH  r3  if_stmts      r  visit_while_stmtzTypeChecker.visit_while_stmt  sG    !&&AFF8T2!++affEr  c                r   | j                  |j                  |j                  |j                         t	        |j                  t
              r'| j                  j                  |j                  d      }n%| j                  j                  |j                        }t        ||j                        \  }}|r| j                  j                  |||j                  |      \  }}t	        t        |      x}t              rt	        |j                  j                  |      x}t              rt|j                   D ]e  }	t	        |	t"              st	        |	j$                  j                  x}
t&              s;t)        |
      |k(  sJ| j+                  |	j$                  |       g t-        ||      s| j.                  j1                  |j                  |       n[t3        |j                  |j                  |j                        }|j5                  |       | j7                  |j                  |dd       | j9                  |       y)z9Type check an operator assignment statement, e.g. x += 1.TF)r/  r,  rj  r@  N)rV  r/  r,  r  r  rv   r"  visit_member_exprrf   infer_operator_assignment_methodcheck_opr   r   rh  r^  r|   r  ra   r  r   r   r  r   r   incompatible_operator_assignmentr{   r  r?  rD  )rH  r3  r0  inplacer#  rn  r
  r  r  r  r  r  s               r  visit_operator_assignment_stmtz*TypeChecker.visit_operator_assignment_stmt  s   ;;AHHahhPQPTPTUahh
+ ++==ahhMK++22188<K:;M'+'8'8'A'A&+WXW_W_ab'c$K/+">>$Ij		,,V446GO !JJD"43&diinn'<slK&s^{:,,TYY: ' k;799!$$B !$$!((3DMM!!!xxQV "  	r  c                   | j                   j                  |j                         t        |j                  t              rBt        |j                  j                        dkD  r | j                  t        j                  |       | j                  |j                        \  }}|j                  '| j                   j                  ||j                  d        | j                  |       y Nr   )r"  rf  r  r  r   rS  r  rk  r   MALFORMED_ASSERTr  r  analyze_cond_branchr  )rH  r3  true_mapr  s       r  visit_assert_stmtzTypeChecker.visit_assert_stmt  s      (affi(S->-BII&77; "77?(5511(AEE4H8$r  c                    |j                   r| j                  |j                   |       |j                  r| j                  |j                  |d       | j                  j	                          y)zType check a raise statement.T)optionalN)r  type_check_raise	from_exprr!  r  r  s     r  visit_raise_stmtzTypeChecker.visit_raise_stmt  sM    66!!!&&!,;;!!!++q4!@!r  c                   t        | j                  j                  |            }t        |t              r| j
                  j                  ||       y | j                  d      }|t        |      g}|r|j                  t                      | j                  |t        j                  |      |t        j                         t        |t               r| j                  j#                  |g g |       t        |t$              rJ|j&                  j(                  dk(  r0| j+                  t        j                  j-                  d      |       y y y )Nbuiltins.BaseExceptionr  z%; did you mean "NotImplementedError"?)r   r"  rf  r  r   r  r3  rj  r   r  r   rA  r   
make_unionr   INVALID_EXCEPTIONr   r  r   rh  r]  rk  rk  )rH  r  r3  r  r  exc_typeexpected_type_itemss          r  r  zTypeChecker.type_check_raise  s   d//66q9:c;'HH&&sA.??#;<'();<  &&xz2%%&9:A?O?a?a	
 c<(((b"a8c8$):):>\)\II 22FF; 	 *]$r  c                   | j                   j                  dd      5  | j                  |t        |j                               |j                  r| j                  |j                         ddd       |j                  r7| j                   j                         s| j                  |j                         yyy# 1 sw Y   MxY w)zType check a try statement.Fr   r  )	try_frameN)r!  r  visit_try_without_finallyr  finally_bodyrf  rb  r  s     r  visit_try_stmtzTypeChecker.visit_try_stmt  s    
 [[&&A&F **1Q^^8L*M~~ANN+ G >> ;;--/ANN+ 0  GFs   AB88Cc                R   | j                   j                  dd|      5  | j                   j                  ddd      5  | j                   j                  ddd      5  | j                  |j                         ddd       t	        t        |j                              D ]  }| j                   j                  dd      5  |j                  |   }|rV| j                  ||j                        }|j                  |   }|r)d|_        | j                  || j                  ||             | j                  |j                  |          |j                  |   }|r\|j                  }t        |j                   t"              rt%        |	      |j                   _        | j                   j)                  |       ddd        	 ddd       |j*                  r| j                  |j*                         ddd       y# 1 sw Y   xY w# 1 sw Y   oxY w# 1 sw Y   SxY w# 1 sw Y   yxY w)
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  rf  r  rv  rS  handlersrz  check_except_handler_testis_starvarsr1  r?  r  r  r  r  r   r   rh  cleanser  )rH  r3  r  r  r  r  r  r  s           r  r  z%TypeChecker.visit_try_without_finally  s    [[&&AQZ&[**ET`a*b[[..AY].^KK' _s1::/A22Dq2Qggaj $ > >sAII NA"#&&)C" 7; 3 $ 5 5c4>>!S;Q RAJJqM2ffQi &)XXF)#((C80;60J KK//4' RQ 0	 c2 {{AKK(9 \[ _^ RQ cb \[sT   HHG79AH C3H	3H?/H7H<HH	HH	HH&c                N   | j                   j                  |      }g }| j                  ||      }t        |      D ]L  }t	        |t
              r|j                  |       &t	        |t              rh|j                  d   }|j                         s3| j                  t        j                  |       | j                  |      c S t        |j                        }nPt	        |t               r|j"                  }n3| j                  t        j                  |       | j                  |      c S t%        || j'                  d            s3| j                  t        j                  |       | j                  |      c S |j                  |       O |rg }	|D ]w  }t)        || j'                  d            rI| j                  t        j*                  |       |	j                  t        t,        j.                               g|	j                  |       y | j1                  |	      S t3        |      S )z,Type check an exception handler test clause.r   r  builtins.BaseExceptionGroup)r"  rf  get_types_from_except_handlerr   r  r   r  r   r  r  rk  r   INVALID_EXCEPTION_TYPEdefault_exception_typer(   r  r   r  r   rj  r   INVALID_EXCEPTION_GROUPr   r  wrap_exception_groupr   )
rH  nr  r  	all_types
test_typesttyper  r  new_all_typess
             r  r  z%TypeChecker.check_except_handler_test+  s   &&q) "	77Q?
%j1E%)  '%.{{1~'')II.EEqI66w??)$--8E8, ::		*AA1E227;;h8P(QR		*AA1E227;;X&+ 2. (*M $S$//:W*XYII.FFJ!((1E1E)FG!((- ! ,,];;$Y//r  c                b    t        t        j                        }|r| j                  d|g      S |S )z:Exception type to return in case of a previous type error.builtins.ExceptionGroup)r   r   r  ri  )rH  r  r
  s      r  r  z"TypeChecker.default_exception_typeT  s0    9//0**+DxjQQr  c                ~    t        |      }t        || j                  d            rd}nd}| j                  ||g      S )zDTransform except* variable type into an appropriate exception group.zbuiltins.Exceptionr  r  )r   r   rj  ri  )rH  rz  r  rP  s       r  r  z TypeChecker.wrap_exception_group[  s?    #E*c4??+?@A,D0D&&tcU33r  c                (   t        |      }t        |t              r|j                  S t        |t              r7|j                         D cg c]  }| j                  ||      D ]  }|  c}}S t        |d      r|j                  d   gS |gS c c}}w )z?Helper for check_except_handler_test to retrieve handler types.r  r   )	r   r  r   r  r   r  r  r   r  )rH  r  r  r  	union_typs        r  r  z)TypeChecker.get_types_from_except_handlerd  s    c"c9%99Y'  ..00D!%!C!CD!!LI !L 0 
 s$45HHQK= 5Ls    Bc                R   |j                   r| j                  |j                        \  }}n| j                  |j                        \  }}||_        ||_        | j                  |j                  ||j                  du |       | j                  |j                  |j                         y)zType check a for statement.N)is_async analyze_async_iterable_item_typer  analyze_iterable_item_typeinferred_item_typeinferred_iterator_typeanalyze_index_variablesr  
index_typer  r  r  )rH  r3  iterator_typer  s       r  visit_for_stmtzTypeChecker.visit_for_stmtu  s    ::'+'L'LQVV'T$M9'+'F'Fqvv'N$M9(#0 $$QWWi9MqQ-r  c                    | j                   }|j                  |      }|j                  d|g g |      d   }|j                  d|g g |      d   }|j                  ||t        j
                        }||fS )zNAnalyse async iterable expression and return iterator and iterator item types.	__aiter__r   	__anext__)r"  rf  check_method_call_by_namecheck_awaitable_exprr   INCOMPATIBLE_TYPES_IN_ASYNC_FOR)rH  r  echkiterableiterator	awaitabler  s          r  r  z,TypeChecker.analyze_async_iterable_item_type  s      ;;t$11+xRQUVWXY22;"bRVWXYZ	--t-MM
	 ""r  c                    | j                  | j                  j                  |      |      \  }}| j                  |      }|r||fS ||fS )zHAnalyse iterable expression and return iterator and iterator item types.rN  )-analyze_iterable_item_type_without_expressionr"  rf  analyze_range_native_int_type)rH  r  r  r  int_types        r  r  z&TypeChecker.analyze_iterable_item_type  s^    !OO$$T*D P 
( 55d;X%%!!r  c                2   | j                   }t        |      }|j                  d|g g |      d   }t        |t              r=|j
                  j                  j                  dk(  r|t        |      j                  d   fS |j                  d|g g |      d   }||fS )zBAnalyse iterable type and return iterator and iterator item types.__iter__r   r  __next__)
r"  r   r  r  r   partial_fallbackrh  r]  r   r  )rH  rh  r  r  r  r  s         r  r  z9TypeChecker.analyze_iterable_item_type_without_expression  s       "4(11*hBPWXYZ[ x+))..77;KK^H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 yt        d |j                  D              rpd}d}|j                  D ]W  }t        | j                  |            }t        |t              s.|j                  j                  t        v sK||}P||k7  sVd}Y |r|r|S y)zTry to infer native int item type from arguments to range(...).

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

        Return None if unsuccessful.
        zbuiltins.ranger   r  c              3  .   K   | ]  }|t         k(    y wrL  )rI   )r  r_  s     r  r  z<TypeChecker.analyze_range_native_int_type.<locals>.<genexpr>  s     ?DGOs   NTF)r  r\   r  r   r]  rS  r  r   rw  r   rC  r   rh  r   )rH  r  
native_intrr  r  argts         r  r  z)TypeChecker.analyze_range_native_int_type  s     tX&4;;0$$(88S^(q(  ???&*JByy&t'7'7'<=dH-$))2D2DH^2^!)%)
+" ! j!!r  c                   t        |      }t        |t              rMg }|j                  D ]'  }| j	                  |      }|s|j                  |       ) t        j                  |      S t        |t              rk|j                  j                  d      rP| j                  d      j                  }t        ||      }t        |j                        dk(  sJ |j                  d   S t        |t              r| j	                  t        |            S y)zzCheck if a type is a nominal container of a union of such.

        Return the corresponding container item type.
        ztyping.Containerr   r   N)r   r  r   r  analyze_container_item_typer  r  r   rh  r  rj  r8   rS  r  r   r   )rH  r  rz  r  c_typer[  super_instances          r  r  z'TypeChecker.analyze_container_item_type  s    
 c"c9% "E		99$?LL( " ''..c8$):):;M)N(:;@@I6sIFN~**+q000!&&q))c9%33N34GHHr  c                J    | j                  || j                  ||      |       y)z>Type check or infer for loop or list comprehension index vars.N)r?  r  )rH  r  r  rj  r  s        r  r  z#TypeChecker.analyze_index_variables  s"     	eT^^Iw%GIZ[r  c                   t        |j                  t              r|j                  }t        |j                  d      }|j
                  |_        |j                  |_        t        ||j                  gt        j                  gd g      }|j
                  |_        |j                  |_        | j                  j                  |d       y |j                  j                  | j                         t        |j                        D ]N  }t        |t              s| j                  j!                  |t#        |j$                        t'        |      d       P y )N__delitem__Tr  r  F)r  r  rp   rv   rP  r  r/  r\   r  r   rI   r"  rf  flattenrx   r!  r@  r   r  r   )rH  r3  r  mr  elts         r  visit_del_stmtzTypeChecker.visit_del_stmt  s    affi(A166=1AVVAFxxAHQWWI?AVVAFxxAH$$Q$$?FFMM$++,qvvc8,KK++[9?3;OQV 'r  c                    |j                   D ]]  }t        |t              s|j                  dk(  s$t	        t
        j                        |j                  _        d|j                  _	         y  | j                  |       y )Nztyping.no_type_checkT)r  r  r   r]  r   r   r  r  rh  ra  r  )rH  r  rn  s      r  r  zTypeChecker.visit_decorator  s[    A!W%::!77!()?)?!@AEEJ%)AEEN  	""1%r  c           	        | j                   r`| j                  j                  |j                        5  | j	                  |j                  |j                  j
                  |       d d d        | j                  |j                        }t        |j                        D ]G  }t        |t              r#|s | j                  t        j                  |       7| j                  j                  |      }| j!                  ||      }d }t#        |t$              r|j&                  xs d }d }|lt#        |t(              r\| j+                  |j,                        rA| j/                  |j,                        }| j                  j1                  ||j
                        }| j3                  |j                  ||       | j                  j5                  ||gt6        j8                  g|||      \  }}	J | j;                  ||j                         t=        ||j                        }||j>                  _         d|j>                  _!        |j                  jD                  rtt#        |tF              rStI        |jJ                  D 
cg c]  }
|
jM                         s|
 c}
      dkD  r| jN                  j                  d|       | jQ                  |       |ry |j                  jR                  r|j                  jU                         s|jV                  s| jY                  |      }|j                  jZ                  r>|s<|:| jN                  j]                  |j                  j
                  |j                         | j_                  |j                  |       |j                  jR                  rm|j                  j
                  dv rU|j@                  rIt#        ta        |j@                        tb        td        f      s | j                  t        jf                  |       |j                  jh                  r.t#        |tb              r| jk                  |j                  |       y y y # 1 sw Y   xY wc c}
w )Nr  rN  )r  r  Tr   zToo many arguments for propertyrI  )6rA  r$  r  r  r  r  r   r  r  r   r   rk  r   r  r"  rf  r  r  r   r]  rv   rB  r  rC  r`  check_for_untyped_decoratorr  r   rI   check_untyped_after_decoratorr   r  rh  ra  r  r   rS  rw  is_requiredr  $check_incompatible_property_overrider  r2  r)  r  r  r  r  r   r   r   BAD_CONSTRUCTOR_TYPEr+  r,  )rH  r  r  r  rn  r  r  r]  r  t2kr  s               r  r  z!TypeChecker.visit_decorator_inner  s-   &&++AFF3$$QVV!&&++;$W 4
 &&qvv.!,,'A!!^4"II.JJAN##**1-C>>#q>1DH!W%::- (,KJq*$=$--PQPVPVBW"..qvv6,,<<[!&&Q,,QVVS!<''22dVemm_axU` 3 GC# (( 	**37QVV,
66#|,3==D=aAMMO=DEIHHMM"CQG55a866;;qvv0021==(,(B(B1(E%++1-9..qvv{{AFFC22166;TU66;;166;;*AAvvj)@<QXBYZ		*??C66:c<#@((5 $Ai 43@ Es   3Q5QQQc                    | j                   j                  rIt        |j                        r3t	        |      r'| j
                  j                  |j                  |       y y y y rL  )r0  disallow_untyped_decoratorsis_typed_callablerh  is_untyped_decoratorr   typed_function_untyped_decoratorr  )rH  r  dec_typedec_exprs       r  r  z'TypeChecker.check_for_untyped_decorator4  sL     LL44!$)),$X.HH55diiJ / - 5r  c                6   |j                   j                  s|j                  j                  r|j                  j                  }|j                  j                  j
                  dd  D ]  }|j                  j                  |      }|s!t        |j                  t              s<|j                  j                  sSt        t        |j                  j                  d         j                   j                  s| j                  t         j"                  |        y y y r{  )r  rd  r  r  r  rM  r:  rg  r  r  r|   r  r   ra   r  rk  r   'READ_ONLY_PROPERTY_OVERRIDES_READ_WRITE)rH  r  r  rP  rW  s        r  r  z0TypeChecker.check_incompatible_property_override>  s    uu))affkk66;;D+ JJNN40	 y~~/@A!22Y	(<(<Q(?@DDYYII.VVXYZ , /:)r  c                   d}t        |j                  |j                        D ]  \  }}|j                  r | j	                  |||j
                  d u       }n| j                  |||j
                  d u       }t        |      }t        |dd      rjt        |dd      s<t        |t              s|j                  j                  dk(  st        j                  sd} |rB| j                  j!                  dd      5  | j#                  |j$                         d d d        y | j#                  |j$                         y # 1 sw Y   y xY w)NFbuiltins.boolT)r  r  )r  r  r  r  check_async_with_itemrF  check_with_itemr   r   r  r   rh  r]  r   r[  r!  r  rf  r  )rH  r3  exceptions_maybe_suppressedr  r  exit_ret_types         r  visit_with_stmtzTypeChecker.visit_with_stmtL  s   &+#1LD&zz $ : :4IZIZ^bIb c $ 4 4T61CTCTX\C\ ] ,M:M}ouE}otD=(3!&&//?B)) /3+1 24 ' **DD*IAFF# JI KK JIs   9D::Ec                    | j                   j                  r| j                  ry t        j                  j                  |      r| j                  j                  ||       y y rL  )r0  disallow_any_decoratedr>  rE  rF  has_any_typer  untyped_decorated_function)rH  r  r  s      r  r  z)TypeChecker.check_untyped_after_decoratorq  sC    ||22dll>>&&s+HH//T: ,r  c                   | j                   }|j                  |      }|j                  d|g g |      d   }|j                  ||t        j
                        }|r#| j                  || j                  ||      |       | j                  t        t        j                        |      }|j                  d||gdz  t        j                  gdz  |      \  }}	|j                  ||t        j                        S )N
__aenter__r   	__aexit__r  )r"  rf  r  r  r   'INCOMPATIBLE_TYPES_IN_ASYNC_WITH_AENTERr?  r  r   r   r  r   rI   &INCOMPATIBLE_TYPES_IN_ASYNC_WITH_AEXIT
rH  r  r  rj  r  r  objr  r  r  s
             r  r  z!TypeChecker.check_async_with_itemx  s       kk$,,\3BMaP'''OO
 !!&$..d*CEVWnnWY%;%;<dC//seai%--1)<d
Q (('NN
 	
r  c                h   | j                   }|j                  |      }|j                  d|g g |      d   }|r#| j                  || j	                  ||      |       | j	                  t        t        j                        |      }|j                  d||gdz  t        j                  gdz  |      \  }}	|S )N	__enter__r   r0  r  )
r"  rf  r  r?  r  r   r   r  r   rI   r%  s
             r  r  zTypeChecker.check_with_item  s       kk$,,[#r2tLQO!!&$..d*CEVWnnWY%;%;<dC//cUQY!(;T
Q 
r  c                8    | j                   j                          y rL  )r!  handle_breakr  s     r  visit_break_stmtzTypeChecker.visit_break_stmt  s      "r  c                8    | j                   j                          y rL  )r!  handle_continuer  s     r  visit_continue_stmtzTypeChecker.visit_continue_stmt  s    ##%r  c           
     ~	   t        |j                  t              rit        |j                  j                  t              r |j                  j                  j
                  nd}d|z   }t        |      }t        |      }||_        n|j                  }| j                  j                  dd      5  t        | j                  j                  |j                              }t        |t              r| j                  j!                  ||       |j"                  D cg c]  }| j$                  j                  ||        }}|D 	cg c]  }	|	j&                   }
}	| j)                  |
      }t+        |j"                  |j,                  |j.                        D ]  \  }}}| j                  j1                  ||      }| j$                  j                  ||      }| j                  j                  dd      5  |j2                  s#t        t        |j4                        t6              r| j9                  d d       i }nt;        ||j4                  |j<                        \  }}| j?                  |j&                  |       | j9                  |d       |r;|jA                         D ](  \  }}| j9                  | jC                  ||      d       * | j9                  |j&                  d       |C| j                  j                  dd	      5  t        | j                  j                  |            }t        |t              r| j                  j!                  ||       | jE                  |      \  }}tG        ||      }t        |tH              rx|jJ                  xs |jL                  }t        |t              rN||fD ]G  }|stO        |      D ]4  }t        |t              r|j
                  |j
                  k(  s-||   ||<   6 I | j9                  |d       | j                  |       d d d        n| j                  |       d d d        | j9                  d        | j                  j                  dd      5  	 d d d        d d d        y c c}w c c}	w # 1 sw Y   axY w# 1 sw Y   exY w# 1 sw Y   4xY w# 1 sw Y   y xY w)
N zdummy-match-Fr   r  Tr  r  r  )(r  subjectr\   r  r   r]  r   rx   r  r!  r  r   r"  rf  r   r  r3  patternsr#  captures#infer_variable_types_from_type_mapsr  guardsbodiesnarrow_type_from_binderrb  rh  r   r  conditional_types_to_typemaps	rest_typeremove_capture_conflictsr  _get_recursive_sub_patterns_mapr  or_conditional_mapsr   patternr  r9  )rH  r3  r:  r  r  named_subjectsubject_typeppattern_typesr  	type_mapsinferred_typesgr  current_subject_typepattern_typer  pattern_mapr  r  r  	guard_mapguard_else_mapcase_targetrX  s                            r  visit_match_stmtzTypeChecker.visit_match_stmt  s   aii* /99I9I7.S!!**Y[B!B&DD	A$TNM!"MIIM[[&&A&F*4+<+<+C+CAII+NOL,4**<; TUS]S]^S]aT1188LIS]M^<I'JMq

MI'J!EEiPN qzz188QXX>1a'+'8'8'P'P!<($  $33::1>RS[[..1.M'':'(9(9:O, **4*G,.0M)<+<+<l>T>T1-X 55l6K6K^\**;*N&-8->->-@	c $ 2 2$($H$Hs$S49 !3 !" .A
 **<+@+@RW*X}![[66TU6V!01B1B1I1I!1L!MB)"k: $ : :2q A8<8R8RST8U5I~':8^'TH  *!Y7./ii.A166#-k8#D5>4I/7,448ND0:40J48MM[EYEY4Y08FNtnH],C 5C 5J !..y%.P KKN3 WV6 A_ N` ""8U"Ck ?r **E*J KS GF _'J: WV) NMh KJS GFsv   -A)R3#R9R3?R
BR3-DR DR	R7;R32R'4R3
R3RRR$ R3'R0	,R33R<c                V   i }t        |      }t        |t              rt        |t              r{t	        |j
                        t	        |j
                        k(  sJ t        |j
                  |j
                        D ]+  \  }}|||<   |j                  | j                  ||             - |S rL  )	r   r  r   r   rS  r  r  updater;  )rH  r  r  sub_patterns_maptyp_	item_expritem_typs          r  r;  z+TypeChecker._get_recursive_sub_patterns_map  s     46s#dI&:dI+Ftzz?c$**o555'*4::tzz'B#	8.6 + ''(L(LYX`(ab (C  r  c           
     `   t        t              }|D ]b  }||j                         D ]J  \  }}t        |t              s|j
                  }t        |t              sJ ||   j                  ||f       L d i }|j                         D ]  \  }}	d}
g }|	D ][  \  }}|j                  |       | j                  |      \  }}}|/d}
| j                  |||t        j                  dd      sW|||<   ] |
rmt        j                  |      }|	d   \  }}|||<   | j                  ||||        |S )NFTzpattern captures typezvariable has type)r  rX  rZ  r   )r   r9  r  r  rx   r  r   r  r+  rA  r   INCOMPATIBLE_TYPES_IN_CAPTUREr   r  rh  )rH  rB  all_capturestmr  r  r  rC  r  r3  already_existsrz  previous_typer  r-  first_occurrences                   r  r4  z/TypeChecker.infer_variable_types_from_type_maps  sP   ?J4?PB~!#ID#!$1#yy)$444$T*114+>	 ",  +-)//1MC"N "E%	cS!&*&7&7&=#q! ,%)N))%,JJ&=(; *  /<s+ &  "$//6&.qk# !&.s#((.>JZ[1 22 r  c                    |rht        |j                               D ]K  \  }}t        |t              s|j                  }t        |t
              sJ ||vst        |||         rI||= M y y rL  )r9  r  r  rx   r  r   r   )rH  rX  rC  r  r  r  s         r  r:  z$TypeChecker.remove_capture_conflicts.  se    !(.."23	cdH-99D%dC000>1CX\I]9^$TN 4 r  c                   |j                   r%| j                  |j                   j                         | j                  j	                         5  | j
                  j                  |j                         d d d        y # 1 sw Y   y xY wrL  )
alias_noder\  alias_tvarsr  r  r"  rf  rD  )rH  os     r  visit_type_alias_stmtz!TypeChecker.visit_type_alias_stmt7  sU    <<''(@(@AXX##%$$QWW- &%%s   &A;;Bc                    t        |t        g             }|dz   |z   |_        t        t	               ||      }||_        ||_        t        |       |j                         |_	        ||fS )N.)
r]   rY   r]  r   r   r  r  rG   calculate_metaclass_typer  )rH  curr_module_fullnameclass_gen_nameclass_short_namer  cdefr  s          r  make_fake_typeinfozTypeChecker.make_fake_typeinfo>  sh     (%)4,s2^Ct-AB	
d";;=Tzr  c                l     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 ]D  }|j                  j                  s|
j                  |	d	|j                  j                   d
f       F |
r|j                  |
       y	  |||      \  }} j                  j                         5 } j                  |       ddd       j!                         rN ||ddd         } |||      \  }} j                  j                         5 } j                  |       ddd       d|_        |j!                         r|j                  |	df       yt'        t(        |      |j*                  |<   t-        |g |d   j.                  xs |d   j.                        S # 1 sw Y   xY w# 1 sw Y   xY w# t$        $ r |j                  |	df       Y yw xY w)a{  Try creating an ad-hoc intersection of the given instances.

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

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

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

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

        Returns None if creating the subclass is impossible (e.g. due to
        MRO errors or incompatible signatures). If we do successfully create
        a subclass, its TypeInfo will automatically be added to the global scope.
        r   Fignore_promotionsc                    g }| D ]J  }|j                   j                  r|j                   j                  }n|g}|D ]  }|j                  |        L |S rL  )rh  is_intersectionr  r  )
instances_base_classes_r  r^  expanded_insts        r  _get_base_classesz:TypeChecker.intersect_instances.<locals>._get_base_classes{  sR    M"99,,#yyH $vH%-M!((7 &. # ! r  c                    t        | D cg c]  }|j                  j                   c}d      }d| d}t        ||j                        }j                  |j                  |||       \  }}||fS c c}w )Nandz<subclass of >)rE   rh  r  gen_unique_namer:  rf  r]  )	rm  curr_module_r  
names_list
short_name
full_name_re  info_rH  s	           r  !_make_fake_typeinfo_and_full_namezJTypeChecker.intersect_instances.<locals>._make_fake_typeinfo_and_full_name  s~     $-$H-QQVV[[-$H%PJ(A6J(\5G5GHJ11%%z:}KD% *$$ %Is   A1T)r0  barerq  rM  z
" is finalNrK  z/would have inconsistent method resolution orderz)would have incompatible method signaturesr   extra_attrs)rl  tuple[Instance, Instance]r  list[Instance])rm  r~  rt  rw   r  ztuple[TypeInfo, str])r&  rT  r  rw   r   rE   rC   r0  rh  rS  r  r  r  r  r  r  rT  rk  rF   r   rN   r:  r   r|  )rH  	instancesr  curr_moduler
  r  ro  ry  base_classespretty_names_list
new_errorsrP  r  	full_namerW  s   `              r  intersect_instanceszTypeChecker.intersect_instancesT  s.   2 jj&&q)+x000  eT5EBKUDEBL
	!		%)		%9A		%!		% )3 '"L$,,TRTY
 
 Dyy!!!!#4$))..9I6T"UV ! MM*%	?kZOD)'')\//5 ***,04R4A"CLR]"^iXX++-33D9 .#'D  &&(MM,.YZ['6tT'B)$bil.F.F.b)TU,JbJbcc# *) .-  	MM,._`a	sC   *&H G<"AH .H H <HH HH H32H3c                ,   | j                   j                  d   }t        |t              sJ t	        d|j
                  j                   d|j                        }t        || j                        }| 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 rr  r  z	.__call__r{  )r&  rT  r  rw   rs  rh  r  r:  rB   r0  rf  r]  ri   rY   	_fullnamer  r   rT   rN   r   r|  )	rH  r  callable_type
cur_modulegen_namerv  re  r  func_defs	            r  intersect_instance_callablez'TypeChecker.intersect_instance_callable  s     ZZ%%a(
*h///"%:388==/#KZM]M]^ &c4<<8
,,Z-@-@(JY\X]^
d :r59mD!]][8!0x!@

:%4T4%@
"bcoo>>r  c                8   | j                  d      }t        t        t        j                        t        t        j                        gt
        j                  t
        j                  gddgt        t        j                        |d      }| j                  ||      S )zIProduce a new type that makes type Callable with a generic callable type.r  NT)r  r  is_ellipsis_args)	rj  r   r   r   explicitr   rJ   rx  r  )rH  r  r  r  s       r  make_fake_callablezTypeChecker.make_fake_callable  s{     ??#67$Y''(')2D2D*EF^^U__-4LY//0!
 //]CCr  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              rQg }g }|j                  D ]:  }| j                  |d      \  }}|j                  |       |j                  |       < ||fS t        |t              r)| j                  t        |      |      \  }}|r|gng }||fS |}t        |t              rt        |      }t        |t              r|j                  j!                  d      }	|	r5|	j                  r)| j                  |	j                  d      \  }}|r|s|gg fS |sf| j#                  |      }
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unsound_partitionr  F)r   r  r   r   r   r   r   r  partition_by_callabler  r   r   r   r   r   rh  r^  r  r  )rH  r  r  	callablesuncallablesrY  subcallablessubuncallablesitypr#  fakes              r  r  z!TypeChecker.partition_by_callable  s    " c"cL(345"9c7#53%<c8$u9c9%IK99 04/I/It 0J 0,n   ."">2 % k))c;' &*%?%?',.?&"I{ $/3%BKk)) c9%!#&DdH%YY))*5F&++)-)C)CKK5 *D *&	; [  5"9$$..t4c9-+4SYY+EDII( II001C588vu}$u9 53%<r  c                   |si i fS t        t        |      t              ri i fS | j                  |d      \  }}|r<|r:|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  )rH  r  current_typer  r  callable_mapuncallable_maps          r  conditional_callable_type_mapz)TypeChecker.conditional_callable_type_map8  s     r6Mol3W=r6M!%!;!;L\a!;!b	;FOD)"6"6y"ABUYLJUdI$8$8$EF[_N//t8ORxr  c                X   g }g }t        |      }t        |t              rt        |j	                               }n|g}t        |      }|D ]  }|rt        |t              rv|D ]p  }||j                  v r|j                  |       #||j                  v s|j                  s#|j                  |       |j                  |       `|j                  |       r |j                  |       |j                  |        |rt        j                  |      nd|rt        j                  |      fS dfS )z
        Narrows the type of `iterable_type` based on the type of `item_type`.
        For now, we only support narrowing unions of TypedDicts based on left operand being literal string(s).
        N)r   r  r   r   r  r   r   required_keysr  r  rS  r  )	rH  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_iterableT  s#     "!#
'6mY/&6}7S7S7U&V#'4o#>yI&=" Z0F%V,C4BBB (>?5;;;CYCbCb (>?"))*@A"))*@A -  67!!"89 '>" /7I  *D0:I  ,
 	
@D
 	
r  c                    t        |t              xrl t        |j                        xrU |j                  j	                  d       xr7 |j                  j	                  d       xr |j                  j
                  dk7  xsK t        |t              xs9 t        |t              xr' t         fdt        |j                        D              S )N__bool____len__r  c              3  @   K   | ]  }j                  |        y wrL  )_is_truthy_typer  r  rH  s     r  r  z.TypeChecker._is_truthy_type.<locals>.<genexpr>  s     S9RA,,Q/9R   )r  r   r  rh  r  r]  r   r   r   r   r  rH  r  s   ` r  r  zTypeChecker._is_truthy_type{  s     1h' 9L922:>>9 229==9 FFOO'88 !\* 1i( TS9I!''9RSS	
r  c                >    t         j                  syt               j                        syd fd}d fd}t	        t
              r5 j                  t        j                  j                   |                    yt	        t              r5 j                  t        j                  j                   |                    yt	        t              rj                  j                  dk(  ru j                  dddg       \  }} j                  t        j                   j                   |       t#        t        |j$                         j&                                     y j                  t        j(                  j                   |                    y)z
        Check if a type can have a truthy value.

        Used in checks like::

            if x: # <---

            not x  # <---
        Nc                    t        j                        } t        t              rdj                   d|  S t        t
              rj                  rdj                   d|  S t        t              rt        j                  t              rdj                  j                   d|  S t        j                  t
              r2j                  j                  rdj                  j                   d|  S d|  S d|  S )NzMember "z" has type rM  z
" returns zCall returns zExpression has type )	rA   r0  r  rv   r  r   r]  r\   r  )r  r  rH  r  s    r  format_expr_typez;TypeChecker.check_for_truthy_type.<locals>.format_expr_type  s    a.C$
+!$))Ku==D'*t}}4==/SE::D(+dkk:6t{{//0
3%@@W5$++:N:Nt{{334JseDD&se,,-cU33r  c                 |    t         t        t        f      rd j                   dS t	        j
                        S )NrM  )r  rx   rv   r  rA   r0  )r  rH  r  s   r  get_expr_namez8TypeChecker.check_for_truthy_type.<locals>.get_expr_name  s8    $: 67499+Q'' #1dll33r  r,  typing
Collection)r  r  )r   r[  r   r  r  r   rk  r   FUNCTION_ALWAYS_TRUErm  r   TYPE_ALWAYS_TRUE_UNIONTYPEr   rh  r]  rf  ITERABLE_ALWAYS_TRUErA   r  r0  TYPE_ALWAYS_TRUE)rH  r  r  r  r  r  r  s   ```    r  check_for_truthy_typez!TypeChecker.check_for_truthy_type  s3    $$A##A&	4	4 a&II&;;BB=?SUYZ9%II&AAHHIYI[\^bc8$<M)M--hlTVWGAtII 55<<$&HT1664JDLL(Y 	 II&77>>?O?QRTXYr  c                |   dd}g }d}d}|D ]  }|j                   |   }t        |t              r' ||      r|j                  |j                  d          I| j                  |      }	|	]|i i fc S t        |t              r0t        |j                  t              r|j                  j                  }|	} |si i fS g }
g }|D ]Y  }| j                  | j                  |      ||      \  }}t        |||      \  }}|
j                  |       |j                  |       [ dd} ||
      }|r ||      }||fS 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
        c                b    t        | j                  d      xr t        | j                        dk(  S )z)Is expr a call to type with one argument?r  r   )r   r  rS  r  )r  s    r  is_type_callz8TypeChecker.find_type_equals_check.<locals>.is_type_call  s&    %dkk?C[DIIZ[H[[r  NFr   c                @    i }| D ]  }||j                  |        |S )z2Combine all typemaps in list_maps into one typemap)rM  )	list_maps
result_maprn  s      r  combine_mapsz8TypeChecker.find_type_equals_check.<locals>.combine_maps  s,    J=%%a(  r  )r  r\   r  r  )r  list[TypeMap]r  r  )operandsr  r\   r  r  get_isinstance_typer   r  r   rS  #conditional_types_with_intersectionrC  r8  )rH  r  expr_indicesr  exprs_in_type_callstype_being_comparedrS  r  r  r  if_maps	else_mapscurrent_if_typecurrent_else_typecurrent_if_mapcurrent_else_mapr  r  r  s                      r  find_type_equals_checkz"TypeChecker.find_type_equals_check  sr   	\
 136:!E=='D$)l4.@#**499Q<8#77='&2 r6M!$0Z		85T#'99#5#5*6') ", #r6M!##%	'D151Y1Y  &(;T2.O. 0Mo'80,N, NN>*-. (	 g&
 #I.H x Hxr  in_boolean_contextc               z    | j                  ||      \  }}| j                  |      }| j                  |      }||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.
        r  )find_isinstance_check_helperpropagate_up_typemap_info)rH  r  r  r  r  
new_if_mapnew_else_maps          r  r  z!TypeChecker.find_isinstance_check  sQ    *  <<%7 = 
 33F;
55h?<''r  c          
        t        |      ri d fS t        |      rd i fS t        |t              rXt	        |j
                        dk7  r?t        |j
                  d         }t        |j                  d      rwt	        |j
                        dk7  ri i fS t        |      t        k(  rt        |g| j                  | j                  |      | j                  |j
                  d         |       S t        |j                  d      rAt	        |j
                        dk7  ri i fS t        |      t        k(  rG| j                  ||      S t        |j                  d      rRt	        |j
                        dk7  ri i fS t        |      t        k(  r| j                  |      }| j!                  ||      S t        |j                  d      rt	        |j
                        dk7  ri i fS t#        |j
                  d   | j                  |j
                  d               }t        |      t        k(  rS|rPt	        |      dk(  rA| j%                  || j                  |      |d         S t        |j                  t&              r|j                  j(                  |j                  j*                  |j,                  d   t.        j0                  k7  rt3        | j                  |j                              }t        |t4              rt7        d||d	
      }|t3        |      }t        |t8        t:        f      r|j<                  d   j>                  d   }||j>                  v r4|j>                  jA                  |      }	t        |j
                  |	         }nM|j                  j(                  dnd}
| jC                  tD        jF                  jI                  |
      |       i i fS t        |      t        k(  r|j                  j(                  #|tK        |j                  j(                        ii fS |j                  j*                  J t        |g| j                  | j                  |      tM        |j                  j*                  d      g|       S t        |tN              r| jQ                  |      S t        |tR              ri }i }| jU                  |jV                        \  }}||jY                  |       ||jY                  |       | jU                  |jZ                  d      \  }}||jY                  |       ||jY                  |       ||d n|||d fS |fS t        |t\              re|j^                  dk(  rV| jU                  |j`                        \  }}| jU                  |jb                        \  }}te        ||      tg        ||d	      fS t        |t\              rc|j^                  dk(  rT| jU                  |j`                        \  }}| jU                  |jb                        \  }}tg        ||      te        ||      fS t        |th              r1|j^                  dk(  r"| jU                  |jj                        \  }}||fS t        |      t        k(  r| jm                  |      r| jo                  | j                  |            rtq        | j                  |      d      s| jr                  jt                  r| jw                  | j                  |      dd      \  }}|ty        |      }n
t{               }|t}        |      }n
t{               }t        |tz              s||ind }t        |tz              s||ind }||fS | j                  |      }|r| j                  ||       t        |d      }ty        |      }t}        |      }t        |tz              s||ind }t        |tz              s||ind }||fS )Nr   zbuiltins.isinstancer  r   zbuiltins.issubclasszbuiltins.callablezbuiltins.hasattrr  Tr  guardnarrowerFr  r  rq  )coalesce_anyornotr  rr  r  )Ais_true_literalr:  r  r\   rS  r  collapse_walrusr   r  r6   rS   r8  r  rC  r  infer_issubclass_mapsr  r   hasattr_type_mapsr   r  rs  rw  r   rI   r   r   r   r   r   r  r}  r  rk  r   TYPE_GUARD_POS_ARG_REQUIREDrm  r   r  r^    comparison_type_narrowing_helperrW   r  r  rM  rD  r{   r  r
  r  and_conditional_mapsr<  r   r  rB  can_be_narrowed_with_lenr   r0  r[  narrow_with_lenr   r   r   r  r   )rH  r  r  r  vartyper  called_typer  r  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_varsr
  r  yes_typeno_typeoriginal_vartypeif_type	else_types                               r  r  z(TypeChecker.find_isinstance_check_helper+  sB    4 t8OD!8OdH%#dii.A*="499Q<0D!$++/DEtyy>Q&r6M4=L08AA ,,T2D4L4LTYYWX\4Z\`  $DKK1FGtyy>Q&r6M4=L055dDAA#DKK1DEtyy>Q&r6M4=L0"..t4G==dGLL#DKK1CDtyy>Q&r6M/		!d>N>NtyyYZ|>\]4=L0Tc$i1n11$8H8H8NPTUVPWXXDKK1;;))59L9L9X~~a(EMM9&5d6F6Ft{{6S&T &k8<#. *KRV$D  $/.=d.C &kL*3MN#.#4#4Q#7#A#A!#DD#t~~5&*nn&:&:4&@'6tyy~'F 04{{/E/E/QGWa !% !%		$4$P$P$W$WX\$]_c!" (*2vt}4  ;;11=$(/$++:P:P*Q#RTV#VV#';;#6#6#BB#B#@ $$!%!I!I$($4$4T$:%.t{{/B/BSX%Y$Z$(""$  n-88>>n- FH595O5OPTP[P[5\22 ,/0". 34373M3M

u 4N 400  +./!- 23 +26F6NTZ,48J8R X`  f%$''U*:+/+E+Edii+P(L.-1-G-G

-S*M?
 %\=A $NORVW  f%$''T/+/+E+Edii+P(L.-1-G-G

-S*M?
 $L-@$^_E  i(TWW-=44TYY?KD%$;DM\)d#--d.>.>t.DE)$*:*:4*@*M,, !% 4 4T5E5Ed5KSRS THg#$X.*,"$W-)+-7/-RdH%X\F.8/.RgX\H8##  ++D1 &&'7>12BOTG$w'	(27O(L$RV,6y/,RD)$X\xr  c           	     Z   |j                   D cg c]  }t        |       }}g }i }t        |      D ]  \  }}| j                  |      si i fc S | j	                  |      }|j                  |       t        |      t        k(  sRt        |      r^| j                  |      rpt        |      }	|	~|	||<    t        |j                         |ddh      }
g }|
D ]  \  }}|dv r| j                  ||||||      \  }}n|dv rt        |      dk(  sJ |\  }}||   }||   }i i }}||v rrt        |      rgt!        t#        |            }|Qt        |      sFt%        |t&              r|j(                  j*                  dk(  st-        ||      rt/        |      |||   <   ||v r3| j1                  ||      \  }}||   }|d}n|||<   |d}n
|||<   ni }i }|dv r||}}|j                  ||f        t3        d	 |D              rt5        |      S t5        | j7                  |      d
      S c c}w )z2Infer type narrowing from a comparison expression.N==is>   r  !=r  is not>   innot inr  r  >   r  r  r  c              3  *   K   | ]  }|i i fk7    y wrL  r  )r  r   s     r  r  z?TypeChecker.comparison_type_narrowing_helper.<locals>.<genexpr>K  s     8&7qRH}&7s   Tuse_meet)r  r  r  rB  rC  r  r6   rS   r]  is_literal_enumr7   group_comparison_operandspairwiseequality_type_narrowing_helperrS  r   r   builtin_item_typer  r   rh  r]  r9   r   r  r  reduce_conditional_mapsfind_tuple_len_narrowing)rH  r  r  r  operand_types narrowable_operand_index_to_hashr  r  r}  hsimplified_operator_listpartial_type_mapsoperatorr  r  r  
left_indexr#  r  r  collection_item_typer  r  s                          r  r  z,TypeChecker.comparison_type_narrowing_helper  s    15>1OA&>+-( *GAt==&2v((.I  + -'-,,T2 &=:;4Q7 +> $=MMO=d|$
  &>"Hl77#'#F#F! 4$  --<(A---*6'
K)*5	 -k :#%r!AA*95/>?PQ^?_/`,0<$78L$M *+? J$8$=$=$F$FJ[$[ ;IG[ \;J9;UF8J#78"BB)-)L)L!=*&GY $K0D!%'.t (#')2 55#+V$$fh%78m '?v 8&788*+<== +4+H+H+NY]^^W ?s   H(c                   |dv rt         }d}d}	nAdd}
dd}|
}d}|D cg c]  }||   	 }}t        t        ||            xr t        |       }	i }i }|	r'| j	                  ||||j                         ||      \  }}|i k(  r*|i k(  r%| j                  ||||j                               \  }}|i k(  r|i k(  r| j                  ||      \  }}||fS c c}w )z@Calculate type maps for '==', '!=', 'is' or 'is not' expression.>   r  r  FTc                4    t        t        |       t              S rL  )r  r   r   r  s    r  is_exactly_literal_typezKTypeChecker.equality_type_narrowing_helper.<locals>.is_exactly_literal_typer  s    !/!"4kBBr  c                B    t        | dd       xr 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_checksu  s1    0x5  N/8uMMNr  r  )r   r   r  is_ambiguous_mix_of_enums%refine_identity_comparison_expressionr  refine_away_none_in_comparisonr  )rH  r  r  r  r  r  r	  is_valid_targetcoerce_only_in_literal_contextshould_narrow_by_identityr  r  r  
expr_typesr  r  s                   r  r  z*TypeChecker.equality_type_narrowing_helperR  s%   4 ''6GO-2*(,%CN
 6O-1*4@ALq-*LJA(++Z8) )</
;; & $#II0557. FH R<HN#BB-7W7\7\7^ FH R<HN#::4NFHx5 Bs   Cc                    |yi }|j                         D ]>  \  }}|||<   | j                  ||      }|j                         D ]  \  }}||v r|||<    @ |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)rH  	new_types
output_mapr  r}  new_mappingparent_exprproposed_parent_types           r  r  z%TypeChecker.propagate_up_typemap_info  s~    2 
(0OD)(Jt 224CK5@5F5F5H11 )+*>
;' 6I  1 r  c                   
 i }	 t        |t              r;t        |j                         j	                        }|j
                  d fd}nt        |t              rot        |j                         j	                        } 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 ]7  } |t        |            }	|	|c S t        |	|      s'|j!                  |       9 |s|S }t#        |      x}|<   V)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'.
        c                    j                   j                         5 }t        | dddj                   | d
      }d d d        j                         ry S # 1 sw Y   xY w)NF)
r  r  r  r  r  r  r  r  r  in_literal_context)r  r  r#   rT  )new_parent_typewmember_typemember_namer"  rH  s      r  replay_lookupz6TypeChecker.refine_parent_types.<locals>.replay_lookup  sf    //1Q&;!, /$/&+%*(- $*9 $/4' 2 '')#**! 21s    AA c                    t        | t              sy 	 J D cg c]  }| j                  |    }}t	        |      S c c}w # t        $ r Y y w xY wrL  )r  r   r  r  r   )r'  r  member_typesstr_literalss      r  r+  z6TypeChecker.refine_parent_types.<locals>.replay_lookup  sg    )/=I#'(#/#;;#;R^+_R^3O,A,A#,FR^L+_  5\BB ,`' (#'(   A ?A A 	AAc                    t        | t              sy 	 J D cg c]  }| j                  |    }}t	        |      S c c}w # t        $ r Y y w xY wrL  )r  r   r  
IndexErrorr   )r'  r  r-  int_literalss      r  r+  z6TypeChecker.refine_parent_types.<locals>.replay_lookup
  sg    #-oy#I'+,'3'? ?'?Vb/cVbs0E0Ec0JVb/c $9#FF 0d#- ,'+,r/  )r'  r   r  Type | None)r  rv   r  r  lookup_type_or_noner  rp   rP  r  r   r   r   r   r   r  r:   r  r   )rH  r  r}  outputparent_typer+  r  new_parent_typesr  r)  r2  r*  r"  r.  s   `         @@@@r  r  zTypeChecker.refine_parent_types  st    *, 
 $
+-dii8"66{C"ii+ +& D),-dii8"66{C!55djjA
%!MA*M+C $Fj#QL#/G  & " *+6Kk95  "-k.?.?@+OD,AB& "M'Y?$++D1 A $D.CDT.UUI{+Q r  c                   d}|rddt        fd|D              }d}g }	|D ]M  }
|
   }|rt        |      } |t        |            s'|rt        ||      sdi fc S |}|	j	                  |
       O |i i fS d}|	D ]	  }
|
|vs|
} |dk(  r|	d   }d}t        |      }t        |t              rJ|j                         st        |j                  t              r |j                  j                  j                  }t        |d      g}g }|D ]U  }
|
|k(  r	|
|vr||
   }t        |
         }|t        ||      }t        ||      }|j	                  t!        |g|        W t#        |      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.
        Tc                    t        |       } t        |       xs( t        | t              xr | j                  j
                  S rL  )r   r   r  r   rh  r  r  s    r  should_coerce_innerzNTypeChecker.refine_identity_comparison_expression.<locals>.should_coerce_innerb  s7    %c*+C0 sH-B#((2B2Br  c              3  4   K   | ]  } |           y wrL  r  )r  r  r  r;  s     r  r  zDTypeChecker.refine_identity_comparison_expression.<locals>.<genexpr>h  s     ]}! 3M!4D E}s   NrK  Fr  r  r   r  r  )r  r   r   r   r  r  r   is_enum_literalrD  r  r  rh  r]  r  r   conditional_typesr8  r  )rH  r  r  chain_indicesnarrowable_operand_indicesr  r  should_coercer  possible_target_indicesr  r}  singleton_indexsum_type_nametarget_typer  r  rz  r;  s     `               @r  r  z1TypeChecker.refine_identity_comparison_expression<  s   F )  ]}]]M""$A%a(I-i8	"?9#=>l69= RxF#**1-  >r6M
 (A22"# )8 b 5b9O (fk*""$
6<<(F"OO0099M >?A O# 22A;D)-*:;I(;I}U	
 &i=E$$%B4%P%%PQ/ 2 ''899r  c                  
 g }|D ]$  }||   }t        |      r|j                  |       & i i }	}|rt        |      t        |      k7  ry|D ]9  }||   
t        
      st        
fd|D              s)t	        
      |||   <   ; t        d |D              r)|D ]$  }||   
t        
      st	        
      |	||   <   & ||	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  6   K   | ]  }t        |        y wrL  )r9   )r  r  r}  s     r  r  z=TypeChecker.refine_away_none_in_comparison.<locals>.<genexpr>  s     ]J\Q29a@J\s   c              3  N   K   | ]  }t        t        |      t                y wrL  )r  r   r   )r  ots     r  r  z=TypeChecker.refine_away_none_in_comparison.<locals>.<genexpr>  s     U}:ob18<}s   #%)r   r  rS  r  r   )rH  r  r  r@  rA  non_optional_typesr  r  r  r  r}  s             @r  r  z*TypeChecker.refine_away_none_in_comparison  s      A"C&s+"))#. 
 r!c*<&=]AS&S 0)!,	*95]J\]]*9)*DF8A;' 0 U}UU3A -a 0I*950?	0J!- 4
 xr  c                ,   t        |t              syt        |j                  d      syt	        |j
                        dk7  ry|j
                  d   }t        |      t        k7  ry| j                  |      sy| j                  | j                  |            S )zIIs this expression a `len(x)` call where x is a tuple or union of tuples?Fzbuiltins.lenr   r   )r  r\   r   r  rS  r  r6   rS   rB  r  rC  )rH  r  s     r  is_len_of_tuplezTypeChecker.is_len_of_tuple  sz    $)!$++~>tyy>Qyy|4=L(}}T",,T-=-=d-CDDr  c                    t        |d      ryt        |      }t        |t              r@t	        d |j
                  D              r#|j                  j                  j                  dk(  S yt        |t              r|j                  j                  d      S t        |t              rt	         fd|j
                  D              S y)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                y wrL  )r  r   r<  s     r  r  z7TypeChecker.can_be_narrowed_with_len.<locals>.<genexpr>  s     Bk:a,kr  r  Tc              3  @   K   | ]  }j                  |        y wrL  )r  r  s     r  r  z7TypeChecker.can_be_narrowed_with_len.<locals>.<genexpr>  s     MAt44Q7r  )r   r   r  r   r  r  r  rh  r]  r   r  r   )rH  r  p_typs   `  r  r  z$TypeChecker.can_be_narrowed_with_len   s     !i0$ eY'BekkBB--22;;?OOOeX&::&&'788eY'MMMMr  c                    | j                  |      sy| j                  |      }t        |      }t        |      }t	        |t
              syt	        |j                  t              sy|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)rB  rC  r   r   r  r   rD  int)rH  r  r}  ry  s       r  literal_int_exprzTypeChecker.literal_int_expr  sc    
 }}T"$$T*	%i0	%i0+{3+++S1   r  c                    g }t               }|j                         D ]  \  }}}t        |t              r|j                  }t        |t              r|j                  }|dv r#|j                  |       |j                  |       f|r&|j                  dt        |      f       t               }|dv s|j                  |||gf        |r|j                  dt        |      f       g }|D ]  \  }}t         fd|D              st         fd|D              s1|dv rt               }	g }
|D ]i  } j                  |      }||	j                  |       ( j                  |      s:t        |t              sJ |
j                  |j                  d          k t        |	      dkD  rdi fgc S |	j                         }|t        kD  r|
D ]J  } j!                   j#                  |      ||      \  }}|dn||i}|dn||i}|j                  ||f       L /|\  }} j                  |      r||}}t%        j&                  ||      } j                  |      }|J |t        kD  rt        |t              sJ  j!                   j#                  |j                  d         ||      \  }}|dn|j                  d   |i}|dn|j                  d   |i}|j                  ||f        |S )	a  Top-level logic to find type restrictions from a length check on tuples.

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

            z: tuple[int, ...]
            if 1 < len(z) < 4:
                x = z  # OK
        and report corresponding type restrictions to the binder.
        )r  r  r  >   >=<rr  r  <=r  c              3  D   K   | ]  }j                  |      d u  y wrL  )rT  r  r  rH  s     r  r  z7TypeChecker.find_tuple_len_narrowing.<locals>.<genexpr>Q  s"     Mut,,R0<us    c              3  @   K   | ]  }j                  |        y wrL  )rM  rZ  s     r  r  z7TypeChecker.find_tuple_len_narrowing.<locals>.<genexpr>S  s     @%Bt++B/%r  Nr   r   )r;  r  r  rW   rD  rv  r  r9  r  rT  rM  r\   r  rS  r6  r   r  rC  r   rg  )rH  r  chained
last_groupr  r
  r  rB  r  literal_valuestuplesr  litsizetplr  r  yes_mapno_mapr_sizes   `                   r  r  z$TypeChecker.find_tuple_len_narrowing&  s   ( U
#}}OBe$/zz%0\!t$u%NND$z*:#;<!$J??NNBu#67  / NND$z"234 	 IBMuMM@%@@ \!!$B//3C&**3/ ++B/)"h777bggaj1   ~&* "2J<'%))+00!C(,(<(<T=M=Mc=RTVX\(]%Hg&.&6dS(OG%,_T3.F$$gv%67	 " $e''."'%D!b"-B..u5)))22!$111$($8$8$$TYYq\2B%!' #+"2$1x8P!(tyy|W6M  '6!23c !d r  c                "   t        |      }t        |t              r| j                  |||      S t        |t              r| j                  |||      S t        |t              rg }g }g }|j                  D ]b  }| j                  |      s|j                  |       &| j                  |||      \  }}	||j                  |       |	R|j                  |	       d ||z  }||z  }|rt        |      }
nd}
|rt        |      }|
|fS d}|
|fS J d       )zIDispatch tuple type narrowing logic depending on the kind of type we got.Nz"Unsupported type for len narrowing)r   r  r   refine_tuple_type_with_lenr   refine_instance_type_with_lenr   r  r  r  r  r   )rH  r  r  ra  	yes_typesno_typesother_typesr  ytntr  r  s               r  r  zTypeChecker.narrow_with_len  s+   c"c9%223DAAX&55c2tDDY'IHKYY44Q7&&q)--aT:B>$$R(>OOB'  $I#H0;/9 W$$ W$$>>>5r  c                |   t        |j                        }|(t        |   } ||j                         |      r|dfS d|fS |j                  |   }t	        |t
              sJ t        |j                        }t	        |t              r|j                         dz
  |j                  z   }|dv r||k  r||fS d|fS |dv r~|dk(  r|dz  }||k  rk|j                  d| }	|j                  |dz   d }
t        |j                  ||j                         z
  dz               }||j                  |	|gz   |
z         fS d|fS | j                  |t        |   |      \  }}||fS t	        |t              r|j                  j                  dk(  sJ |j                         dz
  }|j                  d	   }|j                  d| }	|j                  |dz   d }
|dv r*||k  r!|j                  |	|g||z
  z  z   |
z         |fS d|fS |dv r|dk(  r|dz  }||k  r|j                  |	|g||z
  z  z   |gz   |
z         }g }t!        ||z
        D ]-  }|j#                  |j                  |	|g|z  z   |
z                / t%        j&                  ||j(                  |j*                        |fS d|fS | j                  |t        |   |      \  }}||fS )
z-Narrow a TupleType using length restrictions.Nr   r  r  rW  rX  rX  )min_len)r  r  r   )r   r  r   r!  r  r   r   rh  r   rq  r  rg  r   r   r]  r  rv  r  r   r  r  r/  )rH  r  r  ra  unpack_indexr#  unpackr  rq  r  suffixr  r  r  	yes_itemsr  s                   r  rg  z&TypeChecker.refine_tuple_type_with_len  s'    +3995 &b)FcjjlD)Dy 9<(&*---"6;;/h 01 jjlQ&)9)99G\!d?8OSy {":AIDT> YY}5F YY|a'7'9:F'(>(>tcjjlGZ]^G^(>(_`F 1 1&8IF8R 1 SSSSy $($C$CCQSVZ$[!'(( (H-(--2H2HL\2\\\**,"mmA=L)<!+-.$((v8P/PSY/Y(Z\___9;Tz	~ ++ C5D7N#;;vhFO ,  	tg~.A$$S%6%6Vseai=ORX=X%6%YZ / ++IsxxLgUU9 $ ? ?WR[RV WHgH$$r  c                   t        || j                  d            }|j                  d   }t        | j                  j
                  v xr |j                  j                  dk(  }|dv rt        |g|z  |      |fS |dv r|dk(  r|dz  }|r2t        | j                  d|g            }t        |g|z  |gz   |      }n|}|rag }	t        |      D ]"  }
|	j                  t        |g|
z  |             $ t        j                  |	|j                  |j                         }||fS |}||fS | j#                  |t$        |   |      \  }}||fS )	z5Narrow a homogeneous tuple using length restrictions.r  r   ro  )r  r  rp  rX  r   r  )r8   lookup_typeinfor  r   r0  enable_incomplete_featurerh  r]  r   r   ri  rv  r  r   r  r  r/  rh  r   )rH  r  r  ra  rP  r  allow_precisers  r  r  r  r  s               r  rh  z)TypeChecker.refine_instance_type_with_len  sd    )d.B.BCS.TUiil  4<<#I#II4hh#33 	 C54<#>CC;Tz	#D$;$;<Lse$TU'0ut|vh7NY\']tALLC519s!CD %(1(<(<UCHHcjj(Y W$$ W$$ $ B B3PRUY ZHgH$$r  )r  rV  r  c                    y rL  r  )
rH  rY  r[  r  r  rX  rZ  r  rV  r  s
             r  rA  zTypeChecker.check_subtype  s     r  )r  r  c                    y rL  r  )	rH  rY  r[  r  r  rX  rZ  r  r  s	            r  rA  zTypeChecker.check_subtype  s     r  c               h   t        ||| j                        ryt        |t              rt	        ||      }| j
                  j                         r| j                  ||       y|}
t        |      }|}t        |      }| j
                  j                  ||||||      ryg }d}|xs g }||t        |
|| j                        \  }}||j                  |dz   |z          ||j                  |dz   |z          t        |	xs ||||      }t        |t              rt        |t              rt        |||      }t        |t              r(t        |t              rt!        |||| j                        }|r&|j#                  ddj%                  |      z   d	z         }| j                  ||       |D ]*  }| j
                  j'                  |||j(                         , |r| j'                  |||j(                         | j
                  j+                  ||||j(                         t        |t              r_|j,                  j.                  rIt        |t0        t        t2        t4        f      r)| j
                  j7                  ||||j(                         t        |t0              rJt        |t              r:t9        d
||d      }|r)| j
                  j;                  ||||j(                         t        |t0        t<        f      rt        |t              r|j,                  j.                  rld
|j,                  j>                  v rTt9        d
||d      }|J t        ||| j                        s)| j
                  j;                  ||||j(                         | jA                  ||||j(                         y)zBGenerate an error if the subtype is not compatible with supertype.rY  TrU  Fr0   z (z, r[  r  r  )!r   r0  r  r  r<   r  prefer_simple_messagesrk  r   &try_report_long_tuple_assignment_errorrC   r  rD   r   r?   r   r@   rk  r   r  rV  maybe_note_concatenate_pos_argsrh  r|  r   r   r   report_protocol_problemsr   	note_callr   protocol_memberscheck_possible_missing_await)rH  rY  r[  r  r  rX  rZ  r  rV  r  orig_subtypeorig_supertype
extra_infonote_msgsubtype_strsupertype_strr  r  s                     r  rA  zTypeChecker.check_subtype*  s,    gy$,,?c3s.C88**,IIc7#!'*"#I.	88::Ym_
  "
$(C)?ndll*&K (!!-#"5"CD*!!/C"7-"GH.('9mH '8,Ix1P/w	J'9-*Y	2R)%)T\\R))$:1F*F*LMC		#wDHHMM$chhM7 IIhchhI700)WSVS[S[0\y(+**7\8Y$VWHH--gy'PSPXPX-Yi.:gx3Pz7GND""7D'"Igj9:z)U]?^~~))jINN<[<[.[":y'tT'''!'4FHH&&y$chh&O))'9gCHH)Ur  c                
   t        t        |      t              ry	 | j                  j	                  |t               dd      }|j                         ryt        t        |      t        t        f      ry|S # t        $ r Y yw xY w)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.
        Nr0  T)ignore_binder)
r  r   r   r"  r  r_   r  rT  r   r   )rH  r  rW  aw_types       r  get_precise_awaitable_typez&TypeChecker.get_precise_awaitable_typex  s     oc*K8	''<<WY$ = G &&(og.+0FG  		s   'A6 6	BBc              #  J   K   d| _         	 d  d| _         y # d| _         w xY wwr  )rC  rN  s    r  checking_await_setzTypeChecker.checking_await_set  s&     &*#	0*/D'%D's   # #	 #c                   | j                   ry| j                         5  | j                  j                         5 }| j	                  ||      }|	 ddd       ddd       y| j                  |||t        j                        s	 ddd       ddd       y	 ddd       ddd       | j                  j                  ||       y# 1 sw Y   .xY w# 1 sw Y   2xY w)z7Check if the given type becomes a subtype when awaited.N)r  )	rC  r  r  r  r  rA  r   INCOMPATIBLE_TYPESpossible_missing_await)rH  rY  r[  r  rV  rW  r  s          r  r  z(TypeChecker.check_possible_missing_await  s     &&$$&(>(>(@L55g|LG )A&& %%G1A1T1T &   )A&&	 )A& 	''6 )A(@&&s4   CCC $CCCC	CCc                p   | j                  |      }|j                  }t        |t              r2t        |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   rh  r   r   r   from_omitted_genericsrS  r  r  )rH  r  rv  r  r
  s        r  rj  zTypeChecker.named_type  s     ##D)xxdI&dkk8444;;##D$)))9::;xj3tyy/B/B+CCDDr  c                v    | j                  |      }|D cg c]  }t        |       }}t        ||      S c c}w )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.
        )rx  r)   r   )rH  r  r  r  r  s        r  ri  zTypeChecker.named_generic_type  sA     ##D)BFG$31#6$Gd## Hs   6c                d    | j                  |      }|j                  }t        |t              sJ |S rL  )r  r  r  r   )rH  r]  rv  r  s       r  rx  zTypeChecker.lookup_typeinfo  s0    ##H-xx$)))r  c                $    | j                  d      S )zReturn instance type 'type'.r  rj  rN  s    r  r  zTypeChecker.type_type  s    //r  c                $    | j                  d      S )zReturn instance type 'str'.rX  r  rN  s    r  r.  zTypeChecker.str_type  s    ~..r  c                (    || j                   d   |<   y)z)Store the type of a node in the type map.rK  N)r   )rH  r  r  s      r  rL  zTypeChecker.store_type  s    $'D!r  c                R    t        fdt        | j                        D              S )Nc              3  &   K   | ]  }|v  
 y wrL  r  )r  r   r  s     r  r  z'TypeChecker.has_type.<locals>.<genexpr>  s     @&?419&?s   )r  r  r   r  s    `r  rB  zTypeChecker.has_type  s    @ht&?@@@r  c                N    t        | j                        D ]  }||v s||   c S  y rL  )r  r   )rH  r  r   s      r  r4  zTypeChecker.lookup_type_or_none  s*    $//*Aqyw + r  c                z    t        | j                        D ]  }|j                  |      }||c S  t        |      rL  )r  r   rg  r  )rH  r  r   r  s       r  rC  zTypeChecker.lookup_type  s8    $//*AdA} + tnr  c                @    | j                   d   j                  |       y rK  )r   rM  )rH  rn  s     r  rU  zTypeChecker.store_types  s    ""1%r  c              #     K   i }| j                   j                  |       | | j                   j                          yw)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  r6  )rH  rQ  s     r  rS  zTypeChecker.local_type_map  s5      13}-s   =?c                p    | j                   j                  xs | j                   xs | 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.
        rK  )r0  rL  r)  rN  s    r  r6  zTypeChecker.in_checked_function  s8     LL++c43E3E/EcTM_M_`bMcIc	
r  c                
   || j                   v r| j                   |   S | j                   j                  dd      }|r;t        |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,  rg  r  r  rw   r:  r  )rH  r  r  tables       r  lookupzTypeChecker.lookup  sz    4<<<<%%  6A!!&&(3335= ;&_TF344r  c                   d|vr| j                  |      S |j                  d      }| j                  |d      }t        dt	        |      dz
        D ]Q  }|j
                  j                  ||         }|J d       t        |j                  t              sJ |j                  }S |d   }||j
                  v r|j
                  |   S t	        |      dk(  rZ|d   dv rSdj                  |      }|t        v rdj                  |d   t        |         }nd	}t        d
j                  ||            d}	t        |	j                  ||            )Nr`  r   r   z0Internal error: attempted lookup of unknown namerK  r  )builtinsr  z*, e.g. add '[{} fixtures/{}]' to your testr0  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.  rv  rS  r:  rg  r  r  rw   r   r=   rm  r  )
rH  r  partsr  r  rv  lastr]  
suggestionr  s
             r  r  zTypeChecker.lookup_qualified  sF   d?;;t$$JJsOEU1X&A1c%j1n-ggkk%(+Z(ZZ!#((H555HH	 .
 9Dqwwwwt}$UqU1X1G%G88E?66!M!T!Ta"9("C"J "$J--3VD*-E  Iszz$566r  )r  ry  c             #    K   | j                   xr | j                   d   j                  xs |}| j                   j                  t        i ||             d | j                  j
                  xr | xs5 | j                  j                  xr | j                  xr | j                  d   }| j                   j                         \  }}}| j                  s*|j                         D ]  \  }}| j                  j                   xs |xs |xr | j                  |      }	|	rBt        |j                  t              r(|j                  j                  |st!               |_        }|| j"                  vrN|sL| j$                  j'                  ||| j                  j(                         | j"                  j+                  |       |j                  st-        |j                        }
|
|j                  k7  |_        |
|_         yyw)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.
        rK  N)r*  r  r  r  r0  allow_untyped_globalsrL  r)  r6  r=  r  local_partial_typesis_defined_in_base_classr  rh  r   r   r+  r  r4  r5  rv  r   r}  )rH  r  ry  r  
permissiver*  r  r  r  
allow_nonefixeds              r  r_  zTypeChecker.enter_partial_types+  s     &&J4+=+=b+A+J+JZ{!!"22{H"MN
 ll88I\ 
LL++]0B0B]tGYGYZ\G] 	 #00446q!)) - 3 3 5W 888 I"I GT%B%B3%G  "388[9-&'zCH$"7"77
88gt||GbGbc--11#6xx 2388 <38CHH3D0#(C !6 *s   GH 5H c                   | j                  |      \  }}}|j                  |r|s
t               S |S || j                  s|rt||   }|s| j                  j
                  ss| j                  j                  ||| j                  j                         | j                  j                  |       t        |      S | j                  |j                  |       t        |      S )zjHandle a reference to a partial type through a var.

        (Used by checkexpr and checkmember.)
        ) find_partial_types_in_all_scopesrh  r   r=  r0  r  r  r4  r5  r+  rv  r  r  r   )rH  r  r  r  r  in_scoper  r*  s           r  handle_partial_var_typez#TypeChecker.handle_partial_var_typed  s     -1,Q,QRV,W)(M88 z!
(1K1K+D1Gt||'I'I88 '4<<+F+F --11$7 &c** 55diiI%c**r  c                    j                   syj                   j                  xs+ t        fdj                   j                  dd  D              S )NFc              3  X   K   | ]!  }|j                  j                        d u # y wrL  )rg  r  )r  rP  r  s     r  r  z7TypeChecker.is_defined_in_base_class.<locals>.<genexpr>  s(      /
7GtDHHSXXd*7Gs   '*r   )r  fallback_to_anyr  rM  )rH  r  s    `r  r  z$TypeChecker.is_defined_in_base_class  sG    xxxx'' 
3 /
7:xx||AB7G/
 ,
 	
r  c                6    | j                  |      \  }}}|r|S y)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  )rH  r  r  r  r*  s        r  r>  zTypeChecker.find_partial_types  s'     &*%J%J3%O"!]  r  c                   t        | j                        D ]  }||j                  v s| j                  j                  }t        |j                  t              r$|j                  j                  |j                  rd}| xs& |j                  | j                  d   j                  k(  }||j                  |j                  fc S  y)zLook for partial type scope containing variable.

        Return tuple (is the scope active, is the scope a local scope, scope).
        TrK  )FFN)
r  r*  r  r0  r  r  rh  r   r  r  )rH  r  r&  disallow_other_scopesscope_actives        r  r  z,TypeChecker.find_partial_types_in_all_scopes  s     d001Eeii )-(H(H%chh49RWZW_W_ -1) .-b4CUCUVXCYCbCb1b  $U^^UYY>> 2  "r  c                    t        ||      S )z3Create a temporary node with the given, fixed type.rN  )r   )rH  r  r  s      r  r  zTypeChecker.temp_node  s    7++r  rU  c                   t        |t              r3| j                  j                  |j                  ||j
                         y| j                  j                  |||       y)zProduce an error message.rU  N)r  r<   r  rk  rD  rV  )rH  r  r  rV  s       r  rk  zTypeChecker.fail  sE     c<(HHMM#))W388M<c7.r  c                   t        |t              r3| j                  j                  |j                  ||j
                         y| j                  j                  ||||       y)zProduce a note.rU  N)offsetrV  )r  r<   r  r  rD  rV  )rH  r  r  r  rV  s        r  r  zTypeChecker.note  sG     c<(HHMM#))W388M<c76=r  c                    t        |t              rEt        || j                  d            }|j                  d   }t        t        |      t              s|S | j                  ||      d   S )Nr,  r   r   )r  r   r8   rx  r  r   r   r  )rH  r  r  r  r  s        r  r  zTypeChecker.iterable_item_type  sc     b(#0T5I5IJ[5\]H a(Ioi8'B ! AA"gNqQQr  c                8    t        || j                  d            S )Nr  )r   rj  )rH  r  s     r  r   zTypeChecker.function_type  s    T4??3F#GHHr  r  c                   || j                   j                          y |j                         D ]#  \  }}| j                   j                  |||       % y )Nr  )r!  r  r  put)rH  rX  r  r  rh  s        r  r  zTypeChecker.push_type_map  sC    KK##%&nn.
ddOL /r  c                   | j                  |      }| j                  |j                  d         }t        |t              r|j
                  }t        |      }t        |t              rYg }t        |j                        D ]3  }t        |t              r|j                  |j                         /i i fc S  t        |      }n]t        |t              r|j                  }n@t        |t              r,|j                  j                         r| j!                  d      }ni i fS | j#                  |||      \  }}t%        |||      \  }	}
t'        t(        |	|
f      \  }	}
|	|
fS )z=Infer type restrictions for an expression in issubclass call.r   r  )rC  r  r  r  r   r  r   r   r   r  r   r  r  r   rh  r  rj  r  r8  r  convert_to_typetype)rH  r  r  r  rh  
union_listr  r  r  rc  rd  s              r  r  z!TypeChecker.infer_issubclass_maps  s0   ""4(''		!5g{+))G!'*gy)J%gmm4a*%%aff- r6M 5  
+G*llG*w||/H/H/Joo&78G r6M DDWdTXY'7hP1GV3DEr  c                     y rL  r  rH  r}  type_rangesr  r  s        r  r  z/TypeChecker.conditional_types_with_intersection  s     +.r  c                     y rL  r  r  s        r  r  z/TypeChecker.conditional_types_with_intersection  s      r  c                6   t        |||      }|d   }|d   }t        t        |      t              r|||fS t        |      }t        |t              rt        |j                               }	n|g}	g }
|D ]?  }t        |j                        }t        |t        t        f      s/|
j                  |       A |
s||fS g }g }|	D ]  }t        |t              s||fc S |
D ]f  }t        |t              r,|j                  d|j                  j                   ddf       ?| j                  ||f|      }|V|j                  |       h  |sC| j                         r'|D ]"  \  }}| j                  j!                  |||       $ t               |fS t#        |      }||fS )Nr   r   rM  z" and "NoneType"z"NoneType" is final)r?  r  r   r   r   r   r  r  r   r   r  rh  r  r  rc  r  impossible_intersectionr   )rH  r}  r  r  r  initial_typesr  r  ry  possible_expr_typespossible_target_typesr  r  outr  r  r  intersectionrz  reasonnew_yes_types                        r  r  z/TypeChecker.conditional_types_with_intersection  s    *)['J !.a 0,Q//(3_EI\W$$ &i0k9-"2;3M3M3O"P#.- "B"277+D$8 45%,,T2  %W$$(*$Aa*((*a*MMQqvv{{m3C#DF["\]#77AG'

<( + % 446%+ME6HH44UFCH &,"$i//,S1Y&&r  c                    t        |t              r|j                  r|j                  syyt        |t              rC|j                  r7|j
                  d   }t        |t              sJ |j                  j                  S y)z!Check if an attribute is writableFTr   )r  r   r  rd  r|   r  ra   r  )rH  r  
first_items      r  ri  z!TypeChecker.is_writable_attributeC  sf    dC (A(A/0T5E5EAJj)444>>666r  c                >   t        |t              r|j                  dk(  r| j                  |j                        }|+t        |j                        rt        t               d      g}| j                  |j                        }|+t        |j                        rt        t               d      g}||y ||z   S t        t        | j                  |                  }g }|D ]  }t        |t              rO|j                         r?t        |j                  d   j                         }|j#                  t        |d             ct        |t$              rEd}t        |j&                  t              rd}|j#                  t        |j&                  |             t        |t(              rZ|j*                  j,                  dk(  rAt)        |j*                  j.                  d   g       }	|j#                  t        |	d             "t        |t(              rV|j*                  j,                  dk(  r=|j0                  r1|j#                  t        t3        |j0                        d             t        |t4              r|j#                  t        |d              y  |sy |S )	Nrx  Fr  r   Tr  rK  ztypes.UnionType)r  r{   r  r  r
  r]  r  r   r  r   flatten_typesrC  r   r  r(   r  r  r  r   r  r   rh  r]  rM  r  r   r   )
rH  r  r
  r  r  rz  r  erased_typer  r  s
             r  r  zTypeChecker.get_isinstance_typeO  s   dF#3++DII6D|		 :!(*UCD,,TZZ8E}!<"8:eDE|u}%<$]43C3CD3I%JK	!#C#|,1B -SYYq\-B-BCY{5IJC* "&chh1%*NYsxxOPC*sxx/@/@O/S&sxx||B'7<Y{4HIC*sxx/@/@DU/UZ]ZbZbYy':5QRC)Ys5AB- .  r  c                   t        |t              rt        |j                  t              sy| j	                  |j                        }| j	                  |      }||yt        |      }t        t        |            }t        |t              rt        |t              sy|j                         sy|j                         xr' |j                  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  rv   r  rx   r4  r   r   r   r   r  r>  r  rh  r  )rH  r  r6  r)  s       r  r  zTypeChecker.is_literal_enumz  s    " !Z(
16680L..qvv6..q1+"5%k2%&7&DE+|4J{T_<`&&( '') G$$))[-D-D-FF	
r  c           
        |}t        |      }t        t        j                        }t	        |t
              r3|j                  ||      }|j                  J d|j                  _        |S t	        |t              r.|j                  j                  ||      }|j                  |      S t	        |t              r.|j                  j                  ||      }|j                  |      S t	        |t              rIt	        |j                  t
              r/t        j                   | j#                  |j                  |            S t	        |t$              rU|j                  | j#                  |j&                  |      |j(                  D cg c]  }| j#                  ||       c}      S t	        |t*              rA| j-                  ||      \  }}	t/        ||	D cg c]  }| j#                  ||       c}z         S |S c c}w c c}w )z8Inject an extra attribute with Any type using fallbacks.Nrw  )r  rk  )r   r   r   r  r  r   copy_with_extra_attrr|  mod_namer   r  r  r   r  r   r  r{  add_any_attribute_to_typer   r  rk  r   partition_union_by_attrr   )
rH  r  r  r]  r
  rE  r  r  	with_attrwithout_attrs
             r  r  z%TypeChecker.add_any_attribute_to_type  s   c"9001c8$--dH=F %%111*.F'Mc9%++@@xPH$$h$77c<(||88xHH$$h$77c8$CHHh)G++D,J,J388UY,Z[[c;'$$ ::3??DQILTA66q$?T %   c9%&*&B&B3&M#I|(R^_R^3T;;CFR^__   U
 `s   G7
G<
c                   | j                  ||      r||ii fS t        |      }t        |t              r8| j	                  ||      \  }}|| j                  ||      i}||t        |      ifS | j                  ||      }||k7  r||ii fS i i fS )af  Simple support for hasattr() checks.

        Essentially the logic is following:
            * In the if branch, keep types that already has a valid attribute as is,
              for other inject an attribute with `Any` type.
            * In the else branch, remove types that already have a valid attribute,
              while keeping the rest.
        )has_valid_attributer   r  r   r  r  r   )rH  r  source_typer  r  r  rc  type_with_attrs           r  r  zTypeChecker.hasattr_type_maps  s     ##K6+&**%k2k9-"::;MOA|T;;KNOGT#8#FGGG77TJ[(.)2--2vr  c                    g }g }|j                   D ]7  }| j                  ||      r|j                  |       '|j                  |       9 ||fS rL  )r  r  r  )rH  r  r  r  r  r  s         r  r  z#TypeChecker.partition_union_by_attr  sW     	%%D''d3  &##D)	 &
 ,&&r  c                   t        |      }t        |t              ryt        |t              r9|j                  r-|j                  j
                  r|j                  j                  }nd }| j                  j                         5 }t        ||t        t        t        j                              ddd| j                  || d|       d d d        j                          S # 1 sw Y   xY w)NFT)r  r  r  r  r  r  no_deferralmodule_symbol_table)r   r  r   r   r|  r  rh  r:  r  r  r#   r   r   r  rT  )rH  r  r  rQ  r  watchers         r  r  zTypeChecker.has_valid_attribute  s    $eW%eX&5+<+<ARARA[A["'**"2"2"&XX##%!!7!789!HH! $7 & ))+++ &%s   ACC%c                <    | j                   j                  ||      S )NrT  )r"  rf  )rH  r  rM  s      r  get_expression_typezTypeChecker.get_expression_type   s      ''<'HHr  c                L   t        t              rj                  t        t        t        t
        f      rlj                  _| j                  j                  D ]3  }t        |t              st        fd|j                  D              s3 y | j                  |       yyy)zEWarn if deprecated and not directly imported with a `from` statement.Nc              3  B   K   | ]  }j                   |d    k(    yw)r   Nr(  )r  r  r  s     r  r  z/TypeChecker.check_deprecated.<locals>.<genexpr>  s!     6\R[QtyyAaD7HR[r  )r  ra   r  ri   r|   r   
deprecatedr8  importsro   r  r:  r  )rH  r  r  imps    `  r  rb  zTypeChecker.check_deprecated  s    dI&99DdW&7BCOO'yy((c:.36\RUR[R[6\3\ ) $$T73 ( Dr  c                b   t        |t              r|j                  }t        |t        t        t
        f      rx|j                  x}i| j                  s\| j                  j                  r| j                  j                  n| j                  j                  } |||t        j                         yyyy)zWarn if deprecated.NrU  )r  ra   r  ri   r|   r   r  r@  r0  report_deprecated_as_noter  r  rk  r  
DEPRECATED)rH  r  r  r  warns        r  r  zTypeChecker.warn_deprecated  s    dI&99Dtg'8(CD $/*<))$(LL$J$J488==PTPXPXP]P]DW5+;+;< * = Er  )r]  c               P   t        |      }t        |t              rt        |t              rz|j                  D ]j  }t        |t
              st        |j                  j                  x}t              s;|t        ||      }||k(  sO| j                  |j                  |       l yyy)zLWarn if the overload item corresponding to the given callable is deprecated.N)
r   r  r|   r   r  ra   r  rh  r   r  )rH  r  r  r  r]  r  r  s          r  rc  z)TypeChecker.warn_deprecated_overload_item  s     !(d-.:fl3S

dI.:!%/I4  +$-i$B	 F*,,TYY@ # 4T.r  )r  r.   r.  r-  r0  r   r8  rw   r9  r  r6  r   rI  zdict[int, int]r  r  )r  zlist[Type | None]r  r  rL  )r{  z7Sequence[DeferredNode | FineGrainedDeferredNode] | Noner  r  )r  z.DeferredNodeType | FineGrainedDeferredNodeTyper  r  )r  rw   r  r  )r  r   r  r  r  r  )r  r  r  r_   r  r  )r  r   r  r  )r  r   r  zStatement | Noner  Expression | Noner  r  )r  r|   r  r  )r  r3  r  r_   r  CallableType | None)r  r   r  r  r  r  r=  )r  r   r  r  r  r   )r  r   r  r   )r  ri   r  r  )NNF)
r  rj   r;  r  r  
str | Noner  r  r  r  )r  ri   r-  r   r  r  )r  Iterator[None]F)
r  rj   r  r   r  r  r  r  r  r  )r  r   r  rS  r  r  )r  r   r  r  )r  rj   r  r  r  r  )r  r  r  r  )r  rj   r  r  )r  ri   r  r   r  r  )
r  rj   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  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  rj   r  r   r  z#list[tuple[FuncItem, CallableType]])r  zFuncDef | OverloadedFuncDefr  list[TypeInfo] | Noner  Context | Noner  r  )r  'FuncDef | OverloadedFuncDef | Decoratorr  r  )r  r  rP  r   rO  r  r  bool | None)r  r  r  r  rP  r   r  r  )
rv  r   r  r   rw  r   rx  r   r  r   )r  r   r  r3  )r`  r   r_  r   r  r  r  r  r[  r  rp  r  rm  r  r  r_   r  r  )r  rj   r  r  )r  r]   r  r  )r  r   r  r  )rB  zSequence[TypeVarLikeType]r  r  )r  r]   rP  r   r  r  )rv  r   r  r  )rv  r   r  r3  )
r  r  r  r   r  r   r  r   r  r  )r  ro   r  r  )r  rm   r  r  )r  rl   r  r  )r  rn   r  r  )r  rY   r  r  )r  r  )r3  r   r  r  r3  rX   r  r  )TF)
r/  rt   r,  rd   rj  r  r@  r  r  r  )rl  r   r  r3  )r/  rt   r,  rd   r  r  r  r  )r/  r   r0  r3  r,  rd   r  r  )r/  r   r0  r3  r,  rd   rP  r   r  r   r  ry   r  r  )r  r   rP  r   r  ztuple[Type | None, Node | None])r  r   rP  r   r  Node | Noner  r  )r  r  r  r  r  r_   r  r  )r>  r  r  r  )r3  z8AssignmentStmt | OperatorAssignmentStmt | AssignmentExprr  r  )r/  rt   r  r  )r/  rt   r  r3  r  r  )r  r  r  r  T)
r*  list[Lvalue]r,  rd   r  r_   rj  r  r  r  )
r*  r  r  rS  r  r_   r  
int | Noner  r  )TNF)r*  r  r,  rd   r  r_   rj  r  r  r3  r  r  r  r  )r*  r  r,  rd   rn  r   r  r_   rj  r  r  r  )r*  r  r  r  )r*  r  r,  rd   rn  r   r  r_   r  r  rj  r  r  r  )r*  r  rn  r   r  r   )r  zlist[T]r  rS  r!  rS  r  z tuple[list[T], list[T], list[T]])rh  r   r  r  )
r*  r  rn  r   r  r_   rj  r  r  r  )r/  rt   r  z0tuple[Type | None, IndexExpr | None, Var | None])r3  rt   r  r  )
r  r   r/  rt   r7  r   r  r_   r  r  )r  r   r/  rt   r7  r   r  r  )r  r   r  r  )r  r   r/  rt   rh  r   r  r  rh  r   r  r   )r,  rd   r  r  )r0  r3  r,  rd   r  r_   r  r<   r  r  r  r  r  list[str] | Noner  r   )
rp  r   rd  r   r,  rd   r  r_   r  ztuple[Type, Type, bool])r/  rp   r,  rd   r  r_   r  r  )r/  rp   r,  rd   r  r  )r  r   r  ztuple[str, ErrorCode] | None)r3  re   r  r  )r3  r   r  r  )r3  rk   r  r  )r3  r   r  r  )r3  rz   r  r  )r3  rV   r  r  )r3  r   r  r  )r  rd   r3  r   r  r  r  r  )r3  r   r  r  )r3  r   r  r  r  r  )r  rd   r  r  r  r   )r  r  r  r   )rz  zSequence[Type]r  r   )r  r   r  rd   r  r'  r3  rg   r  r  )r  rd   r  tuple[Type, Type])rh  r   r  r_   r  r  )r  rd   r  r3  )r  r   r  r3  )
r  rd   r  r   rj  r  r  r_   r  r  )r3  rb   r  r  )r  ra   r  r  )r  ra   r  r  r  r  )r  ri   r  r   r  rd   r  r  r3  r   r  r  )r  r   r  ri   r  r  )r  rd   r  r  rj  r  r  r   )r3  rZ   r  r  )r3  r`   r  r  )r3  ru   r  r  )r  rd   r  r   r  dict[Expression, Type])rB  r  r  dict[Var, Type])rX  r  rC  r  r  r  )r]  r   r  r  )
rb  r  rc  r  rd  r  r  r~  r  ztuple[ClassDef, TypeInfo])r  r}  r  zlist[tuple[str, str]]r  zInstance | None)r  r   r  r   r  r   )r  r   r  r   )r  r   r  r  r  tuple[list[Type], list[Type]])r  rd   r  r3  r  tuple[TypeMap, TypeMap])r  r   r  r   r  tuple[Type | None, Type | None])r  r   r  rd   r  r  )r  r^   r  	list[int]r  r  )r  rd   r  r  r  r  )r  r^   r  r  )r  r^   r  r  r  r  r  r'  r  r  r	  zdict[int, tuple[Key, ...]]r  r  )r  r  r  r  )r  rd   r}  r   r  zMapping[Expression, Type])r  r  r  r'  r@  r  rA  AbstractSet[int]r  zCallable[[ProperType], bool]r  r  r  r  )
r  r  r  r'  r@  r  rA  r  r  r  )r  rd   r  r  )r  rd   r  r	  )r  r^   r  list[tuple[TypeMap, TypeMap]])r  r   r  r  ra  rS  r  r  )r  r   r  r  ra  rS  r  r  )r  r   r  r  ra  rS  r  r  r  )rY  r   r[  r   r  r_   r  r  rX  r  rZ  r  r  r  rV  ErrorCode | Noner  r  r  r  )rY  r   r[  r   r  r_   r  r<   rX  r  rZ  r  r  r  r  r  r  r  )rY  r   r[  r   r  r_   r  str | ErrorMessagerX  r  rZ  r  r  r  rV  r  r  r  r  r  )r  r   rW  r/   r  r3  )
rY  r   r[  r   r  r_   rV  r  r  r  )r  r  r  r   )r  r  r  r'  r  r   )r]  r  r  r   )r  r   )r  rd   r  r   r  r  )r  rd   r  r  )r  rd   r  r3  )r  rd   r  r   )rn  r  r  r  )r  z Iterator[dict[Expression, Type]])r  r  r  r   )r  r  ry  r  r  r  )
r  r   r  r  r  r   r  r_   r  r   )r  r   r  r  )r  r   r  zdict[Var, Context] | None)r  r   r  z,tuple[bool, bool, dict[Var, Context] | None])r  r   r  r  r  r   )r  r  r  r_   rV  r  r  r  )r   )
r  r  r  r_   r  rS  rV  r  r  r  )r  z/Instance | CallableType | TypeType | Overloadedr  r_   r  r   )r  rh   r  r   )rX  r  r  r  r  r  )r  r\   r  rd   r  r  )
r}  r   r  list[TypeRange] | Noner  r_   r  r  r  r  )
r}  r   r  r  r  r_   r  r   r  r  )
r}  r   r  r  r  r_   r  r3  r  r  )r  ry   r  r  )r  rd   r  r  r  rd   r  r  )r  r   r  r  r  r   )r  rd   r  r   r  r  r  r  )r  r   r  r  r  r  )r  r   r  r  r  r  )r  rd   rM  r3  r  r   )r  r  r  r_   r  r  )
r  r  r  r_   r  r   r]  r3  r  r  )r  r	  r
  __doc__r>  r  r<  r   r  r=  r@  r1  r2  rJ  propertyrM  rR  rt  r}  rz  r  r  r  rf  r  r  r  r  r  r  r  r  r  r   r"  r%  r$  r  r,  r   r3  r4  r  rj  r  r  rd  ra  r`  re  r  r  r  rf  rg  r0  r7  r]  r  r  rN  rU  rb  r~  rf  r7  r  r  r  r\  r  r  r  r  r  r  r  r  r  r  r!  r$  r'  r   r4  rc  rd  rI  rA  r?  ry  rg  rV  rX  r  r|  r  r  rT  r  r=  rD  ri  r  r  rU  r  r  r  r  r  r  r  r  r  r+  r+  rh  r\  r>  r[  r6  rH  rO  r   r  r  r`  rf  rn  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  r  r  r  r+  r.  rK  r;  r4  r:  r^  rf  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rM  r  rT  r  r  rg  rh  r   rA  r  r  r  rj  ri  rx  r  r.  rL  rB  r4  rC  rU  rS  r6  r  r  r_  r  r  r>  r  r  rk  r  r  r   r  r  r  ri  r  r  r  r  r  r  r  rb  r  rc  r  r  r  r  r  "  sx    GN	 -, "!22##M))    '&H!I " 8<4;"d"  )( N>T>T %>T 	>T
 >T >T >T $2>T 
>T@ . .(**Z OS"K"	"H	&.H: _ '+'
 -1'' $'
 *' 
':205d/bRX^&$ 3D!F#>'9( .2! J J + J 	 J
  J 
 JD)V 2 2 X]q%q%#/q%7Aq%PTq%	q%f	"(!F;>)MV!FV
V
,8V
HKV
V]V
	V
p>N">N >N  	>N
 >N >N >N >N 
>N@2
(2
8<2
LX2
	2
hM4N:W"
 !!#/!	,!8 #'	) $9  	
 
.);)	):'!5'! '! '+	'!
 
'!Rd;dCFdNVd	dL+Z"+Z)5+ZAI+ZW_+Z	+ZZI aa a 	a
 a a #'a #'a a 
aF32H)TS.`U":;088#VJI*.VRVR (VR19VR@HVR	VRp
:   ,"
83Aj3 #' r3r3 r3  	r3
 r3 
r3j *@AV(WW.,','&0,'69,'	,'\44,74AK4	4lMM !M 	M
 M M M 
M^++$,+	(+Z'4?	'4?	:==$/=6==	=(" ) )1Qf
>&H #'RURU RU 	RU
  RU 
RUr %)"" " 	"
 "" 
"R #'#!&44 4 	4
  4 4 4 
4lB&!B& B& 	B&
 B&  B& 
B&H	$ #'[[ [ 	[
 [ [  [ 
[z$ML!!*-!7:!	)! 
 #'== = 	=
  = 
=2%3N !<!<!'!<48!<CJ!<	!<F&P2*$6(, -MM%'< #'< < < 	<
 < < <  < 
<||C!|C37|CAK|CV]|C	 |C|&&)3&>E&	&4%/%/)3%/	%/N"
0"
eIN-2F D
%":,B')R'0R4"	.	#"&&#*&	&&6,\\,0\EI\T[\	\$&86tKK'+K7AK	K[# J;

(9
NR
	
&(9NR	#Xt  %) 	 $L+.!  	
  
#,ad2ad<Qad	adF?8DT T ,0T 	&T l.9	 8%
%
.2%
	(%
N
 4ZlL "L 2;L 	 L ^ ?C((7;(	 (: ?Cr r 7;r 	 r hq_fE E  E  #	E 
 "E   E  +EE  
!E N)VvVpH:"H: "H: !	H:
 %5H: 6H: )-H: 
!H:T) ")  ")  !	) 
 %5)  
!) VE,! [z ?DB%B%"%B%-0B%	(B%H%%!$%,/%	(%H  %)&* #'!%(,  	
  " $    & 
   %)&* #'(,  	
  " $   & 
 & %)&*L #'!%(,LL L 	L
  L "L $L  L L &L 
L\, 0 077(,77>7FV7	7"E	$0/(A& 	 	

57> %*U6)"6)6:6)	6) 6)p+++/+7:+EL+	+:

""	5"2,
 VZ/%/07/BR/	/ 	> "&>> > 	> > 
>RARLSR	RI KO M M<  .. ,. 	.
 . 
). .   ,B IP [_ 	     $6'6' ,6' 	6'
 6' 
)6'p
)V$
LB-19<	 2
'$
',/
'	&
',4I4
= ]aAA*1A>BANYA	Ar  r  c                       e Zd ZdZddZddZy)r  z0Collects the non-nested argument types in a set.c                "    t               | _        y rL  )r;  r  rN  s    r  rJ  zCollectArgTypeVarTypes.__init__/  s    +.5r  c                :    | j                   j                  |       y rL  )r  rv  r  s     r  visit_type_varz%CollectArgTypeVarTypes.visit_type_var2  s    1r  Nr  )r  r   r  r  )r  r	  r
  r  rJ  r   r  r  r  r  r  ,  s    :1r  r  c                     y rL  r  r  proposed_type_rangesr  s      r  r?  r?  6  s     '*r  c                     y rL  r  r"  s      r  r?  r?  <  s     r  c                   |rUt        |      dk(  r|d   j                  }t        |      }t        |t              rV|j                         st        |j                  t              r,|j                  j                  j                  }t        | |      } |D cg c]  }|j                   }}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 cg c]  }|j(                  s|j                   c}      }t+        | |      }	||	fS | |fS c c}w c c}w )a  Takes in the current type and a proposed type of an expression.

    Returns a 2-tuple: The first element is the proposed type, if the expression
    can be the proposed type. The second element is the type it would hold
    if it was not the proposed type, if any. UninhabitedType means unreachable.
    None means no new information can be inferred. If default is set it is returned
    instead.r   r   c              3  4   K   | ]  }|j                     y wrL  r  )r  
type_ranges     r  r  z$conditional_types.<locals>.<genexpr>\  s      
8L*J%%8LrN  Trh  )rS  r  r   r  r   r>  rD  r  r  rh  r]  r   r   r   r  r   r   r:   r   r  r  r   )
r  r#  r  r  	enum_namer'  proposed_itemsproposed_typeproposed_precise_typeremaining_types
             r  r?  r?  B  sg    #$))!,11F$V,F&+.&&(Jv||T,J"OO0099	>|YW<PQ<Pj*//<PQ-n=mW- !')) 
8L
 
mtTO---%lMUYZ"$g-- %.$8$8 ';&:
%44 OO&:%! 3<AVWN .00 W$$9 R$s   E#E"c                *   t        |       } g }||fD ]Z  }t        |      }t        |t              r|j	                  d        0||j	                  i        D|J |j	                  | |i       \ t        t        t        t        f   t        |            S rL  )	r  r   r  r   r  r   r   r  r  )r  r  r  mapsr  ry  s         r  r8  r8  t  s     4 DD'"%c*k?3KK KKO?"?KKs$ # gw&'t55r  c                z    | |vr| S d}| t        |      z   |v r|dz  }| t        |      z   |v r| t        |      z   S )zKGenerate a name that does not appear in table by appending numbers to base.r   )r  )rP  r  r  s      r  rs  rs    sM    5	A
Q-5
 	Q Q-5
 #a&=r  c                `    t        | d      xs! t        | t              xr | j                  dk7  S )z>Returns true if this expression is the 'True' literal/keyword.zbuiltins.Truer   r   r  rq   rD  r  s    r  r  r    s-    a1\Z75K5\PQPWPW[\P\\r  c                `    t        | d      xs! t        | t              xr | j                  dk(  S )z?Returns true if this expression is the 'False' literal/keyword.r  r   r1  r2  s    r  r:  r:    s.    a!12]jG6L6]QRQXQX\]Q]]r  c                D    t        | t              xr | j                  dk(  S )z>Returns true if this expression is the 'None' literal/keyword.r  r  rx   r]  r2  s    r  r]  r]    s    a"Dqzz_'DDr  c                D    t        | t              xr | j                  dk(  S )Nzbuiltins.NotImplementedr5  r2  s    r  r  r    s    a"Nqzz5N'NNr  c                ~   | j                   j                   }t        |      dk(  xr t        |d   x}t              xr |j                  du xs t        |j                        xrZ t        |d   x}t              xrC t        |j                  x}t              xr% |j                  du xs t        |j                        S )z
    Checks whether a function's body is 'return; yield' (the yield being added only
    to promote the function into a generator function).
    r  r   Nr   )r  rS  r  r   r  r]  re   r   )r  r  ret_stmt	expr_stmt
yield_exprs        r  r  r    s    
 99>>DD	Q 	J47*xJ7	J]]d"Dohmm&D	J DG+y^<	J Y^^3zY?		J
 __$H
(Hr  c                   t        |       } t        | t              r[| j                  j                  dv rB| j
                  syt        t        | j
                  d         t              s| j
                  d   S yt        | t              rg }| j                  D ]  }t        |t              rt        |j                        }t        |t              rt        |j                        }t        |t              r|j                  j                  dk(  sJ |j                  |j
                  d          |j                  |        t        d t        |      D              rt        |      S yt        | t               r`| j"                  j                  j$                  D ]6  }|j                  dk(  st'        | j"                  |      j
                  d   c S  J d       y)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__.
    )ru  r  r9  rw  zbuiltins.frozensetz_collections_abc.dict_keysztyping.KeysViewNr   r  c              3  >   K   | ]  }t        |t                 y wrL  )r  r   )r  r  s     r  r  z$builtin_item_type.<locals>.<genexpr>  s     X5Wr:b'**5Ws   ztyping.Mappingz2No Mapping base class found for TypedDict fallback)r   r  r   rh  r]  r  r   r   r  r   r   r  r  r   r   r   r   r  rM  r8   )r  normalized_itemsr  r  rP  s        r  r  r    s    
	B"h77  
 
 77obggaj97Cwwqz!0 / 
B		"((B"j)*2773h(89.x/C/CDHx2x}}7M7MQa7aa ''a(89 ''+  X5EFV5WXX()9::  
B	& KK$$((D}} 000dCHHKK ) 	KJJur  c                V   | |y|j                         }|D ch c]  }t        |       }}| D ]4  }t        |      |vst        t        | |         t              s-| |   ||<   6 |r;| D ]6  }|D ]/  }t        |      t        |      k(  st        | |   ||         ||<   1 8 |S c c}w )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.
    N)copyr7   r  r   r   r;   )m1m2r   rE  n2m2_keysn1s          r  r  r    s     
zRZ WWYF*,-"B|B"G-7*jB9PRY.ZBF2J   B#|B'77!+BrFBrF!;F2J   M .s   B&c                    | |S || S i }| D ]^  }|D ]W  }t        |      t        |      k(  s|r%t        t        | |         t              r	| |   ||<   Bt	        | |   ||   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@  rA  r  rE  rD  rB  s         r  r<  r<  
  s     
z		z	
 &(FBB<#33Jr"v/F$P!#BF2J!6227G!HF2J   Mr  c                    t        |       dk(  ri i fS t        |       dk(  r| d   S | d   \  }}| dd D ]  \  }}t        |||      }t        ||      }! ||fS )a  Reduces a list containing pairs of if/else TypeMaps into a single pair.

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

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

    ...we'd return the output:

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

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

    We only retain the shared expression in the 'else' case because we don't actually know
    whether x was refined or y was refined -- only just that one of the two was refined.
    r   r   Nr  )rS  r  r<  )rB  r   final_if_mapfinal_else_mapr  r  s         r  r  r  $  sy    4 9~2v	Y1	|'0|$n )!"FH/fxXL0JN !. ^++r  c                    i }| y | j                         D ]c  \  }}|}t        |t              r|j                  }t        t	        |      t
        t        t        f      si c S t        j                  |      ||<   e |S rL  )
r  r  r   r  r   r   r   r   r   r{  )rX  converted_type_mapr  r  r  s        r  r  r  K  sw    13^^%	ca%A/!,y(H.MNI#+#;#;C#@4  & r  c                    t        | t        t        f      r,| j                  D cg c]  }t	        |      D ]  }|  c}}S t        | t
              rt	        | j                        S | gS c c}}w )zAFlatten a nested sequence of tuples/lists into one list of nodes.)r  r   rs   r  r  r   r  r  ar  s      r  r  r  [  sZ    !i*+7777aGAJqJ777	Ax	 qvvs
	 8s   A+c                    t        |       } t        | t              r,| j                  D cg c]  }t	        |      D ]  }|  c}}S t        | d      r| j                  d   gS | gS c c}}w )z;Flatten a nested sequence of tuples into one list of nodes.r  r   )r   r  r   r  r  r   r  rL  s      r  r  r  e  sd    A!Y77=7aM!,<q,<7==	1.	/q	{s
	 >s   A'c                b    t        |      }|j                  |       }t        |t              sJ |S rL  )TypeTransformVisitorr  r  rj   )r  r  visitorr  s       r  r@  r@  p  s/    "3'G
,,t
Cc8$$$Jr  c                  (     e Zd Zd fdZddZ xZS )rP  c                0    t         |           || _        y rL  )superrJ  r  )rH  r  	__class__s     r  rJ  zTypeTransformVisitor.__init__x  s    r  c                .    t        || j                        S rL  )r2   r  r&  s     r  rh  zTypeTransformVisitor.type|  s    4**r  )r  dict[TypeVarId, Type]r  r  r
  )r  r	  r
  rJ  rh  __classcell__rU  s   @r  rP  rP  w  s    +r  rP  c                    t        | j                  |j                        }t        | j                         |j                               }||k  S )zOCan a single call match both t and s, based just on positional argument counts?)maxr  minmax_possible_positional_args)r  r3  r  max_argss       r  r  r    s?    1::qzz*H1113Q5S5S5UVHxr  c                "   | j                   D ]  }|j                  j                         st        | |j                  |j                  i      j                  | j                   D cg c]  }|j                  j                         r|! c}      }  n | j                         s| gS g }| j                   D ]V  }t        |t              r(|j                  r|j                  |j                         ;|j                  |j                  g       X g }t        j                  | D ]`  }t        | j                   |      D ci c]  \  }}|j                  | }}}|j                  t        | |      j                  g              b |S c c}w c c}}w )zRExpand a generic callable using all combinations of type variables' values/bounds.rM  )r  r:  is_selfr2   r  r  
is_genericr  r   rk  r  r=  r>  r  )	r  r  r  tvar_valuesrC  variantscombinationrA  tvar_maps	            r  expand_callable_variantsrf    sK   kk 55==?Ar~~67EE&'kkHk1kH F A   <<>s
KdK(T[[t{{+ 0 012	  H ((+647[4QR4Q[b%BEE5L4QRAx0>>>LM 7 O' I" Ss   $F
F
=Fc                J   t        | |      } t        ||      }t        |       D ]}  }t        |      D ]m  }t        |j                  |j                        r$t	        ||t
        ddd d      st	        ||t
        ddd d      sS|rt	        ||t        dddd      rl  y  y)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 rL  is_subset_no_promoter  s     r  r  z;is_unsafe_overlapping_overload_signatures.<locals>.<lambda>      6J1a6P2Pr  T)r  check_args_covariantlyr   r  allow_partial_overlapc                    t        ||        S rL  ri  r  s     r  r  z;is_unsafe_overlapping_overload_signatures.<locals>.<lambda>  rk  r  )r  rl  r   r  rm  )detach_callablerf  rj  r  r   !is_overlapping_types_for_overload)rm  r  class_type_varsr  sig_variantother_variants         r  r  r    s    <  	?;IE?3E
 0	:5e<M $K$8$8-:P:PQ&!?+0&+%P*. *!?+/&+%P*.   '=.'+"'"&*( M = ;P r  c                Z    |s| S | j                  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.
    rM  )r  r9  r  )r  rq  s     r  ro  ro    s.     
tCMM':_'LMMr  c           
         t        | | j                  D ci c]  }|j                  t        |       c}      }t	        ||t
        dd      S c c}w )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.
    Tr  )r2   r  r:  r   r   r   )rm  r  rC  exp_signatures       r  r  r      sZ      9K^K^_K^4DGG8>>K^_M "u4_c  `s   A
c                d   t        | t              r%t        |t              rt        | |t        dd      S yt        | t              rkt        |t              r[t        | j                        t        |j                        k(  r0t        d t        | j                  |j                        D              S y)z#Does t have wider arguments than s?Tr  c              3  :   K   | ]  \  }}t        ||        y wrL  )is_same_arg_prefix)r  r  itemts      r  r  z-is_more_general_arg_prefix.<locals>.<genexpr>#   s!      I^&ue4I^s   F)	r  r   r   r   r   rS  r  r   r  r  r3  s     r  r!  r!     s     !\"a&)1 1TY]   
A|	$a&177|s177|+ ILQWWVWV]V]I^   r  c           	     .    t        | |t        dddd      S )NT)r  r   r  rl  rZ  )r   r   r{  s     r  ry  ry  )   s$    !		#! r  c                    t        |       } t        j                  |   }d}t        | t              rt        | ||      }n't        | t              rt        | j                  ||      }|d|fS d|fS )zDetermine if operator assignment on given value type is in-place, and the method name.

    For example, if operator is '+', return (True, '__iadd__') or (False, '__add__')
    depending on which method is supported by the type.
    NTF)r   r   
op_methodsr  r   _find_inplace_methodr   r  )r  r  r#  existing_methods       r  r  r  5   sr     #
C!!(+FO#x .sFHE	C	'.s||VXN"_$$&=r  c                r    |t         j                  v r%d|dd  z   }| j                  j                  |      r|S y )N__ir  )r   ops_with_inplace_methodrh  r  )r  r#  r  inplace_methods       r  r  r  H   s;    9444+99((8!!r  c                    t        |       }t        |t              r|S t        |t              ry| j	                  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   rf  InvalidInferredTypes)r  rS  ry  s      r  rZ  rZ  P   sC     "#&K+x( 	K	1zz.0111r  c                  <     e Zd ZdZd fdZddZddZd	dZ xZS )
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.
    c                ,    t         |   t               y rL  )rT  rJ  r   )rH  rU  s    r  rJ  zInvalidInferredTypes.__init__p   s    &r  c                    |j                   S rL  )rp  r  s     r  visit_uninhabited_typez+InvalidInferredTypes.visit_uninhabited_types   s    {{r  c                     yr  r  r  s     r  visit_erased_typez&InvalidInferredTypes.visit_erased_typev   s    r  c                6    |j                   j                         S rL  )r:  is_meta_varr  s     r  r   z#InvalidInferredTypes.visit_type_varz   s     tt!!r  r  )r  r   r  r  )r  r   r  r  )r  r   r  r  )	r  r	  r
  r  rJ  r  r  r   rX  rY  s   @r  r  r  i   s    '"r  r  c                       e Zd ZdZddZddZy)rJ  zRReplace all ambiguous Uninhabited types with Any (to avoid spurious extra errors).c                P    |j                   rt        t        j                        S |S rL  )rp  r   r   r  r  s     r  r  z&SetNothingToAny.visit_uninhabited_type   s    ;;9//00r  c                ~    |j                  |j                  D cg c]  }|j                  |        c}      S c c}w )N)r  )r  r  rf  )rH  r  rM  s      r  visit_type_alias_typez%SetNothingToAny.visit_type_alias_type   s3     QVV$DVQXXd^V$DEE$Ds   :N)r  r   r  r   )r  r   r  r   )r  r	  r
  r  r  r  r  r  r  rJ  rJ     s    \
Fr  rJ  c                t    t        | t              r| j                  S t        | t              r| j                  S y)z6Find out if a node describes a static function method.N)r  ri   rt  r   r  )r  s    r  r  r     s1     $ ~~$###r  c                  l    e Zd ZU ded<   ddZddZddZddZddZddZ	e
dd	       Ze
dd
       Zy)r%  z$list[TypeInfo | FuncItem | MypyFile]rT  c                    |g| _         y rL  )rT  )rH  modules     r  rJ  zCheckerScope.__init__   s    X
r  c                `    t        | j                        D ]  }t        |t              s|c S  y rL  )r  rT  r  rj   rH  r  s     r  r  zCheckerScope.top_function   s)    $**%A!X& & r  c                    t        | j                        D ]'  }t        |t              st        |t              r%|c S  y rL  )r  rT  r  rj   rr   r  s     r  r  z$CheckerScope.top_non_lambda_function   s2    $**%A!X&z!Z/H & r  c                \    t        | j                  d   t              r| j                  d   S y rK  )r  rT  r   rN  s    r  r  zCheckerScope.active_class   s%    djjnh/::b>!r  c                    | j                         }|sJ d       | j                  j                  |      }|sJ d       | j                  |dz
     }t        |t              r|S y)z4Is there a class *directly* enclosing this function?z1This method must be called from inside a functionz2CheckerScope stack must always start with a moduler   N)r  rT  r  r  r   )rH  topr  	enclosings       r  r  zCheckerScope.enclosing_class   se    !GGGs

  %JJJuJJuqy)	i*r  c                    | j                         }|s | j                         r| j                         }|rt        |      S y)zAn instance or tuple type representing the current class.

        This returns None unless we are in class body or in a method.
        In particular, inside a function nested in method this returns None.
        N)r  r  r  r   rH  r  s     r  rr  zCheckerScope.active_self_type   s?       "))+'')D &&r  c              #  ~   K   | j                   j                  |       d  | j                   j                          y wrL  rT  r  r6  )rH  r  s     r  rq  zCheckerScope.push_function   )     

$

   ;=c              #  ~   K   | j                   j                  |       d  | j                   j                          y wrL  r  r  s     r  ry  zCheckerScope.push_class   r  r  N)r  rw   r  r  )r  zFuncItem | None)r  r  )r  zInstance | TupleType | None)r  rj   r  r  )r  r   r  r  )r  r	  r
  r  rJ  r  r  r  r  rr  r   rq  ry  r  r  r  r%  r%     sN    //
	  
  r  r%  TKeyTValuec                  8    e Zd ZdZddZd	dZd
dZddZddZy)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()'.
    c                .    i | _         i | _        i | _        y rL  )
_key_to_id_id_to_parent_id_root_id_to_valuesrN  s    r  rJ  zDisjointDict.__init__   s    +-
 13 ;=r  c                H   |sy|D cg c]  }| j                  |       }}|d   }| j                  |   }|j                  |       |dd D ]O  }||k(  s|| j                  vr|| j                  |<   |j                  | j                  j	                  |             Q yc c}w )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.
        Nr   r   )_lookup_or_make_root_idr  rM  r  r6  )rH  r  rk  r  subtree_rootsnew_rootroot_valuessubtree_roots           r  add_mappingzDisjointDict.add_mapping!  s     FJKds55c:dK #--h76")!"-Lx'<t?V?V+V2:D!!,/t66::<HI	 . Ls   Bc                   i }| j                   D ]8  }| j                  |      }||vrt               ||<   ||   j                  |       : g }|j	                         D ]%  \  }}|j                  || j                  |   f       ' |S )z1Returns all disjoint mappings in key-value pairs.)r  _lookup_root_idr;  rv  r  r  r  )rH  root_id_to_keysr  root_idr5  r  s         r  r  zDisjointDict.items!  s    02??C**3/Go-+.5(G$((-	 # ,224MGTMM4!8!8!ABC 5 r  c                    || j                   v r| j                  |      S t        | j                         }|| j                   |<   || j                  |<   t	               | j
                  |<   |S rL  )r  r  rS  r  r;  r  )rH  r  new_ids      r  r  z$DisjointDict._lookup_or_make_root_id%!  sb    $//!'',,)F#)DOOC ,2D!!&).1eD##F+Mr  c                    | j                   |   }|| j                  |   k7  r@| j                  | j                  |      }|| j                  |<   |}|| j                  |   k7  r@|S rL  )r  r  )rH  r  r  
new_parents       r  r  zDisjointDict._lookup_root_id/!  sq    OOC 4((++ ..t/D/DQ/GHJ'1D!!!$A 4((++ r  Nr  )r  z	set[TKey]rk  zset[TValue]r  r  )r  z#list[tuple[set[TKey], set[TValue]]])r  r  r  rS  )	r  r	  r
  r  rJ  r  r  r  r  r  r  r  r  r     s!    4
=J(r  r  c                f   |D ci c]  }|t                }}g }d}t               }t               }t        |       D ]  \  }	\  }
}}||
}|rS|
|k7  s|
|vrJ|s|j                  |t	        |      f       n||   j                  ||       |
}t               }t               }|j                  |	       |j                  |	dz          |
|v s|j                  |	      }||j                  |       |j                  |	dz         }||j                  |        |4|s|j                  |t	        |      f       n||   j                  ||       |j                         D ]9  \  }
}|j                         D ]!  \  }}|j                  |
t	        |      f       # ; |j                  d        |S c c}w )a  Group a series of comparison operands together chained by any operand
    in the 'operators_to_group' set. All other pairwise operands are kept in
    groups of size 2.

    For example, suppose we have the input comparison expression:

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

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

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

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

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

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

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

        same == x < y == same

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

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

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

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

    This function is currently only used to assist with type-narrowing refinements
    and is extracted out to a helper function so we can unit test it.
    Nr   c                    | d   d   S r{  r  )r  s    r  r  z+group_comparison_operands.<locals>.<lambda>!  s    471:r  )r  )
r  r;  r  r  r  r  rv  rg  r  sort)pairwise_comparisonsoperand_to_literal_hashoperators_to_groupr  groupsr  last_operatorcurrent_indicescurrent_hashesr  r  	left_expr
right_expr	left_hash
right_hashdisjoint_dictr  indicess                     r  r  r  :!  s   ` Oa0aN`\^1CN`F0a<> $M #O"uN09:N0O,,Hi $MM 9XM_=_ "(//@W0XY}%11./R$M!eO UN 	AAE" ))/33A6I$""9-044QU;J%"":.7 1P:  $++]F?<S,TU=!--noN $*<<>-*002MD'$++Xvg,GH 3 $2
 !!&=!>##c 1bs   F.c                    t        |       } | rt        | t              syt        d t	        | j
                  | j                  gz         D               S )NFc              3  z   K   | ]3  }t        |t              xr |j                  t        j                  k(   5 y wrL  )r  r   r  r   r  r<  s     r  r  z$is_typed_callable.<locals>.<genexpr>!  s6      =A 	1gI1==I4I4I#II=s   9;)r   r  r   r   r   r  r  )r  s    r  r  r  !  sN    AJq,/ !!++"<=   r  c                X   t        |       } | syt        | t              rt        |        S t        | t              r| j
                  j                  d      }|rt        |t              r@t        |j                  j
                        xs t        |j                  j
                        S t        |j
                  t              r&t        d |j
                  j                  D              S t        |j
                         S yt        | t              rt        d | j                  D              S y)NTr  c              3  2   K   | ]  }t        |        y wrL  r  r4  s     r  r  z'is_untyped_decorator.<locals>.<genexpr>!  s     TBS$/5BSr5  Fc              3  2   K   | ]  }t        |        y wrL  r  r4  s     r  r  z'is_untyped_decorator.<locals>.<genexpr>!  s     D)$'-)r5  )r   r  r   r  r   rh  r^  ra   r  r  r  r   r  r  )r  r#  s     r  r  r  !  s    
#
C	C	&$S)))	C	"$$Z0&),+FKK,<,<= AUJJOOB  &++z2T&++BSBSTTT,V[[999	C	$D#))DDDr  c                    t        | t              rt        | j                        S t        | t              r| j                  S J dt        |               )NzUnexpected func type: )r  ra   rt  r  rh   rh  )r  s    r  rt  rt  !  sE    $	"##	D(	#~~7*4:,775r  c                   t        | t              r| j                  j                  S t        | t              rL| j
                  r@t        | j
                  d   t              r#| j
                  d   j                  j                  S y)Nr   F)r  ra   r  r  r|   r  )r  s    r  r  r  !  s\    $	"yy$$$$)*::*TZZ]I>::a=%%111r  c                    t        | t              rt        | j                        S t        | t              r"t        | j
                  d   j                        S | S r  )r  r   r   r  r   r  r  s    r  rc  rc  !  sC    !\"qzz**!Z qwwqz2233Hr  c                     t        | |dd      S )NT)ri  always_covariantr  r	  s     r  rj  rj  !  s    dETDQQr  c                "    t        | |ddd      S )NT)ri  prohibit_none_typevar_overlapoverlap_for_overloads)r:   r	  s     r  rp  rp  !  s       &*" r  c                L    | j                  d      xr | j                  d       S )z-Check if node is private to class definition.r  )r  endswith)	node_names    r  r  r  !  s&    %Fi.@.@.F*FFr  c                @    t        |       }|d uxr t        |      dk(  S )Nr   )r   rS  )r  strss     r  r3  r3  !  s$    -c2Dt.D	Q.r  c                v    t        | d      ryt        | t              rt        d | j                  D              S y)z<Return True if type is 'bool' or a union with a 'bool' item.r  Tc              3  4   K   | ]  }t        |d         yw)r  N)r   r4  s     r  r  z has_bool_item.<locals>.<genexpr>!  s     R	$T?;	rN  F)r   r  r   r  r  r:  s    r  r  r  !  s1    o.#y!R		RRRr  c                >    t        | t              r| j                  S | S )zIf an expression is an AssignmentExpr, pull out the assignment target.

    We don't make any attempt to pull out all the targets in code like `x := (y := z)`.
    We could support narrowing those if that sort of code turns out to be common.
    )r  rW   r  )r  s    r  r  r  "  s     !^$xxHr  c                R    t        |      }| j                  |       |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.
    )VarAssignVisitorrf  	last_line)r  r  rQ  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 )r  c                .    d| _         d| _        || _        y )NrK  F)r  r/  var_node)rH  r  s     r  rJ  zVarAssignVisitor.__init__"  s    r  c                d    d| _         |j                  D ]  }|j                  |         d| _         y r  )r/  r*  rf  rH  r3  rH  s      r  rI  z&VarAssignVisitor.visit_assignment_stmt"  s)    ))BIIdO r  c                    | j                   r?|j                  | j                  u r&t        | j                  |j
                        | _        y y y rL  )r/  r  r  r[  r  r  r  s     r  visit_name_exprz VarAssignVisitor.visit_name_expr%"  s5    ;;166T]]2 8DN 3;r  c                X    | j                   }d| _         t        | 	  |       || _         y r  )r/  rT  r  rH  r  
old_lvaluerU  s      r  r  z"VarAssignVisitor.visit_member_expr)"  s'    [[
!!$ r  c                X    | j                   }d| _         t        | 	  |       || _         y r  )r/  rT  visit_index_exprr  s      r  r  z!VarAssignVisitor.visit_index_expr/"  s'    [[
 # r  c                    d| _         |j                  D ]  }||j                  |         d| _         |j                  j                  |        y r  )r/  r  rf  r  r  s      r  r  z VarAssignVisitor.visit_with_stmt5"  s@    ((B~		$  	dr  c                    d| _         |j                  j                  |        d| _         |j                  j                  |        |j                  r|j                  j                  |        y y r  )r/  r  rf  r  r  r  s     r  r  zVarAssignVisitor.visit_for_stmt="  sN    	t	d;;KKt$ r  c                    d| _         |j                  j                  |        d| _         |j                  j                  |        y r  )r/  r  rf  rD  r  s     r  visit_assignment_exprz&VarAssignVisitor.visit_assignment_exprE"  s0    		tr  c                    |j                   |j                   j                  |        |j                  *d| _        |j                  j                  |        d| _        y y r  )r=  rf  r  r/  rH  r@  s     r  visit_as_patternz!VarAssignVisitor.visit_as_patternK"  sK    99 IIT"66DKFFMM$DK r  c                p    |j                   *d| _        |j                   j                  |        d| _        y y r  )capturer/  rf  r  s     r  visit_starred_patternz&VarAssignVisitor.visit_starred_patternS"  s1    99 DKIIT"DK !r  )r  r   r  r  r  )r  rx   r  r  )r  rv   r  r  )r  rp   r  r  r  r  )r  rW   r  r  )r@  r   r  r  )r@  r   r  r  )r  r	  r
  rJ  rI  r  r  r  r  r  r  r  r  rX  rY  s   @r  r  r  "  s5    
9!!%  r  r  c                0    t        t        |             dkD  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.
    r   )rS  _ambiguous_enum_variants)rz  s    r  r  r  Z"  s     './!33r  c                   t               }| D ]k  }t        |      }t        |t              r%|j	                  t        |j                               Dt        |t              r|j                  r&|j	                  t        |j                  g             |j                  j                  rLt        d |j                  j                  D              r&|j                  |j                  j                         |j                  j                  r|j                  d       t        |t              r'|j	                  t        |j                   g             It        |t"              r[|j                  d       n |S )Nc              3  8   K   | ]  }|j                   d v   yw))zenum.IntEnumzenum.StrEnumN)r]  )r  rP  s     r  r  z+_ambiguous_enum_variants.<locals>.<genexpr>u"  s      (NXd!AAjs   z<other>)r;  r   r  r   rM  r  r  r   last_known_valuerh  r  r  rM  rv  r]  r   r  r   )rz  rE  r  s      r  r  r  l"  s   UFAa#MM2177;<8$!!68J8J7KLMC (NOffjj( % 

166??+VV^^ 

9%;'MM2AJJ<@A8$JJy!) * Mr  rL  )r  r   r#  r  r  r  r  r  )r  r   r#  r  r  r   r  r  )r  r   r#  r  r  r3  r  r  )r  rd   r  r3  r  r3  r  r  )rP  r  r  r   r  r  r  )r  rj   r  r  )r  r   r  r3  r   )r@  r  rA  r  r   r  r  r  )r@  r  rA  r  r  r  r  r  )rB  r  r   r  r  r  )rX  r  r  r  )r  rd   r  r  )r  r   r  r'  )r  rj   r  rW  r  rj   )r  r   r3  r   r  r  )r  r   r  zlist[CallableType]r  )
rm  r   r  r   rq  list[TypeVarLikeType]r  r  r  r  )r  r   rq  r 	  r  r   )rm  r   r  r   r  r  )r  r   r3  r   r  r  )r  r   r  r  r  ztuple[bool, str])r  r   r#  r  r  r  r  r  )r  r   rS  r  r  r  )r  r  r  r  )r  z,Iterable[tuple[str, Expression, Expression]]r  zMapping[int, Key]r  r3  r  zlist[tuple[str, list[int]]])r  r3  r  r  )r  r3  r  r  )r  zFuncBase | Decoratorr  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  rd   r  rd   )r  ry   r  r   r  rS  )rz  r'  r  r  )rz  r'  r  r3  (f  r  
__future__r   r=  collectionsr   
contextlibr   r   r  r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   typing_extensionsr   r   mypy.checkexprrE  r   r  r   r   r   r   mypy.binderr   r   r   mypy.checkmemberr    r!   r"   r#   r$   mypy.checkpatternr%   mypy.constraintsr&   mypy.erasetyper'   r(   r)   mypy.errorcodesr*   r+   r,   r-   mypy.errorsr.   r/   r0   mypy.expandtyper1   r2   r3   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   mypy.operatorsr   r   r   mypy.optionsr   r   mypy.patternsr   r   mypy.pluginr   r   mypy.pluginsr   r8  
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  r  r  r  r  r  r?  r8  rs  r  r:  r]  r  r  r  r  r<  r  r  r  r  r@  rP  r  rf  r  ro  r  r!  ry  r  r  rZ  r  rJ  r  r%  r  r  r  r  r  r  rt  r  rc  rj  rp  r  r3  r  r  r  r  r  r  r  r  r  <module>r*	     s-    "  # 0    $ 6  N N E E  - ) X X S S C C R R S S 2 S S .
 
 
 -N N N N N N N N N N N N N N N N N N N N^ ? > 5 3 6 :  O O < 1    Y X / b b     *& & & & & & & & & & & & & & & &N c b b 3 3 P P P * * $ $CL 5  !"  !$Wj:KY%VW * W*/CT0T*U Z U%: %%j %$ tJ$456 6

 z GtA+d#%; GtATh1  
VZ**.D*OS*$* 
*
 
.DOS 
 ^b/%/%.D/%OZ/%$/%d6
6 +66A66$]
^
E
O 6r<6 @E$,,$,8<$,$,N ++ + B 	LLL +L 	L
 
L^
N.$	&22"= ".Fn F	: :z v		\74<( \~a$Fa$.a$ !a$ !	a$H28R"G
/
	> ' > B4$r  