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

Classes for opencv-python are not detected and break intellisense autocomplete #3976

Closed
elesbb opened this issue Feb 18, 2023 · 10 comments
Closed
Assignees

Comments

@elesbb
Copy link

elesbb commented Feb 18, 2023

videoReader: cv2.VideoCapture = cv2.VideoCapture(videoFileLocation) fps = int(videoReader.

After typing the "dot" after the "videoReader" variable, there is no autocompletion. After hovering over "cv2.VideoCapture", it shows "Any" as class type.

@karthiknadig karthiknadig transferred this issue from microsoft/vscode-python Feb 18, 2023
@erictraut
Copy link
Contributor

This is a duplicate of #138.

@elesbb
Copy link
Author

elesbb commented Feb 18, 2023

This is a duplicate of #138.

I don't think so.

I read this before posting an issue. The issue is NOT having "cv2." autocomplete, but any classes INSIDE cv2 do not have autocompletion nor class typing. All the "fixes" mentioned there don't fix the issue I am having.

I have never needed any "fixes" or anything for pycharm to do the same thing either. This is definitely a VS Code issue.

@erictraut
Copy link
Contributor

erictraut commented Feb 18, 2023

Which workarounds have you tried? Have you generated your own local type stubs for cv2 using "stubgen" or similar tools? Without type information, pylance will not be able to provide any autocomplete suggestions. Most libraries ship with Python code, and pylance can analyze the code to infer type information. However, cv2 is not written in Python, so pylance has no code to analyze. Most libraries that are not written in Python ship with additional type information in the form of "type stub" (.pyi) files. Unfortunately, cv2 does not. If this library is important to you, you may want to encourage the maintainers to include type stubs in a future version. In the meantime, you would need to create your own type stubs (or install a type stub package created by someone else) if you want pylance to provide completion suggestions for symbols in the cv2 library.

PyCharm will extract minimal type information out of a binary library like cv2. It does so by loading (and therefore executing) the library and then using reflection to extract classes, methods, etc. Pylance does not do this for security and performance reasons. You would need to do this manually using a tool like stubgen to extract the type information.

@elesbb
Copy link
Author

elesbb commented Feb 18, 2023

Which workarounds have you tried? Have you generated your own local type stubs for cv2 using "stubgen" or similar tools? Without type information, pylance will not be able to provide any autocomplete suggestions. Most libraries ship with Python code, and pylance can analyze the code to infer type information. However, cv2 is not written in Python, so pylance has no code to analyze. Most libraries that are not written in Python ship with additional type information in the form of "type stub" (.pyi) files. Unfortunately, cv2 does not. If this library is important to you, you may want to encourage the maintainers to include type stubs in a future version. In the meantime, you would need to create your own type stubs (or install a type stub package created by someone else) if you want pylance to provide completion suggestions for symbols in the cv2 library.

PyCharm will extract minimal type information out of a binary library like cv2. It does so by loading (and therefore executing) the library and then using reflection to extract classes, methods, etc. Pylance does not do this for security and performance reasons. You would need to do this manually using a tool like stubgen to extract the type information.

Here is what I have done:
C:\Users\Seth>stubgen -m cv2 -o C:\Users\Seth\AppData\Local\Programs\Python\Python311\Lib\site-packages\cv2 Processed 1 modules Generated C:\Users\Seth\AppData\Local\Programs\Python\Python311\Lib\site-packages\cv2\cv2/__init__.pyi

And there is still no autocomplete for the classes inside cv2.

I also tried copying the file it generates into multiple different locations.

I even added the location for the pyi to the extrapaths in the python extension settings.

@erictraut
Copy link
Contributor

Once you generate the type stubs via stubgen, you need to move them into a project-local directory called typings/cv2/<stubs go here>. If you don't do this, pylance will not be able to find them. You can opt to check these stubs in with the rest of your project if you want other collaborators to also access them.

@elesbb
Copy link
Author

elesbb commented Feb 19, 2023

Once you generate the type stubs via stubgen, you need to move them into a project-local directory called typings/cv2/<stubs go here>. If you don't do this, pylance will not be able to find them. You can opt to check these stubs in with the rest of your project if you want other collaborators to also access them.

Is there a solution that is not project dependent? Would be very annoying if I have to do this for every single project. I would like it to stay this way for the interpreter that Pylance is set to.

@erictraut
Copy link
Contributor

The best solution is for the maintainers of opencv to include type stubs with their library.

The next best solution is for someone to publish a "cv2-stubs" stub package on pypi that includes type stubs for opencv. This could then be installed in your python environment and used for all of your projects. You could check with the maintainers of typeshed to see if they'd be interested in including stubs for cv2. Then you could contribute yours as a starting point.

@elesbb
Copy link
Author

elesbb commented Feb 19, 2023

The next best solution is for someone to publish a "cv2-stubs" stub package on pypi that includes type stubs for opencv. This could then be installed in your python environment and used for all of your projects. You could check with the maintainers of typeshed to see if they'd be interested in including stubs for cv2. Then you could contribute yours as a starting point.

How could I publish a cv2-stubs package myself?

For example, how could I go about getting this working on my own local machine, then share it with others? Would I be able to use the init.pyi file to do this?

@erictraut
Copy link
Contributor

PEP 561 is a good place to start for information on this topic.

If you are able to incorporate the cv2 stubs into typeshed, the stub package will automatically be published because that project has a bunch of automation for creating and publishing stub packages.

@elesbb
Copy link
Author

elesbb commented Feb 19, 2023

Thanks my friend! I shall look into this!

@elesbb elesbb closed this as not planned Won't fix, can't repro, duplicate, stale Feb 19, 2023
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

No branches or pull requests

3 participants