Skip to content

Type stubs: Slicing a cv2.mat_wrapper.Mat or derived type results in partially unknown ndarray #23780

@Avasam

Description

@Avasam

System Information

OpenCV python: opencv_python_headless-4.7.0+772599a-cp37-abi3-win_amd64.whl
Operating System / Platform: 10.0.19045 Build 19045
Python version: 3.9.13

Detailed description

pyright uses a special Unknown type to represent, well, unknown type, types that can't be inferred, unspecified Generic types, etc. It is semantically different than Any (which means literally any type is valid, or used as an escape hatch). This results in type errors when using strict typing.

mypy does not separate that concept and simply infers Any when type information is loss or unspecified.

Steps to reproduce

import cv2.mat_wrapper
import cv2.typing

# Using casts to simplify for static typing testing purposes
mat_like = cast(cv2.typing.MatLike, object())
mat = cast(cv2.mat_wrapper.Mat, object())

# Type of "mat_like_sliced" is partially unknown
#   Type of "mat_like_sliced" is "ndarray[Any, Unknown] | ndarray[Any, dtype[generic]]" Pylance(reportUnknownVariableType)
mat_like_sliced = mat_like[:, :, 3]
# Type of "mat_sliced" is partially unknown
#   Type of "mat_sliced" is "ndarray[Any, Unknown]" Pylance(reportUnknownVariableType)
mat_sliced = mat[:, :, 3]

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions