Skip to content

Python typing stub #14590

@milliams

Description

@milliams
System information (version)
  • OpenCV => 4.1
  • Operating System / Platform => Linux
  • Compiler => GCC 8.3.1
Detailed description

When importing cv2 as a Python module and using a tool like mypy or an IDE like PyCharm they have no information about the functions etc. in the module.

I propose adapting the Python API generating script gen2.py to do two things:

  1. Change the signature description to include type information in the modern Python style.
  2. Create a Python typing stub file which will allow tools to know what functions are available and what types they expect/return.

By type information in the signature I mean, change for example:

def GaussianBlur(src, ksize, sigmaX[, dst[, sigmaY[, borderType]]]) -> dst

to

def GaussianBlur(src: Union[Mat, UMat],
                 ksize: Size,
                 sigmaX: int,
                 dst: Optional[Union[Mat, UMat]] = None,
                 sigmaY: int = 0,
                 borderType: int = BORDER_DEFAULT
                ) -> Union[Mat, UMat]

Once you have these adapted signature strings, you can put them into a file called __init__.pyi in the cv2 Python directory and the tools will find it.

Even if it is not feasible to get full typing support (due to ambiguity for some types), it will at least provide autocompletion support for IDEs.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions