Skip to content

fix: conditionally define generic NumPy NDArray alias#23838

Merged
asmorkalov merged 1 commit intoopencv:4.xfrom
VadimLevin:dev/vlevin/numpy-array-typing
Jun 20, 2023
Merged

fix: conditionally define generic NumPy NDArray alias#23838
asmorkalov merged 1 commit intoopencv:4.xfrom
VadimLevin:dev/vlevin/numpy-array-typing

Conversation

@VadimLevin
Copy link
Copy Markdown
Contributor

This patch introduces conditional alias definition.

  • Fixes compatibility with NumPy versions < 1.20 and Python < 3.9
  • Updates enums handling to utilise introduced ConditionalAliasTypeNode

Generated cv2/typing/__init__.py content after patch:

import cv2.mat_wrapper
import numpy
import sys
import typing

if numpy.lib.NumpyVersion(numpy.__version__) > "1.20.0" and sys.version_info >= (3, 9):
    NumPyArrayGeneric = numpy.ndarray[typing.Any, numpy.dtype[numpy.generic]]
else:
    NumPyArrayGeneric = numpy.ndarray


if numpy.lib.NumpyVersion(numpy.__version__) > "1.20.0" and sys.version_info >= (3, 9):
    NumPyArrayFloat32 = numpy.ndarray[typing.Any, numpy.dtype[numpy.float32]]
else:
    NumPyArrayFloat32 = numpy.ndarray


if numpy.lib.NumpyVersion(numpy.__version__) > "1.20.0" and sys.version_info >= (3, 9):
    NumPyArrayFloat64 = numpy.ndarray[typing.Any, numpy.dtype[numpy.float64]]
else:
    NumPyArrayFloat64 = numpy.ndarray

MatLike = typing.Union[cv2.mat_wrapper.Mat, NumPyArrayGeneric]
Matx33f = NumPyArrayFloat32
"""NDArray(shape=(3, 3), dtype=numpy.float32)"""
Matx33d = NumPyArrayFloat64
"""NDArray(shape=(3, 3), dtype=numpy.float64)"""
Matx44f = NumPyArrayFloat32
"""NDArray(shape=(4, 4), dtype=numpy.float32)"""
Matx44d = NumPyArrayFloat64
"""NDArray(shape=(4, 4), dtype=numpy.float64)"""

Resolves: #23822

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • 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
  • The PR is proposed to the proper branch
  • 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.
  • The feature is well documented and sample code can be built with the project CMake

@asmorkalov asmorkalov merged commit 94211fb into opencv:4.x Jun 20, 2023
@VadimLevin VadimLevin mentioned this pull request Jul 7, 2023
6 tasks
@asmorkalov asmorkalov mentioned this pull request Jul 27, 2023
thewoz pushed a commit to thewoz/opencv that referenced this pull request Jan 4, 2024
thewoz pushed a commit to thewoz/opencv that referenced this pull request May 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python Typing module is incompatible with some Numpy versions

2 participants