Skip to main content

Type hints for Numpy.

Project description

PyPI version Downloads PyPI version codecov Scrutinizer Code Quality

nptyping

Type hints for Numpy!

Installation

pip install nptyping

Usage

NDArray

nptyping.NDArray lets you define the shape and type of your numpy.ndarray.

You can:

  • specify the number of dimensions;
  • specify the size per dimension;
  • specify the type of the array;
  • instance check your array with your nptying type.

Examples

An Array with any dimensions of any size and any type:

>>> from nptyping import NDArray
>>> from typing import Any


>>> NDArray
NDArray[(typing.Any, ...), typing.Any]

>>> NDArray[(Any, ...)]
NDArray[(typing.Any, ...), typing.Any]

>>> NDArray[(Any, ...), Any]
NDArray[(typing.Any, ...), typing.Any]

An array with 1 dimension of any size and any type:

>>> NDArray[Any]
NDArray[(typing.Any,), typing.Any]

>>> NDArray[(Any,)]
NDArray[(typing.Any,), typing.Any]

>>> NDArray[Any, Any]
NDArray[(typing.Any,), typing.Any]

>>> NDArray[(Any,), Any]
NDArray[(typing.Any,), typing.Any]

An array with 1 dimension of size 3 and any type:

>>> NDArray[3]
NDArray[(3,), typing.Any]

>>> NDArray[(3,)]
NDArray[(3,), typing.Any]

>>> NDArray[(3,), Any]
NDArray[(3,), typing.Any]

An array with 3 dimensions of size 3, 3 and any and any type:

>>> NDArray[3, 3, Any]
NDArray[(3, 3, typing.Any), typing.Any]

>>> NDArray[(3, 3, Any)]
NDArray[(3, 3, typing.Any), typing.Any]

>>> NDArray[(3, 3, Any), Any]
NDArray[(3, 3, typing.Any), typing.Any]

An array with any dimensions of any size and type int:

>>> NDArray[int]
NDArray[(typing.Any, ...), int]

>>> NDArray[(Any, ...), int]
NDArray[(typing.Any, ...), int]

An array with 1 dimension of size 3 and type int:

>>> NDArray[3, int]
NDArray[(3,), int]

>>> NDArray[(3,), int]
NDArray[(3,), int]

An array with any dimensions of size 3 and type int:

>>> NDArray[(3, ...), int]
NDArray[(3, ...), int]

An array with 3 dimensions of sizes 3, 3, 5 and type int:

>>> NDArray[(3, 3, 5), int]
NDArray[(3, 3, 5), int]

Checking your instances

You can use NDArray with isinstance to dynamically check your arrays.

>>> import numpy as np

>>> arr = np.array([[1, 2, 3],
...                 [4, 5, 6]])

>>> isinstance(arr, NDArray[(2, 3), int])
True
>>> isinstance(arr, NDArray[(2, 3), float])
False
>>> isinstance(arr, NDArray[(2, 3, 1), int])
False

Finding the right annotation

You can use NDArray to find the type of a numpy array for you using NDArray.type_of:

>>> NDArray.type_of(np.array([[1, 2], [3, 4.0]]))
NDArray[(2, 2), float64]

py_type

With py_type you can get the Python builtin type that corresponds to a Numpy dtype:

>>> from nptyping import py_type

>>> py_type(np.int32)
<class 'int'>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nptyping-1.0.1-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file nptyping-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: nptyping-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

File hashes

Hashes for nptyping-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9f782826d5749fd8448c156b46b2deb84b3a09db860ac4a9881f4e5bd5181afd
MD5 2bf38020552c381d92599379365e3271
BLAKE2b-256 73119e15ef1cd231182a3b568b65a612a173061d826de805481f44848fc27a32

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page