
    X-PhM`                       U 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 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mZmZmZmZmZ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/m0Z0m1Z1m2Z2m3Z3m4Z4 d dl5m6Z6 eeeef         Z7de8d<    G d de	          Z9 G d d          Z:eee;e<e-e
e-         f                  f         Z= G d d          Z>d dZ?d!dZ@dS )"    )annotations)defaultdict)Iterator)contextmanager)
NamedTupleOptionalUnion)	TypeAlias)!remove_instance_last_known_values)Keyextract_var_from_literal_hashliteralliteral_hashsubkeys)
Expression	IndexExpr
MemberExprNameExprRefExprTypeInfoVar)Options)is_same_type
is_subtype)make_simplified_union)AnyTypeInstanceNoneTypePartialType
ProperType	TupleTypeType	TypeOfAnyTypeTypeTypeVarType	UnionType
UnpackTypefind_unpack_in_listget_proper_type)fill_typevars_with_any
_TypeAliasBindableExpressionc                  $    e Zd ZU ded<   ded<   dS )CurrentTyper"   typeboolfrom_assignmentN)__name__
__module____qualname____annotations__     K/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/mypy/binder.pyr.   r.   $   s'         JJJr7   r.   c                  $    e Zd ZdZddd	ZddZdS )Framea  A Frame represents a specific point in the execution of a program.

    It carries information about the current types of expressions at
    that point, arising either from assignments to those expressions
    or the result of isinstance checks and other type narrowing
    operations. It also records whether it is possible to reach that
    point at all.

    We add a new frame wherenever there is a new scope or control flow
    branching.

    This information is not copied into a new Frame when it is pushed
    onto the stack, so a given Frame only has information about types
    that were assigned in that frame.

    Expressions are stored in dicts using 'literal hashes' as keys (type
    "Key"). These are hashable values derived from expression AST nodes
    (only those that can be narrowed). literal_hash(expr) is used to
    calculate the hashes. Note that this isn't directly related to literal
    types -- the concept predates literal types.
    Fidintconditional_framer0   returnNonec                L    || _         i | _        d| _        || _        d| _        d S )NF)r;   typesunreachabler=   suppress_unreachable_warnings)selfr;   r=   s      r8   __init__zFrame.__init__@   s.    -/
 !2-2***r7   strc           	     H    d| j          d| j         d| j         d| j         d	S )NzFrame(z, ))r;   rA   rB   r=   rD   s    r8   __repr__zFrame.__repr__G   s5    ^^^4:^^1A^^TE[^^^^r7   NF)r;   r<   r=   r0   r>   r?   )r>   rF   )r2   r3   r4   __doc__rE   rJ   r6   r7   r8   r:   r:   )   sQ         ,3 3 3 3 3_ _ _ _ _ _r7   r:   c                  .   e Zd ZU dZdZded<   dDd	ZdEdZdFdGdZdHdIdZ	dJdKdZ
dJdLdZdddMd#ZdNd$ZdNd%ZdOd'ZdPd(ZdPd)ZdQd*ZdRd+ZdSd.ZdTd1ZedUd3            ZdVd5ZdWd7ZdXd8ZdNd9ZdNd:Zed;d<d<ddd=dYdB            ZedZdC            ZdS )[ConditionalTypeBindera  Keep track of conditional types of variables.

    NB: Variables are tracked by literal hashes of expressions, so it is
    possible to confuse the binder when there is aliasing. Example:

        class A:
            a: int | str

        x = A()
        lst = [x]
        reveal_type(x.a)      # int | str
        x.a = 1
        reveal_type(x.a)      # int
        reveal_type(lst[0].a) # int | str
        lst[0].a = 'a'
        reveal_type(x.a)      # int
        reveal_type(lst[0].a) # str
    NzAssigns | Nonetype_assignmentsoptionsr   r>   r?   c                    d| _         t          |                                           g| _        g | _        i | _        i | _        d| _        t                      | _	        g | _
        g | _        |j        | _        d S )N   F)next_idr:   _get_idframesoptions_on_returndeclarationsdependencieslast_pop_changedset
try_framesbreak_framescontinue_framesallow_redefinition_newbind_all)rD   rP   s     r8   rE   zConditionalTypeBinder.__init__f   sz     T\\^^,,- 57 57 24 !& %(EE')*,
  6r7   r<   c                0    | xj         dz  c_         | j         S )NrR   )rS   rI   s    r8   rT   zConditionalTypeBinder._get_id   s    |r7   keyr   value
Key | Nonec                    ||}n:| j                             |t                                                    |           t	          |          D ]}|                     ||           d S N)rX   
setdefaultrZ   addr   _add_dependencies)rD   ra   rb   elts       r8   rh   z'ConditionalTypeBinder._add_dependencies   sq    =EE((cee4488???3<< 	/ 	/C""3....	/ 	/r7   Fr=   r0   r:   c                    t          |                                 |          }| j                            |           | j                            g            |S )z!Push a new frame into the binder.)r:   rT   rU   appendrV   )rD   r=   fs      r8   
push_framez ConditionalTypeBinder.push_frame   sL    $,,.."3441%%b)))r7   r/   r"   r1   indexc                L    t          ||          | j        |         j        |<   d S re   )r.   rU   rA   )rD   ra   r/   r1   ro   s        r8   _putzConditionalTypeBinder._put   s&    (3D/(J(JE %%%r7   CurrentType | Nonec                    |dk     r|t          | j                  z  }t          |dd          D ]0}|| j        |         j        v r| j        |         j        |         c S 1d S )Nr   rn   )lenrU   rangerA   )rD   ra   ro   is       r8   _getzConditionalTypeBinder._get   sr    199S%%%Eub"%% 	1 	1Adk!n***{1~+C0000 +tr7   Tr1   exprr   typc               >   t          |t          t          t          f          sdS t	          |          sdS t          |          }|
J d            || j        vr,t          |          | j        |<   |                     |           | 	                    |||           dS )zDirectly set the narrowed type of expression (if it supports it).

        This is used for isinstance() etc. Assignments should go through assign_type().
        Nz/Internal error: binder tried to put non-literal)

isinstancer   r   r   r   r   rW   get_declarationrh   rq   )rD   ry   rz   r1   ra   s        r8   putzConditionalTypeBinder.put   s    
 $J ABB 	Ft}} 	F4   Qd'''%4T%:%:Dc"""3'''		#sO,,,,,r7   c                *    d| j         d         _        d S NTrn   )rU   rB   rI   s    r8   rB   z!ConditionalTypeBinder.unreachable   s    &*B###r7   c                *    d| j         d         _        d S r   )rU   rC   rI   s    r8   rC   z3ConditionalTypeBinder.suppress_unreachable_warnings   s    8<B555r7   Type | Nonec                x    t          |          }|
J d            |                     |          }|d S |j        S )Nz/Internal error: binder tried to get non-literal)r   rw   r/   )rD   ry   ra   founds       r8   getzConditionalTypeBinder.get   s@    4   Q		#=4zr7   c                >    t          d | j        D                       S )Nc              3  $   K   | ]}|j         V  d S re   rB   .0rl   s     r8   	<genexpr>z7ConditionalTypeBinder.is_unreachable.<locals>.<genexpr>   s$      66Q1=666666r7   anyrU   rI   s    r8   is_unreachablez$ConditionalTypeBinder.is_unreachable   s#     66$+666666r7   c                >    t          d | j        D                       S )Nc              3  $   K   | ]}|j         V  d S re   )rC   r   s     r8   r   zJConditionalTypeBinder.is_unreachable_warning_suppressed.<locals>.<genexpr>   s%      HHq12HHHHHHr7   r   rI   s    r8   !is_unreachable_warning_suppressedz7ConditionalTypeBinder.is_unreachable_warning_suppressed   s!    HHDKHHHHHHr7   c                f    t          |          }|
J d            |                     |           dS )z0Remove all references to a Node from the binder.Nz0Internal error: binder tried cleanse non-literal)r   _cleanse_key)rD   ry   ra   s      r8   cleansezConditionalTypeBinder.cleanse   s9    4   R#r7   c                <    | j         D ]}||j        v r|j        |= dS )z/Remove all references to a key from the binder.N)rU   rA   )rD   ra   frames      r8   r   z"ConditionalTypeBinder._cleanse_key   s5    [ 	% 	%Eek!!K$	% 	%r7   rU   list[Frame]c                    t          d |D                       }d |D             }d}d |D             }|D ]@|                               fd|D             }| j         pt                    du }|rt	          d |D                       r\d |D             }|rt          d	 |D                       r|d
         }|J |j        t          | j                                                }t          |t                    r?t          fd|dd         D                       st          t          j        |          ng }	|D ] }
|
J |	                    |
j                   !t          |	          dk    r	|	d
         nt          |	          t          |t                     rt#          |          s|t          t$                    rt'                    |r`t          t(                    rKt          t$                    r6t%          j        d j        D                       }|| j                 k    r|t/          j                  s|                     d           d}B| | j        d         _        |S )zUpdate the frame to reflect that each key will be updated
        as in one of the frames.  Return whether any item changes.

        If a key is declared as AnyType, only update it if all the
        options are the same.
        c              3  &   K   | ]}|j          V  d S re   r   r   s     r8   r   z<ConditionalTypeBinder.update_from_options.<locals>.<genexpr>   s'      >>!->>>>>>r7   c                     g | ]}|j         	|S r6   r   r   s     r8   
<listcomp>z=ConditionalTypeBinder.update_from_options.<locals>.<listcomp>   s    9991=9!999r7   Fc                &    h | ]}|j         D ]}|S r6   )rA   )r   rl   ra   s      r8   	<setcomp>z<ConditionalTypeBinder.update_from_options.<locals>.<setcomp>   s'    777qw777777r7   c                F    g | ]}|j                                       S r6   )rA   r   )r   rl   current_valuera   s     r8   r   z=ConditionalTypeBinder.update_from_options.<locals>.<listcomp>   s)    PPPAC ? ?PPPr7   Nc              3     K   | ]}|d u V  	d S re   r6   r   xs     r8   r   z<ConditionalTypeBinder.update_from_options.<locals>.<genexpr>   s&      $I$I1Q$Y$I$I$I$I$I$Ir7   c                    g | ]}||S re   r6   r   s     r8   r   z=ConditionalTypeBinder.update_from_options.<locals>.<listcomp>   s    MMMaq}}}}r7   c              3  .   K   | ]}|d uo|j          V  d S re   rx   r   s     r8   r   z<ConditionalTypeBinder.update_from_options.<locals>.<genexpr>   sB       % %<=7a&7"7% % % % % %r7   r   c              3  J   K   | ]}|d uot          |j                  V  d S re   )r   r/   )r   tr/   s     r8   r   z<ConditionalTypeBinder.update_from_options.<locals>.<genexpr>
  sI        EFATM@l4&@&@     r7   rR   )
source_anyc                V    g | ]&}t          t          |          t                    $|'S r6   r|   r)   r   )r   r   s     r8   r   z=ConditionalTypeBinder.update_from_options.<locals>.<listcomp>0  s1    bbb1*_UVEWEWY`:a:abQbbbr7   Trx   rn   )allrw   r_   r   r   r/   r)   rW   r   r|   r   r#   from_another_anyrk   rt   r   r%   r   r&   collapse_variadic_unionr    
make_unionitemsr   rq   rU   rB   )rD   rU   all_reachablechangedkeysresulting_valuesold_semanticscurrent_typedeclaration_typepossible_typesr   
simplifiedr   ra   r/   s               @@@r8   update_from_optionsz)ConditionalTypeBinder.update_from_options   sF    >>v>>>>>99V99977V777 M	 M	C IIcNNMPPPPPPPP !%-[1Ns1S1SW[1[M $I$I8H$I$I$I!I!I  MM+;MMM  % %AQ% % % " "  +A.L+++$D.t/@/D/DS/I/IJJ*G44 +.    JZ[\[][]J^     \ #9#=JZ[[[D!#) 2 2A==="))!&1111~&&!++ *!,DD0@@D!"2K@@ 0.J J 0  0 "$	22 =6t<<%.&tZ88. 'tY77. &/%9bb
bbb& &
 &):3)???#-D$L}?Q,R,R$		#tT	:::*0jB#r7   can_skipfall_throughc                   |dk    r|                      |            | j                                        }| j                                        }|r!|                    d| j        d                    |                     |          | _        |S )zcPop a frame and return it.

        See frame_context() for documentation of fall_through.
        r   rn   )
allow_jumprU   poprV   insertr   rY   )rD   r   r   resultrP   s        r8   	pop_framezConditionalTypeBinder.pop_frame<  s     !OO\M***""(,,.. 	/NN1dk"o... $ 8 8 A Ar7   Iterator[Assigns]c              #  |   K   d}| j         | j         }t          t                    | _         | j         V  || _         dS )a  Push a new map to collect assigned types in multiassign from union.

        If this map is not None, actual binding is deferred until all items in
        the union are processed (a union of collected items is later bound
        manually by the caller).
        N)rO   r   list)rD   old_assignmentss     r8   accumulate_type_assignmentsz1ConditionalTypeBinder.accumulate_type_assignmentsO  sK        ,"3O +D 1 1#### /r7   declared_typec                F   t                    | j        $| j        |                             |f           dS t          |t          t
          t          f          sdS t          |          sdS |                     |           |dS t          |          sdS t          |          }t                    }t          |t                    rt          |t                    r>t          |j        t                    r$|j        j        r|                     |           n_t          |t"                    rUt%          d |j        D                       r7fd|j        D             }|                     |t#          |                     nt          |t"                    r5t%          d |j        D                       r|                     |           n|                     ||           nt          |t                    rit          |t                    r=t          |j        t                    r#|j        j        r|                     |           n-|                     ||           n|                     |           | j        D ]}|                     |           dS )a  Narrow type of expression through an assignment.

        Do nothing if the expression doesn't support narrowing.

        When not narrowing though an assignment (isinstance() etc.), use put()
        directly. This omits some special-casing logic for assignments.
        Nc              3  Z   K   | ]&}t          t          |          t                    V  'd S re   r|   r)   r   r   items     r8   r   z4ConditionalTypeBinder.assign_type.<locals>.<genexpr>  sE       ; ;@D
?400(;;; ; ; ; ; ;r7   c                \    g | ](}t          t          |          t                    rn|)S r6   r   )r   r   r/   s     r8   r   z5ConditionalTypeBinder.assign_type.<locals>.<listcomp>  sD        't'<'<hGGQDDT  r7   c              3  Z   K   | ]&}t          t          |          t                    V  'd S re   r   r   s     r8   r   z4ConditionalTypeBinder.assign_type.<locals>.<genexpr>  sE       ; ;?C
?400'::; ; ; ; ; ;r7   )r   rO   rk   r|   r   r   r   r   invalidate_dependenciesr   r)   r   r   noder   is_inferredr~   r&   r   r   r[   r   )rD   ry   r/   r   
p_declaredp_type	new_itemsrv   s     `     r8   assign_typez!ConditionalTypeBinder.assign_type^  s    166 , !$'..m/DEEEF$J ABB 	Ft}} 	F$$T***  F$.. 	 F$]33
 &&fg&& "	! $(( .Z	3-G-G .DILa . t$$$$J	22 .s ; ;HRHX; ; ; 8 8 .
    * 0  	 y334444J	22 .s ; ;GQGW; ; ; 8 8 .
 t$$$$ }----
G,, 	! $(( .Z	3-G-G .DILa .t$$$$}----HHT4    	 	A OOA	 	r7   r,   c                    t          |          }|J | j                            |t                                D ]}|                     |           dS )a  Invalidate knowledge of types that include expr, but not expr itself.

        For example, when expr is foo.bar, invalidate foo.bar.baz.

        It is overly conservative: it invalidates globally, including
        in code paths unreachable from here.
        N)r   rX   r   rZ   r   )rD   ry   ra   deps       r8   r   z-ConditionalTypeBinder.invalidate_dependencies  sa     4  $((cee44 	# 	#Cc""""	# 	#r7   c                F   |dk     r|t          | j                  z  }t          |                                           }| j        |dz   d          D ]/}|j                            |j                   |j        rd|_        0| j        |                             |           d S )Nr   rR   T)	rt   rV   r:   rT   rU   rA   updaterB   rk   )rD   ro   r   rl   s       r8   r   z ConditionalTypeBinder.allow_jump  s     199S/000Edllnn%%UQY[[) 	) 	)AKqw'''} )$(!u%,,U33333r7   c                n    |                      | j        d                    |                                  d S Nrn   )r   r\   rB   rI   s    r8   handle_breakz"ConditionalTypeBinder.handle_break  s4    )"-...r7   c                n    |                      | j        d                    |                                  d S r   )r   r]   rB   rI   s    r8   handle_continuez%ConditionalTypeBinder.handle_continue  s4    ,R0111r7   rR   r   )r   break_framecontinue_framer=   	try_framer   r   r   Iterator[Frame]c             #    K   t          | j                  dk    sJ |r/| j                            t          | j                  |z
             |r/| j                            t          | j                  |z
             |r/| j                            t          | j                  dz
             |                     |          }|r|                     d           |V  | 	                    ||           |r| j        
                                 |r| j        
                                 |r1| j                            t          | j                  dz
             dS dS )a4  Return a context manager that pushes/pops frames on enter/exit.

        If can_skip is True, control flow is allowed to bypass the
        newly-created frame.

        If fall_through > 0, then it will allow control flow that
        falls off the end of the frame to escape to its ancestor
        `fall_through` levels higher. Otherwise control flow ends
        at the end of the frame.

        If break_frame > 0, then 'break' statements within this frame
        will jump out to the frame break_frame levels higher than the
        frame created by this call to frame_context. Similarly for
        continue_frame and 'continue' statements.

        If try_frame is true, then execution is allowed to jump at any
        point within the newly created frame (or its descendants) to
        its parent (i.e., to the frame that was on top before this
        call to frame_context).

        After the context manager exits, self.last_pop_changed indicates
        whether any types changed in the newly-topmost frame as a result
        of popping this frame.
        rR   rn   N)rt   rU   r\   rk   r]   r[   rg   rm   r   r   r   remove)rD   r   r   r   r   r=   r   	new_frames           r8   frame_contextz#ConditionalTypeBinder.frame_context  so     F 4;!#### 	E$$S%5%5%CDDD 	K ''DK(8(8>(IJJJ 	6ODK 0 01 4555OO$566	 	 OOBx... 	$!!### 	' $$&&& 	9O""3t{#3#3a#788888	9 	9r7   c              #     K   t          | j                  dk    sJ |                                 V  |                     dd           t          | j                  dk    sJ dS )zqA variant of frame_context for use at the top level of
        a namespace (module, function, or class).
        rR   Tr   N)rt   rU   rm   r   rI   s    r8   top_frame_contextz'ConditionalTypeBinder.top_frame_context  sn      
 4;1$$$$ootQ4;1$$$$$$r7   )rP   r   r>   r?   )r>   r<   re   )ra   r   rb   rc   r>   r?   rK   )r=   r0   r>   r:   )rn   )
ra   r   r/   r"   r1   r0   ro   r<   r>   r?   )ra   r   ro   r<   r>   rr   )ry   r   rz   r"   r1   r0   r>   r?   )r>   r?   )ry   r   r>   r   )r>   r0   )ry   r   r>   r?   )ra   r   r>   r?   )rU   r   r>   r0   )r   r0   r   r<   r>   r:   )r>   r   )ry   r   r/   r"   r   r   r>   r?   )ry   r,   r>   r?   )ro   r<   r>   r?   )r   r0   r   r<   r   r<   r   r<   r=   r0   r   r0   r>   r   )r>   r   )r2   r3   r4   rL   rO   r5   rE   rT   rh   rm   rq   rw   r~   rB   rC   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r6   r7   r8   rN   rN   N   s         * (,++++%7 %7 %7 %7N   / / / / /    K K K K K     KO - - - - - - + + + += = = =   7 7 7 7
I I I I   % % % %] ] ] ]~   & 0 0 0 ^0M M M M^# # # #
4 
4 
4 
4       
 "'79 79 79 79 79 ^79r % % % ^% % %r7   rN   ry   r>   r   c                <   t          | t                    rt          | j        t                    r1| j        j        }t          t          |          t                    s|S n;t          | j        t                    r!t          t          | j                            S dS )zGet the declared or inferred type of a RefExpr expression.

    Return None if there is no type or the expression is not a RefExpr.
    This can return None if the type hasn't been inferred yet.
    N)
r|   r   r   r   r/   r)   r   r   r$   r*   )ry   r/   s     r8   r}   r}     s     $   ?di%% 	?9>Dod33[AA 	8,, 	?249==>>>4r7   rz   r&   r"   c                   g }g }| j         D ]Q}t          |          }t          |t                    r|                    |           <|                    |           Rt          |          dk    r| S t          |d           }|d         }|d         }t          |j                   }|| S |j         |         }t          |t                    sJ t          |j	                  }	t          |	t                    s| S |	j	        j        dk    sJ |j         |dz   d         }
t          |j                   t          |
          k     r| S |
r#|j         t          |
           d         |
k    r| S |
r|j         dt          |
                    }n|j         }|	j        d         }t          |dd                   D ]!\  }}|j         ||g|dz   z  z   |
z   k    r| c S "|j         ||gt          | j                   dz
  z  z   |gz   |
z   k    r| S t          |j                   dk    r|	                                }nt          ||gz   |
z   |j                  }t!          j        |g|z             S )	a  Simplify a union involving variadic tuple if possible.

    This will collapse a type like e.g.
        tuple[X, Z] | tuple[X, Y, Z] | tuple[X, Y, Y, *tuple[Y, ...], Z]
    back to
        tuple[X, *tuple[Y, ...], Z]
    which is equivalent, but much simpler form of the same type.
    rR   c                *    t          | j                  S re   )rt   r   )r   s    r8   <lambda>z)collapse_variadic_union.<locals>.<lambda>7  s    CLL r7   )ra   r   rn   Nzbuiltins.tuple)fallback)r   r)   r|   r!   rk   rt   sortedr(   r'   r/   r   fullnameargs	enumeratecopy_modifiedpartial_fallbackr&   r   )rz   tuple_itemsother_itemsr   p_tfirstlastunpack_indexunpackunpackedsuffixprefixargrv   itr   s                   r8   r   r   #  s    KKY " "a  c9%% 	"s####q!!!!
;1
*@*@AAAKNEr?D&tz22L
Z%Ffj)))))v{++Hh)) 
=!%55555Zq(**+F 5;#f++%%
 %+s6{{lnn-77
 ^F|^, -
C;qt,--  28vQ/&888JJJ 9 zVses39~~'9::fXENNN

5;1#1133

v069DDYZZZ
{ :;;;r7   N)ry   r,   r>   r   )rz   r&   r>   r"   )A
__future__r   collectionsr   collections.abcr   
contextlibr   typingr   r   r	   typing_extensionsr
   r+   mypy.erasetyper   mypy.literalsr   r   r   r   r   
mypy.nodesr   r   r   r   r   r   r   mypy.optionsr   mypy.subtypesr   r   mypy.typeopsr   
mypy.typesr   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   mypy.typevarsr*   r,   r5   r.   r:   r   tupleAssignsrN   r}   r   r6   r7   r8   <module>r     s   " " " " " " " # # # # # # $ $ $ $ $ $ % % % % % % . . . . . . . . . . 5 5 5 5 5 5 < < < < < < \ \ \ \ \ \ \ \ \ \ \ \ \ \ Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z             2 2 2 2 2 2 2 2 . . . . . .                                 1 0 0 0 0 0!&y*h'F!G  G G G G    *   
_ _ _ _ _ _ _ _D j$uT8D>-A'B"CC
DB% B% B% B% B% B% B% B%J    9< 9< 9< 9< 9< 9<r7   