Package: data-science-types
Mypy type stubs for NumPy, pandas, and MatplotlibThis is aPEP-561-compliant stub-only package
which provides type information formatplotlib,numpyandpandas.
Themypytype checker (or pytype or PyCharm) canrecognizethe types in these packages by installing this package.NOTE: This is a work in progressMany functions are already typed, but alotis still missing (NumPy and pandas arehugelibraries).
Chances are, you will see a message from Mypy claiming that a function does not exist when it does exist.
If you encounter missing functions, we would be delighted for you to send a PR.
If you are unsure of how to type a function, we can discuss it.InstallingYou can get this package from PyPI:pipinstalldata-science-typesTo get the most up-to-date version, install it directly from GitHub:pipinstallgit+https://github.com/predictive-analytics-lab/data-science-typesOr clone the repository somewhere and dopip install -e ..ExamplesThese are the kinds of things that can be checked:Array creationimportnumpyasnparr1:np.ndarray[np.int64]=np.array([3,7,39,-3])# OKarr2:np.ndarray[np.int32]=np.array([3,7,39,-3])# Type errorarr3:np.ndarray[np.int32]=np.array([3,7,39,-3],dtype=np.int32)# OKarr4: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)# OKOperationsimportnumpyasnparr1: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 errorresult2:np.ndarray[np.float64]=np.divide(arr1,arr2)# OKcompare:np.ndarray[np.bool_]=(arr1==arr2)Reductionsimportnumpyasnparr:np.ndarray[np.float64]=np.array([[1.3,0.7],[-43.0,5.6]])sum1:int=np.sum(arr)# Type errorsum2:np.float64=np.sum(arr)# OKsum3:float=np.sum(arr)# Also OK: np.float64 is a subclass of floatsum4:np.ndarray[np.float64]=np.sum(arr,axis=0)# OK# the same works with np.max, np.min and np.prodPhilosophyThe goal is not to recreate the APIs exactly.
The main goal is to haveusefulchecks 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.ContributingWe 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 viapipinstall-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 for running the CI checks that are triggered when a PR is opened.
To test these out locally, you need to install the type stubs in your environment.
Typically, you would do this withpipinstall-e.Then use thecheck_all.shscript to run all tests:./check_all.shBelow we describe how to run the various checks individually,
butcheck_all.shshould be easier to use.Checking compliance with MypyThe settings for Mypy are specified in themypy.inifile in the repository.
Just runningmypytestsfrom the base directory should take these settings into account.
We enforce 0 Mypy errors.Formatting with blackWe useBlackto format the stub files.
First, installblackand then runblack.from the base directory.Pytestpython-mpytest-vvtests/Flake8flake8*-stubsLicenseApache 2.0