o
    tBh                    @  s  U d Z ddlmZ ddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlZddlZddlZddlZddlZddl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mZ ddlm Z  ddl!Z"ddl#Z"ddl$Z"ddl%Z"ddl&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/m0Z0 G dd dZ1e1 Z2de3d< edZ4ee4e1f Z5de3d< e-ej6ej7dZ8de3d< dddZ9ddd Z:G d!d" d"e;Z<G d#d$ d$Z=dd'd(Z>dd*d+Z?edd2d3Z@dd7d8ZAe@Be'jCdd:d;ZDe@Be'jEdd>d?ZFddBdCZGddIdJZHddKdLZIddPdQZJG dRdS dSee4 ZKddVdWZLe@Be'jMddYdZZNe@Be1dd[d\ZOe@Be'jPdd^d_ZQe@Be'jRddadbZSe@Be'jTddddeZUe@Be'jVddgdhZWddjdkZXddodpZYe@Be'jZddqdrZ[e@Be'j\ddtduZ]e^h dvZ_de3dw< e^h dxZ`de3dy< dd{d|Zadd}d~ZbdddZcdddZddddZedddZfi agde3d< ddddZhdddZi	ddddZjdddZkG dd dZlddddZmdddZndddZoepdkreqeo  dS dS )ziTests for stubs.

Verify that various things in stubs are consistent with how things behave at runtime.

    )annotationsN)redirect_stderrredirect_stdout)singledispatch)Path)AnyGenericIteratorTypeVarUnioncast)
get_origin)nodes)parse_config_file)Options)FancyFormatterbytes_to_human_readable_repr	is_dunderplural_sc                   @  s   e Zd ZdZdddZdS )MissingzGMarker object for things that are missing (from a stub or the runtime).returnstrc                 C  s   dS )NMISSING selfr   r   d/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/mypy/stubtest.py__repr__,      zMissing.__repr__Nr   r   )__name__
__module____qualname____doc__r   r   r   r   r   r   )   s    r   ztyping_extensions.Finalr   Tztyping_extensions.TypeAliasMaybeMissingF
_formattermessager   kwargsr   r   c                 K  s   | dd tj| fi |S )z.Wrapper around mypy.util for fancy formatting.colornone)
setdefaultr&   style)r'   r(   r   r   r   _style8   s   r-   lengthintc                 C  s$   t | |kr| d |d  d S | S )N   z...)len)r'   r.   r   r   r   	_truncate>   s   r2   c                   @     e Zd ZdS )StubtestFailureNr    r!   r"   r   r   r   r   r4   D       r4   c                   @  s>   e Zd ZddddddZdddZdddZddddZdS )ErrorN	stub_descruntime_descobject_path	list[str]r'   r   stub_objectMaybeMissing[nodes.Node]runtime_objectMaybeMissing[Any]r9   
str | Noner:   r   Nonec                C  sR   || _ d|| _|| _|| _|| _|ptt|d|| _|p%t	t
|d| _dS )a  Represents an error found by stubtest.

        :param object_path: Location of the object with the error,
            e.g. ``["module", "Class", "method"]``
        :param message: Error message
        :param stub_object: The mypy node representing the stub
        :param runtime_object: Actual object obtained from the runtime
        :param stub_desc: Specialised description for the stub object, should you wish
        :param runtime_desc: Specialised description for the runtime object, should you wish

        .typed   N)r;   joinobject_descr'   r=   r?   r   getattrr9   r2   reprr:   )r   r;   r'   r=   r?   r9   r:   r   r   r   __init__I   s   zError.__init__boolc                 C  s   t | jtS )z@Whether or not the error is for something missing from the stub.)
isinstancer=   r   r   r   r   r   is_missing_stubf   s   zError.is_missing_stubc                 C  s
   d| j v S )zOWhether or not the error is for something being (or not being) positional-only.zleading double underscore)r'   r   r   r   r   is_positional_only_relatedj   s   
z Error.is_positional_only_relatedFconcisec           
      C  s  |rt | jddd | j S d}d}t| jts| jj}t| jd }|dur,|j	p+d}d}|r7|d| 7 }|rB|dt
| 7 }d}d}t| jtstz
t| jd	 }W n ttfya   Y nw zt| j}W n	 tys   Y nw d}|r|d| 7 }|r|dt
| 7 }t d
dddt | jddd| jddt |dddt | jd ddddt |dddt | jd dddg}	d|	S )zxReturns a description of the error.

        :param concise: Whether to return a concise, one-line description

        T)bold Nr    z	 at line z	 in file    zerror: redr)   rP   
zStub:)dimblue)r)   rW   zRuntime:)r-   rG   r'   rL   r=   r   lineget_stubr;   pathr   r?   inspectgetsourcelinesOSError	TypeErrorgetsourcefiler9   r:   rF   )
r   rO   	stub_line	stub_file	stub_nodestub_loc_strruntime_lineruntime_fileruntime_loc_stroutputr   r   r   get_descriptiono   s^   



zError.get_description)r;   r<   r'   r   r=   r>   r?   r@   r9   rA   r:   rA   r   rB   )r   rK   F)rO   rK   r   r   )r    r!   r"   rJ   rM   rN   ri   r   r   r   r   r7   H   s    

r7   module_nametypes.ModuleTypec                 C  s   t tjd^}t A t|- t| td t	| }t
| dgd W d    n1 s1w   Y  W d    n1 s@w   Y  W d    n1 sOw   Y  W d    |S W d    |S 1 sgw   Y  |S )Nwignore*)fromlist)openosdevnullwarningscatch_warningsr   r   simplefilter	importlibimport_module
__import__)rk   rs   runtimer   r   r   silent_import_module   s"   

  

r{   Iterator[Error]c                 c  s   t | }|du r-t| dd s+| tjv rttj|  nd}t| gdtd|dV  dS zt| }W n% t	yX } zt| gdt
|j d| |tV  W Y d}~dS d}~ww t q td	 zt||| gE dH  W nN t	y } zBtt|jd d
 }|jdd}|dks|dd
 dkr t| gdt
|j d| ||ddt   dV  W Y d}~nd}~ww W d   dS W d   dS 1 sw   Y  dS )zTests a given module's stub against introspecting it at runtime.

    Requires the stub to have been built already, accomplished by a call to ``build_stubs``.

    :param module_name: The module to test

    NrC   zN/Azfailed to find stubsr:   zfailed to import, : rn   r   r    rR   __main__mypyzencountered unexpected error, zThis is most likely the fault of something very dynamic in your library. It's also possible this is a bug in stubtest.
If in doubt, please open an issue at https://github.com/python/mypy

r8   )rZ   is_probably_privatesplitsysmodulesrI   r7   r   r{   	ExceptionrD   r    rt   ru   rv   verifylist	tracebackwalk_tb__traceback__	f_globalsget
format_excstrip)rk   stubr:   rz   ebottom_framebottom_moduler   r   r   test_module   sR   $


"r   r   r>   rz   r@   r;   r<   c                 c  s    t |d| |V  dS )zEntry point for comparing a stub to a runtime object.

    We use single dispatch based on the type of ``stub``.

    :param stub: The mypy node representing a part of the stub
    :param runtime: The runtime object corresponding to ``stub``

    zis an unknown mypy nodeNr7   r   rz   r;   r   r   r   r      s   r   nodes.MypyFileruntime_all_as_setset[str]c                 c  sn    d|j v sJ dd |j  D }t|| }t|| }|s$|s$d S t| dttd| d| dV  d S )N__all__c                 S  s   h | ]	\}}|j r|qS r   )module_public.0mor   r   r   	<setcomp>   s    z)_verify_exported_names.<locals>.<setcomp>znames exported from the stub do not correspond to the names exported at runtime. This is probably due to an inaccurate `__all__` in the stub or things being missing from the stub.z/Names exported in the stub but not at runtime: z/Names exported at runtime but not in the stub: )r=   r?   r9   r:   )namesitemssortedr7   r   )r;   r   r   public_names_in_stubnames_in_stub_not_runtimenames_in_runtime_not_stubr   r   r   _verify_exported_names   s   r   MaybeMissing[types.ModuleType]c           	   	   #  sR   t trt|d| V  d S t tjs t|d| V  d S tdr9tj}d| jv r8t	|| |E d H  nd }fdd| j
 D }ddd |d urR|n fddtD }|| |t t|D ]:}|| jv ry| j| jnt}t |tjrql|d usJ zt|t}W n	 ty   Y qlw t||||g E d H  qld S )Nis not present at runtimezis not a moduler   c                   s,   h | ]\}}|j st|rt |r|qS r   )module_hiddenr   hasattrr   rz   r   r   r   1  s    z"verify_mypyfile.<locals>.<setcomp>rrl   attrr   r   rK   c                 S  sP   t | |}zt |dd }W n
 ty   Y dS w |d ur!|| jkS t|tj S )Nr!   F)rH   r   r    rL   types
ModuleType)r   r   objobj_modr   r   r   _belongs_to_runtime7  s   

z,verify_mypyfile.<locals>._belongs_to_runtimec                   s"   h | ]}t |s |r|qS r   )r   r   r   r   rz   r   r   r   D  s    )r   rl   r   r   r   rK   )rL   r   r7   r   r   r   setr   r   r   r   dirupdatedifference_updateIGNORED_MODULE_DUNDERSr   noder   r   MypyFilerH   r   r   )	r   rz   r;   r   to_checkruntime_public_contentsentry
stub_entryruntime_entryr   r   r   verify_mypyfile  sJ   







r   nodes.TypeInfoMaybeMissing[type[Any]]c              
   #  s   t |trt|d| |t| dV  d S t |ts't|d| |t| dV  d S z
G dd d|}W n& tyO   | jsMt|tj	sMt|d| |t| dV  Y n	 t
yW   Y nw t| j}|dd ttt|D  | jrt|d	 t|D ]j  } d
r d
sd| j   }t fdd| jD t}|d usJ zzt||}W n ty   t||t}Y nw W n	 t
y   Y qxw t |trt |tjrt |ddst!||| g E d H  qxd S )Nr   r9   zis not a typec                   @  r3   )z!verify_typeinfo.<locals>.SubClassNr5   r   r   r   r   SubClassm  r6   r   zIcannot be subclassed at runtime, but isn't marked with @final in the stubc                 s  s$    | ]}t |s|tvr|V  qd S N)r   IGNORABLE_CLASS_DUNDERSr   r   r   r   	<genexpr>  s    z"verify_typeinfo.<locals>.<genexpr>rJ   ___c                 3  s&    | ]} |j v r|j   jV  qd S r   )r   r   r   tr   r   r   r     s   $ Texclude_special)"rL   r   r7   rI   rD   r_   is_final
issubclassenumEnumr   r   r   r   r   r   varsis_protocoldiscardr   
startswithendswithnamenextmror   rH   AttributeErrorr\   getattr_staticr   WrapperDescriptorTyper   r   )r   rz   r;   r   r   mangled_entrystub_to_verifyruntime_attrr   r   r   verify_typeinfo`  sj   




	

r   nodes.FuncBaseIterator[str]c              	   c  s    | j dv rd S t|r(tt|dd t}|r| jsdV  |s&| jr&dV  d S t|d }|dd  D ]}zt	||}W q5 t
yJ   Y  d S w t|trV| jsVdV  t|tsa| jradV  t|trl| jsldV  t|tsy| jr{dV  d S d S d S )	N)__new____init_subclass____class_getitem____self__z(runtime is a classmethod but stub is notz(stub is a classmethod but runtime is notr   rS   z)runtime is a staticmethod but stub is notz)stub is a staticmethod but runtime is not)r   r\   	isbuiltinrL   rH   rD   is_classrw   rx   r   r   classmethodstaticmethod	is_static)r   rz   r;   probably_class_methodstatic_runtimer   r   r   r   _verify_static_class_methods  s4   




r   stub_argnodes.Argumentruntime_arginspect.Parameterfunction_namec                 c  s    t |ddr	dS ddd	}|| jjd
|jkrdS ddd}|jtjjkr0|| jj|jr0dS | jjdkr8dS d| jj d|j dV  dS )z$Checks whether argument names match.Tr   Nsr   prefixr   c                 S  s   |  |r| t|d  S | S r   )r   r1   )r   r   r   r   r   strip_prefix  s   z&_verify_arg_name.<locals>.strip_prefixr   abrK   c                 S  s@   |  d} | d}| |p|| pt| dkpt|dkS )Nr   rS   )r   r   r1   )r   r   r   r   r   names_approx_match  s   

,z,_verify_arg_name.<locals>.names_approx_match_selfstub argument "z!" differs from runtime argument "")r   r   r   r   r   r   )r   r   r   r   r   rK   )r   variabler   kindr\   	ParameterPOSITIONAL_ONLY)r   r   r   r   r   r   r   r   _verify_arg_name  s"   


r  c                 c  s    |j tjjkr_| j rd|j dV  dS t|j }| jj	p"| j
}t|tjjr-|j}|durU|durWt	|j tkrY|j dur[t||s]d|j d| d| V  dS dS dS dS dS dS | j rod| jj dV  dS dS )	z6Checks whether argument default values are compatible.runtime argument "z0" has a default value but stub argument does notN.z" has a default value of type z0, which is incompatible with stub argument type r   z3" has a default value but runtime argument does not)defaultr\   r  emptyr  is_requiredr   get_mypy_type_of_runtime_valuer   rD   type_annotationrL   r   r   TypeVarTypeupper_boundobjectis_subtype_helperis_optional)r   r   runtime_type	stub_typer   r   r   _verify_arg_default_value  s:   




	r  r   argslist[nodes.Argument]c                 C  s(   | dv r|d j jdkr|dd  S |S )N)r   r   r   clsrS   r   r   )r   r  r   r   r   maybe_strip_cls  s   r  c                   @  sJ   e Zd ZdddZdddZedddZedddZedddZdS )	Signaturer   rB   c                 C  s   g | _ i | _d | _d | _d S r   )poskwonlyvarposvarkwr   r   r   r   rJ   %  s   
zSignature.__init__r   c                   s   dddddd	ddddfdd t | j fddd}d}|d fdd| jD | jr?d| j gn| jrEdgng   fdd|D  | jr[d| j gng  7 }|d7 }|S )Nargr   r   r   c                 S  s*   t | tjr	| jS t | tjr| jjS tr   )rL   r\   r  r   r   Argumentr   AssertionErrorr  r   r   r   get_name,  s
   z#Signature.__str__.<locals>.get_namerA   c                 S  s2   t | tjrd S t | tjrt| jjp| jS t	r   )
rL   r\   r  r   r  r   r   rD   r
  r  r   r   r   r   get_type3  s
   z#Signature.__str__.<locals>.get_typerK   c                 S  s4   t | tjr| jtjjkS t | tjr| j S t	r   )
rL   r\   r  r  r  r   r  r  r  r  r   r   r   r   has_default:  s
   
z&Signature.__str__.<locals>.has_defaultc                   s4   | } | |rd| nd | rd S d S )Nr   rR   z = ...r   )r  arg_type)r!  r"  r#  r   r   get_descA  s   
z#Signature.__str__.<locals>.get_descc                   s   |  | fS r   r   )r   )r!  r#  r   r   <lambda>I  s    z#Signature.__str__.<locals>.<lambda>keyzdef (z, c                      g | ]} |qS r   r   r   r  r%  r   r   
<listcomp>L      z%Signature.__str__.<locals>.<listcomp>ro   c                   r)  r   r   r*  r+  r   r   r,  N  r-  z**))r  r   r   r   )r  r   r   rA   )r  r   r   rK   )r   r  valuesrF   r  r  r  )r   kw_onlyretr   )r%  r!  r"  r#  r   __str__+  s"   


$zSignature.__str__r   nodes.FuncItemSignature[nodes.Argument]c                 C  s~   t  }t| j| j}|D ]0}|j r|j| q|j r'||j	|j
j< q|jtjkr1||_q|jtjkr;||_qt|S r   )r  r  r   	argumentsr  is_positionalr  appendis_namedr  r   r   ARG_STARr  	ARG_STAR2r  r  )r   stub_sig	stub_argsr   r   r   r   from_funcitemT  s   

zSignature.from_funcitem	signatureinspect.SignatureSignature[inspect.Parameter]c                 C  s   t  }| j D ]9}|jtjjtjjfv r|j	| q|jtjj
kr*||j|j< q|jtjjkr5||_q|jtjjkr@||_qt|S r   )r  
parametersr/  r  r\   r  r  POSITIONAL_OR_KEYWORDr  r7  KEYWORD_ONLYr  r   VAR_POSITIONALr  VAR_KEYWORDr  r  )r>  runtime_sigr   r   r   r   from_inspect_signaturee  s   z Signature.from_inspect_signaturenodes.OverloadedFuncDefc                   sd  t jdd}i  ttjD ]5}|dusJ tj|j}t|D ]!\}}|jj	ds/|r4d| n|jj} 
|g ||f q"qd fd	d
}d fdd}d fdd}	t }
t |dD ]K} | d d jj}tjt|||dd|	|d}|j r|
j| qd|j r||
j|jj< qd|jtjkr||
_qd|jtjkr||
_qdt|
S )a  Returns a Signature from an OverloadedFuncDef.

        If life were simple, to verify_overloadedfuncdef, we'd just verify_funcitem for each of its
        items. Unfortunately, life isn't simple and overloads are pretty deceitful. So instead, we
        try and combine the overload's items into a single signature that is compatible with any
        lies it might try to tell.

        Tr   Nr   arg_namer   r   r/   c                   s   t dd  |  D S )Nc                 s  s    | ]\}}|V  qd S r   r   )r   r   indexr   r   r   r     s    zISignature.from_overloadedfuncdef.<locals>.get_position.<locals>.<genexpr>)max)rI  all_argsr   r   get_position  s   z6Signature.from_overloadedfuncdef.<locals>.get_positionmypy.types.ProperTypec                   sZ   t jjd dd  |  D }t jdd |D W  d    S 1 s&w   Y  d S )NTc                 S  s   g | ]\}}|j jp|jqS r   )r   rD   r
  r   r  r   r   r   r   r,    s    zFSignature.from_overloadedfuncdef.<locals>.get_type.<locals>.<listcomp>c                 S  s   g | ]}|r|qS r   r   r   r   r   r   r,    r-  )r   statestrict_optional_settypeopsmake_simplified_union)rI  	all_typesrL  r   r   r"    s   $z2Signature.from_overloadedfuncdef.<locals>.get_typenodes.ArgKindc                   s   dd  |  D }t j|v rt jS t j|v rt jS t |  tjk p-t j|v p-t j|v }t j|v p7t j|v }|rB|r?t jS t jS |rGt jS t jS )Nc                 S  s   h | ]\}}|j qS r   )r  rP  r   r   r   r     s    zESignature.from_overloadedfuncdef.<locals>.get_kind.<locals>.<setcomp>)	r   r9  r:  r1   r   ARG_OPTARG_NAMED_OPTARG_POS	ARG_NAMED)rI  kindsis_optis_posrM  r   r   r   get_kind  s   

z2Signature.from_overloadedfuncdef.<locals>.get_kindr'  r   )r
  initializerr  )rI  r   r   r/   )rI  r   r   rO  )rI  r   r   rV  )r   r   map _resolve_funcitem_from_decoratorr   r  r5  	enumerater   r   r+   r7  r  r   r   r  Varr  r6  r  r8  r  r9  r  r:  r  r  )r   assume_positional_onlyfuncr  rJ  r  r   rN  r"  r_  sigrI  example_arg_namer   r^  r   from_overloadedfuncdefx  sH   


z Signature.from_overloadedfuncdefNr   rB   r   )r   r3  r   r4  )r>  r?  r   r@  )r   rH  r   r4  )	r    r!   r"   rJ   r2  r   r=  rG  ri  r   r   r   r   r  $  s    

)r  r4  r@  c                 c  s>   t | j|jD ]b\}}t|||E d H  t||E d H  |jtjjkrJ|jsJ|j	j
dsJ|j	j
ddksJt|ddsJd|j	j
 d|j
 dV  |jtjjkrj|js[|j	j
drjt|ddsjd|j	j
 d	V  qt| jt|jkr|jd u r| jt|jd  D ]}|j	j
|jvrd
|j	j
 dV  qd|j	j
 dV  q| jd urd| jj	j
 dV  n.t| jt|jk r|jt| jd  D ]}|j
| jvrd|j
 dV  qd|j
 dV  qt| jt|jks|jd u r| jd u r|jd urd|jj
 dV  | jd ur|jd u rd| jj	j
 dV  tt| jt|j@ D ]}| j| |j| }}t|||E d H  t||E d H  q&|jd u sXt|jt| jstt| jt|j D ]0}|dd |jD v r|dd |jt| jd  D vrd| dV  qdd
| dV  qdtt|jt| j D ]6}|dd | jD v r|jd u r|dd | jt|jd  D v sd| dV  qd| dV  q| jd u r|jd urdd | jD }t|jt| j|B sd|jj
 dV  | jd ur|jd u rd| jj	j
 dV  d S d S d S )Nr   r   r   Tr   r   zN" should be positional-only (rename with a leading double underscore, i.e. "__z")zD" should be positional or keyword (remove leading double underscore)z runtime does not have argument "r   z" is not keyword-onlyz&runtime does not have *args argument "zstub does not have argument "r  z#stub does not have *args argument "c                 S     h | ]}|j qS r   r   r   r   r   r   r   r         z$_verify_signature.<locals>.<setcomp>c                 S  rk  r   rl  rm  r   r   r   r     rn  c                 S     h | ]}|j jqS r   r  r   r   r   r   r   r     r-  c                 S  ro  r   r  rp  r   r   r   r     r-  c                 S  ro  r   r  rp  r   r   r   r   &  r-  z&stub does not have **kwargs argument "z)runtime does not have **kwargs argument ")zipr  r  r  r  r\   r  r  pos_onlyr   r   r   r   r   r1   r  r  r   r   r  issubset)r   rz   r   r   r   r  stub_pos_namesr   r   r   _verify_signature  s   



 $$$ru  r3  c                 c  sR   t |trt|d| |V  d S t|s"t|d| |V  t|s"d S t | tjr@| jtjk}t	|dd}|r@|s@t|d| |V  t
| ||D ]}t|d| | |V  qFt|}t|}|rxt| }t|}	|rldnd d	| }
t|}nd
\}
}|r| jst|d| |||
dV  |sd S t||	| jdD ]}t|d| | ||
dV  qd S )Nr   is not a function__isabstractmethod__Fz;is inconsistent, runtime method is abstract but stub is notis inconsistent, zasync rR   def )NNz:is an "async def" function at runtime, but not in the stubr8   r   r~   )rL   r   r7   is_probably_a_functioncallabler   FuncDefabstract_statusIS_ABSTRACTrH   r   safe_inspect_signaturer\   iscoroutinefunctionr  r=  rG  r   is_coroutineru  r   )r   rz   r;   stub_abstractruntime_abstractr'   r>  runtime_is_coroutiner;  rF  runtime_sig_descr9   r   r   r   verify_funcitem.  s`   





	
r  c                 c  s    t |d| |V  d S )Nzis not present in stubr   r   r   r   r   verify_noneo  s   r  	nodes.Varc                 c  s    t |trt|dkrt|d| |V  d S | jr+t|r+| js#| js+t|d| |V  t|}|d urf| j	d urht
|| j	sjd}t |tjrWt|j}|d urWt
|| j	rWd}|rlt|d| | |V  d S d S d S d S d S )N   r   +is read-only at runtime but not in the stubTFz#variable differs from runtime type )rL   r   r1   r7   is_initialized_in_classis_read_only_propertyis_settable_propertyis_propertyr	  rD   r  r   r   value)r   rz   r;   r  should_errorr   r   r   
verify_varv  s>   




r  rH  c              
   c  s   t |trt|d| |V  d S | jr!t|rt|d| |V  d S t|s3t|d| |V  t|s3d S t| ||D ]}t|d| | |V  q9t|}|sNd S t	
| }t	|}t||| jdD ]#}d|v rj|d7 }t|d| | |t| jd|  d	t| d
V  q`d S )Nr   r  rv  rx  rz  zhas a default value of typezc. This is often caused by overloads failing to account for explicitly passing in the default value.z
Inferred signature: ry  r8   )rL   r   r7   r  r  r{  r|  r   r  r  ri  rG  ru  r   r   rD   )r   rz   r;   r'   r>  r;  rF  r   r   r   verify_overloadedfuncdef  sB   




r  nodes.TypeVarExprc                 c  sP    t |tr| jdkrd S t|d| |V  d S t |ts&t|d| |V  d S d S )N_NTr   zis not a TypeVar)rL   r   r   r7   r
   r   r   r   r   verify_typevarexpr  s   


r  nodes.ParamSpecExprc                 c  sp    t |trt|d| |V  d S ttdd ttdd f}tdd |D }|r,t ||s6t|d| |V  d S d S )Nr   	ParamSpecc                 s  s    | ]	}|d ur|V  qd S r   r   r   r   r   r   r     s    z'verify_paramspecexpr.<locals>.<genexpr>zis not a ParamSpec)rL   r   r7   rH   typingtyping_extensionstuple)r   rz   r;   maybe_paramspec_typesparamspec_typesr   r   r   verify_paramspecexpr  s   


r  nodes.Decoratorc                 c  sz    | j jsJ t|trd S t|rd S t|}t| j jtj	j
r'| j jjnd }|d ur8|d ur8t||r8d S dV  d S )NzGis inconsistent, cannot reconcile @property on stub with runtime object)rf  r  rL   propertyr\   isdatadescriptorr	  rD   r   r   CallableTyperet_typer  )r   rz   r  	func_typer   r   r   _verify_readonly_property  s   


r  decnodes.OverloadPartnodes.FuncItem | Nonec                   s\   t  tjr S  jjrdS d
 fdd	} j} jD ]}|||}|du r) dS |}q|S )zReturns a FuncItem that corresponds to the output of the decorator.

    Returns None if we can't figure out what that would be. For convenience, this function also
    accepts FuncItems.
    N	decoratornodes.Expressionrf  r3  r   r  c                   s   t | tjsd S | jd u rd S | jdv s| jtjjv r|S | jdkrJ|jd jj	dvr;t
d|jd jj	d j t|}|jdd  |_|S d S )N)zbuiltins.staticmethodzabc.abstractmethodzbuiltins.classmethodr   )r  mcsmetaclszunexpected class argument name z in rS   )rL   r   RefExprfullnamer   r   OVERLOAD_NAMESr5  r   r   r4   copy)r  rf  r1  r  r   r   apply_decorator_to_funcitem  s$   



zE_resolve_funcitem_from_decorator.<locals>.apply_decorator_to_funcitem)r  r  rf  r3  r   r  )rL   r   FuncItemrf  r  original_decorators)r  r  rf  r  resulting_funcr   r  r   rb     s   

rb  c                 c  sv    t |trt|d| |V  d S | jjr&t| |D ]
}t||| |V  qd S t| }|d ur9t|||E d H  d S d S )Nr   )rL   r   r7   rf  r  r  rb  r   )r   rz   r;   r'   rf  r   r   r   verify_decorator0  s   
r  nodes.TypeAliasc           
      c  s    t j| j}d| }t|trt|d| ||dV  d S t|p#|}t|t jjrt|t	s<t|d| ||dV  d S |j	}z|j
}W n tyS   t|dt}Y nw t|trt|dt}t|tr|dksp|dkrr|d	v rrd
}| d| }	tdt|j |	rd S t|||E d H  d S t|t jjr|turtjdkrt|tjst|d| |t|dV  d S t|t jjrtt|ddvrt|d| ||dV  d S t|t jjr|tjjurt|d| ||dV  d S t|t jjrd S t|d| ||dV  d S )NzType alias for r   r   z&is inconsistent, runtime is not a typer    r!   zcollections.abcre>   MatchPatternr  rC   z_?)r0   
   zis not a Union__mro__r   zis not a subclass of tuplez is not a type alias for Callablezis not a recognised type alias)r   r   get_proper_typetargetrL   r   r7   r   InstancerD   r"   r   rH   r   r   r  	fullmatchescaper  r   	UnionTyper   r   version_info	TupleTyper  r  collectionsabcCallableAnyType)
r   rz   r;   stub_targetr9   runtime_originstub_originruntime_nameruntime_moduleruntime_fullnamer   r   r   verify_typealiasA  sl   







r  >   r#   __file__r    __path____spec__
__author__
__cached__
__loader____getattr____package____version____builtins____copyright____annotations__r   >%   __cmp____del____div__r#   __args____dict____hash__	__final__	__slots__
__origin__
__reduce__r  __nonzero____setattr____unicode____weakref____ctype_be____ctype_le____getstate____setstate____reduce_ex____getnewargs____new_member____orig_bases____parameters____pyx_vtable__r  __getinitargs____subclasshook____instancecheck____subclasscheck____text_signature____abstractmethods____dataclass_fields____dataclass_params____vectorcalloffset____ctypes_from_outparam__r   rK   c                 C  s   |  do	t|  S )Nr   )r   r   rl  r   r   r   r        r   c                 C  s6   t | tjtjfpt | tjtjfpt| ot| S r   )	rL   r   FunctionTypeBuiltinFunctionType
MethodTypeBuiltinMethodTyper\   ismethoddescriptorr|  r   r   r   r   r{    s
   r{  r  c                 C  s   t | to	| jd u S r   )rL   r  fsetr   r   r   r   r    r  r  inspect.Signature | Nonec                 C  s"   zt | W S  ty   Y d S w r   )r\   r>  r   r   r   r   r   r    s
   r  leftmypy.types.Typerightc                 C  s   t j| } t j|}t| t jjr-t| jtr-| jdv r-t|t jjr-|jj	dkr-dS t|t jj
rCt| t jjrC| jj	dkrCdS t jjd t j| |W  d   S 1 s\w   Y  dS )z2Checks whether ``left`` is a subtype of ``right``.)r   rS   zbuiltins.boolTzbuiltins.dictN)r   r   r  rL   LiteralTyper  r/   r  rD   r  TypedDictTyperQ  rR  subtypes
is_subtype)r  r  r   r   r   r    s(   

$r  mypy.types.Type | Nonec                   s  | du r	t j S t| trdS ddd t| tjtjtjtjfrt	d}|dus,J |j
d j}t|tjs:J t j|  g}t| }|rg }g }g }|j D ]t}|   ||jtjjkrgdn|j |jtjjk}	|jtjjkr||	rtjntj qT|jtjjkr||	rtjntj qT|jtjjkr||	rtjntj qT|jtjj kr|tj! qT|jtjj"kr|tj# qTt$n    g}tj!tj#g}ddg}t jj%|||  |dd	S t	t&| j'}
|
du rdS t&| j(}||
j
vrdS |
j
| j}t|tj)r|j&S t|tjsdS t| t*r@d
d | D } fdd|D }t j|  g}t j+||S t j| fdd|j,D }t| t-rZt.| }nt| t/j0re| j}nt| t1t2t3frq| }n|S t jj4||dS )zReturns a mypy type object representing the type of ``runtime``.

    Returns None if we can't find something that works.

    Nr   mypy.types.AnyTypec                   S  s   t jt jjjS r   )r   r   r  	TypeOfAnyunannotatedr   r   r   r   anytype  s   z/get_mypy_type_of_runtime_value.<locals>.anytypebuiltinsfunctionT)r  fallbackis_ellipsis_argsc                 S  s   g | ]}t |qS r   )r	  )r   vr   r   r   r,  W  r-  z2get_mypy_type_of_runtime_value.<locals>.<listcomp>c                   s   g | ]}|d ur
|n  qS r   r   )r   ir  r   r   r,  X  s    c                   s   g | ]}  qS r   r   )r   r   r  r   r   r,  \  rn  )r  r  )r   r  )5r   r   NoneTyperL   r  r  r   r  r  rZ   r   r   r   TypeInfor  r  rA  r/  r7  r  r\   r  r  r   r  r  rY  rW  rB  rC  rZ  rX  rD  r9  rE  r:  r  r  rD   r!   r    rd  r  r  	type_varsbytesr   r   r   rK   r/   r   r	  )rz   r  	type_infor  r>  	arg_types	arg_kinds	arg_namesr  r#  r   	type_nameoptional_itemsr   r  r   r  r   r	    s   





r	  zdict[str, nodes.MypyFile]
_all_stubsr   optionsr   find_submodulesc              
     sV  t j }t jg ||}t jj|d|d}g  g }| D ]Y} | |s<||}t|t	s0q|t j
||d q||}	||	   fdd|	D  zt|}
  fddt|
j|
jd D  W q tyu   Y qw |rz
t jj||d}W n t jjy } ztd| |d}~ww |jrtd	d
|j |ja S )a  Uses mypy to construct stub objects for the given modules.

    This sets global state that ``get_stub`` can access.

    Returns all modules we might want to check. If ``find_submodules`` is False, this is equal
    to ``modules``.

    :param modules: List of modules to build stubs for.
    :param options: Mypy options for finding and building stubs.
    :param find_submodules: Whether to attempt to find submodules of the given modules as well.

    N)fscacher$  c                 3       | ]}|j  vr|j V  qd S r   module)r   r   all_modulesr   r   r     s    zbuild_stubs.<locals>.<genexpr>c                 3  r'  r   rl  r   r*  r   r   r     s    
rC   )sourcesr$  zfailed mypy compile:
zmypy build errors:
rV   )r   builddefault_data_dirmodulefindercompute_search_pathsFindModuleCacher7  find_modulerL   r   BuildSourcefind_modules_recursiveextendr{   pkgutilwalk_packagesr  r    r   errorsCompileErrorr4   rF   filesr#  )r   r$  r%  data_dirsearch_pathfind_module_cacher,  r)  module_pathfound_sourcesrz   resr   r   r*  r   build_stubss  sH   





rA  r)  nodes.MypyFile | Nonec                 C  s
   t | S )z?Returns a stub object for the given module, if we've built one.)r#  r   r(  r   r   r   rZ     s   
rZ   custom_typeshed_dirrA   r  tuple[int, int] | Nonec                   s   t j|  du rtjdd tjdk rdd fd	d
}| r't| }n	tt j d }|d }g }|dD ]$}|j	dkrE|j
}d||jdd |j	f }||r_|| q;t|S )zJReturns a list of stdlib modules in typeshed (for current Python version).Nr   r  )r0      r)  r   r   rK   c                   sr   d usJ |  d}tt|ddD ]#}d|d | }| v r6 | \}}|ko3|d u p3|k  S qdS )NrC   r   r}   F)r   ranger1   rF   )r)  partsr  current_moduleminvermaxverstdlib_py_versionsr  r   r   exists_in_version  s   
z6get_typeshed_stdlib_modules.<locals>.exists_in_versiontypeshedstdlibz*.pyirJ   rC   r}   )r)  r   r   rK   )r   r/  load_stdlib_py_versionsr   r  r   r-  r.  rglobstemparentrF   relative_torG  r7  r   )rC  r  rM  typeshed_dir
stdlib_dirr   r[   r)  r   rK  r   get_typeshed_stdlib_modules  s&   



"
rW  allowlist_filec                 c  sZ    ddd}t | }| D ]}||}|r|V  qW d    d S 1 s&w   Y  d S )Nr   r   r   c                 S  s4   z| d |  d  W S  ty   |   Y S w )N#)rJ  r   
ValueError)r   r   r   r   strip_comments  s
   z-get_allowlist_entries.<locals>.strip_comments)r   r   r   r   )rq   	readlines)rX  r[  frY   r   r   r   r   get_allowlist_entries  s   

"r^  c                   @  sf   e Zd ZU ded< ded< ded< ded< ded< ded< ded	< d
ed< d
ed< ded< d
ed< dS )
_Argumentsr<   r   rK   rO   ignore_missing_stubignore_positional_only	allowlistgenerate_allowlistignore_unused_allowlistr   mypy_config_filerC  check_typeshedversionN)r    r!   r"   r  r   r   r   r   r_    s   
 r_  use_builtins_fixturesc                   s  dd | j D }dd |D }t }| j}| jr9| jr'ttddddd d	S t| j}h d
  fdd|D }|sGttddddd d	S t }d|_	| j|_|jr\t
j| j|_| j|_||_|jrvd!dd}t|||jtjtj zt||| j d}W n  ty } zttddddd|  W Y d}~d	S d}~ww d}	d}
|D ]W}t|D ]P}| jr| rq| jr| rq|j|v rd||j< qd}|D ]}|| |jrd||< d} nq|rqd	}	| jr||j qt|j | j!d |
d	7 }
qq| j"s$|D ]}|| s"|| ds"d	}	|
d	7 }
td|  q| jr8t#|D ]}t| q,d}	|	S | j!sp|
r]ttd|
 dt$|
 dt%| dt$| d	ddd |	S ttdt%| dt$| d dd |	S )"z.This is stubtest! It's time to test the stubs!c                 S  s    i | ]}t |D ]}|d qqS rj   )r^  )r   rX  r   r   r   r   
<dictcomp>  s    ztest_stubs.<locals>.<dictcomp>c                 S  s   i | ]}|t |qS r   )r  compile)r   r   r   r   r   ri        zerror:rT   TrU   z7cannot pass both --check-typeshed and a list of modulesrS   >   thisr   antigravityc                   s   g | ]}| vr|qS r   r   r   annoying_modulesr   r   r,    rk  ztest_stubs.<locals>.<listcomp>zno modules to checkFr   rB   c                   S  s   d S r   r   r   r   r   r   set_strict_flags  r   z$test_stubs.<locals>.set_strict_flags)r%  znot checking stubs due to Nr   )rO   rR   znote: unused allowlist entry zFound z errorz
 (checked z moduler.  zSuccess: no issues found in greenrj  )&rb  r   r   rf  printr-   rW  rC  r   incrementalrr   r[   abspathabs_custom_typeshed_dirre  config_filerh  r   r   stdoutstderrrA  r4   r   r`  rM   ra  rN   rG   r  rc  addri   rO   rd  r   r   r1   )r  rh  rb  allowlist_regexesgenerated_allowlistr   r$  rp  stubtest_failure	exit_codeerror_countr)  erroris_allowlistedrm   r   r   rn  r   
test_stubs  s   




r  c                 C  s   t jdd}|jdddd |jddd	d
 |jdddd
 |jdddd
 |jddddg dd |jddddd
 |jddddd
 |jdddd |jddd d |jd!dd"d
 |jd#d$d%tjj d& |j| t d'S )(Nz8Compares stubs to objects introspected from the runtime.)descriptionr   ro   zModules to test)nargshelpz	--concise
store_truez8Makes stubtest's output more concise, one line per error)actionr  z--ignore-missing-stubzAIgnore errors for stub missing things that are present at runtimez--ignore-positional-onlyzLIgnore errors for whether an argument should or shouldn't be positional-onlyz--allowlistz--whitelistr7  FILEzUse file as an allowlist. Can be passed multiple times to combine multiple allowlists. Allowlists can be created with --generate-allowlist. Allowlists support regular expressions.)r  metavarr  r  z--generate-allowlistz--generate-whitelistz:Print an allowlist (to stdout) to be used with --allowlistz--ignore-unused-allowlistz--ignore-unused-whitelistzIgnore unused allowlist entriesz--mypy-config-filezFUse specified mypy config file to determine mypy plugins and mypy path)r  r  z--custom-typeshed-dirDIRzUse the custom typeshed in DIRz--check-typeshedz$Check all stdlib modules in typeshedz	--versionrg  z	%(prog)s )r  rg  )	namespace)argparseArgumentParseradd_argumentr   rg  r  
parse_argsr_  )r  parserr   r   r   parse_optionsm  sl   r  c                   C  s"   t jd tttjdd  S )NstubtestrS   )r   utilcheck_python_versionr  r  r   argvr   r   r   r   main  s   r  r   )r'   r   r(   r   r   r   )r'   r   r.   r/   r   r   )rk   r   r   rl   )rk   r   r   r|   )r   r>   rz   r@   r;   r<   r   r|   )r;   r<   r   r   r   r   r   r|   )r   r   rz   r   r;   r<   r   r|   )r   r   rz   r   r;   r<   r   r|   )r   r   rz   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   r4  rz   r@  r   r   r   r   )r   r3  rz   r@   r;   r<   r   r|   )r   r   rz   r@   r;   r<   r   r|   )r   r  rz   r@   r;   r<   r   r|   )r   rH  rz   r@   r;   r<   r   r|   )r   r  rz   r@   r;   r<   r   r|   )r   r  rz   r@   r;   r<   r   r|   )r   r  rz   r   r   r   )r  r  r   r  )r   r  rz   r@   r;   r<   r   r|   )r   r  rz   r@   r;   r<   r   r|   )r   r   r   rK   )rz   r   r   rK   )rz   r  r   rK   )rz   r   r   r  )r  r  r  r  r   rK   )rz   r   r   r  rj   )r   r<   r$  r   r%  rK   r   r<   )r)  r   r   rB  r   )rC  rA   r  rD  r   r<   )rX  r   r   r   )r  r_  rh  rK   r   r/   )r  r<   r   r_  )r   r/   )rr#   
__future__r   r  collections.abcr  r  r   rw   r\   rr   r6  r  r   r   r   r  r  rt   
contextlibr   r   	functoolsr   pathlibr   r   r   r	   r
   r   r   r   
mypy.buildr   mypy.modulefinder
mypy.state
mypy.typesmypy.versionr   mypy.config_parserr   mypy.optionsr   	mypy.utilr   r   r   r   r   r   r  r$   r%   rw  rx  r&   r-   r2   r   r4   r7   r{   r   r   r   registerr   r   r  r   r   r  r  r  r  ru  r  r  r  rd  r  OverloadedFuncDefr  TypeVarExprr  ParamSpecExprr  r  rb  	Decoratorr  	TypeAliasr  	frozensetr   r   r   r{  r  r  r  r	  r#  rA  rZ   rW  r^  r_  r  r  r  r    exitr   r   r   r   <module>   s     


g
1


E
K
%
!
)	 
%
f@

$
-



0
L
4




e
;
%
y
>
