Show HN: Python Tools for Visual Studio
pytools.codeplex.com
pytools.codeplex.com
Disclaimer: PTVS is built by few Python/OSS enthusiasts at msft & I'll be around in case people have any questions!
Cheers.
Really cool, I love the
a={2:42}
a.values()[0]. <int completion>
example. What are the limitations of such a system in Python? Are there cases where it can be wrong? Can you reveal some more info on how this stuff works under the hood? Do you execute parts of the code or how do you figure these relationships out?Thank you, it's pretty inspirational in regards to how programming in Python could look like
KDevelop's Python plugin is currently in early beta stage, but already offers one of the most sophisticated implementations of interactive static analysis for Python currently available. Meanwhile, if you like C++, it's already rock solid and second to none :). It's noteworthy that two languages that different can be catered to with best-in-class semantic support by the same underlying language plugin framework.
In regards to how this is done, KDevelop works purely statically. One of the smartest Python IDEs on the market, Wing IDE (also available for Linux) implements a hybrid approach of static analysis and gathering additional information by instrumenting programs at runtime. However, KDevelop's static analysis figures out a lot more than Wing's does.
ETA: Since Ruby support was asked about elsewhere in the thread, I thought I'd add that a Ruby support in KDevelop is also in the works in earnest as of this summer, but currently not yet as far along as Python support. The other production-ready language plugin next to C++ (which is KDE's bread and butter as a community) is the PHP one, which supports everything up to PHP 5.4 (i.e. including namespaces).
Basically how it works is that we do an abstract interpretation of the code. To take this specific example first we analyze the a = {2:42} line, and the dictionary literal produces a new unique value in the system, and obviously we know it's key/value types. From there we'll analyze the "a.values" which will produce a new value in the system for the bound "values" method. Then we do an abstract call on that, and it says that it returns a list (and goes off to the original dictionary value, and produces a list which contains the int objects). And then we do the indexing, and finally we get back the original 42 value that the dictionary was created with. So basically at each step along the way we are just propagating sets of abstract values. A naive implementation of a system like this would iterate until it hits a fixed point - we're a little more sophisticated in that we do a bunch of dependency tracking to know what needs to be analyzed based upon changes in the system. And then we also need to deal with a user editing the program in real time which makes things even more complicated as we need to discard old stale information which has been propagated from edited files. The actual implementation of the basic Python semantics is actually the easy part, it's getting this to perform on large programs and working correctly with live editing which is the difficult part.
This can go wrong in a couple of ways. First off we don't model control flow, so you could have something like:
if False: a = 42 else: a = 'bar'
And we'd think that a was an int or a bar. In the future we might model control flow, but it's a big feature. Mainly I just need an excuse to implement it :). Another way it could go wrong is if the user is using exec/eval which we don't have any insight into. And finally there can be things that we simply don't understand. So while we know about the primitive types and the methods on them, we don't model all of Python's built-in types. So for example if you're using deque in collections you won't get the same results.
All of this is open source (under the Apache license) and the parser and analysis engine are entirely stand alone components, so they could even be re-used by other IDEs. You can check out the source code over here: http://pytools.codeplex.com/SourceControl/changeset/view/3b8...
Things like incremental reparsing on edits are handled by KDevelop's DUChain framework, which is shared among the language plugins. The Python language plugin actually reuses the original CPython parser to do the job, and then plugs CPython's AST into KDevelop's DUChain representation.
And just to make me feel less like a thread hijacker, let me add that we've been quite impressed by the work the PTVS team has been doing. You're doing great things for the Python community over there :).
I've spent a lot of time in Visual Studio over the years and have come to really like it when I'm doing C/C++/C#, so when this first came out I jumped all over it. The recently added feature of creating a project from existing code is pretty sharp, and removes the need for a third-party "explorer" plugin.
In general, is there a particular reason we don't see this more often in tooling for other (REPL-friendly) languages, outside of the Lisps? Admittedly I haven't spent a significant amount of time in the Python community lately (so not sure if this is common in Python tooling or not), but after becoming addicted to SLIME via Clojure, I'd have killed to be able to dump Ruby blocks from my editor straight to the REPL.
[0] http://pytools.codeplex.com/wikipage?title=Features%20Intera...
NB: The only tool I've ever found that works in the manner I want it to as far as REPL languages go is RStudio. If I had an RStudio that was language agnostic so it could work with python, ruby, lisps, or what have you, I would be a happy, happy man.
edit: if anyone has any suggestions in this regard, please, leave a comment!
It also has decent vi and emacs keybinding support, really nice interactive debugger (including template debugging for Django) and great intellisense. It's the best Python IDE I have found, the support is great and I have been a very happy customer for three years now. (I was using emacs for everything before).
Good job PTVS team :) You're making me envious!
http://meta.askubuntu.com/questions/1523/imgur-com-is-deleti...
Some things we do in my product would not have been possible without having access to the source of the Python Tools (which again say a lot about the sad story of extending VS).
Shameless plug, if that's still too much work for you, you can contact my company to integrate your language in VS.
Did you find that Visual Studio easy to integrate with, or did you have to fight it to do what you wanted?
Does it integrate with virtualenv/pip?
The priority of this has gone up for us after PEP 405 but we weren't able to get to it for 1.5. You can go to Tools->Options->Python Tools->Interpreter Options and setup a custom interpreter which points to your virtualenv, but we need to make it easier than doing that.
With the IPython notebook on Azure you can basically use Linux/Mac with Chrome, FF, Safari, ... and have a nice browser based Python IDE and completely cut Windows & IE out of the picture :).
edit: missing link.