a
    ³ÐOcÐ;  ã                   @  sn  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Zd dlm	Z	 d dl
mZ d dlmZmZ d dlZd dlZd dlmZ d dlmZmZm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(i Z)de*d< dddœdd„Z+dddœdd„Z,dddœdd„Z-ej.j/ej.j0edd �ej1d!dd"�d#d$œd%d&„ƒƒƒZ2dd'd(œd)d*„Z3ej.j/ej.j0edd �ej. 4d+e3e#ƒ¡d#d$œd,d-„ƒƒƒZ5ej.j/ej.j0edd �ej. 4d+e3e$ƒ¡dd#d.œd/d0„ƒƒƒZ6d1Z7d2Z8dddd3d4d#d5œd6d7„Z9d8d$œd9d:„Z:d8d$œd;d<„Z;e;ƒ Z<d8e*d=< e< =e:ƒ ¡ d>d?d@œdAdB„Z>ej.j/ej.j0edd �ej. 4d+e3e%ƒ¡dd#d.œdCdD„ƒƒƒZ?dEZ@e AdF¡ZBddddd4d#dGœdHdI„ZCej.j/ej.j0edd �ej. 4d+e3e#ƒ¡dd#d.œdJdK„ƒƒƒZDdLdMdNdOdPdQdRdSdTdUdVdWdXœZEej.j/ej.j0edd �d#d$œdYdZ„ƒƒZFdS )[é    )ÚannotationsN)Údefaultdict)ÚIterator)ÚIOÚTYPE_CHECKING)Ú_PRECISION_DICTÚ_EXTENDED_PRECISION_LISTÚ_C_INTP)ÚapiTF)ÚParameterSetÚdataÚpassÚfailÚrevealÚmisczmypy.iniz.mypy_cachezdict[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   úU/var/www/html/gsl.tools/lib/python3.9/site-packages/numpy/typing/tests/test_typing.pyÚ	_key_func.   s    r    )Úmsgr   c                 C  s    t j | ¡\}}| dd¡d S )z'Strip the filename from a mypy message.r   r   éÿÿÿÿ)r   r   r   r   )r!   Ú_r   r   r   r   Ú_strip_filename7   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_func=   s    r'   zMypy is not installed)ÚreasonÚmodule)ZscopeZautouseÚNone)r   c                  C  sÀ   t j t¡r(tt j dd¡ƒr(t t¡ t	t
ttfD ]†} t dtdt| g¡\}}}|rht d|› �¡ n|dvr†t d|› d|› �¡ | d	d
¡}tj| d¡td�}t dd„ |D ƒ¡ q4d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--config-filez--cache-dirz Unexpected mypy standard error

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

Ú*Ú Ú
)r   c                 s  s"   | ]\}}|r|t |ƒfV  qd S ©N)Úlist©Ú.0ÚkÚvr   r   r   Ú	<genexpr>e   ó    zrun_mypy.<locals>.<genexpr>N)r   r   ÚisdirÚ	CACHE_DIRÚboolÚenvironÚgetÚshutilÚrmtreeÚPASS_DIRÚ
REVEAL_DIRÚFAIL_DIRÚMISC_DIRr
   ÚrunÚMYPY_INIÚpytestr   ÚreplaceÚ	itertoolsÚgroupbyr   r    r   Úupdate)Ú	directoryÚstdoutÚstderrZ	exit_codeÚiteratorr   r   r   Úrun_mypyB   s(    
ÿþ
ûrM   zIterator[ParameterSet])rI   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   rD   Úparam)rI   Úrootr#   ÚfilesÚfnameZshort_fnameÚextÚfullpathr   r   r   Úget_test_casesh   s    rW   r   c                 C  s8   t }| |v r4d}|d dd„ ||  D ƒ¡7 }t|ƒ‚d S )NzUnexpected mypy output

r.   c                 s  s   | ]}t |ƒV  qd S r/   )r$   )r2   r4   r   r   r   r5   y   r6   ztest_success.<locals>.<genexpr>)r   r   ÚAssertionError)r   Úoutput_mypyr!   r   r   r   Útest_successq   s
    rZ   )r   r   c                 C  sX  d}t | ƒ�}| ¡ }W d   ƒ n1 s*0    Y  tdd„ ƒ}t}| |v sPJ ‚||  D ]^}t|ƒ dd¡d }t d|¡}|d u r’td|› �ƒ‚t	| 
d	¡ƒ}||  |› d�7  < qXt|ƒD ]’\}	}
|	d }|
 d
¡sÀd|
vrì||vrìqÀ||d  }d|v �r8| d¡\}}}||  ¡ }| ¡ }t| ||||ƒ qÀt d|› d|| › �¡ qÀd S )NTc                   S  s   dS )Nr-   r   r   r   r   r   Ú<lambda>†   r6   ztest_fail.<locals>.<lambda>r.   r   r   z"(?P<lineno>\d+): (error|note): .+$zUnexpected error line format: Úlinenoú#z E:z# E:ú  # E: zUnexpected mypy output at line r+   )ÚopenÚ	readlinesr   r   r$   r   Úrer%   Ú
ValueErrorÚintÚgroupÚ	enumerateÚ
startswithÚ	partitionÚstripÚ
_test_failrD   r   )r   Ú__tracebackhide__ÚfinÚlinesÚerrorsrY   Ú
error_liner%   r\   ÚiÚlineZtarget_lineÚ
expressionr#   ÚmarkerÚexpected_errorr   r   r   Ú	test_fail}   sD    
&þÿþþ
ÿrt   z9Extra error at line {}

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

Expression: {}
Expected error: {!r}
Observed error: {!r}
z
None | strrc   )r   rq   Úerrorrs   r\   r   c                 C  s<   |d u rt t |||¡ƒ‚n||vr8t t ||||¡ƒ‚d S r/   )rX   Ú
_FAIL_MSG1ÚformatÚ
_FAIL_MSG2)r   rq   ru   rs   r\   r   r   r   ri   ·   s    ÿri   zdict[str, str]c                  C  sp   ddddddddd	d
ddddœ} i }i }|   ¡ D ]:\}}tt|ƒƒ }|jj|jjf}| |d|› �¡||< q0|S )NÚc_ubyteÚc_ushortÚc_uintÚc_ulongÚc_ulonglongÚc_byteÚc_shortÚc_intÚc_longÚ
c_longlongÚc_floatÚc_doubleÚc_longdouble)ÚubyteÚushortÚuintcÚuintÚ	ulonglongÚbyteÚshortÚintcÚint_ÚlonglongÚsingleÚdoubleÚ
longdoubleúctypes.)ÚitemsÚgetattrÚnpZdtypeÚkindÚitemsizeÚ
setdefault)ÚdctÚretÚvisitedZnp_nameZct_nameZ	np_scalarr   r   r   r   Ú_construct_ctypes_dictÆ   s*    ór�   c               0   C  s€  dd„ t  ¡ D ƒ} ddddddd	d
dddddddddddddddddd| d › d�d| d › d�d| d  › d�d| d! › d�d| d" › d�d| d# › d�d$| d › d�d$| d › d�d$| d  › d�d$| d! › d�d$| d" › d�d$| d# › d�d%| d& › d�d%| d' › d�d%| d( › d�d%| d) › d�d*| d' › d+| d' › d�d*| d( › d+| d( › d�d*| d) › d+| d) › d�| d" d,t› �d-œ.S ).Nc                 S  s(   i | ] \}}|  d ¡d | dd¡“qS )Ú.r"   Únumpyznumpy._typing)r   rE   r1   r   r   r   Ú
<dictcomp>ç   s   ÿz*_construct_format_dict.<locals>.<dictcomp>z*numpy.unsignedinteger[numpy._typing._8Bit]z+numpy.unsignedinteger[numpy._typing._16Bit]z+numpy.unsignedinteger[numpy._typing._32Bit]z+numpy.unsignedinteger[numpy._typing._64Bit]z,numpy.unsignedinteger[numpy._typing._128Bit]z,numpy.unsignedinteger[numpy._typing._256Bit]z(numpy.signedinteger[numpy._typing._8Bit]z)numpy.signedinteger[numpy._typing._16Bit]z)numpy.signedinteger[numpy._typing._32Bit]z)numpy.signedinteger[numpy._typing._64Bit]z*numpy.signedinteger[numpy._typing._128Bit]z*numpy.signedinteger[numpy._typing._256Bit]z$numpy.floating[numpy._typing._16Bit]z$numpy.floating[numpy._typing._32Bit]z$numpy.floating[numpy._typing._64Bit]z$numpy.floating[numpy._typing._80Bit]z$numpy.floating[numpy._typing._96Bit]z%numpy.floating[numpy._typing._128Bit]z%numpy.floating[numpy._typing._256Bit]zAnumpy.complexfloating[numpy._typing._32Bit, numpy._typing._32Bit]zAnumpy.complexfloating[numpy._typing._64Bit, numpy._typing._64Bit]zAnumpy.complexfloating[numpy._typing._80Bit, numpy._typing._80Bit]zAnumpy.complexfloating[numpy._typing._96Bit, numpy._typing._96Bit]zCnumpy.complexfloating[numpy._typing._128Bit, numpy._typing._128Bit]zCnumpy.complexfloating[numpy._typing._256Bit, numpy._typing._256Bit]znumpy.unsignedinteger[Z	_NBitByteú]Z
_NBitShortZ	_NBitIntCZ	_NBitIntPÚ_NBitIntZ_NBitLongLongznumpy.signedinteger[znumpy.floating[Z	_NBitHalfZ_NBitSingleZ_NBitDoubleZ_NBitLongDoubleznumpy.complexfloating[z, r“   ).Zuint8Zuint16Zuint32Zuint64Úuint128Úuint256Zint8Úint16Úint32Úint64Úint128Úint256Úfloat16Úfloat32Úfloat64Úfloat80Úfloat96Úfloat128Úfloat256Z	complex64Z
complex128Ú
complex160Ú
complex192Ú
complex256Ú
complex512r†   r‡   rˆ   Zuintpr‰   rŠ   r‹   rŒ   r�   ZintprŽ   r�   Zhalfr�   r‘   r’   ZcsingleZcdoubleZclongdoubler¢   Zc_intp)r   r”   r	   )rš   r   r   r   Ú_construct_format_dictæ   s|    ÿÿÿÿÿÿÿÁrµ   ÚFORMAT_DICTzIO[str]z&tuple[npt.NDArray[np.str_], list[str]])Úfiler   c           	      C  st   |   ¡  dd¡}tjj| d¡dd�j\}}}d |¡}tt	 
d|¡ƒ}dd	„ |D ƒ}|jf i |¤Ž}|| d¡fS )
a  Extract and parse all ``"  # E: "`` comments from the passed
    file-like object.

    All format keys will be substituted for their respective value
    from `FORMAT_DICT`, *e.g.* ``"{float64}"`` becomes
    ``"numpy.floating[numpy._typing._64Bit]"``.
    r,   r-   r.   r^   )Úsepz/nz	\{(.*?)\}c              	   S  s"   i | ]}|t  |d |›d�¡“qS )z<UNRECOGNIZED FORMAT KEY ú>)r¶   r;   )r2   r3   r   r   r   r    F  s   ÿz"_parse_reveals.<locals>.<dictcomp>)ÚreadrE   r–   Úcharrg   r   ÚTr   Úsetra   Úfindallrw   )	r·   ÚstringÚexpression_arrayr#   Zcomments_arrayÚcommentsZkey_setÚkwargsZfmt_strr   r   r   Ú_parse_reveals3  s    
ÿ
þrÃ   c                 C  sÆ   d}t | ƒ�}t|ƒ\}}W d  ƒ n1 s.0    Y  t}| |v sHJ ‚||  D ]p}t|ƒ}t d|¡}|du r~td|› �ƒ‚t| d¡ƒd }d|v sœJ ‚|| }	|| }
t	| |
|	|d| ƒ qPdS )z[Validate that mypy correctly infers the return-types of
    the expressions in `path`.
    TNz(?P<lineno>\d+): note: .+$úUnexpected reveal line format: r\   r   zRevealed type is)
r_   rÃ   r   r$   ra   r%   rb   rc   rd   Ú_test_reveal)r   rj   rk   rÀ   Zreveal_listrY   rn   r%   r\   rr   rq   r   r   r   Útest_revealO  s$    
*þrÆ   zWReveal mismatch at line {}

Expression: {}
Expected reveal: {!r}
Observed reveal: {!r}
z(\w+\.)+(\w+))r   rq   r   Úexpected_revealr\   r   c                 C  s8   t  t|¡}t  t|¡}||vr4tt ||||¡ƒ‚dS )z2Error-reporting helper function for `test_reveal`.N)Ú_STRIP_PATTERNÚsubr'   rX   Ú_REVEAL_MSGrw   )r   rq   r   rÇ   r\   Zstripped_revealZstripped_expected_revealr   r   r   rÅ   v  s    ýÿrÅ   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.éþÿÿÿNrž   )r   r   rP   r   r¸   Ú	importlibÚutilÚspec_from_file_locationÚloaderÚmodule_from_specÚexec_module)r   Zpath_without_extensionr#   ÚdirnameÚfilenameÚspecZtest_moduler   r   r   Útest_code_runs‰  s    ÿrÕ   r£   r¤   r¨   r©   r­   r®   r¯   r°   r±   r²   r³   r´   )é   é   é   é   é	   é
   é   é   é   é   é   é   c                  C  s
  t j td¡} t}| |v sJ ‚t| dƒ�}| ¡ }W d   ƒ n1 sF0    Y  ||  D ]¬}| d¡�^ }}}}t|ƒ}t	|ƒ}	||	d   
d¡}
| ¡ }|dv s¦J ‚t|	 tv rè|dkrÊtd|	› �ƒ‚ntt|	  }t| |
|||	ƒ qX|dkrXd	}t| |
|||	ƒ qXd S )
Nzextended_precision.pyiÚrr   r   r.   >   ru   Znoteru   rÄ   zModule has no attribute)r   r   r   rA   r   r_   r`   r   r$   rc   Úrstriprh   ÚLINENO_MAPPINGr   rb   r¶   rÅ   ri   )r   rY   ÚfZexpression_listÚ_msgr#   Z_linenoZmsg_typr!   r\   rq   rr   r   r   r   Útest_extended_precision«  s(    &rç   )GÚ
__future__r   Úimportlib.utilrÌ   rF   r   ra   r<   Úcollectionsr   Úcollections.abcr   Útypingr   r   rD   rŸ   r–   Znumpy.typingZnptZnumpy.typing.mypy_pluginr   r   r	   Zmypyr
   ÚImportErrorZNO_MYPYZ_pytest.mark.structuresr   r   r   rÒ   Ú__file__ZDATA_DIRr>   r@   r?   rA   rC   r8   r   Ú__annotations__r    r$   r'   ÚmarkZslowZskipifZfixturerM   rW   ZparametrizerZ   rt   rv   rx   ri   r�   rµ   r¶   rH   rÃ   rÆ   rÊ   ÚcompilerÈ   rÅ   rÕ   rä   rç   r   r   r   r   Ú<module>   sœ   
	#		) I
ô