Update usages of ConditionalAliasTypeNode following #23838 to use TYPE_CHECKING

pull/23972/head
Avasam 2 years ago
parent 99058ee30b
commit cd9f85dbda
  1. 8
      modules/core/misc/python/package/mat_wrapper/__init__.py
  2. 15
      modules/python/src2/typing_stubs_generation/nodes/type_node.py

@ -4,15 +4,15 @@ import numpy as np
import cv2 as cv import cv2 as cv
from typing import TYPE_CHECKING, Any from typing import TYPE_CHECKING, Any
# Type subscription is not possible in python 3.8 # Same as cv2.typing.NumPyArrayGeneric, but avoids circular dependencies
if TYPE_CHECKING: if TYPE_CHECKING:
_NDArray = np.ndarray[Any, np.dtype[np.generic]] _NumPyArrayGeneric = np.ndarray[Any, np.dtype[np.generic]]
else: else:
_NDArray = np.ndarray _NumPyArrayGeneric = np.ndarray
# NumPy documentation: https://numpy.org/doc/stable/user/basics.subclassing.html # NumPy documentation: https://numpy.org/doc/stable/user/basics.subclassing.html
class Mat(_NDArray): class Mat(_NumPyArrayGeneric):
''' '''
cv.Mat wrapper for numpy array. cv.Mat wrapper for numpy array.

@ -395,9 +395,12 @@ class AliasTypeNode(TypeNode):
class ConditionalAliasTypeNode(TypeNode): class ConditionalAliasTypeNode(TypeNode):
"""Type node representing an alias protected by condition checked in runtime. """Type node representing an alias protected by condition checked in runtime.
For typing-related conditions, prefer using typing.TYPE_CHECKING. For a full explanation, see:
https://github.com/opencv/opencv/pull/23927#discussion_r1256326835
Example: Example:
```python ```python
if numpy.lib.NumpyVersion(numpy.__version__) > "1.20.0" and sys.version_info >= (3, 9) if typing.TYPE_CHECKING
NumPyArray = numpy.ndarray[typing.Any, numpy.dtype[numpy.generic]] NumPyArray = numpy.ndarray[typing.Any, numpy.dtype[numpy.generic]]
else: else:
NumPyArray = numpy.ndarray NumPyArray = numpy.ndarray
@ -407,10 +410,10 @@ class ConditionalAliasTypeNode(TypeNode):
ConditionalAliasTypeNode( ConditionalAliasTypeNode(
"NumPyArray", "NumPyArray",
'numpy.lib.NumpyVersion(numpy.__version__) > "1.20.0" and sys.version_info >= (3, 9)', 'typing.TYPE_CHECKING',
NDArrayTypeNode("NumPyArray"), NDArrayTypeNode("NumPyArray"),
NDArrayTypeNode("NumPyArray", use_numpy_generics=False), NDArrayTypeNode("NumPyArray", use_numpy_generics=False),
condition_required_imports=("import numpy", "import sys") condition_required_imports=("import typing",)
) )
``` ```
""" """
@ -468,14 +471,14 @@ class ConditionalAliasTypeNode(TypeNode):
def numpy_array_(cls, ctype_name: str, export_name: Optional[str] = None, def numpy_array_(cls, ctype_name: str, export_name: Optional[str] = None,
shape: Optional[Tuple[int, ...]] = None, shape: Optional[Tuple[int, ...]] = None,
dtype: Optional[str] = None): dtype: Optional[str] = None):
"""Type subscription is not possible in python 3.8 and older numpy versions."""
return cls( return cls(
ctype_name, ctype_name,
('numpy.lib.NumpyVersion(numpy.__version__) > "1.20.0" ' "typing.TYPE_CHECKING",
'and sys.version_info >= (3, 9)'),
NDArrayTypeNode(ctype_name, shape, dtype), NDArrayTypeNode(ctype_name, shape, dtype),
NDArrayTypeNode(ctype_name, shape, dtype, NDArrayTypeNode(ctype_name, shape, dtype,
use_numpy_generics=False), use_numpy_generics=False),
condition_required_imports=("import numpy", "import sys") condition_required_imports=("import typing",)
) )

Loading…
Cancel
Save