Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get stub files for `matplotlib`, `numpy`, `scipy`, `pandas`, etc.?

Tags:

python

stub

mypy

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.?

like image 542
Sunghee Yun Avatar asked Feb 16 '20 09:02

Sunghee Yun


People also ask

What are Python stub files?

Stub files contain type-hinting information of normal Python modules. The full official documentation can be found in the section about stub-files in PEP-484. For example, if you have a Python module mymodule.py like this: def myfunction(name): return "Hello " + name.

How do you create a stub in Python?

Create a stub file for your own implementationSelect File | New from the main menu, then select Python File (alternatively, use the Alt+Insert shortcut). In the New Python file dialog, select Python stub and specify the filename. The filename should be the same as the name of the implementation file.

What is Python Typeshed?

Typeshed contains external type annotations for the Python standard library and Python builtins, as well as third party packages as contributed by people external to those projects. This data can e.g. be used for static analysis, type checking or type inference.


2 Answers

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.

like image 170
Fabich Avatar answered Oct 04 '22 08:10

Fabich


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

like image 44
mightyandweakcoder Avatar answered Oct 04 '22 07:10

mightyandweakcoder