sizmailov / pybind11-stubgen

Generate stubs for python modules
Other
232 stars 47 forks source link

Pure Py files stubs not showing up in auto-complete #92

Closed nyckmaia closed 1 year ago

nyckmaia commented 1 year ago

Hi, my package is called maialib and inside it, I have 2 modules. They are called: maiacore and maiapy. The maiacore module is a pybind11 compiled module but the maiapy module is just a set of pure Python files. My goal is to create a Python package that when the user runs import maialib, it will import both maiacore and maiapy.

My original folder structure is:

maialib/
├─ maiacore/
│  ├─ __init__.py
│  ├─ maiacore.cpython.so
├─ maiapy/
│  ├─ __init__.py
│  ├─ lib01.py
│  ├─ lib02.py
│  ├─ lib03.py

Generating Python Stubs

To generated the Python stubs, I'am using this command below:

pybind11-stubgen maialib maialib.maiacore maialib.maiapy --output-dir=./maialib/ --root-module-suffix="" --ignore-invalid=all --no-setup-py"

So now I got this files structure below:

maialib/
├─ maiacore/
│  ├─ __init__.py
│  ├─ maiacore.cpython.so
│  ├─ __init__.pyi       # new file
│  │  ├─ maiacore/       # new folder
│  │  │  ├─ __init__.pyi # new file
├─ maiapy/
│  ├─ __init__.py
│  ├─ __init__.pyi       # new file
│  ├─ lib01/             # new folder
│  │  ├─ __init__.pyi    # new file
│  ├─ lib01.py
│  ├─ lib02.py
│  ├─ lib03/             # new folder
│  │  ├─ __init__.pyi # new file
│  ├─ lib03.py

My Problem

After install my package using pip install . I start to use it on VS Code v1.75

A Doubt

Looking in the maiapy module, the pybind11-stubgen generated a new folder to lib01.py and lib03.py, but do not did it for the lib02.py. Why this can be happening?

A Way to Investigate

Looking the most external pybind11-stubgen generated __init__.pyi file, I saw that it imports all maiacore classes, but do not import any maiapy file or function. But, in this same file, the __all__ = [...] variable contains both maiacore and maiapy classes and function names. Is there any way to "force" pybind11-stubgen to include the lines from maialib.maiapy.libXXX import XYZ like it is already doing with maiacore?

Here is this most external generated __init__.pyi file:

from __future__ import annotations
import maialib
import typing
from maialib.maiacore.maiacore import Barline
from maialib.maiacore.maiacore import Chord
from maialib.maiacore.maiacore import Clef
from maialib.maiacore.maiacore import Duration
from maialib.maiacore.maiacore import HeapData
from maialib.maiacore.maiacore import Helper
from maialib.maiacore.maiacore import Interval
from maialib.maiacore.maiacore import Measure
from maialib.maiacore.maiacore import Note
from maialib.maiacore.maiacore import NoteData
from maialib.maiacore.maiacore import NoteDataHeap
from maialib.maiacore.maiacore import Part
from maialib.maiacore.maiacore import Score
from maialib.maiacore.maiacore import ScoreCollection
import maialib.maiacore.maiacore

             # AS YOU CAN SEE, NO 'from maialib.maiapy import XYZ'

__all__ = [
    "Barline",
    "Chord",
    "Clef",
    "Duration",
    "HeapData",
    "Helper",
    "Interval",
    "Measure",
    "Note",
    "NoteData",
    "NoteDataHeap",
    "Part",
    "Score",
    "ScoreCollection",
    "getScoreSamplePath", # HERE IS A MAIAPY FUNCTION NAME
    "maiacore",
    "maiapy",             # HERE IS MAIAPY MODULE
    "plotPartsActivity",  # HERE IS A MAIAPY FUNCTION NAME
    "testFunc",           # HERE IS A MAIAPY FUNCTION NAME
    "testPlot"            # HERE IS A MAIAPY FUNCTION NAME
]

Please, could you help me to generate the correct maiapy *.pyi files in a way that it will be visible on VS Code? Thank you for your attention!

My System

nyckmaia commented 1 year ago

Sorry! My fault!

Now I got that pybind11-stubgen only generates Python stubs for the C++ Pybind11 modules. Not for pure *.py files. To generate stubs for *.py files I can use mypy

Thanks