Merge pull request #24356 from VadimLevin:dev/vlevin/typing-re-export

feat: re-export cv2.typing module as typing
pull/24359/head
Alexander Smorkalov 1 year ago committed by GitHub
commit 670c52f75e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      modules/python/src2/typing_stubs_generation/generation.py
  2. 38
      modules/python/src2/typing_stubs_generation/nodes/type_node.py

@ -444,7 +444,7 @@ def _generate_function_stub(function_node: FunctionNode,
elif function_node.is_static:
decorators.append(" " * indent + "@staticmethod")
if len(function_node.overloads) > 1:
decorators.append(" " * indent + "@typing.overload")
decorators.append(" " * indent + "@_typing.overload")
function_module = get_enclosing_namespace(function_node)
function_module_name = function_module.full_export_name
@ -578,7 +578,7 @@ def _collect_required_imports(root: NamespaceNode) -> Collection[str]:
for cls in for_each_class(root):
if not has_overload and check_overload_presence(cls):
has_overload = True
required_imports.add("import typing")
required_imports.add("import typing as _typing")
# Add required imports for class properties
for prop in cls.properties:
_add_required_usage_imports(prop.type_node, required_imports)
@ -593,7 +593,7 @@ def _collect_required_imports(root: NamespaceNode) -> Collection[str]:
has_protocol = True
if has_overload:
required_imports.add("import typing")
required_imports.add("import typing as _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,
@ -634,6 +634,8 @@ def _populate_reexported_symbols(root: NamespaceNode) -> None:
_reexport_submodule(root)
root.reexported_submodules.append("typing")
# Special cases, symbols defined in possible pure Python submodules
# should be
root.reexported_submodules_symbols["mat_wrapper"].append("Mat")
@ -735,10 +737,10 @@ def _generate_typing_module(root: NamespaceNode, output_path: Path) -> None:
)
return ConditionalAliasTypeNode(
enum_export_name,
"typing.TYPE_CHECKING",
"_typing.TYPE_CHECKING",
positive_branch_type=enum_node_alias,
negative_branch_type=PrimitiveTypeNode.int_(enum_export_name),
condition_required_imports=("import typing", )
condition_required_imports=("import typing as _typing", )
)
def register_alias(alias_node: AliasTypeNode) -> None:

@ -163,11 +163,11 @@ class AnyTypeNode(TypeNode):
"""
@property
def typename(self) -> str:
return "typing.Any"
return "_typing.Any"
@property
def required_usage_imports(self) -> Generator[str, None, None]:
yield "import typing"
yield "import typing as _typing"
class PrimitiveTypeNode(TypeNode):
@ -474,11 +474,11 @@ class ConditionalAliasTypeNode(TypeNode):
"""Type subscription is not possible in python 3.8 and older numpy versions."""
return cls(
ctype_name,
"typing.TYPE_CHECKING",
"_typing.TYPE_CHECKING",
NDArrayTypeNode(ctype_name, shape, dtype),
NDArrayTypeNode(ctype_name, shape, dtype,
use_numpy_generics=False),
condition_required_imports=("import typing",)
condition_required_imports=("import typing as _typing",)
)
@ -499,14 +499,14 @@ class NDArrayTypeNode(TypeNode):
if self._use_numpy_generics:
# NOTE: Shape is not fully supported yet
dtype = self.dtype if self.dtype is not None else "numpy.generic"
return f"numpy.ndarray[typing.Any, numpy.dtype[{dtype}]]"
return f"numpy.ndarray[_typing.Any, numpy.dtype[{dtype}]]"
return "numpy.ndarray"
@property
def required_usage_imports(self) -> Generator[str, None, None]:
yield "import numpy"
# if self.shape is None:
yield "import typing"
yield "import typing as _typing"
class ASTNodeTypeNode(TypeNode):
@ -668,13 +668,13 @@ class ContainerTypeNode(AggregatedTypeNode):
@property
def required_definition_imports(self) -> Generator[str, None, None]:
yield "import typing"
yield "import typing as _typing"
yield from super().required_definition_imports
@property
def required_usage_imports(self) -> Generator[str, None, None]:
if TypeNode.compatible_to_runtime_usage:
yield "import typing"
yield "import typing as _typing"
yield from super().required_usage_imports
@abc.abstractproperty
@ -695,7 +695,7 @@ class SequenceTypeNode(ContainerTypeNode):
@property
def type_format(self) -> str:
return "typing.Sequence[{}]"
return "_typing.Sequence[{}]"
@property
def types_separator(self) -> str:
@ -709,7 +709,7 @@ class TupleTypeNode(ContainerTypeNode):
@property
def type_format(self) -> str:
if TypeNode.compatible_to_runtime_usage:
return "typing.Tuple[{}]"
return "_typing.Tuple[{}]"
return "tuple[{}]"
@property
@ -723,7 +723,7 @@ class UnionTypeNode(ContainerTypeNode):
@property
def type_format(self) -> str:
if TypeNode.compatible_to_runtime_usage:
return "typing.Union[{}]"
return "_typing.Union[{}]"
return "{}"
@property
@ -743,7 +743,7 @@ class OptionalTypeNode(ContainerTypeNode):
@property
def type_format(self) -> str:
if TypeNode.compatible_to_runtime_usage:
return "typing.Optional[{}]"
return "_typing.Optional[{}]"
return "{} | None"
@property
@ -769,7 +769,7 @@ class DictTypeNode(ContainerTypeNode):
@property
def type_format(self) -> str:
if TypeNode.compatible_to_runtime_usage:
return "typing.Dict[{}]"
return "_typing.Dict[{}]"
return "dict[{}]"
@property
@ -810,32 +810,32 @@ class CallableTypeNode(AggregatedTypeNode):
@property
def typename(self) -> str:
return 'typing.Callable[[{}], {}]'.format(
return '_typing.Callable[[{}], {}]'.format(
', '.join(arg.typename for arg in self.arg_types),
self.ret_type.typename
)
@property
def full_typename(self) -> str:
return 'typing.Callable[[{}], {}]'.format(
return '_typing.Callable[[{}], {}]'.format(
', '.join(arg.full_typename for arg in self.arg_types),
self.ret_type.full_typename
)
def relative_typename(self, module: str) -> str:
return 'typing.Callable[[{}], {}]'.format(
return '_typing.Callable[[{}], {}]'.format(
', '.join(arg.relative_typename(module) for arg in self.arg_types),
self.ret_type.relative_typename(module)
)
@property
def required_definition_imports(self) -> Generator[str, None, None]:
yield "import typing"
yield "import typing as _typing"
yield from super().required_definition_imports
@property
def required_usage_imports(self) -> Generator[str, None, None]:
yield "import typing"
yield "import typing as _typing"
yield from super().required_usage_imports
@ -847,7 +847,7 @@ class ClassTypeNode(ContainerTypeNode):
@property
def type_format(self) -> str:
return "typing.Type[{}]"
return "_typing.Type[{}]"
@property
def types_separator(self) -> str:

Loading…
Cancel
Save