U
    b;                     @  sp  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k
r   dZY nX d	Zerd 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   ^/home/fireinfo/NEWAFireInfo/venv/lib/python3.8/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krtd| d|  |d	d
}tj|dtd}tdd |D  q4dS )zClears 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   s      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(    

rL   zIterator[ParameterSet])rH   r   c                 c  sZ   t | D ]J\}}}|D ]:}t j|\}}|dkrt j||}tj||dV  qq
d S )N)z.pyiz.py)id)r   walkr   splitextr   rC   param)rH   rootr#   filesfnameZshort_fnameextfullpathr   r   r   get_test_casesh   s    rV   r   c                 C  s8   t }| |k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   s     ztest_success.<locals>.<genexpr>)r   r   AssertionError)r   output_mypyr!   r   r   r   test_successq   s
    rY   )r   r   c              	   C  sD  d}t | }| }W 5 Q R X tdd }t}| |ks<t||  D ]^}t|ddd }td|}|d kr~t	d| t
|d	}||  | d7  < qDt|D ]\}	}
|	d }|
d
sd|
kr||krq||d  }d|kr$|d\}}}||  }| }t| |||| qtd| d||   qd S )NTc                   S  s   dS )Nr-   r   r   r   r   r   <lambda>       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   rW   r$   r   rer%   
ValueErrorintgroup	enumerate
startswith	partitionstrip
_test_failrC   r   )r   __tracebackhide__finlineserrorsrX   
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 krt t|||n||kr8t t||||d S r/   )rW   
_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   rD   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int8Zint16Zint32Zint64int128int256Zfloat16Zfloat32Z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   sd    r   FORMAT_DICTzIO[str]z&tuple[npt.NDArray[np.str_], list[str]])filer   c           	      C  sp   |   dd}tjj|dddj\}}}d|}tt	
d|}dd	 |D }|jf |}||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>)readrD   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 5 Q R X t}| |ks4t||  D ]p}t|}td|}|dkrjtd| t|	dd }d|kst|| }	|| }
t
| |
|	|d|  q<dS )z[Validate that mypy correctly infers the return-types of
    the expressions in `path`.
    Tz(?P<lineno>\d+): note: .+$NUnexpected reveal line format: r\   r   zRevealed type is)r_   r   r   rW   r$   ra   r%   rb   rc   rd   _test_reveal)r   rj   rk   r   Zreveal_listrX   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|}||kr4tt||||dS )z2Error-reporting helper function for `test_reveal`.N)_STRIP_PATTERNsubr'   rW   _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k	sLt|j	dk	sZttj
|}|j	| dS )z9Validate that the code in `path` properly during runtime.Nr   )r   r   rO   r   r   	importlibutilspec_from_file_locationrW   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}| |kstt| d}| }W 5 Q R X ||  D ]}|d^ }}}}t	|}t
|}	||	d  d}
| }|dkstt|	 tkr|dkrtd|	 qtt|	  }t| |
|||	 qD|dkrDd	}t| |
|||	 qDd S )
Nzextended_precision.pyirr   r   r.   >   ru   noteru   r   zModule has no attribute)r   r   r   r@   r   rW   r_   r`   r   r$   rc   rstriprh   LINENO_MAPPINGr   rb   r   r   ri   )r   rX   fZexpression_list_msgr#   Z_linenoZmsg_typr!   r\   rq   rr   r   r   r   test_extended_precision  s(    r   )G
__future__r   importlib.utilr   rE   r   ra   r;   collectionsr   collections.abcr   typingr   r   rC   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>   r@   rB   r7   r   __annotations__r    r$   r'   markZslowZskipifZfixturerL   rV   ZparametrizerY   rt   rv   rx   ri   r   r   r   rG   r   r   r   compiler   r   r   r   r   r   r   r   r   <module>   s    
	#		) I
