Community Dev Gives VS Code Python Some YAPF
The latest update to Python in Visual Studio Code includes a new extension for Python formatting that was contributed by a member of the open source community.
The tool, titled simply yapf, is currently in preview, published by "EeyoreLee" to provide functionality that originated with Google's YAPF initiative, commonly understood to stand for "yet another Python formatter," though that description isn't mentioned in Google's YAPF GitHub repo.
The repo does say that YAPF (Goole uses uppercase, Microsoft/EeyoreLee use lowercase) is based on Daniel Jasper's clang-format and provides an algorithm to take code and calculate the best formatting that conforms to the configured style in order to lessen the drudgery of maintaining the code. "The ultimate goal is that the code YAPF produces is as good as the code that a programmer would write if they were following the style guide," Google's repo says.
Microsoft, meanwhile, this month said, "This extension provides yapf formatting support for Python files and Notebook cells" in announcing the September 2023 update to Python in VS Code. "Yapf support built into the Python extension will be deprecated in favor of the extension support. Subsequently, the corresponding setting
python.formatting.yapf will be removed from the Python extension."
The extension, which was last updated today (Sept. 25), is nearing 3,000 installs at the time of this writing, receiving a perfect 5.0 rating from one developer who reviewed it.
Then removal of the bits from Microsoft's main Python extension is part of the team's effort started last year to break up its big extension into separate tools. "Our team is working towards breaking the tools support we offer in the Python extension into separate extensions, with the intent of improving performance, stability and no longer requiring the tools to be installed in a Python environment -- as they can be shipped alongside an extension," Microsoft said last year.
The new update also features an experiment in which the Python extension uses environment variables to activate terminals implicitly on terminal launch to provide a faster experience, especially for conda users.
"This month, we are beginning the rollout of terminal activation using environment variables that activate the selected environment in the terminal without requiring any activation commands," the announcement post said. As an experiment, it will be rolled out as the default for 25 percent of pre-release users via a flag that devs can use to opt in or out of the test.
The team also updated the Python: Create Environment command to let devs choose to either recreate or use the existing environment when working on a project working within a workspace that already contains a .venv folder.
The team is responsible for the Python extension (98 million installs) and the Jupyter extension (70.5 million installs) in the VS Code Marketplace. Along with the company's Pylance extension, those tools are the top three most popular offerings in the marketplace.
Other changes and enhancements as listed in the post include:
- Unresolved import errors now indicate in which environment Pylance is looking for packages (pylance-release#4368)
- There's a new experimental setting called
python.analysis.enableSyncServer that enables multi-file IntelliSense support in vscode.dev. Support for github.dev and virtual workspaces is coming soon.
- Pylance no longer crashes on Jupyter Notebook cell deletion (@pylance-release#4685)
- There is a new dedicated topic on Python formatting in our docs where you'll learn how to set a default formatter such as autopep8 or Black formatter and customize it through various settings.
David Ramel is an editor and writer for Converge360.