Skip to content

feat: manual refinement for Python API definition#23801

Merged
asmorkalov merged 2 commits intoopencv:4.xfrom
VadimLevin:dev/vlevin/python-stubs-api-refinement
Jun 21, 2023
Merged

feat: manual refinement for Python API definition#23801
asmorkalov merged 2 commits intoopencv:4.xfrom
VadimLevin:dev/vlevin/python-stubs-api-refinement

Conversation

@VadimLevin
Copy link
Copy Markdown
Contributor

@VadimLevin VadimLevin commented Jun 14, 2023

These patch introduce a customisation point to manually refine autogenerated stubs to fulfil conventions that are not expressed in C++ API.
Example: functions accepting "empty" arguments in their C++ API accepts None in Python (cv::resize accepts empty cv::Size as dsize).

  • Mark resize dsize and calcHist mask arguments as optional regardless of their C++ API optionality

Before patch

@typing.overload
def resize(src: cv2.typing.MatLike, dsize: cv2.typing.Size, dst: cv2.typing.MatLike | None = ..., fx: float = ..., fy: float = ..., interpolation: int = ...) -> cv2.typing.MatLike: ...
@typing.overload
def resize(src: UMat, dsize: cv2.typing.Size, dst: UMat | None = ..., fx: float = ..., fy: float = ..., interpolation: int = ...) -> UMat: ...

@typing.overload
def calcHist(images: typing.Sequence[cv2.typing.MatLike], channels: typing.Sequence[int], mask: cv2.typing.MatLike, histSize: typing.Sequence[int], ranges: typing.Sequence[float], hist: cv2.typing.MatLike | None = ..., accumulate: bool = ...) -> cv2.typing.MatLike: ...
@typing.overload
def calcHist(images: typing.Sequence[UMat], channels: typing.Sequence[int], mask: UMat, histSize: typing.Sequence[int], ranges: typing.Sequence[float], hist: UMat | None = ..., accumulate: bool = ...) -> UMat: ...

After patch:

@typing.overload
def resize(src: cv2.typing.MatLike, dsize: cv2.typing.Size | None, dst: cv2.typing.MatLike | None = ..., fx: float = ..., fy: float = ..., interpolation: int = ...) -> cv2.typing.MatLike: ...
@typing.overload
def resize(src: UMat, dsize: cv2.typing.Size | None, dst: UMat | None = ..., fx: float = ..., fy: float = ..., interpolation: int = ...) -> UMat: ...


@typing.overload
def calcHist(images: typing.Sequence[cv2.typing.MatLike], channels: typing.Sequence[int], mask: cv2.typing.MatLike | None, histSize: typing.Sequence[int], ranges: typing.Sequence[float], hist: cv2.typing.MatLike | None = ..., accumulate: bool = ...) -> cv2.typing.MatLike: ...
@typing.overload
def calcHist(images: typing.Sequence[UMat], channels: typing.Sequence[int], mask: UMat | None, histSize: typing.Sequence[int], ranges: typing.Sequence[float], hist: UMat | None = ..., accumulate: bool = ...) -> UMat: ...

Resolves: #23779

  • Export OpenCV error class to cv2/__init__.pyi - custom exception class
class error(Exception):
    ...

Resolves: #23781

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

Mark `resize` and `calcHist` arguments as optional regardless of
their C++ API optionality
@VadimLevin VadimLevin added this to the 4.8.0 milestone Jun 14, 2023
@VadimLevin VadimLevin requested a review from asmorkalov June 14, 2023 18:40
@VadimLevin VadimLevin self-assigned this Jun 14, 2023
Copy link
Copy Markdown
Contributor

@asmorkalov asmorkalov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@asmorkalov asmorkalov merged commit fc81043 into opencv:4.x Jun 21, 2023
@asmorkalov asmorkalov self-assigned this Jun 21, 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.

Type stubs: Missing the cv2.error class Type stubs: Optional parameters incorrectly marked as not-None in cv2.calcHist and cv2.resize

2 participants