Skip to content

Commit cc660d8

Browse files
Generate stubs directly within setup.py
1 parent 7033c51 commit cc660d8

File tree

2 files changed

+25
-28
lines changed

2 files changed

+25
-28
lines changed

doc/generate_stubs.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

setup.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,30 @@ def __init__(self, name, sourcedir=""):
1616

1717
class CMakeBuild(build_ext):
1818

19+
def generate_stubs(self, ext):
20+
import importlib.util
21+
import pybind11_stubgen as stubgen
22+
23+
global AUTO_STUB_GENERATION
24+
AUTO_STUB_GENERATION = True
25+
26+
extdir = os.path.dirname(os.path.realpath(self.get_ext_fullpath(ext.name)))
27+
28+
spec = importlib.util.spec_from_file_location(
29+
ext.name, os.path.realpath(self.get_ext_fullpath(ext.name)))
30+
31+
module = importlib.util.module_from_spec(spec)
32+
33+
stubgen.FunctionSignature.ignore_invalid_signature = True
34+
stubgen.FunctionSignature.ignore_invalid_defaultarg = True
35+
stubgen.FunctionSignature.signature_downgrade = False
36+
37+
moduleGen = stubgen.ModuleStubsGenerator(module)
38+
moduleGen.parse()
39+
40+
with open(os.path.join(extdir, ext.name + '.pyi'), 'w') as init_pyi:
41+
init_pyi.write('\n'.join(moduleGen.to_lines()))
42+
1943
def build_extension(self, ext):
2044
extdir = os.path.abspath(os.path.dirname(
2145
self.get_ext_fullpath(ext.name)))
@@ -75,12 +99,7 @@ def build_extension(self, ext):
7599
["cmake", "--build", "."] + build_args, cwd=self.build_temp
76100
)
77101

78-
# Automatically generate stubs
79-
subprocess.check_call(
80-
[sys.executable, "doc/generate_stubs.py", ext.name,
81-
os.path.realpath(self.get_ext_fullpath(ext.name))],
82-
cwd=os.path.dirname(os.path.abspath(__file__))
83-
)
102+
self.generate_stubs(ext)
84103

85104

86105
with open("README.md", "r") as readme:

0 commit comments

Comments
 (0)