8000 fix: typing module enums references by VadimLevin · Pull Request #23813 · opencv/opencv · GitHub
[go: up one dir, main page]

Skip to content

fix: typing module enums references #23813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2023

Conversation

VadimLevin
Copy link
Contributor

Enum names exist only during type checking.
During runtime they should be denoted as named integral types

This patch fixes circular import issue introduced by #23798.

Before patch TermCriteria was defined as follows:

TermCriteria = typing.Tuple[cv2.TermCriteria_Type, int, float]
"""Any type providing sequence protocol is supported"""

which works fine during type checking, but introduces runtime failure:

>>> import cv2
...
    TermCriteria = tuple[cv2.TermCriteria_Type, int, float]  # Any type providing sequence protocol is supported
                         ^^^^^^^^^^^^^^^^^^^^^
AttributeError: partially initialized module 'cv2' has no attribute 'TermCriteria_Type' (most likely due to a circular import)

Generated __init__.py after patch will hide cv2.TermCriteria_Type enumeration by typing.TYPE_CHECKNIG guard.

if typing.TYPE_CHECKING:
    TermCriteria_Type = cv2.TermCriteria_Type
else:
    TermCriteria_Type = int

TermCriteria = typing.Tuple[TermCriteria_Type, int, float]
"""Any type providing sequence protocol is supported"""

So everything can be imported without any issues

>>> import cv2
>>> import cv2.typing
>>> cv2.typing.TermCriteria
typing.Tuple[int, int, float]
>>> 

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

Enum names exist only during type checking.
During runtime they should be denoted as named integral types
@VadimLevin VadimLevin force-pushed the dev/vlevin/runtime-typing-module branch from 1db1a9e to a3b6a5b Compare June 15, 2023 18:31
@asmorkalov
Copy link
Contributor
Python 3.8.0 (default, Dec  9 2021, 17:53:27) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/mnt/projects/Projects/OpenCV/opencv-build/install/lib/python3.8/site-packages/cv2/__init__.py", line 181, in <module>
    bootstrap()
  File "/mnt/projects/Projects/OpenCV/opencv-build/install/lib/python3.8/site-packages/cv2/__init__.py", line 175, in bootstrap
    if __load_extra_py_code_for_module("cv2", submodule, DEBUG):
  File "/mnt/projects/Projects/OpenCV/opencv-build/install/lib/python3.8/site-packages/cv2/__init__.py", line 28, in __load_extra_py_code_for_module
    py_module = importlib.import_module(module_name)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/mnt/projects/Projects/OpenCV/opencv-build/install/lib/python3.8/site-packages/cv2/typing/__init__.py", line 73, in <module>
    MatLike = typing.Union[cv2.mat_wrapper.Mat, numpy.ndarray[typing.Any, numpy.dtype[numpy.generic]]]
TypeError: 'numpy._DTypeMeta' object is not subscriptable

@VadimLevin
Copy link
Contributor Author

What NumPy version do you have?

@asmorkalov
Copy link
Contributor

numpy 1.20.2

@asmorkalov
Copy link
Contributor

Works for me with Python 3.9 and fresh numpy. There are some runtime issue with older numpy. will file separate issue on it.

@asmorkalov asmorkalov merged commit 003d048 into opencv:4.x Jun 16, 2023
@asmorkalov asmorkalov mentioned this pull request Jul 27, 2023
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.

2 participants
0