mirror of https://github.com/opencv/opencv.git
Mark `resize` and `calcHist` arguments as optional regardless of their C++ API optionalitypull/23801/head
parent
19f4f2eb92
commit
5859a531e5
5 changed files with 104 additions and 14 deletions
@ -0,0 +1,44 @@ |
||||
__all__ = [ |
||||
"apply_manual_api_refinement" |
||||
] |
||||
|
||||
from typing import Sequence, Callable |
||||
from .nodes import NamespaceNode, FunctionNode, OptionalTypeNode |
||||
from .ast_utils import find_function_node, SymbolName |
||||
|
||||
|
||||
def apply_manual_api_refinement(root: NamespaceNode) -> None: |
||||
for symbol_name, refine_symbol in NODES_TO_REFINE.items(): |
||||
refine_symbol(root, symbol_name) |
||||
|
||||
|
||||
def make_optional_arg(arg_name: 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 overload in function.overloads: |
||||
arg_idx = _find_argument_index(overload.arguments, arg_name) |
||||
# Avoid multiplying optional qualification |
||||
if isinstance(overload.arguments[arg_idx].type_node, OptionalTypeNode): |
||||
continue |
||||
|
||||
overload.arguments[arg_idx].type_node = OptionalTypeNode( |
||||
overload.arguments[arg_idx].type_node |
||||
) |
||||
|
||||
return _make_optional_arg |
||||
|
||||
|
||||
def _find_argument_index(arguments: Sequence[FunctionNode.Arg], name: str) -> int: |
||||
for i, arg in enumerate(arguments): |
||||
if arg.name == name: |
||||
return i |
||||
raise RuntimeError( |
||||
f"Failed to find argument with name: '{name}' in {arguments}" |
||||
) |
||||
|
||||
|
||||
NODES_TO_REFINE = { |
||||
SymbolName(("cv", ), (), "resize"): make_optional_arg("dsize"), |
||||
SymbolName(("cv", ), (), "calcHist"): make_optional_arg("mask"), |
||||
} |
Loading…
Reference in new issue