ML iXUdZddlmZddlmZddlmZddlmZddl m Z m Z m Z m Z mZddlmZddlZddlZdd lmZdd lmZdd lmZmZmZmZmZmZdd l m!Z!dd l"m#Z#ddl$m%Z&ddl'm(Z(m)Z)m*Z*ddl+m,Z,m-Z-m.Z.m/Z/ddl0m1Z1ddl2m3Z3ddl4m5Z5ddl6m7Z7ddl8m9Z9e rddlm:Z:m;Z;ee gdffZ?de@d<dZAde@d<dZBde@d<gdZCde@d<d2d ZDd3d!ZE d4d"ZFd5d#ZGd6d$ZHd7d%ZId6d&ZJ d8d'ZKe=eLeeeeefe efZMde@d(<e=eLeLeeeeefe efZNde@d)<d9d*ZO d:d+ZPd2d,ZQ d;d-ZRd2d.ZS daTop-level logic for the semantic analyzer. The semantic analyzer binds names, resolves imports, detects various special constructs that don't have dedicated AST nodes after parse (such as 'cast' which looks like a call), populates symbol tables, and performs various simple consistency checks. Semantic analysis of each SCC (strongly connected component; import cycle) is performed in one unit. Each module is analyzed as multiple separate *targets*; the module top level is one target and each function is a target. Nested functions are not separate targets, however. This is mostly identical to targets used by mypy daemon (but classes aren't targets in semantic analysis). We first analyze each module top level in an SCC. If we encounter some names that we can't bind because the target of the name may not have been processed yet, we *defer* the current target for further processing. Deferred targets will be analyzed additional times until everything can be bound, or we reach a maximum number of iterations. We keep track of a set of incomplete namespaces, i.e. namespaces that we haven't finished populating yet. References to these namespaces cause a deferral if they can't be satisfied. Initially every module in the SCC will be incomplete. ) annotations)Iterator) nullcontext)groupby) TYPE_CHECKINGCallableFinalOptionalUnion) TypeAliasN)FineGrainedDeferredNode)Errors) DecoratorFuncDefMypyFileOverloadedFuncDefTypeInfoVar)Options)ClassDefContext) dataclasses)SemanticAnalyzerapply_semantic_analyzer_patches#remove_imported_names_from_symtable)add_type_promotioncalculate_class_abstract_statuscalculate_class_varscheck_protocol_status)#infer_decorator_signature_if_simple)find_dataclass_transform_spec)TypeArgumentAnalyzer)SavedAttributes)is_typeshed_file)GraphState _TypeAliasPatchesr MAX_ITERATIONS CORE_WARMUP)typing_collections_abcbuiltinsabc collectionszcollections.abc core_modulescg}t|||t|||t|t|||t |||t |||t ||d|vrt|dyy)zPerform semantic analysis for all modules in a SCC (import cycle). Assume that reachability analysis has already been performed. The scc will be processed roughly in the order the modules are included in the list. r.N)process_top_levelsprocess_functionsrapply_class_plugin_hookscheck_type_argumentscalculate_class_propertiescheck_blockerscleanup_builtin_scc)graphsccerrorspatchess W/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/mypy/semanal_main.pysemantic_analysis_for_sccr?SsuGuc7+eS'*$G,UC0V,uc625#SE*-.c`|jJt|jjdy)a8Remove imported names from builtins namespace. This way names imported from typing in builtins.pyi aren't available by default (without importing them). We can only do this after processing the whole SCC is finished, when the imported names aren't needed for processing builtins.pyi itself. Nr.)treernames)states r>r9r9os( :: !! !' (8(8*Er@c pg}td|Drt||jg|t||jj }|D]`}t |jtrt|||j|jj|j|j|bt|t||jg|jjt|||jjt!||jg|jjy)aSemantically analyze only selected nodes in a given module. This essentially mirrors the logic of semantic_analysis_for_scc() except that we process only some targets. This is used in fine grained incremental mode, when propagating an update. The saved_attrs are implicitly declared instance attributes (attributes defined on self) removed by AST stripper that may need to be reintroduced here. They must be added before any methods are analyzed. c3PK|]}t|jt ywN) isinstancenoder).0ns r> z0semantic_analysis_for_targets..s 7A:affh ' 7s$&N)anyr3idrestore_saved_attrsmanagersemantic_analyzerrHrIrprocess_top_level_functionfullnameactive_typeinforr5r<check_type_arguments_in_targetsr7)rDnodesr: saved_attrsr=analyzerrKs r>semantic_analysis_for_targetsrY{sG 7 775588*g6 $}}..H   affh ' " eUXXqvv@Q@QSZ  $G,UUXXJ 0D0DE#E5%--2F2FGuuxxj%--2F2FGr@c~|jD]\\}}}|j}|j|}||jv}t |j t sJ|Jt |j t r|j js|j js|r||j|<y)zPRestore instance variables removed during AST strip that haven't been added yet.N) itemsinfogetrCrHrIris_abstract_varexplicit_self_type)rWcdefnamesymr\existingdefined_in_this_classs r>rOrOs(..0# tcyy88D> $ 2#((C(((   s + 0M0M HH ' ')"DJJt '#r@c ptt|}|D]D}||}|jJ|jjj |jFjj j||jtfdtDr"ttttzz d}d}|jj}|jjr&|dz }|tkDrN|jJ|j|j|j 5|j#dddy|r$|jj jg}d} rwj%} || }|jJt'| | ||jd||\} } } || z }| xs| } | s%|jj j)| rw|r |rJdtt|| }r%yy#1swYyxYw)Nc3&K|]}|v ywrG)rJmworklists r>rLz%process_top_levels..s /Q1= /Fr%Must not defer during final iteration)listreversedrBrPrQ prepare_fileincomplete_namespacesupdatecopyallr1r+deferral_debug_contextclearr) file_contextoptions report_hangpopsemantic_analyze_targetdiscard)r:r;r=rNrDfinal_iteration iterationrX all_deferred any_progressnext_iddeferred incompleteprogressris @r>r3r3s x} CAb zz%%% ''44UZZ@A  MM''..s3xxzH /, //D,/0;>>OI}}..H ##))+ Q ~ %::) ))&&uzz5==A '$$& '   MM / / 5 5 7"$  llnG'NE::) ))-D%T?G. *Hj( H $L'38L 33;;GD # L%L L#./**;   ' s H,,H5c# Kt|dDcgc]\}}|t|f}}}|Dchc] \}}| | c}} d}|r|t|k\r td||\}}||Ed{|j |At fd|j ddDr|dz }h|Ed{|j | j|d}|ryycc}}wcc}}w7|7;w)zMake sure that superclass methods are always processed before subclass methods. This algorithm is not very optimal, but it is simple and should work well for lists that are already almost correctly ordered. c |dS)Nrgxs r>z&order_by_subclassing..s qtr@keyNrz"Cannot order method targets by MROc3&K|]}|v ywrGrg)rJparentremaining_infoss r>rLz'order_by_subclassing..sIVv(Irjrk)rrmlen ValueErrorryrMmror{) targetskggroupedr\_ next_group next_infogrouprs @r>order_by_subclassingrs)0^(LM147|MGM+2Gad6FtGOJ  W %AB B":. 5     KK #  Iy}}QR7HI I !OJ  J * )  NG  sDC4C$C4 C*C*0C43C04AC46C27*C4"C42C4c g}|D]W}||j}|Jtt|d}|j|D cgc] \}}} |||| fc} }}Yt |D]V\}}}} ||j j } t|tttfsJ|t| |||||| |Xycc} }}w)Nc*|dj|dfS)Nrkr)liners r>rz#process_functions..sAaDIIqQRtCTr@r) rBsortedget_all_leaf_targetsextendrrPrQrHrrrrR) r:r;r= all_targetsmodulerBrtargetrI active_typerXs r>r4r4sK  V}!!-d39TURY Z Z5NVT;ffdK 0 Z   .B+-N )k=((::$*;Y GHN$NH" eFmVVT;   [sCc d}d}|g} |jj|jj|d} | r| dz } | tk(rN|j J|j |j |j5|jdddno| s|r|r|jj|t|||||||\} }} |s%|jjj||r | rJd| sd}| r|jj||jjy#1swYxYw)zAnalyze single top-level function or method. Process the body of the function (including nested functions) again and again, until all names have been resolved (or iteration limit reached). FTrrkNrl) rtrurpaddr)rBrvrwrxr{rzrP saved_locals) rXrDrrrIrr=r|rrr}rs r>rRrR+sBOJxH ##))+ ""&&v.I Q  &::) ))&&uzz5==A '$$& ' J?  * * 2 26 :)@ FE4ow* &*h MM / / 7 7 ?  H!H H<"O' * ""**62 !' ' 's EE  TargetInfoFullTargetInfocg}|jD]J\}}}t|jttt fs-|j ||j|fL|S)zEReturn all leaf targets in a symbol table (module-level and methods).)local_definitionsrHrIrrrappend)fileresultrSrIrs r>rrgsZ!F'+'='='?>#$ dii'+ Mr@c |jjj||f|j}|J|jj}t g|_t g|_|j|_ t |_ d|_ |jd5|} t| tr | j} |j!| ||||j"|t|tr t%||ddd|jD]\} |j'| t(j*j,} | |j.j1| | ksN| |j.| <^d|_|`|j6r|g|j8|jfSg|j8|jfS#1swYxYw)zSemantically analyze a single target. Return tuple with these items: - list of deferred targets - was some definition incomplete (need to run another pass) - were any new names defined (or placeholders replaced) NFr8) file_noderwr)rPprocessed_targetsrrBrQset global_declsnonlocal_declsrCglobalsimportsr wrap_contextrHrfuncrefresh_partialrwradd_dependencymypybuildPRI_LOW prioritiesr] statement cur_mod_noderr) rrrDrIrr|r=rBrX refresh_nodedepprioritys r>rzrzps  MM##**FF+;< ::D   }}..H UGH"ugHzzHuHH   5  1@ lI .',,L    MM# !  dI & /h ?@- S!::%% u''++C: :$,E  S ! -Hx,,h.?.???8&&(9(999;@@s 'AF>>Gc|D]}||}|jsJt||j|jj|j|jj j }|j5tjjj|jj5|jj|ddddddy#1swYxYw#1swYxYwrG) rBr!rwr#rPrQ named_typerrrDstrict_optional_setstrict_optionalaccept)r:r;r<rrDrXs r>r6r6s ,f zzz'  MM JJ ' ' 6 MM + + 6 6      ! ,!!55emm6S6ST , !!(+ , , , , , , , ,s$>C;C/C;/C8 4C;;D c (t||jt|jj|jxsd|j j j}|j5tjjj|jj5|D]}d}t|jtt fr |j}|j"|j$|f}|j&r|j&j)|n t+5|du|_|jj/|ddd ddddddy#1swYxYw#1swYxYw#1swYyxYw)zCheck type arguments against type variable bounds and restrictions. This mirrors the logic in check_type_arguments() except that we process only some targets. This is used in fine grained incremental mode. N)r!rwr#abs_custom_typeshed_dirpathrPrQrrrrDrrrHrIrrrNrTscope saved_scoperrecurse_into_functionsr)rrDr<rXrrsaveds r>rUrUsC$ >> @PbQ ''22 H     1 ZZ   1 1%--2O2O P 1! 1;?fkkG5F+GH!;;D6#9#94@8> V\\--e4+-16:$6FH3KK&&x011  1 1 1 111  1 1 1 1s=3>F1A>E</%E0  E<F0E9 5E<<F FFc Td}d}|r|dksJd|dz }d}|D]}||}|j}|sJ|jD]\\}} }t| jts"t |j j|| j|j||r[d}^|ryy)a3Apply class plugin hooks within a SCC. We run these after to the main semantic analysis so that the hooks don't need to deal with incomplete definitions such as placeholder types. Note that some hooks incorrectly run during the main semantic analysis pass, for historical reasons. rT z1Internal error: too many class plugin hook passesrkFN) rBrrHrIrapply_hooks_to_classrPrQrw) r:r;r< num_passesrrrDrBrrIs r>r5r5sJJ BS SSa   *F&ME::DK4"446 * 4dii2/ 77   &*  *  * r@c $|j}d}|jD]}|j|||5d} |j|} | r|jj | } | t |rtj} | r|xr| t|||}dddt |} | H|j|||5|xr&tj||| |j}ddd|S|S#1swYxYw#1swY|SxYw)NT) defn decoratorsrvget_fullname_for_hookpluginget_class_decorator_hook_2r dataclasses_plugindataclass_class_maker_callbackrDataclassTransformer transform) selfrr\rwrr<rok decoratorhookdecorator_namespecs r>rrs* 99D B__I   y'4 8 ID!77 BN{{==nM| =i H)HHHDy$!GH I II$ ) .D    y'4 8 dc*??dDRVWaacB d I2I1 I I& d IsA'C9+D9D Dc x|dj}|sJ|D]}||}|j}|sJ|jD]\}}}t|jts"|j j j||j|j5t|j|j|t|j|t|jt|j|j||j|jdddy#1swYxYw)Nr.)rBrrHrIrrPrQrvrwris_stubrrrrC) r:r;r<r.rrDrBrrIs r>r7r7sZ %%H O8 f zz t002 JAtQ$))X.]]44AA$ W[W`W`a3DIIt||VT)$))V<(3& 4::uV}/D/Dhnn    s BD00D9 c8|D]}||jyrGr)r:r;rs r>r8r8/s!' f $$&'r@)r:r$r; list[str]r<rreturnNone)rDr%rr) rDr%rVlist[FineGrainedDeferredNode]r:r$rWr"rr)rWr"rr)r:r$r;rr=r'rr)rzlist[FullTargetInfo]rzIterator[FullTargetInfo])rXrrDr%rstrrrrIz'FuncDef | OverloadedFuncDef | DecoratorrTypeInfo | Noner=r'rr)rrrzlist[TargetInfo])rrrrrDr%rIz2MypyFile | FuncDef | OverloadedFuncDef | Decoratorrrr|boolr=r'rztuple[list[str], bool, bool])rrrDr%r<rrr)rrrrr\rrwrrrr<rrr)r:r$r;rrr)W__doc__ __future__rcollections.abcr contextlibr itertoolsrr,rrr r r typing_extensionsr r& mypy.buildr mypy.state mypy.checkerr mypy.errorsr mypy.nodesrrrrrr mypy.optionsr mypy.pluginr mypy.pluginsrr mypy.semanalrrrmypy.semanal_classproprrrrmypy.semanal_inferrmypy.semanal_sharedr mypy.semanal_typeargsr!mypy.server.aststripr" mypy.utilr#r$r%rmtupleintr'__annotations__r)r+r1r?r9rYrOr3rr4rRrrrrrzr6rUr5rr7r8rgr@r>rs4#$"BB50UU ':  C=60&'5hr4x&8!89:: U e/8 FH H6H?DHSbH H@#07+t!H 4/"/" /" /"  /" 2 /" ! /"/" /"dx"3Y> ?(ASS J #eHg'8)CDhxFXX  7: 7: 7: 7: = 7: ! 7:  7:7:"7:t ,1 *1381BH1 14 *F$ $ $ $ $  $  $ $N$'r@