
    X-PhQ                        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 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  G d	 d
e          Zd dZd!dZd"dZd#dZd dZd dZd$dZ d%dZ!dS )&z
This plugin is helpful for mypy development itself.
By default, it is not enabled for mypy users.

It also can be used by plugin developers as a part of their CI checks.

It finds missing ``get_proper_type()`` call, which can lead to multiple errors.
    )annotations)Callable)TypeChecker)TypeInfo)FunctionContextPlugin)is_proper_subtype)
AnyTypeFunctionLikeInstanceNoneTyp
ProperType	TupleTypeType	UnionTypeget_proper_typeget_proper_typesc                      e Zd ZdZddZdS )	ProperTypePlugina  
    A plugin to ensure that every type is expanded before doing any special-casing.

    This solves the problem that we have hundreds of call sites like:

        if isinstance(typ, UnionType):
            ...  # special-case union

    But after introducing a new type TypeAliasType (and removing immediate expansion)
    all these became dangerous because typ may be e.g. an alias to union.
    fullnamestrreturn(Callable[[FunctionContext], Type] | Nonec                T    |dk    rt           S |dk    rt          S |dk    rt          S d S )Nzbuiltins.isinstancezmypy.types.get_proper_typezmypy.types.get_proper_types)isinstance_proper_hookproper_type_hookproper_types_hook)selfr   s     Z/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/mypy/plugins/proper_plugin.pyget_function_hookz"ProperTypePlugin.get_function_hook-   s=    ,,,))333##444$$t    N)r   r   r   r   )__name__
__module____qualname____doc__r     r!   r   r   r       s2        
 
     r!   r   ctxr   r   r   c                   t          | j                  dk    s| j        d         s| j        S t          | j        d         d                   }| j        d         D ]}t	          |          s"t          t          |          t                    rgt          |          rXt          |          r	| j        c S | j	        
                    d| j                   | j	                            d| j                   | j        S )N      r   zTNever apply isinstance() to unexpanded types; use mypy.types.get_proper_type() firstzSIf you pass on the original type after the check, always use its unexpanded version)len	arg_typesdefault_return_typer   is_improper_type
isinstancer
   is_dangerous_targetis_special_targetapifailcontextnote)r'   rightargs      r   r   r   7   s	   
3=QcmA&6&&CM!,Q/00E}Q  S!!	%/0D0Dg%N%N	!%((	 !'' /....GLL:  
 GLLF  
 ""r!   r6   r   boolc                   t          | t                    rn|                                 rZ|                                 j        dk    rdS |                                 j        dv rdS |                                 j        dv rdS n@t          | t
                    r+t          d t          | j                  D                       S dS )zIWhitelist some special cases for use in isinstance() with improper types.zbuiltins.tupleT)mypy.types.Typemypy.types.ProperTypezmypy.types.TypeAliasType)zmypy.types.UnboundTypezmypy.types.TypeVarLikeTypezmypy.types.TypeVarTypezmypy.types.UnpackTypezmypy.types.TypeVarTupleTypezmypy.types.ParamSpecTypezmypy.types.Parameterszmypy.types.RawExpressionTypezmypy.types.EllipsisTypezmypy.types.StarTypezmypy.types.TypeListzmypy.types.CallableArgumentzmypy.types.PartialTypezmypy.types.ErasedTypezmypy.types.DeletedTypezmypy.types.RequiredTypezmypy.types.ReadOnlyTypec              3  4   K   | ]}t          |          V  d S N)r1   .0ts     r   	<genexpr>z$is_special_target.<locals>.<genexpr>s   s+      OOA$Q''OOOOOOr!   F)	r/   r   is_type_objtype_objectr   r   allr   items)r6   s    r   r1   r1   O   s    %&& "P5+<+<+>+> "P'+;;;4' ,
 
 
 4' ,
 
 
* 4+
, 
E9	%	% POO1A%+1N1NOOOOOO5r!   typc                   t          |           } t          | t                    r2| j        }|                    d          o|                    d           S t          | t
                    rt          d | j        D                       S dS )z3Is this a type that is not a subtype of ProperType?r:   r;   c              3  4   K   | ]}t          |          V  d S r=   )r.   r>   s     r   rA   z#is_improper_type.<locals>.<genexpr>~   s+      ::1#A&&::::::r!   F)r   r/   r   typehas_baser   anyrE   )rF   infos     r   r.   r.   w   s    
#

C#x   _x}}.//^F]8^8^4^^#y!! ;::	::::::5r!   c                   t          | t                    rt          d | j        D                       S t          | t                    r;|                                 r'|                                                     d          S dS )zFIs this a dangerous target (right argument) for an isinstance() check?c              3  N   K   | ] }t          t          |                    V  !d S r=   )r0   r   r>   s     r   rA   z&is_dangerous_target.<locals>.<genexpr>   s3      NNq&q'9'9::NNNNNNr!   r:   F)r/   r   rK   rE   r   rB   rC   rJ   )rF   s    r   r0   r0      s{    #y!! ONNCINNNNNN#|$$ =):): =  ))*;<<<5r!   c                N   | j         d         }|rt          |d                   }t          |           }t          |t	          j        t                      |g                    r<t          |t          t          f          r | j	        
                    d| j                   | j        S )z6Check if this get_proper_type() call is not redundant.r   z#Redundant call to get_proper_type())r,   r   get_proper_type_instancer	   r   
make_unionr   r/   r   r2   r3   r4   r-   )r'   r,   arg_typeproper_types       r   r   r      s    a I Q"9Q<00.s33Xy';WYY<T'U'UVV 	Q (Y$9:: QBCKPPP""r!   c                8   | j         d         }|r|d         }t          |           }t          j        t	                      |g          }| j                            d|g          }t          ||          r | j                            d| j	                   | j
        S )z7Check if this get_proper_types() call is not redundant.r   ztyping.Iterablez$Redundant call to get_proper_types())r,   rP   r   rQ   r   r2   named_generic_typer	   r3   r4   r-   )r'   r,   rR   rS   	item_typeok_types         r   r   r      s    a I NQ<.s33('))[)ABB	',,->LLXw// 	NGLL?MMM""r!   r   c                    | j         }t          |t                    sJ |j        d         }|j        d         }t          |j        t                    sJ t          |j        g           S )Nz
mypy.typesr   )r2   r/   r   modulesnamesnoder   r   )r'   checkertypesproper_type_infos       r   rP   rP      sf    gGg{+++++OL)E{<0&+X66666$)2...r!   versionr   type[ProperTypePlugin]c                    t           S r=   )r   )r_   s    r   pluginrb      s    r!   N)r'   r   r   r   )r6   r   r   r8   )rF   r   r   r8   )rF   r   r   r8   )r'   r   r   r   )r_   r   r   r`   )"r%   
__future__r   typingr   mypy.checkerr   
mypy.nodesr   mypy.pluginr   r   mypy.subtypesr	   
mypy.typesr
   r   r   r   r   r   r   r   r   r   r   r   r1   r.   r0   r   r   rP   rb   r&   r!   r   <module>rj      s    # " " " " "       $ $ $ $ $ $       / / / / / / / / + + + + + +                           v   .# # # #0% % % %P      # # # #
# 
# 
# 
#/ / / /     r!   