a :jg  @sUdZddlmZddlmZddlmZmZmZddl Z zbddl Z ddl m Z ddlmZmZddlmZmZmZdd lmZeege fZdZd ed <Wn(eyZzeZWYdZ[n dZ[00gZd ed <ddddZd dddZddddZeZ ded<eZ!ded<eZ"ded<dddddZ#esFedurd dd!d"d#d$Z$d%dd&d'd(d)d*Z%Gd+d,d,eZ&dd-d.d/d0Z'ndd-d.d1d0Z'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 ` 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: https://mypy-lang.org/ .. _configuration file: https://mypy.readthedocs.io/en/stable/config_file.html ) annotations)Iterable)Final TYPE_CHECKINGCallableN)Type)PluginAnalyzeTypeContext)MypyFile ImportFrom Statement)PRI_MEDzNone | ModuleNotFoundErrorMYPY_EXz list[str]__all__zdict[str, str])returnc Csdtjfdtjfdtjfdtjfdtjfdtjfdtjfdtjfd tj fd tj fd tj fg }i}|D],\}}d |j j }d |d|d|<qd|S)NZ _NBitByteZ _NBitShortZ _NBitIntCZ _NBitIntPZ_NBitIntZ _NBitLongZ _NBitLongLongZ _NBitHalfZ _NBitSingleZ _NBitDoubleZ_NBitLongDoubleznumpy._ZBitnumpy._typing._nbit.)npbyteshortZintcZintpint_longZlonglongZhalfsingledoubleZ longdoubledtypeitemsize)namesretnametypnr!B/usr/local/lib/python3.9/site-packages/numpy/typing/mypy_plugin.py_get_precision_dict8s" r#cCsgd}dd|DS)N) Zuint128Zuint256Zint128Zint256Zfloat80Zfloat96Zfloat128Zfloat256Z complex160Z complex192Z complex256Z complex512cSsg|]}tt|r|qSr!)hasattrr).0ir!r!r" ]z0_get_extended_precision_list..r!)Zextended_namesr!r!r"_get_extended_precision_listNsr)strcCs8tdj}|dkrdS|dkr$dS|dkr0dSdSdS)Nr r&Zc_intlZc_longqZ c_longlong)rrchar)r-r!r!r"_get_c_intp_name`s r.r_PRECISION_DICT_EXTENDED_PRECISION_LIST_C_INTPr r)ctxrcCs2|\}}}|jdd}td|}||S)z;Replace a type-alias with a concrete ``NBitBase`` subclass..r)rsplitr/Z named_type)r2r_apirZname_newr!r!r"_hookxs r8zIterable[Statement]int)iterableidrcCs<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: ) enumerategetattr ValueError)r:r;r&valuer!r!r"_indexs  r@r zlist[tuple[str, None | str]]None)filemoduleimportsrcCs<t|d|d}d|_|j|jfD]}t||}|||<q dS)zA mypy plugin for handling versus numpy-specific typing tasks.r*zNone | _HookFunc)fullnamercCs|tvr tSdS)zSet the precision of platform-specific `numpy.number` subclasses. For example: `numpy.int_`, `numpy.longlong` and `numpy.longdouble`. N)r/r8)selfrHr!r!r"get_type_analyze_hooksz"_NumpyPlugin.get_type_analyze_hookr zlist[tuple[int, str, int]])rBrcCsTt|jdfg}|jdkr2t|dddtDdn|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`. r4numpyz!numpy._typing._extended_precisioncSsg|] }||fqSr!r!)r%vr!r!r"r'r(z4_NumpyPlugin.get_additional_deps..)rDznumpy.ctypeslibctypesZ_c_intp)r rHrFr0r1)rIrBrr!r!r"get_additional_depss    z _NumpyPlugin.get_additional_depsN)__name__ __module__ __qualname____doc__rJrNr!r!r!r"rGs rGztype[_NumpyPlugin])versionrcCstS)An entry-point for mypy.)rGrSr!r!r"pluginsrVcCstdS)rTN)rrUr!r!r"rVs)(rR __future__rcollections.abcrtypingrrrrKrZ mypy.typesZmypyrZ mypy.pluginrr Z mypy.nodesr r r Z mypy.buildr Z _HookFuncr__annotations__ModuleNotFoundErrorexrr#r)r.r/r0r1r8r@rFrGrVr!r!r!r"s8!     &