pythonstubs icon indicating copy to clipboard operation
pythonstubs copied to clipboard

Stubs do not recognize classes as importable modules

Open maxsu opened this issue 1 year ago • 0 comments

First I want to express my thanks for pythonstubs. It it has the potential to help our org better maintain our legacy rhinopython code. Second, I would like to express a problem we're having, and propose a solution.

In our rhino python code, we like to explicitly import static functions from Rhino classes when possible, to help telegraph the intention of an import:

from Rhino.Geometry.Rtree import CreatePointCloudTree #  I bet we'll create a point cloud tree later 🤔
{...}
CreatePointCloudTree(some_points) #  How did we know that?! 🤯

However, the current stub setup will fail to recognize these sorts of imports (for example in pycharm):

image

The crux is that RhinoPython lets us import a .net class as if it were a python module. However, the current stub layout - one .pyi per major rhino namespace - can't recognize these "class module" style imports.

To resolve the issue in the most literal way, we could represent the additional class modules as .pyi stubs. These stubs would correctly recognize the import examples I've shown:

# Import                                     #  Stub
import Rhino.Geometry.Sphere   #  /Rhino-stubs/Geometry/Sphere.pyi
import Rhino.Geometry.RTree      #  /Rhino-stubs/Geometry/RTree.pyi
etc.

Pystubbler would dump these "class modules" using the same method it uses to dump namespace modules. This would make the stubs significantly more faithful to the true article, and more fit to our organization's needs.

Feedback would be welcome. This capability would be useful for our organization; if a particular solution design gets maintainer endorsement, I would like to sponsor and submit the fix.

maxsu avatar Oct 02 '23 10:10 maxsu