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 (or pytype or PyCharm) can recognize the types in these packages by installing this package.

NOTE: This is a work in progress

Lots of functions are already typed, but a lot is still missing (numpy and pandas are huge libraries). Chances are you will see a message from Mypy claiming that a function does not exist when it actually does exist. If you encounter missing functions, we would be very happy for you to send a PR. If you are unsure of how to type a function, we can discuss it.

Installing

You can get this package from Pypi:

pip install data-science-types

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

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

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

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 APIs exactly. The main goal is to have useful checks on our code. Often the actual APIs 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]

This will also install numpy, pandas and matplotlib to be able to run the tests.

Running CI locally (recommended)

We include a script that runs the CI checks that will be run when a PR is opened. To test these out locally, use the check_all.sh script.

./check_all.sh

Below we describe how to run the various checks individually, but check_all.sh should be easier to use.

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 .

from the base directory.

Pytest

python -m pytest -vv tests/

Flake8

flake8 *-stubs

License

Apache 2.0

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.18.tar.gz (22.7 kB view details)

Uploaded Source

Built Distribution

data_science_types-0.2.18-py3-none-any.whl (35.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: data-science-types-0.2.18.tar.gz
  • Upload date:
  • Size: 22.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.7.9

File hashes

Hashes for data-science-types-0.2.18.tar.gz
Algorithm Hash digest
SHA256 32f7f53f38286f60e753c062919ad9da7d7637276fa573cbd02cf0b08f707b05
MD5 b7825048a788b9651dbda367170b6d4b
BLAKE2b-256 70a74cbb7f8be138fc6b4ec4e290a4a17cb338d7dc6caca53a3b88df3c78002f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: data_science_types-0.2.18-py3-none-any.whl
  • Upload date:
  • Size: 35.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.7.9

File hashes

Hashes for data_science_types-0.2.18-py3-none-any.whl
Algorithm Hash digest
SHA256 4c127d222fd64ac15ce2a90a969b472d44b12df97d22115131bfcdba59694b45
MD5 734d5e090b87842a5df2afdd5a738814
BLAKE2b-256 fa5f59c5146f2698bbef48cb0cbbead15d33191fa849d0b74fd7754617d52aee

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