Repository: opencv/opencv
File: modules/python/src2/typing_stubs_generation/api_refinement.py (Python)

Code before:
        # 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_names: str) -> Callable[[NamespaceNode, SymbolName], None]:
    def _make_optional_arg(root_node: NamespaceNode,
                           function_symbol_name: SymbolName) -> None:
        for arg_name in arg_names:
            found_overload_with_arg = False

            function = find_function_node(root_node, function_symbol_name)
            for overload in function.overloads:
                arg_idx = _find_argument_index(overload.arguments, arg_name)

                # skip overloads without this argument
                if arg_idx is None:
                    continue

                # Avoid multiplying optional qualification
                if isinstance(overload.arguments[arg_idx].type_node, OptionalTypeNode):
                    continue

                overload.arguments[arg_idx].type_node = OptionalTypeNode(
                    cast(TypeNode, overload.arguments[arg_idx].type_node)
                )

                found_overload_with_arg = True

            if not found_overload_with_arg:
                raise RuntimeError(
                    f"Failed to find argument with name: '{arg_name}'"
                    f" in '{function_symbol_name.name}' overloads"
                )

    return _make_optional_arg


Review comment (suggestion):
Search for `function` node with `function_symbol_name` should be done before the `for arg_name in arg_names` loop.



Code after:
        # 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_names: str) -> Callable[[NamespaceNode, SymbolName], None]:
    def _make_optional_arg(root_node: NamespaceNode,
                           function_symbol_name: SymbolName) -> None:
        function = find_function_node(root_node, function_symbol_name)
        for arg_name in arg_names:
            found_overload_with_arg = False

            for overload in function.overloads:
                arg_idx = _find_argument_index(overload.arguments, arg_name)

                # skip overloads without this argument
                if arg_idx is None:
                    continue

                # Avoid multiplying optional qualification
                if isinstance(overload.arguments[arg_idx].type_node, OptionalTypeNode):
                    continue

                overload.arguments[arg_idx].type_node = OptionalTypeNode(
                    cast(TypeNode, overload.arguments[arg_idx].type_node)
                )

                found_overload_with_arg = True

            if not found_overload_with_arg:
                raise RuntimeError(
                    f"Failed to find argument with name: '{arg_name}'"
                    f" in '{function_symbol_name.name}' overloads"
                )

    return _make_optional_arg