Skip to main content

Type stubs for Python machine learning libraries

Project description

Mypy type stubs for numpy, pandas and matplotlib

This is a PEP-561-compliant stub-only package which provides type information for matplotlib, numpy and pandas. The mypy type checker can recognize the types in these packages by installing this package:

pip install data-science-types

To get the most up-to-date version, install it from GitHub directly:

pip install git+https://github.com/predictive-analytics-lab/data-science-types

Or clone the repository somewhere and do pip install -e ..

There is also minor support for Tensorflow and Tensorflow Probability.

Examples

These are the kinds of things that can be checked:

Array creation

import numpy as np

arr1: np.ndarray[np.int64] = np.array([3, 7, 39, -3])  # OK
arr2: np.ndarray[np.int32] = np.array([3, 7, 39, -3])  # Type error
arr3: np.ndarray[np.int32] = np.array([3, 7, 39, -3], dtype=np.int32)  # OK
arr4: np.ndarray[float] = np.array([3, 7, 39, -3], dtype=float)  # Type error: the type of ndarray can not be just "float"
arr5: np.ndarray[np.float64] = np.array([3, 7, 39, -3], dtype=float)  # OK

Operations

import numpy as np

arr1: np.ndarray[np.int64] = np.array([3, 7, 39, -3])
arr2: np.ndarray[np.int64] = np.array([4, 12, 9, -1])

result1: np.ndarray[np.int64] = np.divide(arr1, arr2)  # Type error
result2: np.ndarray[np.float64] = np.divide(arr1, arr2)  # OK

compare: np.ndarray[np.bool_] = (arr1 == arr2)

Reductions

import numpy as np

arr: np.ndarray[np.float64] = np.array([[1.3, 0.7], [-43.0, 5.6]])

sum1: int = np.sum(arr)  # Type error
sum2: np.float64 = np.sum(arr)  # OK
sum3: float = np.sum(arr)  # Also OK: np.float64 is a subclass of float
sum4: np.ndarray[np.float64] = np.sum(arr, axis=0)  # OK

# the same works with np.max, np.min and np.prod

Philosophy

The goal is not to recreate the class hierarchy exactly. The goal is to have useful checks on our code. Often the actual API in the libraries is more permissive than the type signatures in our stubs; but this is (usually) a feature and not a bug.

Contributing

We always welcome contributions. All pull requests are subject to CI checks. We check for compliance with Mypy and that the file formatting conforms to our Black specification.

You can install these dev dependencies via

pip install -e .[dev]

Checking compliance with Mypy

The settings for Mypy are specified in the mypy.ini file in the repository. Just running

mypy tests

from the base directory should take these settings into account. We enforce 0 mypy errors.

Formatting with black

We use Black to format the stub files. First install black and then run

black -l 100 -t py36 -S .

from the base directory.

License

GPL 3

Project details


Download files

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

Source Distribution

data-science-types-0.2.4.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

data_science_types-0.2.4-py3-none-any.whl (37.2 kB view details)

Uploaded Python 3

File details

Details for the file data-science-types-0.2.4.tar.gz.

File metadata

  • Download URL: data-science-types-0.2.4.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for data-science-types-0.2.4.tar.gz
Algorithm Hash digest
SHA256 7b41ae19d3e51adb3ca1366871796cbf548b4753ffe2b6f94ef7eb49107954b0
MD5 090f04bcbe90cd82cf3602e48bb7debd
BLAKE2b-256 1637a3fa107ff7ba72cc4bef1fddff8d5632be20ed303d19df76d62240724e18

See more details on using hashes here.

File details

Details for the file data_science_types-0.2.4-py3-none-any.whl.

File metadata

  • Download URL: data_science_types-0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 37.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for data_science_types-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 9e46153a949d2d3262e135fbe24e5d82b66af944bec3b1a194ee6ac7f51843c2
MD5 41b5c9a957f729d920b179fe551fc3e5
BLAKE2b-256 8937ecdfd93c74f5c003cd464d135cc71b0a5eff0f96817fb9005e7a6f4c2dba

See more details on using hashes here.

Supported by

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