typemytype / drawbot

http://www.drawbot.com
Other
408 stars 63 forks source link

VScode IntelliSense doesn't work in the suggested examples. #440

Closed setanarut closed 1 year ago

setanarut commented 3 years ago

VScode IntelliSense doesn't work that way. (official example)

import drawBot

drawBot.newDrawing()
drawBot.newPage(1000, 1000)
drawBot.rect(10, 10, 100, 100)
drawBot.saveImage("~/Desktop/aRect.png")
drawBot.endDrawing()

VScode IntelliSense works that way. Would using it this way cause a problem? I'm not an expert, just wondering.

Ekran Resmi 2021-10-18 21 18 31
justvanrossum commented 3 years ago

Not knowing what VS does, let alone IntelliSense, I somehow can't extract the problem from your description. What does not work? What do you expect to work?

setanarut commented 3 years ago

What does not work?; VScode IntelliSense doesn't work(Pylance) when import drawBot

What do you expect to work?; When I import the drawBot module I expect to reach the functions of the class with the dot. for example.

Ekran Resmi 2021-10-18 22 16 12

.

This issue is caused by the global namespace. https://github.com/typemytype/drawbot/blob/master/drawBot/__init__.py

from .drawBotDrawingTools import _drawBotDrawingTool

_drawBotDrawingTool._addToNamespace(globals())
justvanrossum commented 3 years ago

When I import the drawBot module I expect to reach the functions of the class with the dot. for example.

But your first screenshot is showing exactly that, no? db.rect() is indeed a method of the db object.

justvanrossum commented 3 years ago

Oh you mean, if you don't use _drawBotDrawingTool as db but plain import drawBot as db. Right?

Well, Python is a dynamic language, what we're doing is not uncommon or illegal. This is a weakness of the IntelliSense tool, not a bug with DrawBot.

justvanrossum commented 3 years ago

Unless it's caused by a missing __all__ = ...: it would be worth a try to add that.

setanarut commented 3 years ago

Oh you mean, if you don't use _drawBotDrawingTool as db but plain import drawBot as db. Right?

yes.

justvanrossum commented 3 years ago

Unless it's caused by a missing all = ...: it would be worth a try to add that.

Can you try to see whether that makes a difference? Adding just __all__ = ["rect"] to drawBot/__init__.py should be enough to see whether VS picks that up.

setanarut commented 3 years ago

Unless it's caused by a missing all = ...: it would be worth a try to add that.

Can you try to see whether that makes a difference? Adding just __all__ = ["rect"] to drawBot/__init__.py should be enough to see whether VS picks that up.

no

justvanrossum commented 3 years ago

"no" as in "VS doesn't pick it up", or "no" to "Can you try"?

setanarut commented 3 years ago

"no" as in "VS doesn't pick it up", or "no" to "Can you try"?

VS doesn't pick it up

justvanrossum commented 3 years ago

Thanks.

A solution could be to generate that __init__.py file from the drawing tools object during app build, in such a way that it can more easily be statically analyzed. The output could look like this snippet:

from .drawBotDrawingTools import _drawBotDrawingTool

def rect(...arguments all written out...):
    """...doc string...."""  # copied from _drawBotDrawingTool.rect
    _drawBotDrawingTool.rect(...arguments...)
setanarut commented 3 years ago

I am not a developer. I know Python on a basic level. I was just guessing. You know better.

justvanrossum commented 3 years ago

Ok, let's leave this open in case anyone wants to pick it up.

typemytype commented 3 years ago
import drawBot
print(dir(drawBot)) # works

adding __all__ would simple addition

justvanrossum commented 3 years ago

(adding __all__ doesn't help — I am assuming VS does static code analysis)

roberto-arista commented 2 years ago

đź‘‹

I am using VSCode quite often with drawBot, I'll try to get a look into this!

roberto-arista commented 2 years ago

cc @lianghai

lianghai commented 2 years ago

Instead of https://github.com/typemytype/drawbot/issues/440#issuecomment-946148489, simply doing—

from .drawBotDrawingTools import _drawBotDrawingTool

rect = _drawBotDrawingTool.rect

—should be good enough. But why is from drawBot import _drawBotDrawingTool as db a problem at all? Since those functions are really indeed methods of DrawBotDrawingTool, why should they be presented in a more confusing way?

bennettzug commented 2 years ago

@roberto-arista did you ever look into this? No worries if not, just don't want to start digging into something if you've already done a bunch of work towards it.

roberto-arista commented 2 years ago

hey @bennettzug! not really, I am working in VSCode mostly using the import suggested above from drawBot import _drawBotDrawingTool as db and I find it quite ok