1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
U
P±dã
@s UdZddlmZddlmZddlmZmZmZddl    Z
zbddl Z ddl m Z ddlmZmZddlmZmZmZdd    lmZeege fZdZd
ed <Wn&ek
rÂZzeZW5dZ[XYnXgZd ed <ddœdd„Zd dœdd„Zddœdd„ZeƒZ ded<eƒZ!ded<eƒZ"ded<dddœdd„Z#esDedkrŒd dd!d"œd#d$„Z$d%dd&d'd(œd)d*„Z%Gd+d,„d,eƒZ&dd-d.œd/d0„Z'ndd-d.œd1d0„Z'dS)2aA 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_MEDzNone | ModuleNotFoundErrorÚMYPY_EXz    list[str]Ú__all__zdict[str, str])Úreturnc CsŽdtjfdtjfdtjfdtjfdtjfdtjfdtjfdtjfd    tj    fd
tj
fg
}i}|D],\}}d |ƒj j }d |›d |d|›<q\|S)NZ    _NBitByteZ
_NBitShortZ    _NBitIntCZ    _NBitIntPZ_NBitIntZ _NBitLongLongZ    _NBitHalfZ _NBitSingleZ _NBitDoubleZ_NBitLongDoubleéznumpy._ZBitúnumpy._typing._nbit.) ÚnpÚbyteÚshortZintcZintpÚint_ÚlonglongZhalfZsingleÚdoubleÚ
longdoubleÚdtypeÚitemsize)ÚnamesÚretÚnameÚtypÚn©r!úOd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\numpy/typing/mypy_plugin.pyÚ_get_precision_dict8s õ  r#c sBtjtjtjtjg}ddddddddd    d
d d h ‰‡fd d„|DƒS)NZuint128Zuint256Zint128Zint256Zfloat80Zfloat96Zfloat128Zfloat256Z
complex160Z
complex192Z
complex256Z
complex512csg|]}|jˆkr|j‘qSr!)Ú__name__)Ú.0Úi©Zextended_namesr!r"Ú
<listcomp>]s
z0_get_extended_precision_list.<locals>.<listcomp>)rZ    ulonglongrrZ clongdouble)Zextended_typesr!r'r"Ú_get_extended_precision_listMsôr)ÚstrcCs8t d¡j}|dkrdS|dkr$dS|dkr0dSdSdS)NÚpr&Úc_intÚlÚc_longÚqÚ
c_longlong)rrÚchar)r1r!r!r"Ú_get_c_intp_name`s r2rÚ_PRECISION_DICTÚ_EXTENDED_PRECISION_LISTÚ_C_INTPr    r)ÚctxrcCs2|\}}}|j d¡d}td|›}| |¡S)z;Replace a type-alias with a concrete ``NBitBase`` subclass.Ú.éÿÿÿÿr)rÚsplitr3Z
named_type)r6rÚ_ÚapirZname_newr!r!r"Ú_hookxs
r<zIterable[Statement]Úint)ÚiterableÚidrcCs<t|ƒD] \}}t|ddƒ|kr|Sqtd|›ƒ‚dS)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
rDr
zlist[tuple[str, None | str]]ÚNone)ÚfileÚmoduleÚimportsrcCs<t|d|d}d|_|j|jfD]}t||ƒ}|||<q dS)z<Override the first `module`-based import with new `imports`.r)rTN)r Z is_top_levelZdefsrHrD)rFrGrHZ
import_objÚlstr&r!r!r"Ú_override_imports‰s
 
rJc@s0eZdZdZdddœdd„Zddd    œd
d „Zd S) Ú _NumpyPluginz>A mypy plugin for handling versus numpy-specific typing tasks.r*zNone | _HookFunc)ÚfullnamercCs|tkr tSdS)z«Set the precision of platform-specific `numpy.number`
            subclasses.
 
            For example: `numpy.int_`, `numpy.longlong` and `numpy.longdouble`.
            N)r3r<)ÚselfrLr!r!r"Úget_type_analyze_hook›sz"_NumpyPlugin.get_type_analyze_hookr
zlist[tuple[int, str, int]])rFrcCsTt|jdfg}|jdkr2t|ddd„tDƒdn|jdkrPt|dtd    fgd|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_precisioncSsg|] }||f‘qSr!r!)r%Úvr!r!r"r(µsz4_NumpyPlugin.get_additional_deps.<locals>.<listcomp>)rHznumpy.ctypeslibÚctypesZ_c_intp)r rLrJr4r5)rMrFrr!r!r"Úget_additional_deps¥s 
 þ
þz _NumpyPlugin.get_additional_depsN)r$Ú
__module__Ú __qualname__Ú__doc__rNrRr!r!r!r"rK˜s
rKztype[_NumpyPlugin])ÚversionrcCstS)úAn entry-point for mypy.)rK©rVr!r!r"Úplugin¾srYcCst‚dS)rWN)rrXr!r!r"rYÃs)(rUÚ
__future__rÚcollections.abcrÚtypingrrrrOrZ
mypy.typesZmypyrZ mypy.pluginrr    Z
mypy.nodesr
r r Z
mypy.buildr Z    _HookFuncrÚ__annotations__ÚModuleNotFoundErrorÚexrr#r)r2r3r4r5r<rDrJrKrYr!r!r!r"Ú<module>s8!     &