Using Python Stubs in VSCode?

Hi,

I am wondering about the correct usage of the Rhino-Stubs within an outside IDE when writing Python? Specifically, VSCode. I am able to pip install the stubs just fine, and they are mostly recognized properly, but for some reason the the modules listed in the stub-file’s __all__ are not being recognized, and I wonder if I should be setting this up differently to make it work as expected?

For instance, in VSCode, if I do something like:

from Rhino import Geometry

class MyClass(object):

    def __init__(self, _geom):
        # type: (list[Geometry.GeometryBase]) -> None
        self.geom = _geom

then it can find Rhino (I can Command-Click and follow the Rhino ref successfully), but not Geometry, even though .Geometry is listed within the stub __all__:

In VSCode, Rhino.___ autocomplete lists the only classes within the file, as expected, but not the ones in __all__:

Does anyone know of a solution there that I am missing? I’ve just been adding # type: ignore to these lines, but obviously that’s not ideal at all.

Has anyone been able to get these stubs to work in this context? Or maybe I’m not supposed to be imported ‘Geometry’ and the like through Rhino.___?

thanks!!
@ed.p.may


Environment:

1 Like

@ed.p.may Rhino.Geometry is a namespace so from Rhino import Geometry does not work in python 3. Try this instead

from typing import List
import Rhino.Geometry as RG

class MyClass(object):
    def __init__(self, _geom):
        self.geom: List[RG.GeometryBase] = _geom
1 Like

Thanks @eirannejad !

That worked! I didn’t realize there would be a difference in accessing them that way?

Autocomplete and type-checking does work now, but do you know if there is anything I can do to resolve the import warning in VSCode:

or would you suggest just continuing to use # type: ignore on those lines?

That doesn’t seem to affect any of the later type-hinting or automcomplete functionality? But seems like I should try and avoid it is there is a better way to resolve those warnings?

thanks!!
@ed.p.may

Yeah I would put # type: ignore on these imports. It is an assumption that ‘you’ know but the linter does not so it’s good to specify that.