ML i5QdZddlmZddlmZmZddlmZmZm Z m Z m Z m Z m Z mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!ddl"m#Z#ddl$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;mZ>m?Z?m@Z@ddlAmBZBddlCmDZDmEZE dd ZF dd ZG dd ZHed e ZIGdde#ZJGdde4dZK ddZLddZMy)a Merge a new version of a module AST and symbol table to older versions of those. When the source code of a module has a change in fine-grained incremental mode, we build a new AST from the updated source. However, other parts of the program may have direct references to parts of the old AST (namely, those nodes exposed in the module symbol table). The merge operation changes the identities of new AST nodes that have a correspondence in the old AST to the old ones so that existing cross-references in other modules will continue to point to the correct nodes. Also internal cross-references within the new AST are replaced. AST nodes that aren't externally visible will get new, distinct object identities. This applies to most expression and statement nodes, for example. We perform this merge operation so that we don't have to update all external references (which would be slow and fragile) or always perform translation when looking up references (which would be hard to retrofit). The AST merge operation is performed after semantic analysis. Semantic analysis has to deal with potentially multiple aliases to certain AST nodes (in particular, MypyFile nodes). Type checking assumes that we don't have multiple variants of a single AST node visible to the type checker. Discussion of some notable special cases: * If a node is replaced with a different kind of node (say, a function is replaced with a class), we don't perform the merge. Fine-grained dependencies will be used to rebind all references to the node. * If a function is replaced with another function with an identical signature, call sites continue to point to the same object (by identity) and don't need to be reprocessed. Similarly, if a class is replaced with a class that is sufficiently similar (MRO preserved, etc.), class references don't need any processing. A typical incremental update to a file only changes a few externally visible things in a module, and this means that often only few external references need any processing, even if the modified module is large. * A no-op update of a module should not require any processing outside the module, since all relevant object identities are preserved. * The AST diff operation (mypy.server.astdiff) and the top-level fine-grained incremental logic (mypy.server.update) handle the cases where the new AST has differences from the old one that may need to be propagated to elsewhere in the program. See the main entry point merge_asts for more details. ) annotations)TypeVarcast)MDEFSYMBOL_NODE_EXPRESSION_TYPESAssertTypeExprAssignmentStmtBlockCallExprCastExprClassDef EnumCallExprFuncBaseFuncDef LambdaExpr MemberExprMypyFileNamedTupleExprNameExpr NewTypeExprOverloadedFuncDefRefExpr Statement SuperExpr SymbolNode SymbolTable TypeAlias TypedDictExprTypeInfoVar)TraverserVisitor)AnyTypeCallableArgument CallableType DeletedType EllipsisType ErasedTypeInstance LiteralTypeNoneType Overloaded Parameters ParamSpecType PartialTypePlaceholderTypeRawExpressionTypeSyntheticTypeVisitor TupleTypeType TypeAliasType TypedDictTypeTypeListTypeTypeTypeVarTupleType TypeVarType UnboundTypeUninhabitedType UnionType UnpackType) type_state) get_prefixreplace_object_statec|j|jk(sJt|||j}|||<t||}||usJt||y)aMerge a new version of a module AST to a previous version. The main idea is to preserve the identities of externally visible nodes in the old AST (that have a corresponding node in the new AST). All old node state (outside identity) will come from the new AST. When this returns, 'old' will refer to the merged AST, but 'new_symbols' will be the new symbol table. 'new' and 'old_symbols' will no longer be valid. )prefixN)fullname!replacement_map_from_symbol_tablereplace_nodes_in_astreplace_nodes_in_symbol_table)old old_symbolsnew new_symbolsreplacement_mapnodes Z/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/mypy/server/astmerge.py merge_astsrNtsc <<3<< '' '8[OOC _ 5D 3;;"+?ci}|jD]\}}||vs |jtk(s0|js,t |jj |k(sO||}t |jt |jk(s|js|js|jj |jj k(s|j|jk(s|j||j<t|jtst|jts3t|jj|jj|}|j||jjs|jjs|jj||jj<|S)aoCreate a new-to-old object identity map by comparing two symbol table revisions. Both symbol tables must refer to revisions of the same module id. The symbol tables are compared recursively (recursing into nested class symbol tables), but only within the given module prefix. Don't recurse into other modules accessible through the symbol table. ) itemskindrrLr?rCtype isinstancerrDnamesupdate special_alias)rGrIrB replacementsnamerLnew_node type_repls rMrDrDsP24Liik\ d 3; II z$)):L:L/MQW/W4yHX]]#tDII6MMIIMM**dii.@.@@MMTYY..2ii X]]+dii2z(--QY7Z A )<)dZ!d?d Z"xZ#S)@r]zTransform some nodes to new identities in an AST. Only nodes that live in the symbol table may be replaced, which simplifies the implementation some. Also replace all references to the old identities. c||_yNrXselfrXs rM__init__zNodeReplaceVisitor.__init__ (rOc|j|}|j|j|_t||yre)fixupreplace_statementsdefssupervisit_mypy_filerhrL __class__s rMrpz"NodeReplaceVisitor.visit_mypy_files4zz$++DII6  %rOcd|j|j|_t| |yre)rmbodyro visit_blockrqs rMruzNodeReplaceVisitor.visit_blocks&++DII6  D!rOch|j|}|j|t| |yre)rlprocess_base_funcrovisit_func_defrqs rMrxz!NodeReplaceVisitor.visit_func_defs,zz$ t$ t$rOcF|j|t| |yre)rwrovisit_overloaded_func_defrqs rMrzz,NodeReplaceVisitor.visit_overloaded_func_defs t$ )$/rOc|j|j|_|j|jj|j_|j}|j D]$}t |ts|j|&|r/|jr|j|n|j|t|5|yre)fixup_and_reset_typeinfoinformrnrt type_varsrTr9process_type_var_defis_named_tupleprocess_synthetic_type_infoprocess_type_inforovisit_class_def)rhrLr}tvrrs rMrz"NodeReplaceVisitor.visit_class_defs11$))< 00@ yy.. .B"k*))"- . ""006&&t, %rOc|j|j|j|j|_|jr|j|jyyre) fixup_typerSrlr}unanalyzed_typerhrLs rMrwz$NodeReplaceVisitor.process_base_funcsF  "JJtyy)    OOD00 1 rOc|jD]}|j||j|j|j|jyre)valuesr upper_bounddefault)rhrvalues rMrz'NodeReplaceVisitor.process_type_var_defsAYY #E OOE " # '  #rOcp|j|j|j|jyrerrrrhrs rMprocess_param_spec_defz)NodeReplaceVisitor.process_param_spec_def" '  #rOcp|j|j|j|jyrerrs rMprocess_type_var_tuple_defz-NodeReplaceVisitor.process_type_var_tuple_defrrOcZ|j|jt| |yre)rrSrovisit_assignment_stmtrqs rMrz(NodeReplaceVisitor.visit_assignment_stmts   " %d+rOc&|j|yre)visit_ref_exprrs rMvisit_name_exprz"NodeReplaceVisitor.visit_name_expr s D!rOc|jr |j|j|_|j|t||yre)def_varrlrrovisit_member_exprrqs rMrz$NodeReplaceVisitor.visit_member_exprs9 <<::dll3DL D! !$'rOc|jW|j|j|_t|jtr|jj |yyyre)rLrlrTr r^rs rMrz!NodeReplaceVisitor.visit_ref_exprsJ 99  499-DI$))S)   &* !rOct|||j|j|_|j |jyre)rovisit_namedtuple_exprr|r}rrqs rMrz(NodeReplaceVisitor.visit_namedtuple_exprs8 %d+11$))<  ((3rOcZt|||j|jyre)rovisit_cast_exprrrSrqs rMrz"NodeReplaceVisitor.visit_cast_expr"s  %  "rOcZt|||j|jyre)rovisit_assert_type_exprrrSrqs rMrz)NodeReplaceVisitor.visit_assert_type_expr&s  &t,  "rOc~t|||j!|j|j|_yyre)rovisit_super_exprr}rlrqs rMrz#NodeReplaceVisitor.visit_super_expr*s3  & 99  499-DI !rOct||t|jtr!|j |j|_yyre)rovisit_call_exprrTanalyzedrrlrqs rMrz"NodeReplaceVisitor.visit_call_expr/s9 % dmm%A B JJt}}5DM CrOc|jr;|j|j|_|j|j|j|jt ||yre)r}r|rrold_typerovisit_newtype_exprrqs rMrz%NodeReplaceVisitor.visit_newtype_expr4sO 9955dii@DI  , ,TYY 7  & "4(rOcd|j|j|_t| |yre)rlr}rovisit_lambda_exprrqs rMrz$NodeReplaceVisitor.visit_lambda_expr;s$JJtyy)  !$'rOct|||j|j|_|j |jyre)rovisit_typeddict_exprr|r}rrqs rMrz'NodeReplaceVisitor.visit_typeddict_expr?s8 $T*11$))<  ((3rOc|j|j|_|j|jt||yre)r|r}rrovisit_enum_call_exprrqs rMrz'NodeReplaceVisitor.visit_enum_call_exprDs811$))<  ((3 $T*rOc|j|j|_|j|j|j|jt ||yre)rlr}rrS setter_typero visit_varrqs rMrzNodeReplaceVisitor.visit_varKsFJJtyy)   " (() $rOc|j|j|jD]}|j|t||yre)rtarget alias_tvarsrovisit_type_alias)rhrLvrrs rMrz#NodeReplaceVisitor.visit_type_aliasQsA  $!! A OOA    &rOc||jvrv|j|}t|tr0t|tr t|j|jt||t |t t|S|SN) skip_slots)rXrTrr@rW_get_ignored_slotsrrarhrLrIs rMrlzNodeReplaceVisitor.fixupYsn 4$$ $##D)C$)jh.G%S%6%68J8JK d7I#7N OC=  rOc||jvr6|j|}t|tsJtj||j |S)zFix-up type info and reset subtype caches. This needs to be called at least once per each merged TypeInfo, as otherwise we may leak stale caches. )rXrTrr>reset_all_subtype_caches_forrlrs rMr|z+NodeReplaceVisitor.fixup_and_reset_typeinfodsQ 4$$ $##D)Cc8, ,,  3 3C 8zz$rOcR|%|jt|jyyre)r^TypeReplaceVisitorrXrhtyps rMrzNodeReplaceVisitor.fixup_typers$ ? JJ)$*;*;< = rOc|y|j|j|j|j|jD]}|j||j|j|j|j |j r%|j|j j|j||j_ t|j|jt|jD]0\}}|j|j||j|<2t|j D]#\}}|j|j |%yre)rdeclared_metaclassmetaclass_type_promote tuple_typetypeddict_typerWrrldefnr}rFrUrX enumeratemrobases)rhr}riitembases rMrz$NodeReplaceVisitor.process_type_infovs" <  //0 ++,mm $F OOF # $ ( ++,    OOD..55 6D) %djj$2C2CD * 2GAt**TXXa[1DHHQK 2 , +GAt OODJJqM * +rOc|j||jjD]*}|js|jj |,yre)rrUrrLr^)rhr}rLs rMrz.NodeReplaceVisitor.process_synthetic_type_infosG t$JJ%%' 'Dyy   & 'rOc|g}|D]4}t|tr|j|}|j|6|Sre)rTrrlappend)rhnodesresultrLs rMrmz%NodeReplaceVisitor.replace_statementssA D$ +zz$' MM$   rOrXdict[SymbolNode, SymbolNode]returnNone)rLrrr)rLr rr)rLrrr)rLrrr)rLr rr)rLrrr)rr9rr)rr-rr)rr8rr)rLr rr)rLrrr)rLrrr)rLrrr)rLrrr)rLr rr)rLrrr)rLrrr)rLr rr)rLrrr)rLrrr)rLrrr)rLrrr)rLr rr)rLrrrrLrarra)rLrrr)rz Type | Nonerr)r}zTypeInfo | Nonerr)r}rrr)rlist[Statement]rr)$__name__ __module__ __qualname____doc__rirprurxrzrrwrrrrrrrrrrrrrrrrrrrlr|rrrrm __classcell__)rrs@rMr]r]s)& "% 0 &2$ $$, "( '4 ##. 6 )(4 + '   >+$'rOr]ceZdZdZddZd dZd!dZd"dZd#dZd$dZ d%dZ d&d Z d'd Z d(d Z d)d Zd*d Zd+dZd,dZd-dZd.dZd/dZd0dZd1dZd2dZd3dZd4dZd5dZd6dZd7dZd8dZd9dZd:dZy);rzSimilar to NodeReplaceVisitor, but for type objects. Note: this visitor may sometimes visit unanalyzed types such as 'UnboundType' and 'RawExpressionType' For example, see NodeReplaceVisitor.process_base_func. c||_yrerfrgs rMrizTypeReplaceVisitor.__init__rjrOc|j|j|_|jD]}|j||jr|jj|yyre)rlrSargsr^last_known_valuerhrargs rMvisit_instancez!TypeReplaceVisitor.visit_instancesW::chh'88 C JJt       ' ' - rOc|jJ|j|j|_|jD]}|j|yre)aliasrlrr^rs rMvisit_type_alias_typez(TypeReplaceVisitor.visit_type_alias_typesEyy$$$JJsyy) 88 C JJt  rOcyrers rM visit_anyzTypeReplaceVisitor.visit_any rOcyrerrs rMvisit_none_typez"TypeReplaceVisitor.visit_none_typerrOc |jD]}|j||jj||jr5|jj |j|j|_|j |j j||jD]P}t|ts|jj||jD]}|j|Ryre) arg_typesr^ret_type definitionrXr_fallback variablesrTr9rr)rhrrrrs rMvisit_callable_typez&TypeReplaceVisitor.visit_callable_types== C JJt   D! >>!..223>>3>>RCN << # LL   %-- 'B"k*%%d+YY'ELL&' 'rOc|jD]}|j||j|jj|yyre)rQr^rrhtrs rMvisit_overloadedz#TypeReplaceVisitor.visit_overloadedsDGG D KK   :: ! JJ  d # "rOctd)NzCannot handle erased type RuntimeErrorrhrs rMvisit_erased_typez$TypeReplaceVisitor.visit_erased_types677rOcyrerrs rMvisit_deleted_typez%TypeReplaceVisitor.visit_deleted_typerrOctd)NzCannot handle partial typerrs rMvisit_partial_typez%TypeReplaceVisitor.visit_partial_types788rOc|jD]}|j||j|jj|yyre)rQr^partial_fallbackrhrrs rMvisit_tuple_typez#TypeReplaceVisitor.visit_tuple_typesHII D KK      +  ' ' - ,rOc:|jj|yre)rr^rs rMvisit_type_typez"TypeReplaceVisitor.visit_type_type rOc|jj||jj||jD]}|j|yre)rr^rr)rhrrs rMvisit_type_varz!TypeReplaceVisitor.visit_type_varsE t$ 4 ZZ E LL  rOcp|jj||jj|yrerr^rrs rMvisit_param_specz#TypeReplaceVisitor.visit_param_spec& t$ 4 rOcp|jj||jj|yrerrs rMvisit_type_var_tuplez'TypeReplaceVisitor.visit_type_var_tuplerrOc:|jj|yre)rSr^rs rMvisit_unpack_typez$TypeReplaceVisitor.visit_unpack_typer rOcH|jD]}|j|yre)rr^rs rMvisit_parametersz#TypeReplaceVisitor.visit_parameterss!== C JJt  rOc|jjD]}|j||jj|yre)rQrr^r)rhr value_types rMvisit_typeddict_typez'TypeReplaceVisitor.visit_typeddict_types>))**, $J   d # $ D!rOcyrerrs rMvisit_raw_expression_typez,TypeReplaceVisitor.visit_raw_expression_typerrOc:|jj|yre)rr^rs rMvisit_literal_typez%TypeReplaceVisitor.visit_literal_types D!rOcH|jD]}|j|yrerr^rs rMvisit_unbound_typez%TypeReplaceVisitor.visit_unbound_types!88 C JJt  rOcH|jD]}|j|yrerQr^r s rMvisit_type_listz"TypeReplaceVisitor.visit_type_list!II D KK  rOc:|jj|yre)rr^rs rMvisit_callable_argumentz*TypeReplaceVisitor.visit_callable_argument s trOcyrerrs rMvisit_ellipsis_typez&TypeReplaceVisitor.visit_ellipsis_typerrOcyrerrs rMvisit_uninhabited_typez)TypeReplaceVisitor.visit_uninhabited_typerrOcH|jD]}|j|yrer%r s rMvisit_union_typez#TypeReplaceVisitor.visit_union_typer'rOcH|jD]}|j|yrer"rs rMvisit_placeholder_typez)TypeReplaceVisitor.visit_placeholder_types!FF D KK  rOc`||jvr|j|}tt|S|Sre)rXrrars rMrlzTypeReplaceVisitor.fixups1 4$$ $##D)CC=  rONr)rr(rr)rr4rr)rr"rr)rr*rr)rr$rr)rr+rr)rr'rr)rr%rr)rr.rr)rr2rr)rr7rr)rr9rr)rr-rr)rr8rr)rr=rr)rr,rr)rr5rr)rr0rr)rr)rr)rr:rr)rr6rr)rr#rr)rr&rr)rr;rr)rr<rr)rr/rrr) rrrrrirrrrrrrrrr r rrrrrrrr r#r&r)r+r-r/r1rlrrOrMrrs).   ' $8 9. !!"  "   rOrNc`|jD]}|js|j|vr9||j}|j}t||t|||_t |jt t fsx|jjt|yr) rrLr@rrTr rr^r])symbolsrXrLrIrGs rMrFrF%s C 99yyL("499-ii$S#:LS:QR $))c9%56   !3L!AB CrOcHt|tryt|tryy)N)setter)rWr)rTrr)rLs rMrr4s $)*$!! rO) rGrrHrrIrrJrrr)rGrrIrrBstrrr)rLrrXrrr)r4rrXrrr)rLrrztuple[str, ...])Nr __future__rtypingrr mypy.nodesrrrr r r r r rrrrrrrrrrrrrrrrrrr mypy.traverserr! mypy.typesr"r#r$r%r&r'r(r)r*r+r,r-r.r/r0r1r2r3r4r5r6r7r8r9r:r;r<r=mypy.typestater> mypy.utilr?r@rNrDrErar]rrFrrrOrMr?s?-^# :,<&6@ @ +@2:@IT@ @> &03!D (  ($@ ( ( T$Q)QhH-d3HV C  C(D C  CrO