Skip to content
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

Module "cv2" does not explicitly export attribute "typing" #901

Closed
4 tasks done
rassie opened this issue Sep 14, 2023 · 8 comments · Fixed by opencv/opencv#24356
Closed
4 tasks done

Module "cv2" does not explicitly export attribute "typing" #901

rassie opened this issue Sep 14, 2023 · 8 comments · Fixed by opencv/opencv#24356
Assignees

Comments

@rassie
Copy link

rassie commented Sep 14, 2023

Expected behaviour

Type hints (e.g. with mypy) work properly on cv2.imdecode

Actual behaviour

cv2.imdecode return values resolve to Any.

Steps to reproduce

  • create a clean project with opencv-python-headless 4.8.0.76 and mypy 1.5.1
  • add a main.py with the following code:
import cv2

reveal_type(cv2.typing.MatLike)
reveal_type(cv2.imdecode)
  • run mypy on main.py
  • observe the following:
main.py:3: error: Module "cv2" does not explicitly export attribute "typing"  [attr-defined]
main.py:3: note: Revealed type is "Any"
main.py:4: note: Revealed type is "Overload(def (buf: Any, flags: builtins.int) -> Any, def (buf: cv2.UMat, flags: builtins.int) -> Any)"

It seems like the original override definition

@typing.overload
def imdecode(buf: cv2.typing.MatLike, flags: int) -> cv2.typing.MatLike: ...
@typing.overload
def imdecode(buf: UMat, flags: int) -> cv2.typing.MatLike: ...

can't resolve cv2.typing.MatLike and replaces it by Any. The reason seems to be the error message Module "cv2" does not explicitly export attribute "typing".

Issue submission checklist
  • This is not a generic OpenCV usage question (looking for help for coding, other usage questions, homework etc.)
  • I have read the README of this repository and understand that this repository provides only an automated build toolchain for OpenCV Python packages (there is no actual OpenCV code here)
  • The issue is related to the build scripts in this repository, to the pre-built binaries or is a feature request (such as "please enable this additional dependency")
  • I'm using the latest version of opencv-python
@asmorkalov
Copy link
Collaborator

Thanks a lot for the issue report. Most probably the issue has been resolved in OpenCV 4.x branch and the fix will be released in 4.9.0. Could you try current 4.x branch?
Possibly related PRs:

@asmorkalov asmorkalov self-assigned this Sep 18, 2023
@rassie
Copy link
Author

rassie commented Sep 18, 2023

@asmorkalov Gladly! How do I do that properly? Do I just build a wheel from this repository or is this a more involved process?

@rassie
Copy link
Author

rassie commented Sep 18, 2023

I've taken a wheel out of https://github.com/opencv/opencv-python/actions/runs/6221534986 (opencv_python_headless-4.8.0+8621b7c-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl) and tried running my test case. Sadly, it's the same problem again, so if that wheel is the right one, I'd have to assume the problem is not fixed in the newest branch.

@rassie
Copy link
Author

rassie commented Sep 18, 2023

Or maybe that's the wrong branch, let me check...

@rassie
Copy link
Author

rassie commented Sep 18, 2023

Same result with opencv_python_headless_rolling-4.8.0.76-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl from https://github.com/opencv/opencv-python/actions/runs/6204924813.

@asmorkalov asmorkalov assigned VadimLevin and unassigned asmorkalov Sep 19, 2023
@Avasam
Copy link

Avasam commented Oct 2, 2023

This is a static type-checking error saying that the module cv2 does seem to have an existing symbol typing. But it doesn't seem to be intended to be re-exported. (pyright flat out gives a "typing" is not a known member of module "cv2", mypy is a bit clearer here)

Big edit: scrap that. It's a mypy issue. it gets confused between the cv2 module importing typing and cv2.typing being its own module.

pyright handles it correctly.

import cv2
import cv2.typing

_ = cv2.typing.MatLike  # Module "cv2" does not explicitly export attribute "typing" Mypy([attr-defined](https://mypy.readthedocs.io/en/latest/_refs.html#code-attr-defined))

Edit 2: Created issue: python/mypy#16216
In the meantime, opencv could work around the issue by aliasing typing in cv2: import typing as _typing (opencv/opencv#24352). I think whether mypy is updated to handle that case, it would probably a change for the better in opencv

@rassie
Copy link
Author

rassie commented Oct 4, 2023

Thanks, guys! Will try to test it this week!

@rassie
Copy link
Author

rassie commented Oct 4, 2023

@asmorkalov @VadimLevin @Avasam I have built the wheels and can confirm it's working as expected! Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants