Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 880
codeAndStuff Avatar asked Oct 24 '25 15:10

codeAndStuff


1 Answers

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.

like image 132
Masklinn Avatar answered Oct 26 '25 05:10

Masklinn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!