pythonrustpylintpyo3maturin

Using Pylint with PyModule generated using PyO3 and maturin


Pylint will not recognize any of the functions from a PyModule that I created using PyO3 and maturin. All of the functions import and run fine in the python code base, but for some reason Pylint is throwing E1011: no-member warnings.

Below is a (likely) incomplete dummy example, but is provided in order to show the way I am decorating using pymodule and pyfunction:

#[pyfunction]
fn add_nums(
    _py: Python<'_>,
    a: f32,
    b: f32,
) -> PyResult<f32> {
    let res:f32 = a+b;
    Ok(res)
}
#[pymodule]
fn my_module(_py: Python, m: &PyModule) -> PyResult<()> {
    m.add_function(wrap_pyfunction!(add_nums, m)?)?;
    Ok(())
}

Then if I build that using maturin build --release and install the module, from the resulting wheelfile, into my python environment and import into a script:

import my_module

my_module.add_nums(5, 6) # ignore that these are not f32 - irrelevant this is a dummy example

If I then run pylint on that file (from terminal - VS Code pylint extension actually does not complain about this...), I end up with something like: E1101: Module 'my_module' has no 'add-nums' member (no-member) even though the code (not this code - but the real code which I cannot include here) runs just fine.

Has anyone successfully built wheelfiles using maturin, used them in another project, then had Pylint play nicely with that project and recognize that the methods do actually exist?


Solution

  • Pylint has a extension-pkg-allow-list setting which you can use to inspect non-python modules. It will need to load the extension into pylint's interpreter though, which is why it's not enabled by default.

    There's also requests to support (and lint) pyi, but AFAIK that's not supported yet, cf #2873 and #4987.

    Before Pylint 2.8, the setting is extension-pkg-whitelist.