Skip to content

Stub files for empty modules with submodules won't be generated #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
konn opened this issue Nov 28, 2024 · 0 comments
Open

Stub files for empty modules with submodules won't be generated #107

konn opened this issue Nov 28, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@konn
Copy link
Collaborator

konn commented Nov 28, 2024

Summary

Currently, stub files are not generated for modules without any function/class, even if it has submodules.
This prevents pyright and mypy to retrieve information of modules qualified with partially imported module.

Steps to reproduce

Consider the following library definition:

use pyo3::prelude::*;
use pyo3_stub_gen::{define_stub_info_gatherer, derive::*};

#[gen_stub_pyfunction(module = "empty_super_module.sub_mod")]
#[pyfunction(name = "greet")]
pub fn greet() {
    println!("Hello from sub_mod!")
}

#[pymodule]
fn main_mod(m: &Bound<PyModule>) -> PyResult<()> {
    sub_mod(m)?;
    Ok(())
}

fn sub_mod(parent: &Bound<PyModule>) -> PyResult<()> {
    let py = parent.py();
    let sub = PyModule::new_bound(py, "sub_mod")?;
    sub.add_function(wrap_pyfunction!(greet, &sub)?)?;
    parent.add_submodule(&sub)?;
    Ok(())
}

define_stub_info_gatherer!(stub_info);

/// Test of unit test for testing link problem
#[cfg(test)]
mod test {
    #[test]
    fn test() {
        assert_eq!(2 + 2, 4);
    }
}

Running appropriate gen_stub with this, pyo3-stub-gen generates the following stub file:

  • python/empty_super_module/sub_mod.pyi:
    # This file is automatically generated by pyo3_stub_gen
    # ruff: noqa: E501, F401
    
    
    def greet() -> None:
        ...

Note that there is no python/empty_super_module/__init__.py. Consider the following test function:

import empty_super_module


def test_sum_mod():
    empty_super_module.sub_mod.greet()

Expected Behaviour

All of pytest, mypy, and pyright should work fine.

Actual Behaviour

Pytest passes, but mypy warns that Cannot find implementation or library stub for module named "empty_super_module" and pyright fails with the following error:

/path/to/tests/test_empty_super_module.py
  /path/to/tests/test_empty_super_module.py:5:24 - error: "sub_mod" is not a known attribute of module "empty_super_module" (reportAttributeAccessIssue)
1 error, 0 warnings, 0 informations 

Possible solutions

  1. Taking submodules into account when generating stub files.
  2. Unconditionally generating stub files even for empty modules.
@konn konn added the bug Something isn't working label Nov 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant