labscript and VS Code Intellisense?

44 views
Skip to first unread message

Trey Porto

unread,
May 12, 2023, 7:16:53 AM5/12/23
to the labscript suite
Hi All,

Does anyone know if there is a way to register the labscript run-time defined devices and global variables to VS Code Intellisense?  

My brief poke around the internet leads me to believe it is non-trivial, but I thought I'd ask if anyone had any experience with it.

Thanks,
Trey

Philip Starkey

unread,
May 13, 2023, 12:49:43 AM5/13/23
to labscri...@googlegroups.com
Hi Trey,

Unfortunately I think this is impossible for the current version of labscript. Due to the way labscript code injects the created device/channel instances into the Python builtins (so they are available everywhere), there is no way for Intellisense (or equivalent in other IDEs) to know the type of the variables used in the experiment logic. A similar problem occurs with the globals.

To be honest, I think we should seriously consider not injecting into builtins for the next major version of labscript (for a variety of reasons including hopefully getting Intellisense working). This would bring the project in line with other Python libraries. The main reason we injected into builtins is so that you didn't have to define a name twice. For example, this is overly verbose:

my_channel = DigitalOut("my_channel", ni_card, "port0/line1", ...)

But there are other ways to avoid defining the name twice. A possible way around this (in a future labscript version) would be to define the connection table in a class:

class Dev(labscript.ConnectionTable):
    my_channel = labscript.DigitalOut(ni_card, "port0/line1", ...)

The reason this can work is you can configure a Python metaclass for labscript.ConnectionTable that detects attributes using labsccript device/channel classes, and have the metaclass tell the labscript objects the name of the attribute they are assigned to. That allows labscript to keep track of the name of devices/channels without needing it to be specified twice, in a way that things like Intellisense can also keep track of.

It would also allow you group (namespace) devices/channels based on their function, across multiple ConnectionTable classes:

class Dev(labscript.ConnectionTable):
    ni_card = NIPCIe6363(...)
    my_channel = labscript.DigitalOut(ni_card, "port0/line1", ...)

class MOT(labscript.ConnectionTable):
   shutter = labscript.DigitalOut(Dev.ni_card, "port0/line3")

And then you use them like:

Dev.my_channel.go_high(t=4)
MOT.shutter.open(t=7)

Globals would be a bit harder, I suspect you'd have to (a) namespace them using classes like we'd do above for devices and (b) have runmanager write out something dynamically that Intellisense could process to understand what globals were available inside each class. I also don't see this as a bad thing, as I think it would make sense to namespace globals inside a labscript script in the same way they are grouped in runmanager already, and it would allow us to inject the globals units into the labscript file so they didn't have to be maintained in 2 places.

So I guess that is the way I'd see we could (and probably should) make it work in the future. It's a big, backwards incompatible change though, and would need some community consultation as to whether the added syntactic burden is worth it for the gains.

Cheers,
Phil

--
You received this message because you are subscribed to the Google Groups "the labscript suite" group.
To unsubscribe from this group and stop receiving emails from it, send an email to labscriptsuit...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/labscriptsuite/5016600a-06c4-43a0-a55a-0599bc7c3eacn%40googlegroups.com.

Trey Porto

unread,
May 15, 2023, 3:29:56 PM5/15/23
to labscri...@googlegroups.com
HI Phil,

At first glance it looked non-trivial, thank  you for confirming… 

Thanks,

Trey


Dr. Trey Porto
Fellow, Joint Quantum Institute
National Institute of Standards and Technology
and the University of Maryland
College Park, MD 20742
UMD Office: PSC 2147
UMD Phone: 301-405-0854
Preferred e-mail: po...@jqi.umd.edu
Official NIST Business: tr...@nist.gov

You received this message because you are subscribed to a topic in the Google Groups "the labscript suite" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/labscriptsuite/YEMyGhQRrsM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to labscriptsuit...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/labscriptsuite/CALhC6Gh6BOq5GdYZtY4vXkAkaxO2PT14t2%2BbxamsFRCh9vEN_g%40mail.gmail.com.

Philip Starkey

unread,
May 17, 2023, 8:40:49 AM5/17/23
to labscri...@googlegroups.com
Hi Trey,

I should really learn to sit on things for a few days before I post here! I'm pretty sure I've worked out 2 different ways to get intellisense working for devices/channels (both require slight modifications to connection table syntax, but can be implemented in a way that doesn't require removing the existing syntax, thus making it backwards compatible). Globals are a little trickier, I think you'd need to get runmanager to write out a .pyi stub file for the currently loaded globals (but that should be possible). If that proved too hard, there is at least a simple way that would stop it complaining about undefined variables (even if it couldn't autocomplete the names of global variables)

It'll probably take me a while to actually get around to implementing it. Hopefully I'll have an update in a couple of weeks with a proof of concept!

Cheers,
Phil

Trey Porto

unread,
May 30, 2023, 3:15:55 PM5/30/23
to the labscript suite
Hi Phil,

just to follow up on this: I would be extremely grateful if you figured out a simpel way to get Intellisense working with Labscript.  It would be ENORMOUSLY helpful, for both devices/channels/methods, as well as globals.  ENORMOUSLY.

Cheers,
Trey
Reply all
Reply to author
Forward
0 new messages