49

I know that the stub files for built-in Python library for type checking and static analysis come with mypy or PyCharm installation. How can I get stub files for matplotlib, numpy, scipy, pandas, etc.?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Sunghee Yun
  • 722
  • 1
  • 6
  • 11
  • 1
    See https://mypy.readthedocs.io/en/stable/getting_started.html#library-stubs-and-typeshed, they come from https://pypi.org/project/typeshed/ – jonrsharpe Feb 16 '20 at 09:44
  • @jonrsharpe Awesome! This really helps! – Sunghee Yun Feb 16 '20 at 18:56
  • 7
    The packages you mentioned are not contained in `typeshed`. For `numpy`, there is an official [stub project](https://github.com/numpy/numpy-stubs) and can be installed via e.g. `pip install git+https://github.com/numpy/numpy-stubs`. For `pandas` etc, there are no official type hints yet, but lots of unofficial projects available, e.g. [`data-science-types`](https://pypi.org/project/data-science-types/). – hoefling Feb 16 '20 at 20:25
  • @hoefling This is great information. Thank you very much! – Sunghee Yun Feb 17 '20 at 03:38
  • 2
    If anyone wonders in 2021, scipy is now typed since 1.5 release – krassowski Feb 16 '21 at 12:19
  • 2
    @krassowski I have scipy 1.6.0, but mypy doesn't find its type stubs yet. – quant_dev Feb 17 '21 at 13:58

3 Answers3

22

Type stubs are sometimes packaged directly with the library. Otherwise there can be some external libraries to provide them.

Numpy

Starting with numpy 1.20 type stubs will be included in numpy. See this changelog and this PR adding them

Before that they could added with the library https://github.com/numpy/numpy-stubs

Pandas and Matplotlib

There is no official support for these libraries stubs but you can find unofficial stubs in this project: https://pypi.org/project/data-science-types/

You can either install this library as a dependency or copy only the relevant part in the type stubs folder of your project.

Fabich
  • 2,768
  • 3
  • 30
  • 44
12

As of July 2022 there are official pandas stubs: https://github.com/pandas-dev/pandas-stubs

This is the home for pandas typing stubs supported by the pandas core team. The stubs are likely incomplete in terms of covering the published API of pandas.

Ken Jiiii
  • 474
  • 9
  • 21
9

If you are using VSCode and Pylance extension with strict type checking enabled, you can generate stub files automatically by just pressing quickfix option and press "Create Type Stub for "module" "

enter image description here