eeyorelee / vscode-extension-yapf Goto Github PK
View Code? Open in Web Editor NEWVscode formatter extension support for python files using `yapf`.
License: MIT License
Vscode formatter extension support for python files using `yapf`.
License: MIT License
When the yapf
extension is used together with format on save, it is impossible to create empty __init__.py
files. They will always contain a newline and hence cause Pylint convention message trailing-newlines / C0305 or pycodestyle (PEP8) warning W391 (blank line at end of file).
"files.trimFinalNewlines": false
"editor.formatOnSave": false
__init__.py
, observe that a removed newline will be added again.yapf
itselfWhile autopep8
would remove the newline from the otherwise empty file, yapf
will not. yapf
will only remove extra newlines and leave the file with a single newline. However, if the file is empty, yapf
will also not add the newline if used manually. For some reason, the last part is not true for the yapf
extension. Here a newline will always be added to an otherwise empty file.
Activate VS Code setting to trim final newlines: "files.trimFinalNewlines": true
In vs code I cannot get the extension to work, this is the trace log:
2023-12-04 08:47:45.212 [info] [Trace - 8:47:45 AM] Sending notification 'textDocument/didChange'.
2023-12-04 08:48:19.264 [info] [Trace - 8:48:19 AM] Sending request 'textDocument/formatting - (3)'.
2023-12-04 08:48:19.267 [info] [Trace - 8:48:19 AM] Received notification 'window/logMessage'.
2023-12-04 08:48:19.267 [info] c:\Users\*\AppData\Local\Continuum\anaconda3\envs\Alarmanalys\python.exe -m yapf based_on_style = google spaces_before_comment = 2 split_before_logical_operator = true SPLIT_BEFORE_ARITHMETIC_OPERATOR = true SPLIT_BEFORE_BITWISE_OPERATOR = true indent_width = 4 column_limit = 120 SPLIT_COMPLEX_COMPREHENSION = true
2023-12-04 08:48:19.268 [info] [Trace - 8:48:19 AM] Received notification 'window/logMessage'.
2023-12-04 08:48:19.268 [info] CWD Linter: m:\Operations\1. Projekt\2020\Ny Ram\Rammaskin Prod statistik
2023-12-04 08:48:19.277 [info] [Trace - 8:48:19 AM] Received notification 'window/logMessage'.
2023-12-04 08:48:19.278 [info] yapf: input filenames did not match any python files
2023-12-04 08:48:19.278 [info] [Trace - 8:48:19 AM] Received notification 'window/logMessage'.
2023-12-04 08:48:19.278 [info] file:///m%3A/Operations/1.%20Projekt/2020/Ny%20Ram/Rammaskin%20Prod%20statistik/AlarmanalysDashboard2.py :
2023-12-04 08:48:19.278 [info] [Trace - 8:48:19 AM] Received response 'textDocument/formatting - (3)' in 14ms.
YAPF: v2023.11.122090301
VS Code: 1.84.2
Win 10
It did work properly before the VS code change to python formatting settings, perhaps I configured it incorrectly?
Relevant configs:
"[python]": {
"editor.defaultFormatter": "eeyore.yapf",
"editor.formatOnType": false
},
"yapf.args": [
"based_on_style = google",
"spaces_before_comment = 2",
"split_before_logical_operator = true",
"SPLIT_BEFORE_ARITHMETIC_OPERATOR = true",
"SPLIT_BEFORE_BITWISE_OPERATOR = true",
"indent_width = 4",
"column_limit = 120",
"SPLIT_COMPLEX_COMPREHENSION = true"
],
"yapf.showDebugLog": true,
Just worked on prerelease version and will also work on next release version. So it's better if you attach the version to issue here that I can easily know which commit corresponds to
Hi,
I have a conda Python 3.7 environment that I always use and it seems to be unsupported by the extension. Output tab:
..
File "/Users/me/.vscode/extensions/eeyore.yapf-2023.11.109102916/bundled/libs/pygls/server.py", line 35, in <module>
import cattrs
File "/Users/me/.vscode/extensions/eeyore.yapf-2023.11.109102916/bundled/libs/cattrs/__init__.py", line 1, in <module>
from .converters import BaseConverter, Converter, GenConverter, UnstructureStrategy
File "/Users/me/.vscode/extensions/eeyore.yapf-2023.11.109102916/bundled/libs/cattrs/converters.py", line 26, in <module>
from ._compat import (
File "/Users/me/.vscode/extensions/eeyore.yapf-2023.11.109102916/bundled/libs/cattrs/_compat.py", line 25, in <module>
from typing_extensions import TypedDict as ExtensionsTypedDict
File "/Users/me/.vscode/extensions/eeyore.yapf-2023.11.109102916/bundled/libs/typing_extensions.py", line 874
def TypedDict(typename, fields=_marker, /, *, total=True, **kwargs):
^
SyntaxError: invalid syntax
Hi,
First, thank you for the wonderful extension.
My style is set as follows:
{use_tabs: 1, column_limit: 999, allow_multiline_dictionary_keys: 1}
Despite allow_multiline_dictionary_keys
, the following code is compressed to 1 line upon save:
params = {
"display_name": "Echoproof",
"is_tab": False,
"debug": False,
"context_delimiter":"\\n",
"negative_delimiter":" ",
"max_messages":10,
"max_multiplier":4,
"scaling":"exponential",
"enable": True,
"blacklist":"",
"tab":"chat"
}
After:
params = {"display_name": "Echoproof", "is_tab": False, "debug": False, "context_delimiter": "\\n", "negative_delimiter": " ", "max_messages": 10, "max_multiplier": 4, "scaling": "exponential", "enable": True, "blacklist": "", "tab": "chat"}
I am running VSCode v1.85.1 and extension v2023.12.104100706. I am fairly certain the flag behaved correctly in the past. Thank you.
Hello!
I would be very keen on using the extension, but I don't seem to be able to start the server/
Here's the output logs:
2023-10-25 15:03:39.610 [info] [Error - 3:03:39 PM] Server initialization failed.
2023-10-25 15:03:39.610 [info] Message: Pending response rejected since connection got disposed
Code: -32097
2023-10-25 15:03:39.611 [info] [Info - 3:03:39 PM] Connection to server got closed. Server will restart.
2023-10-25 15:03:39.611 [info] true
2023-10-25 15:03:39.611 [info] [Error - 3:03:39 PM] yapf client: couldn't create connection to server.
2023-10-25 15:03:39.611 [info] Message: Pending response rejected since connection got disposed
Code: -32097
2023-10-25 15:03:39.611 [info] [Error - 3:03:39 PM] Restarting server failed
2023-10-25 15:03:39.611 [info] Message: Pending response rejected since connection got disposed
Code: -32097
2023-10-25 15:03:43.203 [info] Traceback (most recent call last):
File "c:\Users\myUser\.vscode\extensions\eeyore.yapf-0.1.10\bundled\tool\lsp_server.py", line 46, in <module>
from pygls import server, uris, workspace
File "c:\Users\myUser\.vscode\extensions\eeyore.yapf-0.1.10\bundled\libs\pygls\server.py", line 35, in <module>
import cattrs
File "c:\Users\myUser\.vscode\extensions\eeyore.yapf-0.1.10\bundled\libs\cattrs\__init__.py", line 1, in <module>
from .converters import BaseConverter, Converter, GenConverter, UnstructureStrategy
File "c:\Users\myUser\.vscode\extensions\eeyore.yapf-0.1.10\bundled\libs\cattrs\converters.py", line 26, in <module>
from ._compat import (
File "c:\Users\myUser\.vscode\extensions\eeyore.yapf-0.1.10\bundled\libs\cattrs\_compat.py", line 25, in <module>
from typing_extensions import TypedDict as ExtensionsTypedDict
File "c:\Users\myUser\.vscode\extensions\eeyore.yapf-0.1.10\bundled\libs\typing_extensions.py", line 874
def TypedDict(typename, fields=_marker, /, *, total=True, **kwargs):
^
SyntaxError: invalid syntax
I have python 3.11.5 as my system level python, with yapf 0.40.2 installed globally.
I have the following in my settings.json:
"python.formatting.provider": "none",
...
"[python]": {
"editor.formatOnSaveMode": "file",
"editor.formatOnSave": true,
"editor.defaultFormatter": "eeyore.yapf"
}
...
Am I doing something wrong?
Thank you for the help!
Best.
Hi,
firstly, thanks for creating this extension ๐
Now to the issue:
When trying to format jupyter notbeook cells, the following error is shown: AttributeError: 'NoneType' object has no attribute 'stdout'
. Is it possible to fix this and make yapf working for cells (or the whole) notebook?
Best Pascal
I use the VSCode plugin to format my Python code, so I won't install yapf separately when creating a new environment. However, for formal projects, a yapf configuration file is sometimes needed to unify the team's configuration. Since I haven't installed yapf, I can't directly export it locally, and it's also troublesome to search online. Therefore, I hope to add a command to generate a default yapf configuration file. Thank you.
In Jupyter Notebook(.ipynb) file, When a cell contains magic function such as %matplotlib inline
, yapf cannot format this cell, but autopep8(builtin formatter) can.
When I delete %matplotlib inline
, yapf works fast and well.
I hope this issue will be fix soon.
Thanks
my settings.json:
"[python]": {
"editor.formatOnSaveMode": "file",
"editor.formatOnSave": true,
"editor.defaultFormatter": "eeyore.yapf"
},
"yapf.args": [
"--style",
"{based_on_style: pep8, indent_width: 2}"
],
in cmd is ok
py -m yapf --style='{based_on_style: pep8, indent_width: 2}' a.py
in vscode the output is
2023-08-24 12:16:14.960 [info] yapf: "'{based_on_style: pep8, indent_width: 2}'" is not a valid style or file path
Hi,
your extension cannot handle new f-string capabilities of Python 3.12, writing a single line of code like this
f"https://ldomain.com.com{user["x1"]["x2"]["x3"]["x3"]["x5"]}",
makes the whole extension break down silently and stops it from any further formatting.
Only if you change the inside " to ' it works again.
But this is perfectly valid now in 3.12 and a massive improvement for using f-string. Please make it possible that your extension can handle that.. And on top does not just fail silently
Thanks
@EeyoreLee Can we mention yapf
extension in the Python extension blogs?
Hi, I'm trying to get this extension to work. I've added the following lines to my settings.json
:
"python.formatting.provider": "none",
"[python]": {
"editor.wordBasedSuggestions": false,
"editor.defaultFormatter": "eeyore.yapf"
}
But whenever I try to use the "format document" command, VSCode shows a dialog "There is no formatter for 'Python' files installed". Interestingly, VSCode tries to help with configuration suggestions when one is editing settings.json
. There's no suggestion inside a "[python]"
section though for "editor.defaultFormatter"
, where it does suggest this configuration key in, for example, in an "[xml]"
section. See screenshots.
VSCode version 1.84.2
. eeyore.yapf version 0.1.10
.
Editor suggestions for an "[xml]"
section:
Editor suggestions for an "[python]"
section:
This is a weird one. Given this block of code
def __setProjAndEnv(self, project: str, env: str):
with tryBlockSignals(self.__sw.envComboBox, self.__sw.projectComboBox), \
waitCursor():
pass
running YAPF through VS Code on Windows will strip out the trailing \, making it invalid python code. running yapf -i from the command line does not have the same behavior. We're using a custom app path for YAPF set in the plugin config. Works fine on Linux
Neither yapf nor black python formatting seems to work installed on a Remote SSH machine.
Yapf works fine locally, but when I install on the remote, it throws an error:
Extension 'yapf' is configured as formatter but it cannot format 'Python'-files
And even the MS -provided Black provider doesn't crash but it doesn't seem to do anything? Did something break upstream?
I need to be able to set the path to my .style.yapf
file as I am using a monorepo and it is not in the workspace root. Is this possible?
From my experience working with the ma-python.black-formatter
and ms-python.autopep8
, users often seem to trigger selection formatting. I have implemented this support in black and autopep8. You could replicate that here to provide a better experience in those scenarios.
If range formatting is not supported, black
does not have range formatting. so in that case we leave a warning in logs and return edits for the whole file.
microsoft/vscode-black-formatter#342
if range formating is supported. autopep8
supports range formatting. For this case we use the range formatting option in autopep8. Edits are returned only for the lines with changes.
microsoft/vscode-autopep8#175
there is error in output
`2023-08-25 11:48:26.165 [info] import attrs
File "c:\xxx\xxx\.vscode\extensions\eeyore.yapf-0.1.6\bundled\libs\attrs_init_.py", line 62, in
2023-08-25 11:48:26.168 [info] getattr = _make_getattr(name)
2023-08-25 11:48:26.168 [info] NameError: name '_make_getattr' is not defined`
We're using our own internal version of yapf and would like to be able to just provide a path to the executable for that version as you can currently do with the
"python.formatting.yapfPath"
setting in VS Code.
You triggered formatting with Autopep8, would you like to install one of our new formatter extension? This will also set it as the default formatter for Python.
If there is a notification from Python extension like above when you press ctrl-s, setup python.formatting.provider
to "none"
.
Python 3.7 is not supported, I think due to the version of typing_extensions in use? This should either be supported or a minimum python version should be documented on the extension. Thanks!
Cause I do a check for syntax on FormatOnType
mode. Break if code can't be parse to ast tree, but syntax also to mark as having magic function.
I'll fix it on next release
The formatter formats instantly when editing my file. This makes it impossible to do certain edits (eg. adding a newline between two function params, to insert another param) because it gets reverted instantly. This is very annoying, the formatter should format on save only (if this is enabled of course).
If a file or a notebook cell is formatted it will cause the last line to be an empty line.
While for files this might be desireable, in a Jupyter Notebook I personally find it irritating, as there, cells often are only a few lines long anyway and the additional empty lines at the end of the cells now add up fast (and cause a lot of changes for git).
When I used yapf previously with the built-in support via the "python.formatting.provider" : "yapf"
setting this was not the case and I don't think this is a yapf setting either (couldn't find anything, and I had the same style setting before with the old built-in support, when I didn't have this issue, and now the same behaviour even if I use the default style...)?
Is there any way to make this configurable or supress adding a trailing empty line in notebook cells?
Thanks!
yapf extension version: 0.1.9
yapf python package version: 0.40.2
VS Code Version:
Version: 1.83.1 (user setup)
Commit: f1b07bd25dfad64b0167beb15359ae573aecd2cc
Date: 2023-10-10T23:48:05.904Z
Electron: 25.8.4
ElectronBuildId: 24154031
Chromium: 114.0.5735.289
Node.js: 18.15.0
V8: 11.4.183.29-electron.0
OS: Windows_NT x64 10.0.19045
I am having an issue where doing Format Selection
is making changes outside the selected range. To clarify, doing Format Document
does yield different results (more changes), so they are at least different.
I am using:
My .vscode/settings.json is
{
"python.languageServer": "Pylance",
"python.analysis.diagnosticSeverityOverrides": {
},
"python.analysis.inlayHints.variableTypes": "off",
"python.analysis.inlayHints.functionReturnTypes": true,
"python.analysis.inlayHints.callArgumentNames": "off",
"[python]": {
"editor.defaultFormatter": "eeyore.yapf"
},
}
My yapf formatter is just not sticking. It keeps asking me that there are multiple default formatters. I'm on devcontainer. There aren't multiple formatter, just yapf. What is going on? for the record I'm doing ctrl+s to save the file before having to resort to the Format Document in the command pallete.
These used to work before, I just changed to latest version of devcontainers definitions according to this
These are my vscode devcontainer settings:
"settings": {
"python.defaultInterpreterPath": "/usr/local/bin/python",
"python.testing.pytestPath": "/usr/local/py-utils/bin/pytest",
"python.languageServer": "Pylance",
"python.analysis.typeCheckingMode": "basic",
"python.analysis.completeFunctionParens": true,
"python.autoComplete.addBrackets": true,
"autoDocstring.startOnNewLine": true,
// The character to replace whitespace in new branch names.
"git.branchWhitespaceChar": "-",
"git.pruneOnFetch": true,
"git.autofetch": true,
"git.enableSmartCommit": true,
"git.confirmSync": false,
"gitlens.gitCommands.skipConfirmations": [
"fetch:command",
"stash-push:command",
"switch:command",
"branch-create:command"
],
"gitlens.views.repositories.files.layout": "tree",
"prettier.printWidth": 120,
"prettier.tabWidth": 4,
"editor.minimap.enabled": false,
"editor.renderControlCharacters": true,
"editor.renderWhitespace": "all",
"editor.formatOnPaste": true,
"editor.formatOnSave": true,
"editor.formatOnType": true,
"files.insertFinalNewline": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
"[yaml]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
},
"[json]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
},
"[jsonc]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
},
"[python]": {
"editor.formatOnSaveMode": "file",
"editor.formatOnSave": true,
"editor.defaultFormatter": "eeyore.yapf"
}
}
I'd like to point out same does not happen outside the devcontainer. On my host it works fine.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.