a
    ž˜ëjr   ã                   @  sö  U d dl m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Zd dlmZ dejv Zer€edvr€d	Zejje d
d�Zzd dlmZ W n ey¸   d	ZY n0 dZerÎd dlmZ ej ej e¡d¡Zej ed¡Z ej ed¡Z!ej ed¡Z"ej ed¡Z#ej ed¡Z$ej ed¡Z%ee&ƒZ'de(d< dddœdd„Z)dddœdd„Z*d dd!œd"d#„Z+ej,d$d	d%�d&d'œd(d)„ƒZ-dd*d+œd,d-„Z.ejj/ejjed.d�ej 0d/e.e ƒ¡d&d'œd0d1„ƒƒƒZ1ejj/ejjed.d�ej 0d/e.e!ƒ¡dd&d2œd3d4„ƒƒƒZ2d5Z3d6Z4dddd7d8d&d9œd:d;„Z5d<Z6ejj/ejjed.d�ej 0d/e.e"ƒ¡dd&d2œd=d>„ƒƒƒZ7ejj/ejjed.d�ej 0d/e.e ƒ¡dd&d2œd?d@„ƒƒƒZ8dAdBdCdDdEdFdGdHdIdJdKdLdMœZ9ejj/ejjed.d�d&d'œdNdO„ƒƒZ:dS )Pé    )ÚannotationsN)Údefaultdict)ÚIterator)ÚTYPE_CHECKING)Ú_EXTENDED_PRECISION_LISTZNPY_RUN_MYPY_IN_TESTSUITE)Ú0Ú ÚfalseTz#`NPY_RUN_MYPY_IN_TESTSUITE` not set)Úreason)ÚapiF)ÚParameterSetÚdataÚpassÚfailZrevealÚmisczmypy.iniz.mypy_cachezdefaultdict[str, list[str]]ÚOUTPUT_MYPYÚstr)ÚkeyÚreturnc                 C  s*   t j | ¡\}}t j || dd¡d ¡S )zySplit at the first occurrence of the ``:`` character.

    Windows drive-letters (*e.g.* ``C:``) are ignored herein.
    ú:é   r   )ÚosÚpathÚ
splitdriveÚjoinÚsplit)r   ÚdriveÚtail© r   ú]/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/numpy/typing/tests/test_typing.pyÚ	_key_func6   s    r    ztuple[int, str])Úmsgr   c                 C  s2   t j | ¡\}}| dd¡\}}} t|ƒ|  ¡ fS )z7Strip the filename and line number from a mypy message.r   é   )r   r   r   r   ÚintÚstrip)r!   Ú_r   Úlinenor   r   r   Ú_strip_filename?   s    r'   zre.Match[str])Úmatchr   c                 C  s   |   ¡ d S )z4`re.sub` helper function for stripping module names.r   )Úgroups)r(   r   r   r   Ú
strip_funcF   s    r*   Úmodule)ZscopeZautouseÚNone)r   c                  C  sú   t j t¡r(tt j dd¡ƒr(t t¡ t	 
d¡} ttttfD ]¶}t dtdt|g¡\}}}|rrt d|› �¡ n|dvr�t d|› d	|› �¡ d
}d}| d¡D ]P}d|v r°q¢|du rÀt|ƒ}||› d�7 }|  |¡dur¢t|  |¡ d
}d}q¢q>dS )zøClears the cache and run mypy before running any of the typing tests.

    The mypy results are cached in `OUTPUT_MYPY` for further use.

    The cache refresh can be skipped using

    NUMPY_TYPING_TEST_CLEAR_CACHE=0 pytest numpy/typing/tests
    ZNUMPY_TYPING_TEST_CLEAR_CACHETz(\s+)?\^(\~+)?z--config-filez--cache-dirz Unexpected mypy standard error

>   r   r   zUnexpected mypy exit code: ú

r   NÚ
znote:)r   r   ÚisdirÚ	CACHE_DIRÚboolÚenvironÚgetÚshutilÚrmtreeÚreÚcompileÚPASS_DIRÚ
REVEAL_DIRÚFAIL_DIRÚMISC_DIRr   ÚrunÚMYPY_INIÚpytestr   r   r    r(   r   Úappend)Zsplit_patternÚ	directoryÚstdoutÚstderrZ	exit_codeZ
str_concatÚfilenameÚir   r   r   Úrun_mypyK   s<    
ÿþ

ûrE   zIterator[ParameterSet])r@   r   c                 c  sZ   t  | ¡D ]J\}}}|D ]:}t j |¡\}}|dv rt j ||¡}tj||d�V  qq
d S )N)z.pyiz.py)Úid)r   Úwalkr   Úsplitextr   r>   Úparam)r@   Úrootr%   ÚfilesÚfnameZshort_fnameÚextÚfullpathr   r   r   Úget_test_casesy   s    rO   zMypy is not installedr   c                 C  s8   t }| |v r4d}|d dd„ ||  D ƒ¡7 }t|ƒ‚d S )NzUnexpected mypy output

r.   c                 s  s   | ]}t |ƒd  V  qdS )r   N)r'   )Ú.0Úvr   r   r   Ú	<genexpr>Š   ó    ztest_success.<locals>.<genexpr>)r   r   ÚAssertionError)r   Úoutput_mypyr!   r   r   r   Útest_success‚   s
    rV   )r   r   c                 C  s  d}t | ƒ�}| ¡ }W d   ƒ n1 s*0    Y  tdd„ ƒ}t}| |v sPJ ‚||  D ]&}t|ƒ\}}||  |› d�7  < qXt|ƒD ]�\}}	|d }|	 d¡sˆd|	vr´||vr´qˆ||d  }
d|
v rþ|
 d	¡\}}}||  ¡ }| ¡ }t	| ||||ƒ qˆt
 d
|› d|| › �¡ qˆd S )NTc                   S  s   dS )Nr   r   r   r   r   r   Ú<lambda>—   rS   ztest_fail.<locals>.<lambda>r.   r   ú#z E:z# E:z  # E: zUnexpected mypy output at line r-   )ÚopenÚ	readlinesr   r   r'   Ú	enumerateÚ
startswithÚ	partitionr$   Ú
_test_failr>   r   )r   Ú__tracebackhide__ZfinÚlinesÚerrorsrU   Ú
error_liner&   rD   ÚlineZtarget_lineÚ
expressionr%   ÚmarkerÚexpected_errorr   r   r   Ú	test_failŽ   s6    
&ÿþþÿrg   z9Extra error at line {}

Expression: {}
Extra error: {!r}
zRError mismatch at line {}

Expression: {}
Expected error: {}
Observed error: {!r}
z
None | strr#   )r   rd   Úerrorrf   r&   r   c                 C  s<   |d u rt t |||¡ƒ‚n||vr8t t ||||¡ƒ‚d S )N)rT   Ú
_FAIL_MSG1ÚformatÚ
_FAIL_MSG2)r   rd   rh   rf   r&   r   r   r   r^   Â   s    ÿr^   zReveal mismatch at line {}

{}
c                 C  sB   d}t }| |vrdS ||  D ] }t|ƒ\}}tt ||¡ƒ‚qdS )z[Validate that mypy correctly infers the return-types of
    the expressions in `path`.
    TN)r   r'   rT   Ú_REVEAL_MSGrj   )r   r_   rU   rb   r&   r   r   r   Útest_reveal×   s    rm   c                 C  sv   t j | ¡\}}|  t j¡dd… \}}tj |› d|› �| ¡}|dusLJ ‚|jdusZJ ‚tj 	|¡}|j 
|¡ dS )z9Validate that the code in `path` properly during runtime.éþÿÿÿNÚ.)r   r   rH   r   ÚsepÚ	importlibÚutilÚspec_from_file_locationÚloaderÚmodule_from_specÚexec_module)r   Zpath_without_extensionr%   ÚdirnamerC   ÚspecZtest_moduler   r   r   Útest_code_runsé   s    ÿry   Zuint128Zuint256Zint128Zint256Zfloat80Zfloat96Zfloat128Zfloat256Z
complex160Z
complex192Z
complex256Z
complex512)é   é   é   é   é   é   é   é   é   é   é   é   c                  C  s´   t j td¡} t}| |v sJ ‚t| ƒ�}| ¡ }W d   ƒ n1 sD0    Y  ||  D ]X}t|ƒ\}}||d   d¡}t	| t
v r–tt ||¡ƒ‚qVd|vrVt| ||d|ƒ qVd S )Nzextended_precision.pyir   r.   rh   zExpression is of type "Any")r   r   r   r;   r   rY   rZ   r'   ÚrstripÚLINENO_MAPPINGr   rT   rl   rj   r^   )r   rU   ÚfZexpression_listÚ_msgr&   r!   rd   r   r   r   Útest_extended_precision  s    
&
ÿrŠ   );Ú
__future__r   Úimportlib.utilrq   r   r6   r4   Úcollectionsr   Úcollections.abcr   Útypingr   r>   Znumpy.typing.mypy_pluginr   r2   ZRUN_MYPYÚmarkZskipifZ
pytestmarkZmypyr   ÚImportErrorZNO_MYPYZ_pytest.mark.structuresr   r   r   rw   Ú__file__ZDATA_DIRr8   r:   r9   r;   r=   r0   Úlistr   Ú__annotations__r    r'   r*   ZfixturerE   rO   ZslowZparametrizerV   rg   ri   rk   r^   rl   rm   ry   r‡   rŠ   r   r   r   r   Ú<module>   s’   
þ
	-		#ô