ML i,UdZddlmZddlmZddlmZmZddlm Z ddl m Z m Z mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$ddl%m&Z&ddl'm(Z(dd l)m*Z*e+e,ee-fe!fZ.d e/d < dd Z0Gd de&Z1y)aHStrip/reset AST in-place to match state after semantic analyzer pre-analysis. Fine-grained incremental mode reruns semantic analysis main pass and type checking for *existing* AST nodes (targets) when changes are propagated using fine-grained dependencies. AST nodes attributes are sometimes changed during semantic analysis main pass, and running semantic analysis again on those nodes would produce incorrect results, since this pass isn't idempotent. This pass resets AST nodes to reflect the state after semantic pre-analysis, so that we can rerun semantic analysis. (The above is in contrast to behavior with modules that have source code changes, for which we re-parse the entire module and reconstruct a fresh AST. No stripping is required in this case. Both modes of operation should have the same outcome.) Notes: * This is currently pretty fragile, as we must carefully undo whatever changes can be made in semantic analysis main pass, including changes to symbol tables. * We reuse existing AST nodes because it makes it relatively straightforward to reprocess only a single target within a module efficiently. If there was a way to parse a single target within a file, in time proportional to the size of the target, we'd rather create fresh AST nodes than strip them. (This is possible only in Python 3.8+) * Currently we don't actually reset all changes, but only those known to affect non-idempotent semantic analysis behavior. TODO: It would be more principled and less fragile to reset everything changed in semantic analysis main pass and later. * Reprocessing may recreate AST nodes (such as Var nodes, and TypeInfo nodes created with assignment statements) that will get different identities from the original AST. Thus running an AST merge is necessary after stripping, even though some identities are preserved. ) annotations)Iterator)contextmanager nullcontext) TypeAlias)CLASSDEF_NO_INFOAssignmentStmtBlockCallExprClassDef DecoratorForStmtFuncDef ImportAll ImportFrom IndexExprListExpr MemberExprMypyFileNameExprNodeOpExprOverloadedFuncDefRefExprStarExpr SuperExprSymbolTableNode TupleExprTypeInfoVar)TraverserVisitor) CallableType) type_state _TypeAliasSavedAttributesct|}t|tr|j|y|j |y)aReset a fine-grained incremental target to state before semantic analysis. All TypeInfos are killed. Therefore we need to preserve the variables defined as attributes on self. This is done by patches (callbacks) returned from this function that re-add these variables when called. Args: node: node to strip saved_attrs: collect attributes here that may need to be re-added to classes afterwards if stripping a class body (this dict is mutated) N)NodeStripVisitor isinstancerstrip_file_top_levelaccept)node saved_attrsvisitors Z/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/mypy/server/aststrip.py strip_targetr/Js2{+G$!$$T* GceZdZddZddZdfd Zdfd ZddZdfd ZddZ dfd Z dfd Z dd Z d d Z d!fd Zd"d Zd#fd Zd$fd Zd%fd Zd&dZd'fd Zd(fd Zd)dZed*dZed*dZxZS)+r'c<d|_d|_d|_||_y)NFT)type is_class_bodyrecurse_into_functionssaved_class_attrs)selfr6s r.__init__zNodeStripVisitor.__init__`s%%) "'+#"3r0cd|_|jj|j||jj D]}d|vs|j|=y)z:Strip a module top-level (don't recursive into functions).F@N)r5 plugin_depsclearr*namescopy)r7 file_nodenames r.r)z%NodeStripVisitor.strip_file_top_levelms[&+###%OO((* *D$OOD)  *r0c>|jryt| |yN)is_unreachablesuper visit_block)r7b __class__s r.rEzNodeStripVisitor.visit_blockxs    Ar0c|js|j||jjj Dchc]}|j s|j }}g|_|jj|jg|_ |jjDcgc] }||vs| c}|j_ |j|j5t|=|ddd|jjj|j g|_t#j$|jt&|_d|_ycc}wcc}w#1swY{xYw)z8Strip class body and type info, but don't strip methods.N)r5save_implicit_attributesinfor=valuesplugin_generatedr+ type_varsbase_type_exprsextendremoved_base_type_exprsdefsbody enter_classrDvisit_class_defremoved_statementsr#reset_subtype_caches_forranalyzed)r7r+v to_deletesrGs r.rTz NodeStripVisitor.visit_class_def}s& **  ) )$ /&*YY__%;%;%=TASASQVVT T ##D$@$@A')$yy~~ ));A    dii ( * G #D ) * d556"$++DII6$  U  * *s$E-E-2 E2<E2-E77Fc|jjjD]>\}}t|jt s!|j s.||j||f<@y)z9Produce callbacks that re-add attributes defined on self.N)rJr=itemsr(r+r implicitr6)r7r+r@syms r.rIz)NodeStripVisitor.save_implicit_attributessN..0 9ID##((C(S\\58&&tTz2 9r0cl|jsyg|_|j|_|jr-t |jt sJd|j_|jr|j|jn t5t|-|dddy#1swYyxYw)N) r5expandedunanalyzed_typer3r(r" variablesrJ enter_methodrrDvisit_func_defr7r+rGs r.rezNodeStripVisitor.visit_func_defs**  (( 99 dii6 66"$DII -1YYT  tyy )KM ) G "4 ( ) ) )s B**B3cd|j_|jD]}|j||jr|j j|yd|j_d|j _yNF)varr3 decoratorsr*r5funcis_final)r7r+exprs r.visit_decoratorz NodeStripVisitor.visit_decoratorsa OO D KK    & & II  T "!&DHH !&DII r0c|jsy|jj|_d|_d|_t ||yrh)r5unanalyzed_itemsr>r\implrlrDvisit_overloaded_func_defrfs r.rrz*NodeStripVisitor.visit_overloaded_func_defs@** **//1    )$/r0c|j|_d|_d|_|jr.|js"|j D]}|j |t|!|yrh) rbr3 is_final_def is_alias_defr4lvaluesprocess_lvalue_in_methodrDvisit_assignment_stmt)r7r+lvaluerGs r.rxz&NodeStripVisitor.visit_assignment_stmts_(( !! 99T//,, 6--f5 6 %d+r0cg|_yrB assignmentsr7r+s r.visit_import_fromz"NodeStripVisitor.visit_import_from r0cg|_yrBr{r}s r.visit_import_allz!NodeStripVisitor.visit_import_allrr0cb|j|_d|_d|_t||yrB)unanalyzed_index_type index_typeinferred_item_typeinferred_iterator_typerDvisit_for_stmtrfs r.rzNodeStripVisitor.visit_for_stmts-44"&&*# t$r0c&|j|yrB)strip_ref_exprr}s r.visit_name_exprz NodeStripVisitor.visit_name_exprs D!r0cF|j|t| |yrB)rrDvisit_member_exprrfs r.rz"NodeStripVisitor.visit_member_exprs D! !$'r0c2d|_t| |yrB)rWrDvisit_index_exprrfs r.rz!NodeStripVisitor.visit_index_exprs   &r0c2d|_t| |yrB)rWrD visit_op_exprrfs r.rzNodeStripVisitor.visit_op_exprs  d#r0cJd|_d|_d|_d|_d|_y)NF)kindr+fullname is_new_defis_inferred_defr}s r.rzNodeStripVisitor.strip_ref_exprs&   $r0c2d|_t| |yrB)rWrDvisit_call_exprrfs r.rz NodeStripVisitor.visit_call_exprs  %r0c2d|_t| |yrB)rJrDvisit_super_exprrfs r.rz!NodeStripVisitor.visit_super_exprs   &r0c(t|tr|jr|jJ|j|jj vr!|jj |j=|jj |jf}||jvr|j|=yyyt|ttfr#|jD]}|j|yt|tr|j|jyyrB)r(rrr3r@r=defnr6rrr\rwrrm)r7rykeyitems r.rwz)NodeStripVisitor.process_lvalue_in_methods fj )  yy,,,;;$))//1  4yy~~v{{3$000..s31!H 5 6  4--d3 4  )  ) )&++ 6*r0c#|K|j}|j}||_d|_d||_||_yw)NTr3r4r7rJold_typeold_is_class_bodys r.rSzNodeStripVisitor.enter_classs<99 .. !  .:<c#|K|j}|j}||_d|_d||_||_ywrhrrs r.rdzNodeStripVisitor.enter_methods<99 .. "  .r)r6r%returnNone)r?rrr)rFr rr)r+r rr)r+rrr)r+r rr)r+rrr)r+r rr)r+rrr)r+rrr)r+rrr)r+rrr)r+rrr)r+rrr)r+rrr)r+rrr)r+r rr)r+rrr)ryrrr)rJrrzIterator[None])__name__ __module__ __qualname__r8r)rErTrIrernrrrxr~rrrrrrrrrrwrrSrd __classcell__)rGs@r.r'r'_s 3 * 49 ) '0,% "('$%&'7$////r0r'N)r+z&MypyFile | FuncDef | OverloadedFuncDefr,r%rr)2__doc__ __future__rcollections.abcr contextlibrrtyping_extensionsrr$ mypy.nodesrr r r r r rrrrrrrrrrrrrrrrrrr mypy.traverserr! mypy.typesr"mypy.typestater#dicttuplestrr%__annotations__r/r'r`r0r.rsB#$256,#%"53#7#HII 0?N *z/'z/r0