Pylance: The best Python extension for VS Code | by Dimitris Poulopoulos | Towards Data Science

Visual Studio Code is arguably the best open-source code editor, and, of course, Python is treated as a first-class citizen inside its ecosystem. The corresponding Microsoft extension comes with “rich support for the Python Language, including features like IntelliSense, linting, debugging, code navigation, code formatting, code refactoring, test explorer, snippets, Jupyter Notebook support, and much more.”
However, the enemy of good is better; thus, Microsoft created Pylance, a new, improved, faster, and feature-packed Python language server. Pylance, a reference to Monty Python's Sir Lancelot the Brave, depends on the core Python extension and builds upon that experience. What is more, with the VS Code December update, Pylance can perform long-awaited actions that take the Python developing experience to a new level.
This story presents Pylance, what it is, and how you can leverage its full functionality today. Finally, we examine the new version that was released as part of the latest December update.
Learning Rate is my weekly newsletter for those who are curious about the world of AI and MLOps. You’ll hear from me every Friday with updates and thoughts on the latest AI news, research, repos and books. Subscribe here!
Pylance is the new Python language server from Microsoft. It offers fast, feature-rich language support for Python in Visual Studio Code, is dependent on the core Python extensions but brings a hell of a lot more to the table. Let’s see what the Microsoft VS Code Python team has been building for months.
Pylance leverages type stub files (.pyi files) and lazy type inferencing to provide an efficient development experience. But what are stub files, you may ask?
As a quick sidenote, Stub files are used to provide type-hinting information for Python modules. The full official documentation can be found in the section about stub-files in PEP-484. For example, consider the following Python function that resides in my_function.py module:
def add(a, b):   return a + b
You can create a new stub file, my_function.pyi, to provide type-hinting:
def add(a: int, b: int) -> int: ...
NOTE: The ... at the end of the function definition in the stub file is part of the syntax
However, we know that we can insert type-hints inside the Python module like that:
def add(a: int, b: int) -> int:   return a + b
So, why should we use stub files? There are a couple of reasons. For example, if you want to keep your .py files backward compatible or if you want to provide type-hints into an existing code-base and want to minimize the changes in the source-code itself.
Coming back to Pylance, the use of stub files can supercharge your Python IntelliSense experience with rich type information, helping you write code faster. What is more, it already comes with a collection of stubs for popular modules out of the box. The built-in stub library provides accurate type checking as well as fast auto-completion.
But is that all? Of course not; we are just scratching the surface.
Pylance main characteristic is its performance, but many other features make this extension a must-have.
Type information: You can now get the type definition of a function when you hover over it.
Auto-imports: You are now able to auto-import suggestions for installed and standard library modules. This is probably the most requested feature by Python developers, and it’s finally available.
Type-checking: You can now validate that the arguments you pass to a function are of the right type prior to its execution. You have to enable it through settings; set python.analysis.typeCheckingMode to basic or strict.
The December 2020 Visual Studio Code update introduced several new Pylance features, with code extraction and the Pylance Insiders program being the most important.
Code extraction: You are now able to extract methods and variables with one click.
Pylance Insiders: Pylance now has its own club of dedicated users, the Pylance Insiders program, which offers early access to new language server features and improvements. To enable insiders, set "pylance.insidersChannel": "daily".
Pylance seems to be the future of the Python language server for VS Code. It is still in preview, but I would recommend turning it on immediately. The new features and improvements coming almost every day will make you more productive and elevate your overall developing experience.
The new VS Code update features many improvements and features outside Pylance, like Ipywidget Support in Native Notebooks. You can read the changelog for more details.
Learning Rate is my weekly newsletter for those who are curious about the world of AI and MLOps. You’ll hear from me every Friday with updates and thoughts on the latest AI news, research, repos and books. Subscribe here!
My name is Dimitris Poulopoulos, and I’m a machine learning engineer working for Arrikto. I have designed and implemented AI and software solutions for major clients such as the European Commission, Eurostat, IMF, the European Central Bank, OECD, and IKEA.
If you are interested in reading more posts about Machine Learning, Deep Learning, Data Science, and DataOps, follow me on Medium, LinkedIn, or @james2pl on Twitter. Also, visit the resources page on my website, a place for great books and top-rated courses, to start building your own Data Science curriculum!
Opinions expressed are solely my own and do not express the views or opinions of my employer.