
    X-PhX                       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
mZmZmZmZ ddlmZ ddlZddl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 ddl m!Z! ddl"m#Z# ddl$m%Z& ddl'm(Z(m)Z)m*Z* ddl+m,Z,m-Z-m.Z.m/Z/ ddl0m1Z1 ddl2m3Z3 ddl4m5Z5 ddl6m7Z7 ddl8m9Z9 e
rddlm:Z:m;Z; e<e=e>eg df         f                  Z?de@d<   dZAde@d<   dZBde@d<   g dZCde@d<   dZd(ZDd[d+ZEd\d0ZFd]d1ZGd^d3ZHd_d7ZId^d8ZJd`dBZKe=eLeeeeef         ee         f         ZMde@dC<   e=eLeLeeeeef         ee         f         ZNde@dD<   dadHZOdbdMZPdZdNZQdcdOZRdZdPZSdddWZTdZdXZUdedYZVdS )fa  Top-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_CHECKINGCallableFinalOptionalUnion)	TypeAliasN)FineGrainedDeferredNode)Errors)	DecoratorFuncDefMypyFileOverloadedFuncDefTypeInfoVar)Options)ClassDefContext)dataclasses)SemanticAnalyzerapply_semantic_analyzer_patches#remove_imported_names_from_symtable)add_type_promotioncalculate_class_abstract_statuscalculate_class_varscheck_protocol_status)#infer_decorator_signature_if_simple)find_dataclass_transform_spec)TypeArgumentAnalyzer)SavedAttributes)is_typeshed_file)GraphState
_TypeAliasPatches   r	   MAX_ITERATIONS   CORE_WARMUP)typing_collections_abcbuiltinsabccollectionszcollections.abccore_modulesgraphr$   scc	list[str]errorsr   returnNonec                (   g }t          | ||           t          | ||           t          |           t          | ||           t	          | ||           t          | ||           t          | |           d|v rt          | d                    dS dS )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_levelsprocess_functionsr   apply_class_plugin_hookscheck_type_argumentscalculate_class_propertiescheck_blockerscleanup_builtin_scc)r2   r3   r5   patchess       Q/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/mypy/semanal_main.pysemantic_analysis_for_sccrB   S   s     G uc7+++eS'*** $G,,,UC000V,,,uc62225#SE*-.....     stater%   c                L    | j         J t          | j         j        d           dS )a8  Remove 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.   )treer   names)rD   s    rA   r?   r?   o   s-     :!!!'
(8*EEEEErC   nodeslist[FineGrainedDeferredNode]saved_attrsr"   c           
        g }t          d |D                       rt          || j        g|           t          |           | j        j        }|D ]K}t          |j        t                    rt          || | j        |j        j
        |j        |j        |           Lt          |           t          || j        g| j        j                   t          || | j        j                   t!          || j        g| j        j                   dS )a  Semantically 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.
    c              3  J   K   | ]}t          |j        t                    V  d S N)
isinstancenoder   ).0ns     rA   	<genexpr>z0semantic_analysis_for_targets.<locals>.<genexpr>   s.      
7
7A:afh''
7
7
7
7
7
7rC   N)anyr9   idrestore_saved_attrsmanagersemantic_analyzerrN   rO   r   process_top_level_functionfullnameactive_typeinfor   r;   r5   check_type_arguments_in_targetsr=   )rD   rH   r2   rJ   r@   analyzerrQ   s          rA   semantic_analysis_for_targetsr]   {   s    G

7
7
7
7
777 7558*g666$$$}.H 
 
afh'' 	"eUXqv@QSZ	
 	
 	
 	
 $G,,,UUXJ0DEEE#E5%-2FGGGuuxj%-2FGGGGGrC   c                B   |                                  D ]\  \  }}}|j        }|                    |          }||j        v }t	          |j        t                    sJ |4t	          |j        t                    r|j        j        s|j        j        r|s
||j        |<   dS )zPRestore instance variables removed during AST strip that haven't been added yet.N)	itemsinfogetrG   rN   rO   r   is_abstract_varexplicit_self_type)rJ   cdefnamesymr`   existingdefined_in_this_classs          rA   rU   rU      s    (..00 # #tcy88D>> $
 2#(C(((((
  s++  190M  H'  *   #DJt'# #rC   r@   c           	     f   t          t          |                    }|D ]7}| |         }|j        J |j        j                            |j                   8|j        j                            |           |                                t          fdt          D                       r,t          t          t                              t          z  z  d}d}|j        j        }|j                                         r.|dz  }|t          k    rW|j        J |                    |j        |j                  5  |                                 d d d            n# 1 swxY w Y   d S |r|j        j                                         g }d}	ro                                }
| |
         }|j        J t'          |
|
||j        d ||          \  }}}||z  }|	p|}	|s|j        j                            |
           o|r|r
J d            t          t          |                    |	 },d S d S )Nc              3      K   | ]}|v V  	d S rM    )rP   mworklists     rA   rR   z%process_top_levels.<locals>.<genexpr>   s'      
/
/Q1=
/
/
/
/
/
/rC   Fr      %Must not defer during final iteration)listreversedrF   rV   rW   prepare_fileincomplete_namespacesupdatecopyallr1   r+   deferral_debug_contextclearr)   file_contextoptionsreport_hangpopsemantic_analyze_targetdiscard)r2   r3   r@   rT   rD   final_iteration	iterationr\   all_deferredany_progressnext_iddeferred
incompleteprogressrm   s                 @rA   r9   r9      s   
 x}}

C  A Ab	z%%%'44UZ@@@@ 
M'..s333xxzzH 
/
/
/
/,
/
/
/// ?D,//00;>>OI}.H#))+++
 +Q	~%%:)))&&uz5=AA ' '$$&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' 'E 	8M/55777"$ 
	EllnnG'NE:)))-D%T?G. .*Hj( H$L'38L E3;;GDDD  
	E  	M#LL%LLLL ..//**;  + + + + +s   <EE!$E!targetslist[FullTargetInfo]Iterator[FullTargetInfo]c              #    K   d t          | d           D             }d |D             d}|r|t          |          k    rt          d          ||         \  }}||E d{V  |                    |           Ot	          fd|j        d	d         D                       r|d	z  }}|E d{V  |                    |                               |           d}|dS dS )
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                6    g | ]\  }}|t          |          fS rk   )rp   )rP   kgs      rA   
<listcomp>z(order_by_subclassing.<locals>.<listcomp>   s&    MMM1477|MMMrC   c                    | d         S )N   rk   xs    rA   <lambda>z&order_by_subclassing.<locals>.<lambda>   s
    qt rC   keyc                    h | ]	\  }}||
S rM   rk   )rP   r`   _s      rA   	<setcomp>z'order_by_subclassing.<locals>.<setcomp>   s!    GGGad6Ft6F6F6FrC   r   z"Cannot order method targets by MRONc              3      K   | ]}|v V  	d S rM   rk   )rP   parentremaining_infoss     rA   rR   z'order_by_subclassing.<locals>.<genexpr>  s(      IIVv(IIIIIIrC   rn   )r   len
ValueErrorr|   rS   mror~   )r   grouped
next_group	next_infogroupr   s        @rA   order_by_subclassingr      sO      NM^^(L(L(LMMMGGG7GGGOJ
 W%% ABBB":.	5KK
###IIIIy}QRR7HIIIII 	!OJJ	*** 
)      rC   c           
       
 g }|D ]S
| 
         j         }|J t          t          |          d           }|                    
fd|D                        Tt	          |          D ]X\  
}}}| 
         j        j        }	t          |t          t          t          f          sJ t          |	| 
         
||||           Yd S )Nc                ,    | d         j         | d         fS )Nrn   r   )liner   s    rA   r   z#process_functions.<locals>.<lambda>  s    AaDIqQRtCT rC   r   c                $    g | ]\  }}}|||fS rk   rk   )rP   targetrO   active_typemodules       rA   r   z%process_functions.<locals>.<listcomp>   s)    ZZZ5NVT;ffdK0ZZZrC   )rF   sortedget_all_leaf_targetsextendr   rV   rW   rN   r   r   r   rX   )r2   r3   r@   all_targetsrF   r   r   rO   r   r\   r   s             @rA   r:   r:     s	   K 
 
V}! -d339T9TUUUZZZZRYZZZ	
 	
 	
 	
 .B+-N-N 
 
)k=(:$*;Y GHHHHH"eFmVVT;	
 	
 	
 	

 
rC   r\   r   r   strr   rO   'FuncDef | OverloadedFuncDef | Decoratorr   TypeInfo | Nonec           	        d}d}|g}	| j                                          | j                            |           d}
|	r|
dz  }
|
t          k    rV|j        J |                     |j        |j                  5  |                                  ddd           n# 1 swxY w Y   nn|	s|r|r| j        	                    |           t          |||||||          \  }	}}|s|j        j        	                    |           |r|	r
J d            |sd}|	| j        	                    |           | j                                         dS )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).
    FTr   rn   Nro   )rw   rx   rs   addr)   rF   ry   rz   r{   r~   r}   rV   saved_locals)r\   rD   r   r   rO   r   r@   r   r   r   r   r   s               rA   rX   rX   +  s    OJ xH#))+++"&&v...I
 #Q	&&:)))&&uz5=AA ' '$$&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' 	;J 	;? 	;*226:::)@FE4ow*
 *
&*h  	@M/77??? 	IHH!HHHH 	#"O'  #* "**6222 !!!!!s   8BB B
TargetInfoFullTargetInfofiler   list[TargetInfo]c                    g }|                                  D ]J\  }}}t          |j        t          t          t
          f          r|                    ||j        |f           K|S )zEReturn all leaf targets in a symbol table (module-level and methods).)local_definitionsrN   rO   r   r   r   append)r   resultrY   rO   r   s        rA   r   r   g  sf    !F'+'='='?'? > >#$di'+<i!HII 	>MM8TY<===MrC   2MypyFile | FuncDef | OverloadedFuncDef | Decoratorr   booltuple[list[str], bool, bool]c           	     @   |j         j                            || f           |j        }|J |j         j        }t                      g|_        t                      g|_        |j        |_	        t                      |_
        d|_        |                    d          5  |}	t          |	t                    r|	j        }	|                    |	||||j        |           t          |t                    rt%          ||           ddd           n# 1 swxY w Y   |j
        D ]Q}
|                    |
           t(          j        j        }||j                            |
|          k    r
||j        |
<   Rd|_        |`|j        r| g|j        |j        fS g |j        |j        fS )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)
    NFr>   )	file_noderz   r   )rV   processed_targetsr   rF   rW   setglobal_declsnonlocal_declsrG   globalsimportsr   wrap_contextrN   r   funcrefresh_partialrz   r   add_dependencymypybuildPRI_LOW
prioritiesra   	statementcur_mod_noder   r   )r   r   rD   rO   r   r   r@   rF   r\   refresh_nodedepprioritys               rA   r}   r}   p  s     
M#**FF+;<<<:D}.H UUGH"uugHzHuuHH			5		1	1 @ @lI.. 	-',L  M# 	! 	
 	
 	
 dI&& 	@/h???@ @ @ @ @ @ @ @ @ @ @ @ @ @ @  - -S!!!:%u'++C::::$,ES! H :x,h.???8&(999s   A$DDDc                   |D ]}| |         }|j         sJ t          ||j        |j                             |j                  |j        j        j                  }|                                5  t          j	        j	        
                    |j        j                  5  |j                             |           d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   d S rM   )rF   r!   rz   r#   rV   rW   
named_typer   r   rD   strict_optional_setstrict_optionalaccept)r2   r3   r5   r   rD   r\   s         rA   r<   r<     s\    , ,fz'MJ''66M+6	
 
 !! 	, 	,!55em6STT , ,
!!(+++, , , , , , , , , , , , , , ,	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,, ,s6   -/CC7CCC
CCC	!C	c           	        t          ||j        t          |j        j        |j        pd          |j        j        j                  }|                                5  t          j
        j
                            |j        j                  5  | D ]}d}t          |j        t          t           f          r|j        }|j        |j        |f}|j        r|j                            |          nt+                      5  |du|_        |j                            |           ddd           n# 1 swxY w Y   	 ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w Y   dS )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!   rz   r#   abs_custom_typeshed_dirpathrV   rW   r   r   r   rD   r   r   rN   rO   r   r   rT   rZ   scopesaved_scoper   recurse_into_functionsr   )r   rD   r5   r\   r   r   saveds          rA   r[   r[     s    $>
@PbQQ'2	 H 
				 	1 	1Z11%-2OPP 	1 	1! 1 1;?fkG5F+GHH '!;D6#94@8>WV\--e444+-- 1 16:$6FH3K&&x0001 1 1 1 1 1 1 1 1 1 1 1 1 1 11	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1s[   /E	A-D?6$D&	D?&D**D?-D*.D?3E?E	EE	EEEc           
     6   d}d}|r|dk     s
J d            |dz  }d}|D ]t}| |         }|j         }|sJ |                                D ]J\  }}	}t          |	j        t                    r*t          |j        j        ||	j        |j        ||          sd}Ku|dS dS )a3  Apply 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.
    r   T
   z1Internal error: too many class plugin hook passesrn   FN)	rF   r   rN   rO   r   apply_hooks_to_classrV   rW   rz   )
r2   r3   r5   
num_passesr   r   rD   rF   r   rO   s
             rA   r;   r;     s     JJ  *B Sa

 	* 	*F&ME:DKKK"4466 
* 
*
4di22 	*/7	  * &*

*  * * * * *rC   selfr`   r   rz   r   r   c           	     <   |j         }d}|j        D ]}|                     |||          5  d }	|                     |          }
|
r| j                            |
          }	|	t          |          rt          j        }	|	r|o |	t          |||                     }d d d            n# 1 swxY w Y   t          |          }|Z|                     |||          5  |o(t          j
        ||||                                           }d d d            n# 1 swxY w Y   |S )NT)defn
decoratorsry   get_fullname_for_hookpluginget_class_decorator_hook_2r    dataclasses_plugindataclass_class_maker_callbackr   DataclassTransformer	transform)r   r   r`   rz   r   r5   r   ok	decoratorhookdecorator_namespecs               rA   r   r     s    9D	B_ I I	y'488 	I 	ID!77	BBN N{==nMM | =i H H|)H IHDDy$!G!GHH	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I" )..Dy'488 	d 	d c*?dDRVWWaaccB	d 	d 	d 	d 	d 	d 	d 	d 	d 	d 	d 	d 	d 	d 	d
 Is$   A/B%%B)	,B)	,DDDc           	     (   | d         j         }|sJ |D ]}| |         }|j         }|sJ |                                D ]\  }}}t          |j        t                    r|j        j                            ||j        |j                  5  t          |j        |j
        |           t          |j        |           t          |j                   t          |j        |j        | |         j        |j                   d d d            n# 1 swxY w Y   Ԍd S )Nr.   )rF   r   rN   rO   r   rV   rW   ry   rz   r   is_stubr   r   r   rG   )	r2   r3   r5   r.   r   rD   rF   r   rO   s	            rA   r=   r=     s]   Z %HOOO  fz0022 	 	JAtQ$)X.. ]4AA$W[W`aa  3DIt|VTTT)$)V<<<(333&	4:uV}/Dhn  	              		 s   A1DD	D	c                D    |D ]}| |                                           d S rM   r   )r2   r3   r   s      rA   r>   r>   /  s4     ' 'f$$&&&&' 'rC   )r2   r$   r3   r4   r5   r   r6   r7   )rD   r%   r6   r7   )
rD   r%   rH   rI   r2   r$   rJ   r"   r6   r7   )rJ   r"   r6   r7   )r2   r$   r3   r4   r@   r'   r6   r7   )r   r   r6   r   )r\   r   rD   r%   r   r   r   r   rO   r   r   r   r@   r'   r6   r7   )r   r   r6   r   )r   r   r   r   rD   r%   rO   r   r   r   r   r   r@   r'   r6   r   )r   rI   rD   r%   r5   r   r6   r7   )r   r   r   r   r`   r   rz   r   r   r   r5   r   r6   r   )r2   r$   r3   r4   r6   r7   )W__doc__
__future__r   collections.abcr   
contextlibr   	itertoolsr   r,   r   r   r	   r
   r   typing_extensionsr   r&   
mypy.buildr   
mypy.statemypy.checkerr   mypy.errorsr   
mypy.nodesr   r   r   r   r   r   mypy.optionsr   mypy.pluginr   mypy.pluginsr   r   mypy.semanalr   r   r   mypy.semanal_classpropr   r   r   r   mypy.semanal_inferr   mypy.semanal_sharedr    mypy.semanal_typeargsr!   mypy.server.aststripr"   	mypy.utilr#   r$   r%   rp   tupleintr'   __annotations__r)   r+   r1   rB   r?   r]   rU   r9   r   r:   rX   r   r   r   r   r}   r<   r[   r;   r   r=   r>   rk   rC   rA   <module>r     st    4 # " " " " " $ $ $ $ $ $ " " " " " "       B B B B B B B B B B B B B B 5 5 5 5 5 5         0 0 0 0 0 0       U U U U U U U U U U U U U U U U             ' ' ' ' ' ' : : : : : :         
            C B B B B B = = = = = = 6 6 6 6 6 6 0 0 0 0 0 0 & & & & & & ('''''''' 5hr4x&8!89: : : : :                / / / /8	F 	F 	F 	FH H H H@# # # #07+ 7+ 7+ 7+t! ! ! !H
 
 
 
4/" /" /" /"d x"3Y>	?(ASS
    
 #eHg'8)CDhxFXX    
   7: 7: 7: 7:t, , , ,1 1 1 14 *  *  *  *F$ $ $ $N   $' ' ' ' ' 'rC   