Skip to content

Commit cfb227c

Browse files
committed
Added typing information
1 parent 9ef5978 commit cfb227c

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[build-system]
2-
requires = ["setuptools", "wheel"] # Must be preinstalled "cmake>=3.2.0"
2+
requires = ["setuptools", "wheel", "mypy"] # Must be preinstalled "cmake>=3.2.0"

setup.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from setuptools.command.build_ext import build_ext
1212
from distutils.version import LooseVersion
1313

14+
### NAME
15+
MODULE_NAME = 'depthai'
1416

1517
### VERSION
1618
here = 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\nimport typing\nimport 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

176203
setup(
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

Comments
 (0)