December 11th, 2024
heart4 reactions

Python in Visual Studio Code – December 2024 Release

jikang
Software Engineer

We’re excited to announce the December 2024 release of thePython,Pylance andJupyter extensions for Visual Studio Code!

This release includes the following announcements:

  • Docstring generation features using Pylance and Copilot
  • Python Environments extension in preview
  • Pylance “full” language server mode

If you’re interested, you can check the full list of improvements in our changelogs for thePython,Jupyter andPylance extensions.

Docstring generation using Pylance and Copilot

A docstring is a string literal that appears right after the definition of a function, method, class, or module used to document the purpose and usage of the code it describes. Docstrings are essential for understanding and maintaining code, as they provide a clear explanation of what the code does, including parameters and return values. Writing docstrings manually can be time-consuming and prone to inconsistencies, however automating this process can ensure your code is well-documented, making it easier for others, and yourself, to understand and maintain. Automated docstring generation can also help enforce documentation standards across your codebase.

How to enable docstring generation

To start, open the Command Palette (Ctrl+Shift+P (Windows/Linux) orCmd+Shift+P (macOS)) and selectPreferences: Open Settings (JSON).

Add the followingPylance setting to enable support for generating docstring templates automatically within VS Code:

  "python.analysis.supportDocstringTemplate": true

Add the following settings to enable generation with AI code actions:

    "python.analysis.aiCodeActions": {        "generateDocstring": true    }

Triggering docstring templates

  1. Define Your Function or Method:
    def my_function(param1: int, param2: str) -> bool:    pass
  2. Add an Empty Docstring:
    • Directly below the function definition, add triple quotes for a docstring.
      def my_function(param1: int, param2: str) -> bool:""""""pass
  3. Place the Cursor Inside the Docstring:
    • Place your cursor between the triple quotes.
      def my_function(param1: int, param2: str) -> bool:"""|  # Place cursor here """pass

When using Pylance, there are different ways you can request that docstrings templates are added to your code.

Using IntelliSense Completion

    • PressCtrl+Space (Windows/Linux) orCmd+Space (macOS) to trigger IntelliSense completion suggestions.

Using Code Actions

  1. Open the Context Menu:
    • Right-click inside the docstring or pressCtrl+. (Windows/Linux) orCmd+. (macOS).
  2. Select Generate Docstring:
    • From the context menu, selectGenerate Docstring.
    • Pylance will suggest a docstring template based on the function signature.

Using Code Actions to Generate Docstring with Copilot

  1. Select Generate Docstring With Copilot:
    • From the context menu, selectGenerate Docstring With Copilot.
  2. Accept Suggestions:
    • GitHub Copilot chat will appear. PressAccept to take the suggestions or continue to iterate with Copilot.

Python Environments extension in preview

We’re excited to introduce the newPython Environments extension, now available in preview on the Marketplace.

This extension simplifies Python environment management with an Environments view accessible via the VS Code Activity Bar. Here you can create, delete, and switch between environments, and manage packages within the selected environment. It also uniquely supports specifying environments for specific files or entire Python projects, including multi-root and mono-repo scenarios.

Gif showing the Python environments extension managing environments for a multiroot project structure and managing packages in various environments.

By default, the extension uses thevenv environment manager andpip package manager to determine how environments and packages are handled. You can customize these defaults by settingpython-envs.defaultEnvManager andpython-envs.defaultPackageManager to your preferred environment and package managers. Furthermore, if you haveuv installed the extension will use it for quick and efficient environment creation and package installation.

Designed to integrate seamlessly with your preferred environment managers viavarious APIs, it supports Global Python interpreters, venv, and Conda out of the box. Developers can build extensions to add support for their favorite Python environment managers and integrate with our extension UI, enhancing functionality and user experience.

This extension is poised to eventually replace the environment functionality in the main Python extension and will be installed alongside it by default. In the meantime, you can download thePython Environments extensions from the Marketplace and use it inVS Code – Insiders (v1.96 or greater) and with the pre-release version of the Python extension (v2024.23 or greater). We are looking forward to hearing your feedback on improvements by opening issues in thevscode-python-environments repository.

Pylance “full” language server mode

Thepython.analysis.languageServerMode setting now supportsfull mode, allowing you to take advantage of the complete range of Pylance’s functionality and the most comprehensive IntelliSense experience. It’s worth noting that this comes at the cost of lower performance, as it can cause Pylance to be resource-intensive, particularly in large codebases.

Thepython.analysis.languageServerMode setting now changes the default values of the following settings, depending on whether it’s set tolight,default orfull:

Settinglightdefaultfull
python.analysis.exclude[“**”][][]
python.analysis.useLibraryCodeForTypesfalsetruetrue
python.analysis.enablePytestSupportfalsetruetrue
python.analysis.indexingfalsetruetrue
python.analysis.autoImportCompletionsfalsefalsetrue
python.analysis.showOnlyDirectDependenciesInAutoImportfalsefalsetrue
python.analysis.packageIndexDepths[ { "name": "sklearn", "depth": 2 }, { "name": "matplotlib", "depth": 2 }, { "name": "scipy", "depth": 2 }, { "name": "django", "depth": 2 }, { "name": "flask", "depth": 2 }, { "name": "fastapi", "depth": 2 } ] |[ { "name": "sklearn", "depth": 2 }, { "name": "matplotlib", "depth": 2 }, { "name": "scipy", "depth": 2 }, { "name": "django", "depth": 2 }, { "name": "flask", "depth": 2 }, { "name": "fastapi", "depth": 2 } ] |{ "name": "", "depth": 4, "includeAllSymbols": true }
python.analysis.regenerateStdLibIndicesfalsefalsetrue
python.analysis.userFileIndexingLimit20002000-1
python.analysis.includeAliasesFromUserFilesfalsefalsetrue
python.analysis.functionReturnTypesfalsefalsetrue
python.analysis.pytestParametersfalsefalsetrue
python.analysis.supportRestructuredTextfalsefalsetrue
python.analysis.supportDocstringTemplatefalsefalsetrue

Other Changes and Enhancements

We have also added small enhancements and fixed issues requested by users that should improve your experience working with Python and Jupyter Notebooks in Visual Studio Code. Some notable changes include:

We would also like to extend special thanks to this month’s contributors:

Try out these new improvements by downloading thePython extension and theJupyter extension from the Marketplace, or install them directly from the extensions view in Visual Studio Code (Ctrl + Shift + X or ⌘ + ⇧ + X). You can learn more aboutPython support in Visual Studio Code in the documentation. If you run into any problems or have suggestions,please file an issue on thePython VS Code GitHub page.

Category
Share

Author

jikang
Software Engineer

0 comments

Discussion is closed.

    Stay informed

    Get notified when new posts are published.
    Follow this blog
    youtube