System Information
- opencv-python-headless 4.10.0.82
- numpy 1.26.4
- mypy 1.10.0
- MacOS 14.5
- Python 3.12.2
Detailed description
Many return types in common OpenCV functions have a return type of cv2.typing.MatLike. The problem is that when an array with a specific dtype is passed in, the resulting output type does not consider this and instead returns a looser type.
Steps to reproduce
As one example, consider cv2.putText.
Note: This is just one example, but the underlying problem is pervasive across many OpenCV Python function return type annotations.
import numpy as np
import cv2
image = np.zeros((100, 100, 3), dtype=np.uint8)
image = cv2.putText(image, "Text", (20, 20), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 255, 0), 1)
Running Mypy:
$ mypy main.py
main.py:5: error: Incompatible types in assignment (expression has type "ndarray[Any, dtype[integer[Any] | floating[Any]]]", variable has type "ndarray[Any, dtype[unsignedinteger[_8Bit]]]") [assignment]
Found 1 error in 1 file (checked 1 source file)
This is blocking us from upgrading from OpenCV 4.8 to 4.10 (upgrading to 4.9 was similarly blocked on other type checking problems):
We cannot update to OpenCV 4.10 because every time we pass in a uint8 dtype array into OpenCV, the type checker loses this specific type on the return of the OpenCV function, which generates lots of downstream type checking errors when we pass these results into other functions or dataclasses where we restrict the type annotation to a specific dtype (e.g., ndarray[Any, np.uint8]).
Mypy configuration:
[tool.mypy]
strict = true
plugins = [
"numpy.typing.mypy_plugin",
]
Issue submission checklist
System Information
Detailed description
Many return types in common OpenCV functions have a return type of
cv2.typing.MatLike. The problem is that when an array with a specificdtypeis passed in, the resulting output type does not consider this and instead returns a looser type.Steps to reproduce
As one example, consider
cv2.putText.Running Mypy:
This is blocking us from upgrading from OpenCV 4.8 to 4.10 (upgrading to 4.9 was similarly blocked on other type checking problems):
We cannot update to OpenCV 4.10 because every time we pass in a
uint8dtype array into OpenCV, the type checker loses this specific type on the return of the OpenCV function, which generates lots of downstream type checking errors when we pass these results into other functions or dataclasses where we restrict the type annotation to a specificdtype(e.g.,ndarray[Any, np.uint8]).Mypy configuration:
Issue submission checklist