isl-org / Open3D

Open3D: A Modern Library for 3D Data Processing
http://www.open3d.org
Other
11.55k stars 2.32k forks source link

Open3D type checking bug/issue #3052

Open BojanMatovski opened 3 years ago

BojanMatovski commented 3 years ago

Describe the bug Since the Open3D update I get this kind of a warning (see screenshots) in PyCharm when I try to add a point cloud as a geometry in a visualizer.

I've also seen issues like "Expected type 'PointCloud.py', got 'PointCloud' instead", where the only difference is that the expected type is the type of the object I am already giving but followed by a ".py"

To Reproduce

  1. Start PyCharm
  2. Write a test program that uses the geometry, or pointcloud objects (even some others)
  3. Create an object of some of those types, but explicitly define the type, i.e.: some_pointcloud: open3d.geometry.PointCloud = open3d.geometry.PointCloud() (added another screenshot for this example)
  4. Give it as an input to the method that uses such object

Expected behavior The method should accept the input object and PyCharm shouldn't complain about it.

Screenshots image image

Environment (please complete the following information):

Additional context I opened this discussion on Discord, but there were no responses, so I assume that it is a bug. Also, in the geometry issue, I've tried casting it to a geometry object type, but there were some issues with the geometry object constructor. I searched the documentation to try and see if I'm doing something wrong, but I couldn't find anything specific to this.

By the way, nothing crashes, everything works as intended, it's just these silly warnings that the static code analyzer complains about.

ssheorey commented 3 years ago

Hi @BojanMatovski which static analyser does PyCharm use? Can you reproduce this with pylint or flake8?

BojanMatovski commented 3 years ago

I am not sure which static analyzer PyCharm uses. I tried looking out and finding it, but couldn't really do it.

I tried using flake8, but had some issues with it. When I try using it, it prints: _AttributeError: 'TreeRebuilder3' object has no attribute 'visitconstant'

I also tried catching it with pylint and it doesn't catch it, but I might need to configure it or something, because I also added these two lines as a check:

        a: int = 'test'
        print(a)

and pylint still doesn't complain about the variable "being" an integer and having a string assigned.

Finally, I tried mypy, but for some reason it says that open3d has no type hinting: error: Skipping analyzing 'open3d': found module but no type hints or library stubs

ssheorey commented 2 years ago

@BojanMatovski Open3D doesn't use type hints, since the code is a compiled C++ module. I don't believe type hints are useful here since C++ already has strong type safety. Looks like PyCharm is not able to analyze compiled modules, I would just configure it to skip checking Open3D code if possible. Closing this issue. Please re-open if you have suggestions on improving compatibility with these tools.

ManuCorrea commented 1 year ago

Hello @ssheorey!

I am trying to apply type checking to my project in order to have a robust CI. I type my functions which usually have Open3D objects.

Using mypy I get the following error: error: Skipping analyzing "open3d": module is installed, but missing library stubs or py.typed marker [import-untyped]

In order to improve the compatibility with these kind of tools Open3D should be PEP 561, the following links give a starting point: https://mypy.readthedocs.io/en/stable/installed_packages.html#installed-packages https://peps.python.org/pep-0561/#type-checker-module-resolution-order

ssheorey commented 1 year ago

Hi @ManuCorrea here are two options for generating stubs:

https://github.com/sizmailov/pybind11-stubgen https://mypy.readthedocs.io/en/stable/stubgen.html#automatic-stub-generation-stubgen

with a discussion here: https://github.com/pybind/pybind11/issues/2350

If any of these tools work for you, please let us know. Also happy to accept a PR to package these stubs in the Open3D wheel based on your results.

martincerven commented 5 months ago

@ssheorey OpenCV also added type hints with .pyi files https://github.com/opencv/opencv/issues/14590

The stub generation with https://github.com/sizmailov/pybind11-stubgen works, although in vscode you have to copy generated file in root of your workspace or some pylance folder https://github.com/isl-org/Open3D/issues/5802

Is the reason why Open3D doesn't have type hints that the functions are generated with pybind11? Would it be possible to generate .pyi files https://github.com/sizmailov/pybind11-stubgen and ship them with the library?