Pylint is a static code analysis tool which can be used to perform static code analysis. Sometimes Pylint does not recognize certain members of libraries such as NumPya and due to this, we might see some false positive warnings or errors. This will happen even if our code is proper.

In this article, we will see how to configure the Pylint to recognize NumPy members effectively.

Install the Pylint & NumPy

I hope you have installed the NumPy and Pylint libraries in your environment. If you not you can follow the below command to install these Numpy and update the Pylint to the latest version.

pip install numpy
pip install --upgrade pylint

Configure the Pylint for Numpy

If Pylint still doesn’t recognize NumPy members, we can configure Pylint to handle NumPy using Pylint’s configuration options.

As a first step, generate the Pylint configuration file using the following command

pylint --generate-rcfile > .pylintrc

Now, open the generated configuration file in the text editor and locate the following section i.e. [TYPECHECK] and the following settings to improve the compatibility with the Numpy.

generated-members=numpy.*, scipy.*

The above setting tells Pylint to consider all members of NumPy and SciPy as dynamically generated and this will prevent false positive warnings about missing members.

Type Hints

Adding the type hints to the code can help Pylint to easily understand the types of variables and functions. This can be very useful when we use complex library like NumPy.

See the following example for more information on this

import numpy as np
from numpy.typing import NDArray

def add_arrays(a: NDArray[np.float64], b: NDArray[np.float64]) -> NDArray[np.float64]:
    return np.add(a, b)

data1 = np.array([1.0, 2.0, 3.0])
data2 = np.array([4.0, 5.0, 6.0])
result = add_arrays(data1, data2)

Categorized in:

Tagged in: