From 871ef3e60fdd8d30c0e4818dbe257875e1b14ca5 Mon Sep 17 00:00:00 2001 From: Vladimir Iglovikov Date: Thu, 15 Oct 2026 00:26:41 +0545 Subject: [PATCH] fix(python): accept scalar and empty InputArray arguments in stubs --- .../typing_stubs_generation/api_refinement.py | 3 +++ .../python/test/test_input_array_typing.py | 27 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 modules/python/test/test_input_array_typing.py diff --git a/modules/python/src2/typing_stubs_generation/api_refinement.py b/modules/python/src2/typing_stubs_generation/api_refinement.py index f23ea905d0..f1f31fbae5 100644 --- a/modules/python/src2/typing_stubs_generation/api_refinement.py +++ b/modules/python/src2/typing_stubs_generation/api_refinement.py @@ -438,6 +438,9 @@ def make_matlike_or_scalar_arg(*arg_names: str) -> Callable[[NamespaceNode, Symb NODES_TO_REFINE = { + SymbolName(("cv", ), (), "add"): make_matlike_or_scalar_arg("src2"), + SymbolName(("cv", ), (), "multiply"): make_matlike_or_scalar_arg("src2"), + SymbolName(("cv", ), (), "remap"): make_optional_arg("map2"), SymbolName(("cv", ), (), "resize"): make_optional_arg("dsize"), SymbolName(("cv", ), (), "calcHist"): make_optional_arg("mask"), SymbolName(("cv", ), (), "floodFill"): make_optional_arg("mask"), diff --git a/modules/python/test/test_input_array_typing.py b/modules/python/test/test_input_array_typing.py new file mode 100644 index 0000000000..9996499528 --- /dev/null +++ b/modules/python/test/test_input_array_typing.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python + +import cv2 as cv +import numpy as np + +from tests_common import NewOpenCVTests + + +class input_array_typing_test(NewOpenCVTests): + def test_scalar_operands(self): + image = np.ones((3, 5, 3), dtype=np.float32) + scalar = (1.0, 2.0, 3.0, 0.0) + added = cv.add(image, scalar) + multiplied = cv.multiply(image, scalar) + np.testing.assert_array_equal(added, image + np.array([1.0, 2.0, 3.0], dtype=np.float32)) + np.testing.assert_array_equal(multiplied, image * np.array([1.0, 2.0, 3.0], dtype=np.float32)) + + def test_empty_second_remap_map(self): + image = np.arange(15, dtype=np.uint8).reshape(3, 5) + x, y = np.meshgrid(np.arange(5, dtype=np.float32), np.arange(3, dtype=np.float32)) + map1, _ = cv.convertMaps(x, y, cv.CV_16SC2, nninterpolation=True) + remapped = cv.remap(image, map1, None, cv.INTER_NEAREST) + np.testing.assert_array_equal(remapped, image) + + +if __name__ == '__main__': + NewOpenCVTests.bootstrap()