
    X-Ph,                    "   U d Z ddlmZ ddlmZ ddlmZ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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* e+e,ee-f         e!f         Z.d
e/d<   ddZ0 G d de&          Z1dS )aH  Strip/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_INFOAssignmentStmtBlockCallExprClassDef	DecoratorForStmtFuncDef	ImportAll
ImportFrom	IndexExprListExpr
MemberExprMypyFileNameExprNodeOpExprOverloadedFuncDefRefExprStarExpr	SuperExprSymbolTableNode	TupleExprTypeInfoVar)TraverserVisitor)CallableType)
type_state
_TypeAliasSavedAttributesnode&MypyFile | FuncDef | OverloadedFuncDefsaved_attrsreturnNonec                    t          |          }t          | t                    r|                    |            dS |                     |           dS )a  Reset 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
isinstancer   strip_file_top_levelaccept)r&   r(   visitors      T/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/mypy/server/aststrip.pystrip_targetr2   J   sU     {++G$!! $$T*****G    c                      e Zd Zd4dZd5dZd6 fdZd7 fdZd7dZd8 fdZd9dZ	d: fdZ
d; fdZd<dZd=dZd> fdZd?dZd@ fd!ZdA fd#ZdB fd%ZdCd'ZdD fd)ZdE fd+ZdFd.ZedGd2            ZedGd3            Z xZS )Hr,   saved_class_attrsr%   r)   r*   c                >    d | _         d| _        d| _        || _        d S )NFT)typeis_class_bodyrecurse_into_functionsr5   )selfr5   s     r1   __init__zNodeStripVisitor.__init__`   s+    %)	" '+# "3r3   	file_noder   c                    d| _         |j                                         |                    |            |j                                        D ]}d|vr|j        |= dS )z:Strip a module top-level (don't recursive into functions).F@N)r9   plugin_depsclearr/   namescopy)r:   r<   names      r1   r.   z%NodeStripVisitor.strip_file_top_levelm   sp    &+###%%%O((** 	* 	*D $OD)		* 	*r3   br
   c                \    |j         rd S t                                          |           d S N)is_unreachablesupervisit_block)r:   rD   	__class__s     r1   rI   zNodeStripVisitor.visit_blockx   s1     	FAr3   r&   r   c                   | j         s|                     |           d |j        j                                        D             g |_        |j                            |j                   g |_        fd|j	        j
        D             |j	        _
        |                     |j                  5  t                                          |           ddd           n# 1 swxY w Y   |j	        j
                            |j                   g |_        t          j        |j                   t"          |_        d|_        dS )z8Strip class body and type info, but don't strip methods.c                *    h | ]}|j         	|j        S  )plugin_generatedr&   ).0vs     r1   	<setcomp>z3NodeStripVisitor.visit_class_def.<locals>.<setcomp>   s"    TTTASTQVTTTr3   c                    g | ]}|v|	S rM   rM   )rO   s	to_deletes     r1   
<listcomp>z4NodeStripVisitor.visit_class_def.<locals>.<listcomp>   s*     
 
 
)););A);););r3   N)r9   save_implicit_attributesinforA   values	type_varsbase_type_exprsextendremoved_base_type_exprsdefsbodyenter_classrH   visit_class_defremoved_statementsr#   reset_subtype_caches_forr   analyzed)r:   r&   rT   rJ   s     @r1   r`   z NodeStripVisitor.visit_class_def}   sq    * 	0))$/// UTTY_%;%;%=%=TTT	##D$@AAA')$
 
 
 
y~
 
 
	 di(( 	* 	*GG##D)))	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	*	d5666"$+DI666$	s   0"CC"%C"c                    |j         j                                        D ]2\  }}t          |j        t
                    r|j        r|| j        ||f<   3dS )z9Produce callbacks that re-add attributes defined on self.N)rW   rA   itemsr-   r&   r    implicitr5   )r:   r&   rC   syms       r1   rV   z)NodeStripVisitor.save_implicit_attributes   s^    ..00 	9 	9ID##(C(( 9S\ 958&tTz2	9 	9r3   r   c                p   | j         sd S g |_        |j        |_        |j        r(t	          |j        t
                    sJ g |j        _        |j        r|                     |j                  nt                      5  t                                          |           d d d            d S # 1 swxY w Y   d S rF   )r9   expandedunanalyzed_typer7   r-   r"   	variablesrW   enter_methodr   rH   visit_func_defr:   r&   rJ   s     r1   rm   zNodeStripVisitor.visit_func_def   s    * 	F(	9 	%
 di66666"$DI-1YITty)))KMM 	) 	)GG""4(((	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)s   <"B++B/2B/r   c                    d |j         _        |j        D ]}|                    |            | j        r|j                            |            d S d|j         _        d|j        _        d S NF)varr7   
decoratorsr/   r9   funcis_final)r:   r&   exprs      r1   visit_decoratorz NodeStripVisitor.visit_decorator   st    O 	 	DKK& 	'IT""""" !&DH!&DIr3   r   c                    | j         sd S |j                                        |_        d |_        d|_        t                                          |           d S rp   )r9   unanalyzed_itemsrB   re   implrt   rH   visit_overloaded_func_defrn   s     r1   rz   z*NodeStripVisitor.visit_overloaded_func_def   sU    * 	F*//11
	))$/////r3   r	   c                    |j         |_        d|_        d|_        | j        r&| j        s|j        D ]}|                     |           t                                          |           d S rp   )	rj   r7   is_final_defis_alias_defr8   lvaluesprocess_lvalue_in_methodrH   visit_assignment_stmt)r:   r&   lvaluerJ   s      r1   r   z&NodeStripVisitor.visit_assignment_stmt   sw    (	!!9 	6T/ 	6, 6 6--f5555%%d+++++r3   r   c                    g |_         d S rF   assignmentsr:   r&   s     r1   visit_import_fromz"NodeStripVisitor.visit_import_from       r3   r   c                    g |_         d S rF   r   r   s     r1   visit_import_allz!NodeStripVisitor.visit_import_all   r   r3   r   c                ~    |j         |_        d |_        d |_        t	                                          |           d S rF   )unanalyzed_index_type
index_typeinferred_item_typeinferred_iterator_typerH   visit_for_stmtrn   s     r1   r   zNodeStripVisitor.visit_for_stmt   s;    4"&&*#t$$$$$r3   r   c                0    |                      |           d S rF   )strip_ref_exprr   s     r1   visit_name_exprz NodeStripVisitor.visit_name_expr   s    D!!!!!r3   r   c                t    |                      |           t                                          |           d S rF   )r   rH   visit_member_exprrn   s     r1   r   z"NodeStripVisitor.visit_member_expr   s5    D!!!!!$'''''r3   r   c                X    d |_         t                                          |           d S rF   )rc   rH   visit_index_exprrn   s     r1   r   z!NodeStripVisitor.visit_index_expr   s(      &&&&&r3   r   c                X    d |_         t                                          |           d S rF   )rc   rH   visit_op_exprrn   s     r1   r   zNodeStripVisitor.visit_op_expr   s(    d#####r3   r   c                L    d |_         d |_        d|_        d|_        d|_        d S )N F)kindr&   fullname
is_new_defis_inferred_defr   s     r1   r   zNodeStripVisitor.strip_ref_expr   s,    		$r3   r   c                X    d |_         t                                          |           d S rF   )rc   rH   visit_call_exprrn   s     r1   r   z NodeStripVisitor.visit_call_expr   s(    %%%%%r3   r   c                X    d |_         t                                          |           d S rF   )rW   rH   visit_super_exprrn   s     r1   r   z!NodeStripVisitor.visit_super_expr   s(    	  &&&&&r3   r   r   c                   t          |t                    r_|j        rT| j        J |j        | j        j        v r| j        j        |j        = | j        j        |j        f}|| j        v r| j        |= d S d S d S t          |t          t          f          r!|j
        D ]}|                     |           d S t          |t                    r|                     |j                   d S d S rF   )r-   r   r   r7   rC   rA   defnr5   r   r   re   r   r   ru   )r:   r   keyitems       r1   r   z)NodeStripVisitor.process_lvalue_in_method   s   fj)) 	7  	4 y,,,;$)/11	4y~v{3$000.s333	4 	4 10H 566 	7 4 4--d33334 4)) 	7))&+66666	7 	7r3   rW   r   Iterator[None]c              #  f   K   | j         }| j        }|| _         d| _        d V  || _         || _        d S )NTr7   r8   r:   rW   old_typeold_is_class_bodys       r1   r_   zNodeStripVisitor.enter_class  sC      9 .	!	.r3   c              #  f   K   | j         }| j        }|| _         d| _        d V  || _         || _        d S rp   r   r   s       r1   rl   zNodeStripVisitor.enter_method  sC      9 .	"	.r3   )r5   r%   r)   r*   )r<   r   r)   r*   )rD   r
   r)   r*   )r&   r   r)   r*   )r&   r   r)   r*   )r&   r   r)   r*   )r&   r   r)   r*   )r&   r	   r)   r*   )r&   r   r)   r*   )r&   r   r)   r*   )r&   r   r)   r*   )r&   r   r)   r*   )r&   r   r)   r*   )r&   r   r)   r*   )r&   r   r)   r*   )r&   r   r)   r*   )r&   r   r)   r*   )r&   r   r)   r*   )r   r   r)   r*   )rW   r   r)   r   )__name__
__module____qualname__r;   r.   rI   r`   rV   rm   rv   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r_   rl   __classcell__)rJ   s   @r1   r,   r,   _   s]       3 3 3 3	* 	* 	* 	*     
     49 9 9 9) ) ) ) ) )
' 
' 
' 
'0 0 0 0 0 0, , , , , ,      % % % % % %" " " "( ( ( ( ( (' ' ' ' ' '$ $ $ $ $ $% % % %& & & & & &' ' ' ' ' '7 7 7 7$ / / / ^/ / / / ^/ / / / /r3   r,   N)r&   r'   r(   r%   r)   r*   )2__doc__
__future__r   collections.abcr   
contextlibr   r   typing_extensionsr   r$   
mypy.nodesr   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    mypy.traverserr!   
mypy.typesr"   mypy.typestater#   dicttuplestrr%   __annotations__r2   r,   rM   r3   r1   <module>r      s"    B # " " " " " $ $ $ $ $ $ 2 2 2 2 2 2 2 2 5 5 5 5 5 5                                                     6 , + + + + + # # # # # # % % % % % %"53#7#HI I I I I   *z/ z/ z/ z/ z/' z/ z/ z/ z/ z/r3   