U
    ;vfO                  
   @  s  U d Z ddlmZ ddlmZ ddlmZmZmZ ddl	Z
zbddlZddlmZ ddlmZmZ ddlmZmZmZ dd	lmZ eegef ZdZd
ed< W n& ek
r Z zeZW 5 dZ[X Y nX g Zded< ddddZddddZddddZe Z ded< e Z!ded< e Z"ded< dddddZ#esDedkrd dd!d"d#d$Z$d%dd&d'd(d)d*Z%G d+d, d,eZ&dd-d.d/d0Z'ndd-d.d1d0Z'dS )2a  A mypy_ plugin for managing a number of platform-specific annotations.
Its functionality can be split into three distinct parts:

* Assigning the (platform-dependent) precisions of certain `~numpy.number`
  subclasses, including the likes of `~numpy.int_`, `~numpy.intp` and
  `~numpy.longlong`. See the documentation on
  :ref:`scalar types <arrays.scalars.built-in>` for a comprehensive overview
  of the affected classes. Without the plugin the precision of all relevant
  classes will be inferred as `~typing.Any`.
* Removing all extended-precision `~numpy.number` subclasses that are
  unavailable for the platform in question. Most notably this includes the
  likes of `~numpy.float128` and `~numpy.complex256`. Without the plugin *all*
  extended-precision types will, as far as mypy is concerned, be available
  to all platforms.
* Assigning the (platform-dependent) precision of `~numpy.ctypeslib.c_intp`.
  Without the plugin the type will default to `ctypes.c_int64`.

  .. versionadded:: 1.22

Examples
--------
To enable the plugin, one must add it to their mypy `configuration file`_:

.. code-block:: ini

    [mypy]
    plugins = numpy.typing.mypy_plugin

.. _mypy: http://mypy-lang.org/
.. _configuration file: https://mypy.readthedocs.io/en/stable/config_file.html

    )annotations)Iterable)FinalTYPE_CHECKINGCallableN)Type)PluginAnalyzeTypeContext)MypyFile
ImportFrom	Statement)PRI_MEDzNone | ModuleNotFoundErrorMYPY_EXz	list[str]__all__zdict[str, str])returnc                  C  s   dt jfdt jfdt jfdt jfdt jfdt jfdt jfdt jfd	t j	fd
t j
fg
} i }| D ],\}}d| jj }d| d|d| < q\|S )NZ	_NBitByteZ
_NBitShortZ	_NBitIntCZ	_NBitIntPZ_NBitIntZ_NBitLongLongZ	_NBitHalfZ_NBitSingleZ_NBitDoubleZ_NBitLongDouble   znumpy._ZBitnumpy._typing._nbit.)npbyteshortZintcZintpint_longlongZhalfZsingledouble
longdoubledtypeitemsize)namesretnametypn r!   </tmp/pip-unpacked-wheel-sdaoq5e2/numpy/typing/mypy_plugin.py_get_precision_dict8   s     r#   c                    sB   t jt jt jt jg} ddddddddd	d
ddh  fdd| D S )NZuint128Zuint256Zint128Zint256Zfloat80Zfloat96Zfloat128Zfloat256Z
complex160Z
complex192Z
complex256Z
complex512c                   s   g | ]}|j  kr|j qS r!   )__name__).0iZextended_namesr!   r"   
<listcomp>]   s     
 z0_get_extended_precision_list.<locals>.<listcomp>)r   Z	ulonglongr   r   Zclongdouble)Zextended_typesr!   r'   r"   _get_extended_precision_listM   s    r)   strc                  C  s8   t dj} | dkrdS | dkr$dS | dkr0dS dS d S )Npr&   c_intlc_longq
c_longlong)r   r   char)r1   r!   r!   r"   _get_c_intp_name`   s    r2   r   _PRECISION_DICT_EXTENDED_PRECISION_LIST_C_INTPr	   r   )ctxr   c                 C  s2   | \}}}|j dd }td|  }||S )z;Replace a type-alias with a concrete ``NBitBase`` subclass..r   )r   splitr3   Z
named_type)r6   r   _apir   Zname_newr!   r!   r"   _hookx   s    
r<   zIterable[Statement]int)iterableidr   c                 C  s<   t | D ] \}}t|dd|kr|  S qtd|dS )z>Identify the first ``ImportFrom`` instance the specified `id`.r?   NzBFailed to identify a `ImportFrom` instance with the following id: )	enumerategetattr
ValueError)r>   r?   r&   valuer!   r!   r"   _index   s    
rD   r
   zlist[tuple[str, None | str]]None)filemoduleimportsr   c                 C  s<   t |d|d}d|_| j| jfD ]}t||}|||< q dS )z<Override the first `module`-based import with new `imports`.r   )r   TN)r   Zis_top_levelZdefsrH   rD   )rF   rG   rH   Z
import_objlstr&   r!   r!   r"   _override_imports   s
    
rJ   c                   @  s0   e Zd ZdZdddddZddd	d
dZdS )_NumpyPluginz>A mypy plugin for handling versus numpy-specific typing tasks.r*   zNone | _HookFunc)fullnamer   c                 C  s   |t krtS dS )zSet the precision of platform-specific `numpy.number`
            subclasses.

            For example: `numpy.int_`, `numpy.longlong` and `numpy.longdouble`.
            N)r3   r<   )selfrL   r!   r!   r"   get_type_analyze_hook   s    z"_NumpyPlugin.get_type_analyze_hookr
   zlist[tuple[int, str, int]])rF   r   c                 C  sT   t |jdfg}|jdkr2t|ddd tD d n|jdkrPt|dtd	fgd |S )
a.  Handle all import-based overrides.

            * Import platform-specific extended-precision `numpy.number`
              subclasses (*e.g.* `numpy.float96`, `numpy.float128` and
              `numpy.complex256`).
            * Import the appropriate `ctypes` equivalent to `numpy.intp`.

            r8   numpyz!numpy._typing._extended_precisionc                 S  s   g | ]}||fqS r!   r!   )r%   vr!   r!   r"   r(      s     z4_NumpyPlugin.get_additional_deps.<locals>.<listcomp>)rH   znumpy.ctypeslibctypesZ_c_intp)r   rL   rJ   r4   r5   )rM   rF   r   r!   r!   r"   get_additional_deps   s    
 
 z _NumpyPlugin.get_additional_depsN)r$   
__module____qualname____doc__rN   rR   r!   r!   r!   r"   rK      s   
rK   ztype[_NumpyPlugin])versionr   c                 C  s   t S )An entry-point for mypy.)rK   rV   r!   r!   r"   plugin   s    rY   c                 C  s   t dS )rW   N)r   rX   r!   r!   r"   rY      s    )(rU   
__future__r   collections.abcr   typingr   r   r   rO   r   Z
mypy.typesZmypyr   Zmypy.pluginr   r	   Z
mypy.nodesr
   r   r   Z
mypy.buildr   Z	_HookFuncr   __annotations__ModuleNotFoundErrorexr   r#   r)   r2   r3   r4   r5   r<   rD   rJ   rK   rY   r!   r!   r!   r"   <module>   s8   !&