ML iZSUdZddlmZddlmZddlmZddlmZ ddl m Z ddl m Z mZmZmZmZmZmZmZmZmZmZmZmZmZmZddlmZdd lmZdd lm 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/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8dd l9m:Z:ee;efZ?d e@d <eAee?dfdfZBd e@d<eAeCdfZDd e@d< ddZEddZFddZGd dZHd!dZId"dZJd dZKd#dZLGdde4eBZMd$dZNy)%ab Utilities for comparing two versions of a module symbol table. The goal is to find which AST nodes have externally visible changes, so that we can fire triggers and re-process other parts of the program that are stale because of the changes. Only look at detail at definitions at the current module -- don't recurse into other modules. A summary of the module contents: * snapshot_symbol_table(...) creates an opaque snapshot description of a module/class symbol table (recursing into nested class symbol tables). * compare_symbol_table_snapshots(...) compares two snapshots for the same module id and returns fully qualified names of differences (which act as triggers). To compare two versions of a module symbol table, take snapshots of both versions and compare the snapshots. The use of snapshots makes it easy to compare two versions of the *same* symbol table that is being mutated. Summary of how this works for certain kinds of differences: * If a symbol table node is deleted or added (only present in old/new version of the symbol table), it is considered different, of course. * If a symbol table node refers to a different sort of thing in the new version, it is considered different (for example, if a class is replaced with a function). * If the signature of a function has changed, it is considered different. * If the type of a variable changes, it is considered different. * If the MRO of a class changes, or a non-generic class is turned into a generic class, the class is considered different (there are other such "big" differences that cause a class to be considered changed). However, just changes to attributes or methods don't generally constitute a difference at the class level -- these are handled at attribute level (say, 'mod.Cls.method' is different rather than 'mod.Cls' being different). * If an imported name targets a different name (say, 'from x import y' is replaced with 'from z import y'), the name in the module is considered different. If the target of an import continues to have the same name, but it's specifics change, this doesn't mean that the imported name is treated as changed. Say, there is 'from x import y' in 'm', and the type of 'x.y' has changed. This doesn't mean that that 'm.y' is considered changed. Instead, processing the difference in 'm' will be handled through fine-grained dependencies. ) annotations)Sequence)Union) TypeAlias) expand_type)SYMBOL_FUNCBASE_TYPESUNBOUND_IMPORTED DecoratorFuncDefFuncItemMypyFileOverloadedFuncDef ParamSpecExpr SymbolNode SymbolTablerTypeInfo TypeVarExprTypeVarTupleExprVar)find_dataclass_transform_spec)state)AnyType CallableType DeletedType ErasedTypeInstance LiteralTypeNoneType Overloaded Parameters ParamSpecType PartialType TupleTypeType TypeAliasType TypedDictTypeTypeType TypeVarIdTypeVarLikeTypeTypeVarTupleType TypeVarType TypeVisitor UnboundTypeUninhabitedType UnionType UnpackType) get_prefix _TypeAlias Primitive SnapshotItem.SymbolSnapshotcF|Dchc] }|d| }}|Dchc] }|d| }}||z }t|jt|jzD]}||}||}|d} |d} |d|} | | k7r|j| 5| dk(r]|dd|ddk7r|j| t|dtsJt|dtsJ|t | |d|dz}||||k7s|j| |Scc}wcc}w)aReturn names that are different in two snapshots of a symbol table. Only shallow (intra-module) differences are considered. References to things defined outside the module are compared based on the name of the target only. Recurse into class symbol tables (if the class is defined in the target module). Return a set of fully-qualified names (e.g., 'mod.func' or 'mod.Class.method'). .rrN)setkeysadd isinstancedictcompare_symbol_table_snapshots) name_prefix snapshot1 snapshot2namenames1names2triggersitem1item2kind1kind2 item_names Y/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/mypy/server/astdiff.pyr>r>|sX3< <$ Qtf% LL # j SbzU3BZ' Y'eBi. ..eBi. .. 6y%)USUYW WH)D/1 Y')(, O7=  dH % (0F4L k * 37;;?%u%?d../dll+ F4Li (t//0 &t{{3 F4Lm , d../dll+ F4L . / d../dll+ F4L;;"22 22 4==1[@!+FDJ4G4GHt 24@t c1Ad MG@sH ct|tr|jrt|j}n t |}d}t|t r|}nH|j r|j@|jBt|jDt|jFt|jH|jJD cgc]} | jLc} tOd|jPjRD|jTD cgc] } t| c} |jVD cgc] } t| c} ||j+nd|j"f} |jL} tY| |jZ}dtOt]|j^f|d <d || |fSJt|cc} wcc} wcc} wcc} w) zCreate a snapshot description of a symbol table node. The representation is nested tuples and dicts. Only externally visible attributes are included. NrFFuncrr c32K|]}t|ywNrY).0tdefs rK z&snapshot_definition..>sF$-%FAbstractz (abstract)r)0r<rrbrYsnapshot_untyped_signaturer implr funcrrR is_propertyr`var setter_typeis_trivial_bodyr deprecatedis_finalis_class is_static serializer is_overloadrdrdataclass_transform_spec is_abstractis_enum is_protocolfallback_to_anymeta_fallback_to_anyis_named_tuple is_newtypemetaclass_type tuple_typetypeddict_typemrorTtupledefn type_varsbases_promoterjnamessortedabstract_attributes)rSrh signaturervrz first_itemr{rr|ibasepattrsprefix symbol_tables rKrdrdsx $-. 99,9$)),DI248I# dG $D YY%/ 9%E499>>499D d- .4::AJ*i0Z__5P5P4Z^^5O5OP 26$..E#@#F 48 dG $J / 0//*+/::.&'Ay9Q!!.J      MM MM NN  4L4X $ . . 0    _c     D# v5dii@$--PP D) $     "488== 1  6 2   D( ##'#@#@ # +'DT'J $    LL      % %    OO "4#6#6 7 "4?? 3 "4#6#6 7'+xx 0tT]] 0 F$))2E2EF F-1ZZ 8T]4 8'+}} 5!]1  54L4X $ . . 0^b OO3 6,VTZZ@ &0%t?W?W8X2Y%Z \"FE<88 !d4j uO.b 1 9 5s2P-P-4P2<P7P<c4|jtS)z?Create a snapshot representation of a type using nested tuples.)acceptSnapshotTypeVisitortyps rKrYrYNs ::)+ ,,c|r t|Sy)N)z rors rKr`r`Ss S!!rc&td|DS)Nc32K|]}t|ywrnro)rpitems rKrrz!snapshot_types..[s7t$7rs)r)typess rKr\r\Zs 77 77rc.t|jfSrn)rbrcrs rKsnapshot_simple_typer^s I    rc |y|S)Nz)ss rKencode_optional_strrbsyrceZdZdZddZddZddZddZddZddZ ddZ d d Z d!d Z d"d Z d#d Zd$d Zd%dZd&dZd'dZd(dZd)dZd*dZd+dZd,dZd-dZd.dZy)/ra<Creates a read-only, self-contained snapshot of a type object. Properties of a snapshot: - Contains (nested) tuples and other immutable primitive objects only. - References to AST nodes are replaced with full names of targets. - Has no references to mutable or non-primitive objects. - Two snapshots represent the same object if and only if they are equal. - Results must be sortable. It's important that tuples have consistent types and can't arbitrarily mix str and None values, for example, since they can't be compared. crd|j|j|jt|jfS)Nr-)rBoptionalempty_tuple_indexr\argsselfrs rKvisit_unbound_typez&SnapshotTypeVisitor.visit_unbound_typexs2  HH LL  ! ! 388 $   rct|Srnrrs rK visit_anyzSnapshotTypeVisitor.visit_any #C((rct|Srnrrs rKvisit_none_typez#SnapshotTypeVisitor.visit_none_typerrct|Srnrrs rKvisit_uninhabited_typez*SnapshotTypeVisitor.visit_uninhabited_typerrct|Srnrrs rKvisit_erased_typez%SnapshotTypeVisitor.visit_erased_typerrct|Srnrrs rKvisit_deleted_typez&SnapshotTypeVisitor.visit_deleted_typerrc|jr_ttfd|jjj Dt|jj f}nd}dt |jjt|j|jd|fSt|j|fS)Nc3JK|]\}}||jfywrn)r)rpkvrs rKrrz5SnapshotTypeVisitor.visit_instance..s"[TQa$0[s #rr)None) extra_attrsrrrrR immutablerrbrTr\rlast_known_valuerY)rrrs` rKvisit_instancez"SnapshotTypeVisitor.visit_instances ??f[S__=R=R=X=X=Z[[\coo//0K K   1 1 2 388 $--5I    .F%d+Frsc34K|]}|jywrnrirprs rKrrz7SnapshotTypeVisitor.visit_parameters..1a!''1)r\ arg_typesr arg_names arg_kindsrs rKvisit_parametersz$SnapshotTypeVisitor.visit_parameterss?  3== ) F F F 13==1 1   rc x|jr|j|}dt|jt |j t d|jDt d|jD|j|jt|j|jf S)Nrc32K|]}t|ywrnrrs rKrrz:SnapshotTypeVisitor.visit_callable_type..rrsc34K|]}|jywrnrrs rKrrz:SnapshotTypeVisitor.visit_callable_type..rr) is_genericnormalize_callable_variablesr\rrYret_typerrr is_type_objis_ellipsis_args variablesis_boundrs rKvisit_callable_typez'SnapshotTypeVisitor.visit_callable_types >> 33C8C  3== ) #,, ' F F F 13==1 1 OO   3== ) LL  rcg}i}t|jD]\}}td|z }t|tr|j |}nGt|t r|j |}n$t|tsJ|j |}|j||||j<tjd5t||j |cdddS#1swYyxYw)zBNormalize all type variable ids to run from -1 to -len(variables).r8)rT)rN) enumeraterr(r<r+ copy_modifiedr*r!appendrrstrict_optional_setr)rrtvstvmaprrtidtvs rKrz0SnapshotTypeVisitor.normalize_callable_variabless')cmm, DAqBF#C![)&'ooo&=A/0___,!!]333___, JJrNE!$$K  & &t , HsE*8838G H H Hs C55C>c0dt|jfS)Nr#r\rRrs rKvisit_tuple_typez$SnapshotTypeVisitor.visit_tuple_types^CII677rctd|jjD}tt|j}tt|j}d|||fS)Nc3<K|]\}}|t|fywrnro)rpkey item_types rKrrz;SnapshotTypeVisitor.visit_typeddict_type..s^.#ysM)45^sr&)rrRr required_keys readonly_keys)rrrRrequiredreadonlys rKvisit_typeddict_typez(SnapshotTypeVisitor.visit_typeddict_typesV^CIIOOL]^^ 1 123 1 123(;;rcFdt|j|jfS)Nr)rYfallbackrirs rKvisit_literal_typez&SnapshotTypeVisitor.visit_literal_types}S\\:CIIFFrc|jDchc] }t|}}tt|}d|fScc}w)Nr/)rRrYrr)rrrrRr^s rKvisit_union_typez$SnapshotTypeVisitor.visit_union_types>25;t$;;6%=) Z((?MM";< 8>? @V}rN)r?strr@dict[str, SymbolSnapshot]rArrzset[str])r?rrerrr)rSzSymbolNode | Nonerhr5rr5)rr$rr4)rz Type | Nonerr4)rzSequence[Type]rr4)rz str | Nonerr)rwzOverloadedFuncDef | FuncItemrr5)Or __future__rcollections.abcrtypingrtyping_extensionsrr2mypy.expandtyper mypy.nodesrr r r r r rrrrrrrrmypy.semanal_sharedr mypy.stater mypy.typesrrrrrrrrr r!r"r#r$r%r&r'r(r)r*r+r,r-r.r/r0 mypy.utilr1rfloatintboolr3__annotations__rr4objectr5r>rjrdrYr`r\rrrrurrrKr,s2h#$5'">6!c5#t34 :4 y.'@!A3!FG jG#63;/ /((!:(G`( (V=@d!N- 8!dO+l3dONr