Replies: 3 comments 2 replies
-
It comes with Python for now, but we are moving folks to standalone extensions for formatting, e.g. https://marketplace.visualstudio.com/items?itemName=ms-python.autopep8 .
Same as above: we are moving folks off of the built-in linter support and over to dedicated extensions.
There isn't one because you can run mulitple linters but you can only have one formatter. So you can turn on/off individual linters, e.g.
Auto-fixes are going to come from various extensions, but Ruff would be one of them.
That's coming from Pylance.
It will invoke whichever formatter you have set up as your formatter for Python code.
You can set "[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
"editor.formatOnType": true
}, Some extra settings you might be interested in: "editor.rulers": [
80, # python-dev
88 # Black
],
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true # If you have something like Ruff or isort installed.
},
"editor.formatOnType": true # Auto-indent from Pylance.
},
That command just runs whatever extension you have set up to handle import sorting; we don't implement anything custom. A common issue we see with isort is people turn it on but forget to turn on its Black-compatible mode, and then the two tools "fight" each other over how imports should look.
|
Beta Was this translation helpful? Give feedback.
-
Cool, thanks for that comprehensive answer. I do have a couple of follow-up questions, though: I installed the ruff and black extensions as you suggested.
My user settings is as follows - as you see, it's pretty bare bones, and I don't think I added many of these myself, other than by clicking on "do you want X enabled?" dialogs when they pop up (such as the cmake one, I don't think I've ever dome more than look at the odd cmake file, so that's likely just from me shutting up a dialog).
The On an unrelated note, I think the
I still have the isort extension installed at the moment, I'll likely remove it when I work out how to set things up. But ruff doesn't sort imports by default, so I need to work out how to tell VS Code to include that. It might be worth noting that I want a lot of this set up by default, because I often use VS Code to edit standalone Python files that aren't in a project directory, so I don't have a Hmm, I just did a quick test and right click "Sort Imports" now prompts me to use ruff or isort. And when I pick ruff, it does it even though command line ruff doesn't by default. So that's rather cool (although a little confusing). I think what's frustrating me here is that a lot seems to happen "by magic" and it's hard for me to understand precisely what's going on.
Yes, I'm pretty sure I've seen this in the past. What I'm not clear on is how I "turn on Black-compatible mode" globally, so it applies for every Python file I edit, regardless of whether I'm in a project. To be clear, I'm not asking here how to configure command line isort/black, just how to get VS Code to work everywhere without this inconsistency. Maybe I'll configure the command line tools the same, although I might well do that on a per-project basis, as having projects not depend on global config is a good thing1, and I don't care for standalone files if my editor is the only way I can reformat the code without weird things happening 😉 Footnotes
|
Beta Was this translation helpful? Give feedback.
-
The Profile feature could be a quick, custom setup for VSCode + Python development environment. |
Beta Was this translation helpful? Give feedback.
-
Until now, I have basically just used VS Code more or less "out of the box" for my Python coding. But I want to start standardising on ruff as my linter, replacing flake8 and isort, and black as my code formatter (at least until ruff gets auto-formatter functionality). I can easily enough install the ruff plugin, and remove the isort plugin, but to be honest, I'm not at all clear what I need to do with the Python and pylance extensions.
I currently have "Formatting: Provider" set to "autopep8" in my config, but as far as I know I don't have autopep8 installed - does it come with the Python extension or is my config messed up somehow? I have "Linting: Enabled", but the same questions apply as I don't install any linter - and even more confusingly, there doesn't seem to be a "Linting provider" setting, so I'm not sure which linter I'm using in any case... And I don't even know if the ruff and black plugins supply all of the functionality that I currently get from wherever it comes from - things like auto-fix suggestions, auto-indentation, etc. Do I still need to configure the python/pylance plugins to cover areas like that?
There's also some options and actions where I'm not at all clear what is providing them. "Format document" is one (on the right click menu). Will that invoke black? Or how do I configure it to do so? If it uses some other formatter, what's to stop it applying rules that don't match black's? Similarly there's a "Sort Imports" action (which I can only find right now under the command palette as "Python Refactor: Sort Imports") which seems to use different rules than isort (I know I've used it in the past and it's "fixed" things in a way that isort then rejected).
Sorry - this is sort of a "help, I'm confused" rant. Really, what I'm asking is how do I set VS Code up to use black and ruff, or better still, how do I find out what I need to know to understand what's going on well enough to work out for myself how to do it.
I understand that a lot of this is down to the interaction between different extensions, and so there's something of a "not my problem" issue here. But as a user, I honestly don't care who is responsible, all I'm looking for is someone who can help me rationalise things so that I'm just using a single set of tools, and configuring things once in the way I want my project(s) to work.
If there's a better place where I can get support for this sort of question, then I'd love it if someone could point me there. One of the other issues with having so many extensions interacting is that it's very difficult to know where to go to get help with higher level questions like this.
Beta Was this translation helpful? Give feedback.
All reactions