
    X-Ph!R                       U d Z ddlmZ ddlmZ ddlmZ ddlmZ	 ddl
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 ddlmZ dd	l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/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8 ddl9m:Z: ee;e<e=e>f         Z?de@d<   eAee?df         df         ZBde@d<   eAeCdf         ZDde@d<   d2dZEd3dZFd4dZGd5d"ZHd6d$ZId7d'ZJd5d(ZKd8d+ZL G d, d-e4eB                   ZMd9d0ZNd1S ):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_TYPESUNBOUND_IMPORTED	DecoratorFuncDefFuncItemMypyFileOverloadedFuncDefParamSpecExpr
SymbolNodeSymbolTabler   TypeInfoTypeVarExprTypeVarTupleExprVar)find_dataclass_transform_spec)state)AnyTypeCallableTypeDeletedType
ErasedTypeInstanceLiteralTypeNoneType
Overloaded
ParametersParamSpecTypePartialType	TupleTypeTypeTypeAliasTypeTypedDictTypeTypeType	TypeVarIdTypeVarLikeTypeTypeVarTupleTypeTypeVarTypeTypeVisitorUnboundTypeUninhabitedType	UnionType
UnpackType)
get_prefix
_TypeAlias	PrimitiveSnapshotItem.SymbolSnapshotname_prefixstr	snapshot1dict[str, SymbolSnapshot]	snapshot2returnset[str]c                     fd|D             } fd|D             }||z  }t          |                                          t          |                                          z  D ]}||         }||         }|d         }	|d         }
  d| }|	|
k    r|                    |           E|	dk    r|dd         |dd         k    r|                    |           t          |d         t                    sJ t          |d         t                    sJ |t          ||d         |d                   z  }||         ||         k    r|                    |           |S )a  Return 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').
    c                    h | ]	} d | 
S . .0namer6   s     S/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/mypy/server/astdiff.py	<setcomp>z1compare_symbol_table_snapshots.<locals>.<setcomp>   '    <<<$%%t%%<<<    c                    h | ]	} d | 
S r?   rA   rB   s     rE   rF   z1compare_symbol_table_snapshots.<locals>.<setcomp>   rG   rH   r   r@   r   N)setkeysadd
isinstancedictcompare_symbol_table_snapshots)r6   r8   r:   names1names2triggersrD   item1item2kind1kind2	item_names   `           rE   rP   rP   |   s    =<<<)<<<F<<<<)<<<FH INN$$%%INN,<,<(=(== ( ($$aa"++T++	E>>LL####j  SbSzU3B3Z''Y'''eBi.....eBi.....6y%)USUYWWWHH )D/11Y'''OrH   tabler   c                   i }|                                 D ]\  }}|j        }|r|j        nd}||j        |j        f}t          |t                    rd|f||<   Dt          |t                    rCd|j        d |j	        D             t          |j                  t          |j                  f||<   t          |t                    r9dt          |j                  |j        |j        t%          |j                  f||<   t          |t(                    r4d|j        t          |j                  t          |j                  f||<   3t          |t*                    r4d|j        t          |j                  t          |j                  f||<   ||j        t,          k    sJ |r4t/          |j                  | k    rd|t1          |          j        f||<   t5          ||          ||<   |S )	am  Create a snapshot description that represents the state of a symbol table.

    The snapshot has a representation based on nested tuples and dicts
    that makes it easy and fast to find differences.

    Only "shallow" state is included in the snapshot -- references to
    things defined in other modules are represented just by the names of
    the targets.
    N	ModulerefTypeVarc                ,    g | ]}t          |          S rA   snapshot_type)rC   values     rE   
<listcomp>z)snapshot_symbol_table.<locals>.<listcomp>   s     ???%u%%???rH   r   	ParamSpecTypeVarTupleCrossRef)itemsnodefullnamekindmodule_publicrN   r   r   variancevaluesr_   upper_bounddefaultr   snapshot_typesalias_tvars
normalizedno_argssnapshot_optional_typetargetr   r   r	   r1   type__name__snapshot_definition)r6   rY   resultrD   symbolrf   rg   commons           rE   snapshot_symbol_tablerz      s    )+F 1A 1Af{$(24==dFK)=>dH%% ,	A
 (0F4LLk** &	A??4;???d.//dl++F4LL i(( 	At/00&t{33F4LL m,, 	Ad.//dl++	F4LL .// 	Ad.//dl++	F4LL ;"22222 A
4=11[@@ !+FDJJ4GHt24@@tMrH   rf   SymbolNode | Nonery   c                ~   t          | t                    rK| j        rt          | j                  }nt	          |           }d}t          | t
                    r| }n4| j        r-t          | j        t                    r| j        j        n| j        }d}t          | t                    rN| j
        rG| j
        d         }t          |t                    r%|j        j        rt          |j        j                  }|r|j        nd}t!          |           }d|| j        | j        | j        | j        ||||                                ndt          | t
                    r| j        nd|fS t          | t,                    rd|t          | j                  | j        fS t          | t                    r5d| j        t          | j        j                  t1          | j        |          fS t          | t2                    r4| j        }|t!          |           }| j        | j        | j        | j        | j        | j         | j!        t          | j"                  t          | j#                  t          | j$                  d | j%        D             tM          d | j'        j(        D                       d	 | j)        D             d
 | j*        D             ||                                nd| j        f}| j+        }	tY          |	| j-                  }
dtM          t]          | j/                            f|
d<   d|||
fS J t          |                       )zCreate a snapshot description of a symbol table node.

    The representation is nested tuples and dicts. Only externally
    visible attributes are included.
    Nr   FFuncr   r
   c                    g | ]	}|j         
S rA   )rg   rC   bases     rE   ra   z'snapshot_definition.<locals>.<listcomp>,  s    000tT]000rH   c              3  4   K   | ]}t          |          V  d S Nr^   )rC   tdefs     rE   	<genexpr>z&snapshot_definition.<locals>.<genexpr>5  s*      FF$-%%FFFFFFrH   c                ,    g | ]}t          |          S rA   r^   r   s     rE   ra   z'snapshot_definition.<locals>.<listcomp>6  s     888T]4  888rH   c                ,    g | ]}t          |          S rA   r^   )rC   ps     rE   ra   z'snapshot_definition.<locals>.<listcomp>7  s     555!]1555rH   Abstractz
(abstract)r   )0rN   r   rt   r_   snapshot_untyped_signaturer   implr
   funcr   re   is_propertyrr   varsetter_typeis_trivial_bodyr   is_finalis_class	is_static	serialize
deprecatedr   is_overloadrv   r   dataclass_transform_specis_abstractis_enumis_protocolfallback_to_anymeta_fallback_to_anyis_named_tuple
is_newtypemetaclass_type
tuple_typetypeddict_typemrotupledefn	type_varsbases_promoterg   rz   namessortedabstract_attributes)rf   ry   	signaturer   r   
first_itemr   r   attrsprefixsymbol_tables              rE   rv   rv      sF    $-.. U!9 	9,9$),D,DII2488I#dG$$ 	UDDY 	U%/	9%E%ET49>>49Dd-.. 	Q4: 	QAJ*i00 QZ_5P Q4Z^5OPP26A$..E#@#F#F MMN4L4X$..000^b)$88BDOOd
 	
 
D#		 6!v5di@@$-PP	D)	$	$ 4! "48=11	622	
 	
 
D(	#	# '!#'#@ #+'DT'J'J$ L %O"4#677"4?33"4#67700tx000 FF$)2EFFFFF88TZ88855t}5554L4X$..000^bO3
6 ,VTZ@@&0%t?W8X8X2Y2Y%Z\"FE<88 	!d4jj   rH   typr$   c                D    |                      t                                S )z?Create a snapshot representation of a type using nested tuples.)acceptSnapshotTypeVisitorr   s    rE   r_   r_   E  s    ::)++,,,rH   Type | Nonec                (    | rt          |           S dS )N)z	<not set>r^   r   s    rE   rr   rr   J  s    
 S!!!~rH   typesSequence[Type]c                4    t          d | D                       S )Nc              3  4   K   | ]}t          |          V  d S r   r^   rC   items     rE   r   z!snapshot_types.<locals>.<genexpr>R  s*      77t$$777777rH   )r   )r   s    rE   rn   rn   Q  s    77777777rH   c                ,    t          |           j        fS r   )rt   ru   r   s    rE   snapshot_simple_typer   U  s    II  rH   s
str | Nonec                    | dS | S )Nz<None>rA   )r   s    rE   encode_optional_strr   Y  s    yxrH   c                      e Zd ZdZd1dZd2dZd3d
Zd4dZd5dZd6dZ	d7dZ
d8dZd9dZd:dZd;dZd<dZd=dZd>dZd?d!Zd@d#ZdAd%ZdBd'ZdCd)ZdDd+ZdEd-ZdFd/Zd0S )Gr   a<  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.
    r   r-   r;   r4   c                R    d|j         |j        |j        t          |j                  fS )Nr-   )rD   optionalempty_tuple_indexrn   argsselfr   s     rE   visit_unbound_typez&SnapshotTypeVisitor.visit_unbound_typeo  s,    HL!38$$
 	
rH   r   c                     t          |          S r   r   r   s     rE   	visit_anyzSnapshotTypeVisitor.visit_anyx      #C(((rH   r   c                     t          |          S r   r   r   s     rE   visit_none_typez#SnapshotTypeVisitor.visit_none_type{  r   rH   r.   c                     t          |          S r   r   r   s     rE   visit_uninhabited_typez*SnapshotTypeVisitor.visit_uninhabited_type~  r   rH   r   c                     t          |          S r   r   r   s     rE   visit_erased_typez%SnapshotTypeVisitor.visit_erased_type  r   rH   r   c                     t          |          S r   r   r   s     rE   visit_deleted_typez&SnapshotTypeVisitor.visit_deleted_type  r   rH   r   c                h    |j         r^t          t           fd|j         j                                        D                                 t          |j         j                  f}nd}dt          |j        j                  t          |j
                  |j        dnt          |j                  |fS )Nc              3  L   K   | ]\  }}||                               fV  d S r   )r   )rC   kvr   s      rE   r   z5SnapshotTypeVisitor.visit_instance.<locals>.<genexpr>  s6      [[TQa$0[[[[[[rH   rA   r   )None)extra_attrsr   r   r   re   	immutabler   rt   rg   rn   r   last_known_valuer_   )r   r   r   s   `  rE   visit_instancez"SnapshotTypeVisitor.visit_instance  s    ? 	f[[[[S_=R=X=X=Z=Z[[[[[\\co/00KK
 K 12238$$-5II=I];^;^
 	
rH   r+   c           
         d|j         |j        |j        j        |j        j        t          |j                  t          |j                  t          |j	                  |j
        f	S )Nr\   )rD   rg   idraw_id
meta_levelrn   rk   r_   rl   rm   rj   r   s     rE   visit_type_varz"SnapshotTypeVisitor.visit_type_var  sT    HLFMF3:&&#/**#+&&L

 
	
rH   r!   c                    d|j         j        |j         j        |j        t	          |j                  t	          |j                  fS )Nrb   )r   r   r   flavorr_   rl   rm   r   s     rE   visit_param_specz$SnapshotTypeVisitor.visit_param_spec  s=    FMFJ#/**#+&&
 	
rH   r*   c                    d|j         j        |j         j        t          |j                  t          |j                  fS )Nr*   )r   r   r   r_   rl   rm   r   s     rE   visit_type_var_tuplez(SnapshotTypeVisitor.visit_type_var_tuple  s8    FMF#/**#+&&
 	
rH   r0   c                .    dt          |j                  fS )Nr0   )r_   rt   r   s     rE   visit_unpack_typez%SnapshotTypeVisitor.visit_unpack_type  s    mCH5566rH   r    c                    dt          |j                  t          d |j        D                       t          d |j        D                       fS )Nr    c              3  4   K   | ]}t          |          V  d S r   r   rC   rD   s     rE   r   z7SnapshotTypeVisitor.visit_parameters.<locals>.<genexpr>  +      FF%d++FFFFFFrH   c              3  $   K   | ]}|j         V  d S r   r`   rC   r   s     rE   r   z7SnapshotTypeVisitor.visit_parameters.<locals>.<genexpr>  $      11a!'111111rH   )rn   	arg_typesr   	arg_names	arg_kindsr   s     rE   visit_parametersz$SnapshotTypeVisitor.visit_parameters  sT    3=))FFFFFFF113=11111	
 	
rH   r   c           
     r   |                                 r|                     |          }dt          |j                  t	          |j                  t          d |j        D                       t          d |j        D                       |	                                |j
        t          |j                  fS )Nr   c              3  4   K   | ]}t          |          V  d S r   r   r   s     rE   r   z:SnapshotTypeVisitor.visit_callable_type.<locals>.<genexpr>  r   rH   c              3  $   K   | ]}|j         V  d S r   r   r   s     rE   r   z:SnapshotTypeVisitor.visit_callable_type.<locals>.<genexpr>  r   rH   )
is_genericnormalize_callable_variablesrn   r   r_   ret_typer   r   r   is_type_objis_ellipsis_args	variablesr   s     rE   visit_callable_typez'SnapshotTypeVisitor.visit_callable_type  s    >> 	933C88C3=))#,''FFFFFFF113=11111OO 3=))	
 		
rH   c                N   g }i }t          |j                  D ]\  }}t          d|z
            }t          |t                    r|                    |          }nYt          |t                    r|                    |          }n-t          |t                    sJ |                    |          }|                    |           |||j	        <   t          j        d          5  t          ||                              |          cddd           S # 1 swxY w Y   dS )zBNormalize all type variable ids to run from -1 to -len(variables).rJ   )r   T)r   N)	enumerater   r(   rN   r+   copy_modifiedr*   r!   appendr   r   strict_optional_setr   )r   r   tvstvmapir   tidtvs           rE   r   z0SnapshotTypeVisitor.normalize_callable_variables  su   ')cm,, 
	 
	DAqBF##C![)) -&'ooo&=&=A/00 -___,,!!]33333___,,JJrNNNE!$KK&t,, 	H 	HsE**8838GG	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	Hs   )$DD!Dr#   c                .    dt          |j                  fS )Nr#   rn   re   r   s     rE   visit_tuple_typez$SnapshotTypeVisitor.visit_tuple_type  s    ^CI6677rH   r&   c                    t          d |j                                        D                       }t          t          |j                            }t          t          |j                            }d|||fS )Nc              3  >   K   | ]\  }}|t          |          fV  d S r   r^   )rC   key	item_types      rE   r   z;SnapshotTypeVisitor.visit_typeddict_type.<locals>.<genexpr>  s3      ^^.#ysM)445^^^^^^rH   r&   )r   re   r   required_keysreadonly_keys)r   r   re   requiredreadonlys        rE   visit_typeddict_typez(SnapshotTypeVisitor.visit_typeddict_type  si    ^^CIOOL]L]^^^^^ 12233 12233(;;rH   r   c                :    dt          |j                  |j        fS )Nr   )r_   fallbackr`   r   s     rE   visit_literal_typez&SnapshotTypeVisitor.visit_literal_type  s    }S\::CIFFrH   r/   c                d    d |j         D             }t          t          |                    }d|fS )Nc                ,    h | ]}t          |          S rA   r^   r   s     rE   rF   z7SnapshotTypeVisitor.visit_union_type.<locals>.<setcomp>  s     ;;;t$$;;;rH   r/   )re   r   r   )r   r   re   rp   s       rE   visit_union_typez$SnapshotTypeVisitor.visit_union_type  s7     <;;;;6%==))
Z((rH   r   c                .    dt          |j                  fS )Nr   r  r   s     rE   visit_overloadedz$SnapshotTypeVisitor.visit_overloaded  s    nSY7788rH   r"   c                    t           r   )RuntimeErrorr   s     rE   visit_partial_typez&SnapshotTypeVisitor.visit_partial_type  s
     rH   r'   c                .    dt          |j                  fS )Nr'   )r_   r   r   s     rE   visit_type_typez#SnapshotTypeVisitor.visit_type_type  s    M#(3344rH   r%   c                V    |j         J d|j         j        t          |j                  fS )Nr%   )aliasrg   rn   r   r   s     rE   visit_type_alias_typez)SnapshotTypeVisitor.visit_type_alias_type  s,    y$$$!3^CH5M5MNNrH   N)r   r-   r;   r4   )r   r   r;   r4   )r   r   r;   r4   )r   r.   r;   r4   )r   r   r;   r4   )r   r   r;   r4   )r   r   r;   r4   )r   r+   r;   r4   )r   r!   r;   r4   )r   r*   r;   r4   )r   r0   r;   r4   )r   r    r;   r4   )r   r   r;   r4   )r   r   r;   r   )r   r#   r;   r4   )r   r&   r;   r4   )r   r   r;   r4   )r   r/   r;   r4   )r   r   r;   r4   )r   r"   r;   r4   )r   r'   r;   r4   )r   r%   r;   r4   )ru   
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r   r   r   r  r   r  r  r  r  r  r"  r$  r'  rA   rH   rE   r   r   `  s        
 
 
 
) ) ) )) ) ) )) ) ) )) ) ) )) ) ) )
 
 
 
"
 
 
 

 
 
 

 
 
 
7 7 7 7
 
 
 

 
 
 
H H H H$8 8 8 8< < < <G G G G) ) ) )9 9 9 9   
5 5 5 5O O O O O OrH   r   r   OverloadedFuncDef | FuncItemc                   t          | t                    r(t          | j                  t          | j                  fS g }| j        D ]}t          |t                    rO|j        j        r-|	                    t          |j        j                             P|	                    d           f|	                    t          |                     t          |          S )a  Create a snapshot of the signature of a function that has no explicit signature.

    If the arguments to a function without signature change, it must be
    considered as different. We have this special casing since we don't store
    the implicit signature anywhere, and we'd rather not construct new
    Callable objects in this module (the idea is to only read properties of
    the AST here).
    )DecoratorWithoutType)rN   r   r   r   r   re   r
   r   rt   r  r_   r   )r   rw   r   s      rE   r   r     s     $!! dn%%uT^'<'<==')J 	@ 	@D$	** @8= =MM-">">????MM";<<<<8>>????V}}rH   N)r6   r7   r8   r9   r:   r9   r;   r<   )r6   r7   rY   r   r;   r9   )rf   r{   ry   r5   r;   r5   )r   r$   r;   r4   )r   r   r;   r4   )r   r   r;   r4   )r   r   r;   r7   )r   r+  r;   r5   )Or*  
__future__r   collections.abcr   typingr   typing_extensionsr   r2   mypy.expandtyper   
mypy.nodesr   r	   r
   r   r   r   r   r   r   r   r   r   r   r   mypy.semanal_sharedr   
mypy.stater   
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/   r0   	mypy.utilr1   r7   floatintboolr3   __annotations__r   r4   objectr5   rP   rz   rv   r_   rr   rn   r   r   r   r   rA   rH   rE   <module>r=     s  2 2 2h # " " " " " $ $ $ $ $ $       5 5 5 5 5 5 ' ' ' ' ' '                                 " > = = = = =                                                           6 !           c5#t34	 4 4 4 4 y.'@!A3!FG G G G G #63;/ / / / /( ( ( (V= = = =@[! [! [! [!|- - - -
   8 8 8 8! ! ! !   cO cO cO cO cO+l3 cO cO cOL     rH   