codeAndStuff
codeAndStuff

Reputation: 557

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?

Upvotes: 3

Views: 816

Answers (2)

Masklinn
Masklinn

Reputation: 42217

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.

Upvotes: 3

codeAndStuff
codeAndStuff

Reputation: 557

Similar to the answer by @Masklinn except it looks like the term 'extension-pkg-whitelist' exists in older versions and later the 'extension-pkg-allow-list' does not (though it was introduced for obvious societal reasons).

add the following into the [MASTER] section of your .pylintrc:

[MASTER]

# A comma-separated list of package or module names from where C extensions may
# be loaded. Extensions are loading into the active Python interpreter and may
# run arbitrary code.
extension-pkg-allow-list=
      my_module

for versions where this is not supported (someone please update which version it changed here) use the extension-pkg-whitelist instead.

Upvotes: 0

Related Questions