From caa09aca368066011bdf4f2adbffa935ffe60912 Mon Sep 17 00:00:00 2001 From: Vadim Levin Date: Fri, 12 Apr 2024 15:10:59 +0300 Subject: [PATCH] feat: use numeric dtype for MatLike instead of generic --- modules/core/misc/python/package/mat_wrapper/__init__.py | 9 +++++---- .../src2/typing_stubs_generation/predefined_types.py | 7 +++++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/modules/core/misc/python/package/mat_wrapper/__init__.py b/modules/core/misc/python/package/mat_wrapper/__init__.py index 7cbc0645de..8a1e4580c9 100644 --- a/modules/core/misc/python/package/mat_wrapper/__init__.py +++ b/modules/core/misc/python/package/mat_wrapper/__init__.py @@ -4,15 +4,16 @@ import numpy as np import cv2 as cv from typing import TYPE_CHECKING, Any -# Same as cv2.typing.NumPyArrayGeneric, but avoids circular dependencies +# Same as cv2.typing.NumPyArrayNumeric, but avoids circular dependencies if TYPE_CHECKING: - _NumPyArrayGeneric = np.ndarray[Any, np.dtype[np.generic]] + _NumPyArrayNumeric = np.ndarray[Any, np.dtype[np.integer[Any] | np.floating[Any]]] else: - _NumPyArrayGeneric = np.ndarray + _NumPyArrayNumeric = np.ndarray # NumPy documentation: https://numpy.org/doc/stable/user/basics.subclassing.html -class Mat(_NumPyArrayGeneric): + +class Mat(_NumPyArrayNumeric): ''' cv.Mat wrapper for numpy array. diff --git a/modules/python/src2/typing_stubs_generation/predefined_types.py b/modules/python/src2/typing_stubs_generation/predefined_types.py index 0514a14dda..f5ba0bc29e 100644 --- a/modules/python/src2/typing_stubs_generation/predefined_types.py +++ b/modules/python/src2/typing_stubs_generation/predefined_types.py @@ -34,7 +34,10 @@ _PREDEFINED_TYPES = ( PrimitiveTypeNode.str_("char"), PrimitiveTypeNode.str_("String"), PrimitiveTypeNode.str_("c_string"), - ConditionalAliasTypeNode.numpy_array_("NumPyArrayGeneric"), + ConditionalAliasTypeNode.numpy_array_( + "NumPyArrayNumeric", + dtype="numpy.integer[_typing.Any] | numpy.floating[_typing.Any]" + ), ConditionalAliasTypeNode.numpy_array_("NumPyArrayFloat32", dtype="numpy.float32"), ConditionalAliasTypeNode.numpy_array_("NumPyArrayFloat64", dtype="numpy.float64"), NoneTypeNode("void"), @@ -42,7 +45,7 @@ _PREDEFINED_TYPES = ( AliasTypeNode.union_( "Mat", items=(ASTNodeTypeNode("Mat", module_name="cv2.mat_wrapper"), - AliasRefTypeNode("NumPyArrayGeneric")), + AliasRefTypeNode("NumPyArrayNumeric")), export_name="MatLike" ), AliasTypeNode.sequence_("MatShape", PrimitiveTypeNode.int_()),