From ad833bee960c2325be32d135b09c05641ed2ce7b Mon Sep 17 00:00:00 2001 From: Maidul Hasan Masum <79536779+mh-masum6875@users.noreply.github.com> Date: Wed, 28 Oct 2026 19:28:52 +0800 Subject: [PATCH] Merge pull request #30549 from mh-masum6875:docs-python-signatures-5x python: add documentation signatures for hand-wrapped highgui functions - #30549 Port of #30508 to 5.x `createTrackbar`, `createButton` and `setMouseCallback` are registered by hand in `cv2.cpp`, so `gen2.py` never sees them and they end up with no Python signature in the generated docs. Argument names come from the keyword lists in `cv2_highgui.cpp`, which is why `setMouseCallback` reads `window_name, on_mouse` rather than the camelCase in its `cv2.cpp` docstring. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake --- modules/python/src2/cv2.cpp | 2 +- modules/python/src2/gen2.py | 10 ++++++++++ .../src2/typing_stubs_generation/api_refinement.py | 8 ++++---- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/modules/python/src2/cv2.cpp b/modules/python/src2/cv2.cpp index 8dcf25eb99..a5301f7746 100644 --- a/modules/python/src2/cv2.cpp +++ b/modules/python/src2/cv2.cpp @@ -124,7 +124,7 @@ static PyMethodDef special_methods[] = { #ifdef HAVE_OPENCV_HIGHGUI {"createTrackbar", (PyCFunction)pycvCreateTrackbar, METH_VARARGS, "createTrackbar(trackbarName, windowName, value, count, onChange) -> None"}, {"createButton", CV_PY_FN_WITH_KW(pycvCreateButton), "createButton(buttonName, onChange [, userData, buttonType, initialButtonState]) -> None"}, - {"setMouseCallback", CV_PY_FN_WITH_KW(pycvSetMouseCallback), "setMouseCallback(windowName, onMouse [, param]) -> None"}, + {"setMouseCallback", CV_PY_FN_WITH_KW(pycvSetMouseCallback), "setMouseCallback(window_name, on_mouse [, param]) -> None"}, #endif #ifdef HAVE_OPENCV_DNN {"dnn_registerLayer", CV_PY_FN_WITH_KW(pyopencv_cv_dnn_registerLayer), "registerLayer(type, class) -> None"}, diff --git a/modules/python/src2/gen2.py b/modules/python/src2/gen2.py index c8e12c60c4..0134a0a5f8 100755 --- a/modules/python/src2/gen2.py +++ b/modules/python/src2/gen2.py @@ -1508,6 +1508,16 @@ class PythonWrapperGenerator(object): for name, constinfo in constlist: self.gen_const_reg(constinfo) + # step 6: add documentation signatures for the functions wrapped by hand in cv2.cpp, + # because they are not parsed from the headers + manual_signatures = [ + ("cv::createTrackbar", "cv.createTrackbar", "trackbarName, windowName, value, count, onChange", "None"), + ("cv::createButton", "cv.createButton", "buttonName, onChange[, userData[, buttonType[, initialButtonState]]]", "None"), + ("cv::setMouseCallback", "cv.setMouseCallback", "window_name, on_mouse[, param]", "None"), + ] + for cname, py_name, arg, ret in manual_signatures: + self.py_signatures.setdefault(cname, []).append(dict(name=py_name, arg=arg, ret=ret)) + # All symbols are collected and AST is reconstructed, generating # typing stubs... self.typing_stubs_generator.generate(output_path) diff --git a/modules/python/src2/typing_stubs_generation/api_refinement.py b/modules/python/src2/typing_stubs_generation/api_refinement.py index 12868ad609..f7e851643d 100644 --- a/modules/python/src2/typing_stubs_generation/api_refinement.py +++ b/modules/python/src2/typing_stubs_generation/api_refinement.py @@ -254,17 +254,17 @@ def refine_highgui_module(root: NamespaceNode) -> None: ) """ def setMouseCallback( - windowName: str, - onMouse: Callback[[int, int, int, int, Any | None], None], + window_name: str, + on_mouse: Callback[[int, int, int, int, Any | None], None], param: Any | None = ... ) -> None: ... """ root.add_function( "setMouseCallback", [ - FunctionNode.Arg("windowName", PrimitiveTypeNode.str_()), + FunctionNode.Arg("window_name", PrimitiveTypeNode.str_()), FunctionNode.Arg( - "onMouse", + "on_mouse", CallableTypeNode("MouseCallback", [ PrimitiveTypeNode.int_(), PrimitiveTypeNode.int_(),