mirror of
https://github.com/opencv/opencv.git
synced 2026-07-29 15:23:05 +04:00
Merge pull request #24022 from VadimLevin:dev/vlevin/python-typing-cuda
Fix python typing stubs generation for CUDA modules #24022 resolves #23946 resolves #23945 resolves opencv/opencv-python#871 ### 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. - [x] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
@@ -3,17 +3,20 @@ __all__ = ("generate_typing_stubs", )
|
||||
from io import StringIO
|
||||
from pathlib import Path
|
||||
import re
|
||||
from typing import (Generator, Type, Callable, NamedTuple, Union, Set, Dict,
|
||||
from typing import (Type, Callable, NamedTuple, Union, Set, Dict,
|
||||
Collection, Tuple, List)
|
||||
import warnings
|
||||
|
||||
from .ast_utils import get_enclosing_namespace, get_enum_module_and_export_name
|
||||
from .ast_utils import (get_enclosing_namespace,
|
||||
get_enum_module_and_export_name,
|
||||
for_each_function_overload,
|
||||
for_each_class)
|
||||
|
||||
from .predefined_types import PREDEFINED_TYPES
|
||||
from .api_refinement import apply_manual_api_refinement
|
||||
|
||||
from .nodes import (ASTNode, ASTNodeType, NamespaceNode, ClassNode, FunctionNode,
|
||||
EnumerationNode, ConstantNode)
|
||||
from .nodes import (ASTNode, ASTNodeType, NamespaceNode, ClassNode,
|
||||
FunctionNode, EnumerationNode, ConstantNode)
|
||||
|
||||
from .nodes.type_node import (TypeNode, AliasTypeNode, AliasRefTypeNode,
|
||||
AggregatedTypeNode, ASTNodeTypeNode,
|
||||
@@ -105,8 +108,9 @@ def _generate_typing_stubs(root: NamespaceNode, output_path: Path) -> None:
|
||||
|
||||
# NOTE: Enumerations require special handling, because all enumeration
|
||||
# constants are exposed as module attributes
|
||||
has_enums = _generate_section_stub(StubSection("# Enumerations", EnumerationNode),
|
||||
root, output_stream, 0)
|
||||
has_enums = _generate_section_stub(
|
||||
StubSection("# Enumerations", EnumerationNode), root, output_stream, 0
|
||||
)
|
||||
# Collect all enums from class level and export them to module level
|
||||
for class_node in root.classes.values():
|
||||
if _generate_enums_from_classes_tree(class_node, output_stream, indent=0):
|
||||
@@ -536,30 +540,6 @@ def check_overload_presence(node: Union[NamespaceNode, ClassNode]) -> bool:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def _for_each_class(node: Union[NamespaceNode, ClassNode]) \
|
||||
-> Generator[ClassNode, None, None]:
|
||||
for cls in node.classes.values():
|
||||
yield cls
|
||||
if len(cls.classes):
|
||||
yield from _for_each_class(cls)
|
||||
|
||||
|
||||
def _for_each_function(node: Union[NamespaceNode, ClassNode]) \
|
||||
-> Generator[FunctionNode, None, None]:
|
||||
for func in node.functions.values():
|
||||
yield func
|
||||
for cls in node.classes.values():
|
||||
yield from _for_each_function(cls)
|
||||
|
||||
|
||||
def _for_each_function_overload(node: Union[NamespaceNode, ClassNode]) \
|
||||
-> Generator[FunctionNode.Overload, None, None]:
|
||||
for func in _for_each_function(node):
|
||||
for overload in func.overloads:
|
||||
yield overload
|
||||
|
||||
|
||||
def _collect_required_imports(root: NamespaceNode) -> Set[str]:
|
||||
"""Collects all imports required for classes and functions typing stubs
|
||||
declarations.
|
||||
@@ -582,7 +562,7 @@ def _collect_required_imports(root: NamespaceNode) -> Set[str]:
|
||||
has_overload = check_overload_presence(root)
|
||||
# if there is no module-level functions with overload, check its presence
|
||||
# during class traversing, including their inner-classes
|
||||
for cls in _for_each_class(root):
|
||||
for cls in for_each_class(root):
|
||||
if not has_overload and check_overload_presence(cls):
|
||||
has_overload = True
|
||||
required_imports.add("import typing")
|
||||
@@ -600,8 +580,9 @@ def _collect_required_imports(root: NamespaceNode) -> Set[str]:
|
||||
if has_overload:
|
||||
required_imports.add("import typing")
|
||||
# Importing modules required to resolve functions arguments
|
||||
for overload in _for_each_function_overload(root):
|
||||
for arg in filter(lambda a: a.type_node is not None, overload.arguments):
|
||||
for overload in for_each_function_overload(root):
|
||||
for arg in filter(lambda a: a.type_node is not None,
|
||||
overload.arguments):
|
||||
_add_required_usage_imports(arg.type_node, required_imports) # type: ignore
|
||||
if overload.return_type is not None:
|
||||
_add_required_usage_imports(overload.return_type.type_node,
|
||||
@@ -625,11 +606,13 @@ def _populate_reexported_symbols(root: NamespaceNode) -> None:
|
||||
|
||||
_reexport_submodule(root)
|
||||
|
||||
# Special cases, symbols defined in possible pure Python submodules should be
|
||||
# Special cases, symbols defined in possible pure Python submodules
|
||||
# should be
|
||||
root.reexported_submodules_symbols["mat_wrapper"].append("Mat")
|
||||
|
||||
|
||||
def _write_reexported_symbols_section(module: NamespaceNode, output_stream: StringIO) -> None:
|
||||
def _write_reexported_symbols_section(module: NamespaceNode,
|
||||
output_stream: StringIO) -> None:
|
||||
"""Write re-export section for the given module.
|
||||
|
||||
Re-export statements have from `from module_name import smth as smth`.
|
||||
|
||||
Reference in New Issue
Block a user