Skip to content

Commit 6632a0a

Browse files
committed
Merge branch 'typing_support' into main
2 parents 9ef5978 + 634ecde commit 6632a0a

File tree

7 files changed

+47
-14
lines changed

7 files changed

+47
-14
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ endif()
2929

3030
# Pybindings project
3131
set(TARGET_NAME depthai)
32-
project(depthai VERSION "0") # revision of bindings [depthai-core].[rev]
32+
project(depthai VERSION "1") # revision of bindings [depthai-core].[rev]
3333

3434
# Set default build type depending on context
3535
set(default_build_type "Release")

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='[email protected]',
@@ -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
},

src/DeviceBindings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ void DeviceBindings::bind(pybind11::module& m, void* pCallstack){
212212
// Bind the rest
213213
deviceBase
214214
// Python only methods
215-
.def("__enter__", [](py::object obj){ return obj; })
215+
.def("__enter__", [](DeviceBase& d) -> DeviceBase& { return d; })
216216
.def("__exit__", [](DeviceBase& d, py::object type, py::object value, py::object traceback) {
217217
py::gil_scoped_release release;
218218
d.close();

src/DeviceBootloaderBindings.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,14 @@ void DeviceBootloaderBindings::bind(pybind11::module& m, void* pCallstack){
110110

111111
deviceBootloader
112112
// Python only methods
113-
.def("__enter__", [](py::object obj){ return obj; })
113+
.def("__enter__", [](DeviceBootloader& d) -> DeviceBootloader& { return d; })
114114
.def("__exit__", [](DeviceBootloader& bl, py::object type, py::object value, py::object traceback) { bl.close(); })
115115
.def("close", &DeviceBootloader::close, "Closes the connection to device. Better alternative is the usage of context manager: `with depthai.DeviceBootloader(deviceInfo) as bootloader:`")
116116

117117
.def_static("getFirstAvailableDevice", &DeviceBootloader::getFirstAvailableDevice, DOC(dai, DeviceBootloader, getFirstAvailableDevice))
118118
.def_static("getAllAvailableDevices", &DeviceBootloader::getAllAvailableDevices, DOC(dai, DeviceBootloader, getAllAvailableDevices))
119119
.def_static("saveDepthaiApplicationPackage", py::overload_cast<std::string, const Pipeline&, std::string, bool>(&DeviceBootloader::saveDepthaiApplicationPackage), py::arg("path"), py::arg("pipeline"), py::arg("pathToCmd") = "", py::arg("compress") = false, DOC(dai, DeviceBootloader, saveDepthaiApplicationPackage))
120-
.def_static("saveDepthaiApplicationPackage", py::overload_cast<std::string, const Pipeline&, bool>(&DeviceBootloader::saveDepthaiApplicationPackage), py::arg("path"), py::arg("pipeline"), py::arg("compress") = false, DOC(dai, DeviceBootloader, saveDepthaiApplicationPackage, 2))
120+
.def_static("saveDepthaiApplicationPackage", py::overload_cast<std::string, const Pipeline&, bool>(&DeviceBootloader::saveDepthaiApplicationPackage), py::arg("path"), py::arg("pipeline"), py::arg("compress"), DOC(dai, DeviceBootloader, saveDepthaiApplicationPackage, 2))
121121
.def_static("createDepthaiApplicationPackage", py::overload_cast<const Pipeline&, std::string, bool>(&DeviceBootloader::createDepthaiApplicationPackage), py::arg("pipeline"), py::arg("pathToCmd") = "", py::arg("compress") = false, DOC(dai, DeviceBootloader, createDepthaiApplicationPackage))
122122
.def_static("createDepthaiApplicationPackage", py::overload_cast<const Pipeline&, bool>(&DeviceBootloader::createDepthaiApplicationPackage), py::arg("pipeline"), py::arg("compress"), DOC(dai, DeviceBootloader, createDepthaiApplicationPackage, 2))
123123
.def_static("getEmbeddedBootloaderVersion", &DeviceBootloader::getEmbeddedBootloaderVersion, DOC(dai, DeviceBootloader, getEmbeddedBootloaderVersion))

src/pipeline/CommonBindings.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "depthai-shared/common/Point3f.hpp"
1414
#include "depthai-shared/common/Size2f.hpp"
1515
#include "depthai-shared/common/UsbSpeed.hpp"
16+
#include "depthai-shared/common/DetectionNetworkType.hpp"
1617

1718
void CommonBindings::bind(pybind11::module& m, void* pCallstack){
1819

@@ -34,6 +35,7 @@ void CommonBindings::bind(pybind11::module& m, void* pCallstack){
3435
py::class_<EepromData> eepromData(m, "EepromData", DOC(dai, EepromData));
3536
py::enum_<UsbSpeed> usbSpeed(m, "UsbSpeed", DOC(dai, UsbSpeed));
3637
py::enum_<ProcessorType> processorType(m, "ProcessorType");
38+
py::enum_<DetectionNetworkType> detectionNetworkType(m, "DetectionNetworkType");
3739

3840

3941
///////////////////////////////////////////////////////////////////////
@@ -184,4 +186,8 @@ void CommonBindings::bind(pybind11::module& m, void* pCallstack){
184186
.value("LEON_MSS", ProcessorType::LEON_MSS)
185187
;
186188

189+
detectionNetworkType
190+
.value("YOLO", DetectionNetworkType::YOLO)
191+
.value("MOBILENET", DetectionNetworkType::MOBILENET)
192+
;
187193
}

src/pipeline/NodeBindings.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -479,18 +479,18 @@ void NodeBindings::bind(pybind11::module& m, void* pCallstack){
479479
.value("SSender", Node::Output::Type::SSender)
480480
;
481481
pyOutput
482-
.def("canConnect", &Node::Output::canConnect, py::arg("in"), DOC(dai, Node, Output, canConnect))
483-
.def("link", &Node::Output::link, py::arg("in"), DOC(dai, Node, Output, link))
484-
.def("unlink", &Node::Output::unlink, py::arg("in"), DOC(dai, Node, Output, unlink))
482+
.def("canConnect", &Node::Output::canConnect, py::arg("input"), DOC(dai, Node, Output, canConnect))
483+
.def("link", &Node::Output::link, py::arg("input"), DOC(dai, Node, Output, link))
484+
.def("unlink", &Node::Output::unlink, py::arg("input"), DOC(dai, Node, Output, unlink))
485485
.def("getConnections", &Node::Output::getConnections, DOC(dai, Node, Output, getConnections))
486486
;
487487

488488

489489
nodeConnection
490-
.def_property("outputId", [](Node::Connection& conn) { return conn.outputId; }, [](Node::Connection& conn, Node::Id id) {conn.outputId = id; }, DOC(dai, Node, Connection, outputId))
491-
.def_property("outputName", [](Node::Connection& conn) { return conn.outputName; }, [](Node::Connection& conn, std::string name) {conn.outputName = name; }, DOC(dai, Node, Connection, outputName))
492-
.def_property("inputId", [](Node::Connection& conn) { return conn.inputId; }, [](Node::Connection& conn, Node::Id id) {conn.inputId = id; }, DOC(dai, Node, Connection, inputId))
493-
.def_property("inputName", [](Node::Connection& conn) { return conn.inputName; }, [](Node::Connection& conn, std::string name) {conn.inputName = name; }, DOC(dai, Node, Connection, inputName))
490+
.def_property("outputId", [](Node::Connection& conn) -> Node::Id& { return conn.outputId; }, [](Node::Connection& conn, Node::Id id) {conn.outputId = id; }, DOC(dai, Node, Connection, outputId))
491+
.def_property("outputName", [](Node::Connection& conn) -> std::string& { return conn.outputName; }, [](Node::Connection& conn, std::string name) {conn.outputName = name; }, DOC(dai, Node, Connection, outputName))
492+
.def_property("inputId", [](Node::Connection& conn) -> Node::Id& { return conn.inputId; }, [](Node::Connection& conn, Node::Id id) {conn.inputId = id; }, DOC(dai, Node, Connection, inputId))
493+
.def_property("inputName", [](Node::Connection& conn) -> std::string& { return conn.inputName; }, [](Node::Connection& conn, std::string name) {conn.inputName = name; }, DOC(dai, Node, Connection, inputName))
494494
;
495495

496496
pyNode

0 commit comments

Comments
 (0)