DJX DJX - 3 months ago 20
Python Question

Python Tools for Visual Studio - tell the IDE what type a parameter will be

I just started using Visual Studio 2015, and I really like it, especially the code completion features. Right now I'm writing some simple Python code to draw an object on a Tkinter Canvas:

def draw(self, can):
""" can is a Tkinter canvas on which this node will draw itself. """
can.<something>


This being Python, nowhere in the actual code is it indicated that "can" is going to be a Canvas, so VS can't autocomplete or suggest anything after I type "can." Is there a way I can tell Visual Studio that can will be of type tkinter.Canvas?

Answer

Yes, you can use an assertion statement. This will trigger VS autocomplete settings. It's not the most beautiful way to do it, but I think right now that's the best option.

assert isinstance(can, tkinter.Canvas)

Take a look at this answer for more.