o 6a,@sBUdZddlmZddlmZmZer*ddlZejdkr#ddlmZn ddl mZnddZes5gd Z nee e d <eGd d d Z Gd dde ZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZddlmZmZmZmZmZmZmZmZmZm Z ddl!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;mZ>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHdd lImJZJmKZKmLZLmMZMmNZNmOZOmPZPmQZQmRZRmSZSdd!lTmUZUmVZVdd"lWmXZXmYZYmZZZm[Z[m\Z\m]Z]m^Z^m_Z_m`Z`maZambZbmcZcmdZdmeZemfZfdd#lgmhZhmiZimjZjmkZkmlZlmmZmmnZnmoZompZpmqZqmrZrmsZsmtZtmuZumvZvmwZwmxZxmyZydd$lzm{Z{m|Z|errdd%l}m~Z~mZmZmZmZn eZ~eZeZeZeZ[[[[edurdd&lmZee7Zed'7Z[dd(lmZeeZ[dS))a ============================ Typing (:mod:`numpy.typing`) ============================ .. warning:: Some of the types in this module rely on features only present in the standard library in Python 3.8 and greater. If you want to use these types in earlier versions of Python, you should install the typing-extensions_ package. Large parts of the NumPy API have PEP-484-style type annotations. In addition a number of type aliases are available to users, most prominently the two below: - `ArrayLike`: objects that can be converted to arrays - `DTypeLike`: objects that can be converted to dtypes .. _typing-extensions: https://pypi.org/project/typing-extensions/ Mypy plugin ----------- A mypy_ plugin is distributed in `numpy.typing` for managing a number of platform-specific annotations. Its function can be split into to 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 notable 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. 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 Differences from the runtime NumPy API -------------------------------------- NumPy is very flexible. Trying to describe the full range of possibilities statically would result in types that are not very helpful. For that reason, the typed NumPy API is often stricter than the runtime NumPy API. This section describes some notable differences. ArrayLike ~~~~~~~~~ The `ArrayLike` type tries to avoid creating object arrays. For example, .. code-block:: python >>> np.array(x**2 for x in range(10)) array( at ...>, dtype=object) is valid NumPy code which will create a 0-dimensional object array. Type checkers will complain about the above example when using the NumPy types however. If you really intended to do the above, then you can either use a ``# type: ignore`` comment: .. code-block:: python >>> np.array(x**2 for x in range(10)) # type: ignore or explicitly type the array like object as `~typing.Any`: .. code-block:: python >>> from typing import Any >>> array_like: Any = (x**2 for x in range(10)) >>> np.array(array_like) array( at ...>, dtype=object) ndarray ~~~~~~~ It's possible to mutate the dtype of an array at runtime. For example, the following code is valid: .. code-block:: python >>> x = np.array([1, 2]) >>> x.dtype = np.bool_ This sort of mutation is not allowed by the types. Users who want to write statically typed code should instead use the `numpy.ndarray.view` method to create a view of the array with a different dtype. DTypeLike ~~~~~~~~~ The `DTypeLike` type tries to avoid creation of dtype objects using dictionary of fields like below: .. code-block:: python >>> x = np.dtype({"field1": (float, 1), "field2": (int, 3)}) Although this is valid NumPy code, the type checker will complain about it, since its usage is discouraged. Please see : :ref:`Data type objects ` Number precision ~~~~~~~~~~~~~~~~ The precision of `numpy.number` subclasses is treated as a covariant generic parameter (see :class:`~NBitBase`), simplifying the annotating of processes involving precision-based casting. .. code-block:: python >>> from typing import TypeVar >>> import numpy as np >>> import numpy.typing as npt >>> T = TypeVar("T", bound=npt.NBitBase) >>> def func(a: "np.floating[T]", b: "np.floating[T]") -> "np.floating[T]": ... ... Consequently, the likes of `~numpy.float16`, `~numpy.float32` and `~numpy.float64` are still sub-types of `~numpy.floating`, but, contrary to runtime, they're not necessarily considered as sub-classes. Timedelta64 ~~~~~~~~~~~ The `~numpy.timedelta64` class is not considered a subclass of `~numpy.signedinteger`, the former only inheriting from `~numpy.generic` while static type checking. 0D arrays ~~~~~~~~~ During runtime numpy aggressively casts any passed 0D arrays into their corresponding `~numpy.generic` instance. Until the introduction of shape typing (see :pep:`646`) it is unfortunately not possible to make the necessary distinction between 0D and >0D arrays. While thus not strictly correct, all operations are that can potentially perform a 0D-array -> scalar cast are currently annotated as exclusively returning an `ndarray`. If it is known in advance that an operation _will_ perform a 0D-array -> scalar cast, then one can consider manually remedying the situation with either `typing.cast` or a ``# type: ignore`` comment. API --- )ufunc) TYPE_CHECKINGListN))finalcCs|S)N)frr7/usr/lib/python3/dist-packages/numpy/typing/__init__.pyrsr) ArrayLike DTypeLikeNBitBaseNDArray__all__cs"eZdZdZdfdd ZZS)r a An object representing `numpy.number` precision during static type checking. Used exclusively for the purpose static type checking, `NBitBase` represents the base of a hierarchical set of subclasses. Each subsequent subclass is herein used for representing a lower level of precision, *e.g.* ``64Bit > 32Bit > 16Bit``. Examples -------- Below is a typical usage example: `NBitBase` is herein used for annotating a function that takes a float and integer of arbitrary precision as arguments and returns a new float of whichever precision is largest (*e.g.* ``np.float16 + np.int64 -> np.float64``). .. code-block:: python >>> from __future__ import annotations >>> from typing import TypeVar, Union, TYPE_CHECKING >>> import numpy as np >>> import numpy.typing as npt >>> T1 = TypeVar("T1", bound=npt.NBitBase) >>> T2 = TypeVar("T2", bound=npt.NBitBase) >>> def add(a: np.floating[T1], b: np.integer[T2]) -> np.floating[Union[T1, T2]]: ... return a + b >>> a = np.float16() >>> b = np.int64() >>> out = add(a, b) >>> if TYPE_CHECKING: ... reveal_locals() ... # note: Revealed local types are: ... # note: a: numpy.floating[numpy.typing._16Bit*] ... # note: b: numpy.signedinteger[numpy.typing._64Bit*] ... # note: out: numpy.floating[numpy.typing._64Bit*] returnNcs(hd}|j|vr tdtdS)N> _8Bit_16Bit_32Bit_64Bit_80Bit_96Bit_128Bit_256Bitr z*cannot inherit from final class "NBitBase")__name__ TypeErrorsuper__init_subclass__)cls allowed_names __class__rr rs zNBitBase.__init_subclass__)rN)r __module__ __qualname____doc__r __classcell__rrrr r s)r c@ eZdZdS)rNrr!r"rrrr r rc@r%)rNr&rrrr rr'rc@r%)rNr&rrrr rr'rc@r%)rNr&rrrr rr'rc@r%)rNr&rrrr rr'rc@r%)rNr&rrrr rr'rc@r%)rNr&rrrr rr'rc@r%)rNr&rrrr rr'r) _NBitByte _NBitShort _NBitIntC _NBitIntP_NBitInt _NBitLongLong _NBitHalf _NBitSingle _NBitDouble_NBitLongDouble)' _BoolCodes _UInt8Codes _UInt16Codes _UInt32Codes _UInt64Codes _Int8Codes _Int16Codes _Int32Codes _Int64Codes _Float16Codes _Float32Codes _Float64Codes_Complex64Codes_Complex128Codes _ByteCodes _ShortCodes _IntCCodes _IntPCodes _IntCodes_LongLongCodes _UByteCodes _UShortCodes _UIntCCodes _UIntPCodes _UIntCodes_ULongLongCodes _HalfCodes _SingleCodes _DoubleCodes_LongDoubleCodes _CSingleCodes _CDoubleCodes_CLongDoubleCodes _DT64Codes _TD64Codes _StrCodes _BytesCodes _VoidCodes _ObjectCodes) _CharLike_co _BoolLike_co _UIntLike_co _IntLike_co _FloatLike_co_ComplexLike_co _TD64Like_co_NumberLike_co_ScalarLike_co _VoidLike_co)_Shape _ShapeLike)r _SupportsDType_VoidDTypeLike_DTypeLikeBool_DTypeLikeUInt _DTypeLikeInt_DTypeLikeFloat_DTypeLikeComplex_DTypeLikeTD64_DTypeLikeDT64_DTypeLikeObject_DTypeLikeVoid _DTypeLikeStr_DTypeLikeBytes_DTypeLikeComplex_co)r _ArrayLike_NestedSequence_RecursiveSequence_SupportsArray _ArrayLikeInt_ArrayLikeBool_co_ArrayLikeUInt_co_ArrayLikeInt_co_ArrayLikeFloat_co_ArrayLikeComplex_co_ArrayLikeNumber_co_ArrayLikeTD64_co_ArrayLikeDT64_co_ArrayLikeObject_co_ArrayLikeVoid_co_ArrayLikeStr_co_ArrayLikeBytes_co)r _GenericAlias)_UFunc_Nin1_Nout1_UFunc_Nin2_Nout1_UFunc_Nin1_Nout2_UFunc_Nin2_Nout2_GUFunc_Nin2_Nout1) _docstringsz& .. autoclass:: numpy.typing.NBitBase ) PytestTester)r#numpyrtypingrrsys version_infortyping_extensionsrstr__annotations__r rrrrrrrr_nbitr)r*r+r,r-r.r/r0r1r2 _char_codesr3r4r5r6r7r8r9r:r;r<r=r>r?r@rArBrCrDrErFrGrHrIrJrKrLrMrNrOrPrQrRrSrTrUrVrWrXrY_scalarsrZr[r\r]r^r_r`rarbrc_shaperdre _dtype_liker rfrgrhrirjrkrlrmrnrorprqrrrs _array_liker rtrurvrwrxryrzr{r|r}r~rrrrrr_generic_aliasrr_ufuncrrrrr_add_docstringrnumpy._pytesttesterrrtestrrrr s\ $   50 0) DP