Hi there, I'm Savannah, a program manager working on the
Python team at Microsoft.
In this demo I'm going to showcase some of the
amazing features of our new language server Pylance and show
how it enhances the Python in Visual Studio Code experience
by providing a performant and user-friendly language support.
So at this point you might be wondering what is
Pylance or what is a language server,
and you can really just think of Pylance as a
provider of all the great development features that you know
and love,
like Autocompletion, syntax highlighting, code navigation and much more.
As of the May release of the core Python extension
Pylance is also now the default language server for Python
in Visual Studio Code.
So what makes Pylance unique is that Pylance is ultra
performant.
It doesn't do any upfront analysis of your project,
which means that you get to writing code in seconds
instead of in minutes,
and in the process less memory is used.
Additionally, Pylance is built on top of an open source
static type checking tool developed here at Microsoft called Pyright,
which means that it has the power to expose rich
type information in tooltips in your workspace,
which can help you write less buggy code because you'll
know the parameter types of the functions you're trying to
invoke.
An also lessens the amount of time that you'll have
to go and look at external documentation.
Making you more productive.
As I mentioned before,
Pylance is now the default language server for Python in Visual
Studio Code and and this means that no separate installation
of Pylance is needed.
If you never set language server before,
you'll have all Pylance functionality working right out of the
box.
If you've previously set a language server,
you'll want to double check that your language server setting
is set to Default or Pylance to make sure that
Pylance is activated.
So let's hop over to Visual Studio Code and I'll
show you some of that Pylance functionality I was talking
about.
I have this data science project loaded in VS
Code,
so I'm going to actually start typing and I'm looking
at using the gc module.
So in this case what you're seeing here is completions
and I think everyone is pretty familiar with completions.
Completions are
what takes a plain text file that makes it
an actual development environment.
But what's interesting about Pylance is we provide this thing
called an auto import,
and so auto imports are Pylance
being smart and saying, hey,
I know that you can use this module in your
In your file, but actually haven't imported it yet,
and so I'm going to provide you a completion,
but upon selection of the completion,
I'm actually going to add this import to the file
for you so you don't have to do it later.
So in the tooltip here you can see that Pylance
is saying hey,
this is an auto import and this is the exact
import line that I'm going to add to your files,
so this preview is here so that there's no confusion
about what it is exactly that you're importing.
And if I go down this list you can see
other auto imports that contain the gc string.
This is a matplotlib
Auto import, here's a math auto import.
But in this case, this is the one that I'm
looking for,
so I'm going to go ahead and select it.
I can now see that gc was added to my file,
and so this works for installed third party modules that
are in your virtual environment or standard library modules.
So I'm going to keep writing code I see more
completions now after I hit dot for different functions.
In this case I'm interested in this collect method and
so let's talk about the tooltip that Pylance is providing.
At the very top we can see the function
signature,
but what's pretty neat is that Pylance has injected this
rich type information for me,
so I'm not writing typed Python,
I'm not really interested in writing typed Python right now,
but pyright allows us to embed this rich type information
and actually see that
the parameter that I could pass in here is of type int
and that the return type of the function is also type int.
So this kind of gives you like a TypeScript type
flavor in Python,
even if you're not looking to write typed Python.
We also have this really,
really clear docstring here that helps me understand what function
that I'm trying to invoke,
which is helpful and prevents me from having to look
at external documentation and keeps me in the editor
so that I can actually save time.
I'm gonna go ahead and use that. And then
now I can see this signature help,
which has the same information
we saw in the completion tooltip,
but I can see it while I'm invoking the function
If I need to pass in parameters,
which is really helpful. And in this case we have
the generation parameter bolded because that's the active parameter,
and if I had many more parameters to pass in,
Pylance would actually incrementally move to the next parameter as
I start to pass them in,
which helps me keep track.
And this is pretty helpful for functions that have a
lot of different parameters that you're looking to pass in.
You know, especially for some of those really nitty gritty
data science functions like read_csv,
there's tons and tons of parameters you could pass in,
so this is kind of helpful. In this case
I'm actually not going to pass anything in.
Next thing I want to highlight in terms of imports
is this dead code detection that Pylance does.
So in this case we're looking at this line 2
here where OS is kind of grayed out.
What this is saying to me is that it's
unused,
and I feel like this is a pretty common
thing where you know you're prototyping something,
and import a bunch of things, and then at the end
you end up actually not needing them.
Then maybe you have a bunch of stale imports
at the top of your file.
Pylance is smart enough to know
"Hey, this is actually not being used."
You can actually go ahead and remove that,
so I'm going to go ahead and click the code
action light bulb here,
it says remove the unused import.
I'm going to go ahead and do that.
Pylance also does this for unreachable code.
It will completely gray out a block of code that's
unreached,
which can help you find edge cases and bugs in
your code more easily.
The other thing I want to mention is that auto imports
are maybe a little bit contentious.
There are people that really like them and people that
really don't like them and we're all about flexibility here
on the Python team,
so you can actually disable them if you're really interested.
If you want to still use something that kind
of helps you manage adding imports to your file,
you can do something that allows you a little bit
more control.
So I can instead, let's say I remove this gc
import line.
I'm going to get a diagnostic on this line that
says,
"Hey, Pylance is detecting that gc is
not defined here"
and if I go and click that it's going to
say "hey, do you want to add it?
And I'm going to say yes, because I do,
so it's going to re-add it to the top
of the file.
So instead you can use that add import code action.
The other thing in terms of code actions that you
can do here,
is, let's say I was interested in making this a
method,
We could use that the extract method
code action and Pylance would wrap that in a function.
Or if I was interested I could also extract it as a
variable and Pylance would do that for you as well,
and this also applies to like bigger blocks of code.
So let's say I want to do this whole block here
Pylance would also do that for me,
which is pretty handy sometimes.
You know, if you want to do a quick refactor.
And then of course, Pylance can also do go to
definition,
so I can I can Ctrl + click through to the
pandas module and read source code there and then finally
something that I'm pretty excited about is
the notion of semantic highlighting or semantic colorization.
Semantic colorization is really just an extension of syntax
highlighting that uses the semantic meaning of symbols and applies
different colors to them in a semantic color supporting theme.
So in this case I'm using a theme that supports
semantic tokens called One Dark Pro.
And you can see that yellow is being used for
modules.
Blue is being used for functions and so on,
and this just helps with the readability and understanding of
your code.
If I were to use a theme that didn't support
semantic color,
an I toggled to just the plain dark theme,
you'd see that I lose all that semantic meaning.
So you do need to use a theme that supports
semantic tokens,
but Pylance does provide them.
Flipping back over to the deck,
I just wanted to show a quick feature summary of
all the things that I showcased in the demo today.
I also wanted to highlight two other extensions that Pylance
is compatible with,
one being IntelliCode, which prioritizes your completions based on your
specific code context,
helping make you more productive, as well as Jupyter notebooks.
Finally, if you're interested in trying out writing typed Python
or already writing typed Python,
you can get the power of Pyright directly in
the box with Pylance by enabling a strict or basic
type checking mode.
Again, if you're interested in using Pylance,
it already comes in the box with the core Python
extension
as of this month, and so you can get the
core Python extension if you don't already have it at aka.ms/vscode-python.
So that's pretty much it.
If you have any questions,
comments, feedback, I'd love to hear from you on Twitter
@pythonvscode.
Thanks for hanging out.