o
    2gQ                     @  s   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
 d
eZd(ddZd)ddZd*ddZd+ddZd(ddZd(ddZd,d d!Z d-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                   @  s   e Zd ZdZd	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                 C  s(   |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    r   R/home/garg/my-data/venv/lib/python3.10/site-packages/mypy/plugins/proper_plugin.pyget_function_hook-   s   z"ProperTypePlugin.get_function_hookN)r   r   r   r   )__name__
__module____qualname____doc__r   r   r   r   r   r       s    r   ctxr   r   r   c                 C  s   t | jdks| jd s| jS t| jd d }| jd D ]*}t|s*tt|trGt|rGt|r7| j  S | j	
d| j | j	d| j q| 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argr   r   r   r   7   s,   
r   r1   r   boolc                 C  sr   t | tr&|  r&|  jdkrdS |  jdv rdS |  jdv r$dS dS t | tr7td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                 s      | ]}t |V  qd S N)r,   .0tr   r   r   	<genexpr>s       z$is_special_target.<locals>.<genexpr>F)	r*   r
   is_type_objtype_objectr   r   allr   items)r1   r   r   r   r,   O   s   
r,   typc                 C  sP   t | } t| tr| j}|do|d S t| tr&tdd | jD S dS )z3Is this a type that is not a subtype of ProperType?r4   r5   c                 s  r6   r7   )r)   r8   r   r   r   r;   ~   r<   z#is_improper_type.<locals>.<genexpr>F)r   r*   r   typehas_baser   anyr@   )rA   infor   r   r   r)   w   s   

r)   c                 C  sB   t | trtdd | jD S t | tr|  r|  dS dS )zFIs this a dangerous target (right argument) for an isinstance() check?c                 s  s    | ]	}t t|V  qd S r7   )r+   r   r8   r   r   r   r;      s    z&is_dangerous_target.<locals>.<genexpr>r4   F)r*   r   rD   r@   r
   r=   r>   rC   )rA   r   r   r   r+      s
   
r+   c                 C  s\   | j d }|r+t|d }t| }t|t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   r-   r.   r/   r(   )r#   r'   arg_typeproper_typer   r   r   r      s   
r   c                 C  s^   | j d }|r,|d }t| }t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'   rF   r   rG   r   r-   named_generic_typer   r.   r/   r(   )r#   r'   rH   rI   	item_typeok_typer   r   r   r      s   

r   r   c                 C  sD   | j }t|ts
J |jd }|jd }t|jtsJ t|jg S )Nz
mypy.typesr   )r-   r*   r   modulesnamesnoder   r   )r#   checkertypesproper_type_infor   r   r   rF      s   

rF   versionr   type[ProperTypePlugin]c                 C  s   t S r7   )r   )rS   r   r   r   plugin   s   rU   N)r#   r   r   r   )r1   r   r   r3   )rA   r   r   r3   )rA   r   r   r3   )r#   r   r   r   )rS   r   r   rT   )"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   r,   r)   r+   r   r   rF   rU   r   r   r   r   <module>   s"    	0


(

	

	