diff --git a/modules/python/src2/typing_stubs_generation/api_refinement.py b/modules/python/src2/typing_stubs_generation/api_refinement.py index 599830f8d83e4d965996c00e0ce7bc8a5081e805..f7d3a9dd083fcdb8408dd889bf3084c3c33a62b2 100644 --- a/modules/python/src2/typing_stubs_generation/api_refinement.py +++ b/modules/python/src2/typing_stubs_generation/api_refinement.py @@ -4,11 +4,13 @@ __all__ = [ from typing import Sequence, Callable -from .nodes import NamespaceNode, FunctionNode, OptionalTypeNode, ClassProperty, PrimitiveTypeNode +from .nodes import (NamespaceNode, FunctionNode, OptionalTypeNode, + ClassProperty, PrimitiveTypeNode) from .ast_utils import find_function_node, SymbolName def apply_manual_api_refinement(root: NamespaceNode) -> None: + export_matrix_type_constants(root) # Export OpenCV exception class builtin_exception = root.add_class("Exception") builtin_exception.is_exported = False @@ -17,6 +19,33 @@ def apply_manual_api_refinement(root: NamespaceNode) -> None: refine_symbol(root, symbol_name) +def export_matrix_type_constants(root: NamespaceNode) -> None: + MAX_PREDEFINED_CHANNELS = 4 + + depth_names = ("CV_8U", "CV_8S", "CV_16U", "CV_16S", "CV_32S", + "CV_32F", "CV_64F", "CV_16F") + for depth_value, depth_name in enumerate(depth_names): + # Export depth constants + root.add_constant(depth_name, str(depth_value)) + # Export predefined types + for c in range(MAX_PREDEFINED_CHANNELS): + root.add_constant(f"{depth_name}C{c + 1}", + f"{depth_value + 8 * c}") + # Export type creation function + root.add_function( + f"{depth_name}C", + (FunctionNode.Arg("channels", PrimitiveTypeNode.int_()), ), + FunctionNode.RetType(PrimitiveTypeNode.int_()) + ) + # Export CV_MAKETYPE + root.add_function( + "CV_MAKETYPE", + (FunctionNode.Arg("depth", PrimitiveTypeNode.int_()), + FunctionNode.Arg("channels", PrimitiveTypeNode.int_())), + FunctionNode.RetType(PrimitiveTypeNode.int_()) + ) + + def make_optional_arg(arg_name: str) -> Callable[[NamespaceNode, SymbolName], None]: def _make_optional_arg(root_node: NamespaceNode, function_symbol_name: SymbolName) -> None: diff --git a/modules/python/src2/typing_stubs_generation/generation.py b/modules/python/src2/typing_stubs_generation/generation.py index ea6b64bde0c4228f6ad62d36b0c4ba57f35717f4..ec80bbbc05eefc142b04f6addc0b8e0dcbefc9cd 100644 --- a/modules/python/src2/typing_stubs_generation/generation.py +++ b/modules/python/src2/typing_stubs_generation/generation.py @@ -103,9 +103,6 @@ def _generate_typing_stubs(root: NamespaceNode, output_path: Path) -> None: _write_reexported_symbols_section(root, output_stream) - # Write constants section, because constants don't impose any dependencies - _generate_section_stub(StubSection("# Constants", ConstantNode), root, - output_stream, 0) # NOTE: Enumerations require special handling, because all enumeration # constants are exposed as module attributes has_enums = _generate_section_stub(StubSection("# Enumerations", EnumerationNode),