1111from setuptools .command .build_ext import build_ext
1212from distutils .version import LooseVersion
1313
14+ ### NAME
15+ MODULE_NAME = 'depthai'
1416
1517### VERSION
1618here = os .path .abspath (os .path .dirname (__file__ ))
@@ -171,10 +173,35 @@ def build_extension(self, ext):
171173 subprocess .check_call (['cmake' , ext .sourcedir ] + cmake_args , cwd = self .build_temp , env = env )
172174 subprocess .check_call (['cmake' , '--build' , '.' ] + build_args , cwd = self .build_temp )
173175
176+ # Create stubs, add PYTHONPATH to find the build module
177+ # CWD to to extdir where the built module can be found to extract the types
178+ subprocess .check_call (['stubgen' , '-p' , MODULE_NAME , '-o' , f'{ extdir } ' ], cwd = extdir )
179+
180+ # Add py.typed
181+ open (f'{ extdir } /depthai/py.typed' , 'a' ).close ()
182+
183+ # imports and overloads
184+ with open (f'{ extdir } /depthai/__init__.pyi' ,'r+' ) as file :
185+ # Read
186+ contents = file .read ()
187+
188+ # Add imports
189+ stubs_import = 'import depthai.node as node\n import typing\n import json\n ' + contents
190+ # Create 'create' overloads
191+ nodes = re .findall ('def \S*\(self\) -> node.(\S*):' , stubs_import )
192+ overloads = ''
193+ for node in nodes :
194+ overloads = overloads + f'\\ 1@overload\\ 1def create(self, arg0: typing.Type[node.{ node } ]) -> node.{ node } : ...'
195+ print (f'{ overloads } ' )
196+ final_stubs = re .sub (r"([\s]*)def create\(self, arg0: object\) -> Node: ..." , f'{ overloads } ' , stubs_import )
197+
198+ # Writeout changes
199+ file .seek (0 )
200+ file .write (final_stubs )
174201
175202
176203setup (
177- name = 'depthai' ,
204+ name = MODULE_NAME ,
178205 version = __version__ ,
179206 author = 'Luxonis' ,
180207 author_email = 'support@luxonis.com' ,
@@ -183,7 +210,7 @@ def build_extension(self, ext):
183210 long_description = long_description ,
184211 long_description_content_type = "text/markdown" ,
185212 url = "https://github.com/luxonis/depthai-python" ,
186- ext_modules = [CMakeExtension ('depthai' )],
213+ ext_modules = [CMakeExtension (MODULE_NAME )],
187214 cmdclass = {
188215 'build_ext' : CMakeBuild
189216 },
0 commit comments