Using Ruff Linter with Python in VS Code
Asked Answered
B

3

6

I have installed the Ruff extension and enabled it in VS Code, but it doesn't seem to be underlining my code at all and providing suggestions like my previous linters did.

I did a clean install of VS Code, so most of the Python/Ruff extension settings are default. Is there an additional step I need to take to get it to start underlining my code and providing recommendations?

Here is a screenshot:

enter image description here

It's highlighting the imports for not being used, but I would expect other things to be highlighted like the line length, the additional spaces at the end of the file, not having 2 spaces before function declaration, etc.

Here is the sample code as requested:

import pandas as pd
import numpy as np

print('kkkkkkkkjlskdjflksdjflsdjflkdsjflksdjflkjdslkfjsdlkjflsdjflsdjfldsjflsdkjflsdjflksdjflksdjflksdjflskdjflsdkjfklsdjkl')
def test_func(x):
    y=x+1
    return y
Banneret answered 20/1 at 17:54 Comment(4)
Could you provide a screenshot (and the code in question as text)?Protasis
Apologies, I have updated the question with that information.Banneret
For Python linting in VS Code, why not use extensions like Pylint, Flake8, or Pyright. These do better for Python in VSCode.Sherman
I was getting a message from VS Code that my linter was being deprecated and upon further research, it seemed like Ruff was the new linter of choice for most people. It is much faster than any other linter available.Banneret
H
5

Take another look at the ruff documentation. You must enable or disable your desired linter rules and/or your formatting rules. For example, if you create a ruff.toml configuration in the root of your project with

[lint]
select = ["ALL"]

the output looks more like what you expect.

enter image description here

Hebrides answered 21/1 at 7:14 Comment(2)
Thank you. I ended up discovering this after about a day of research and trial and error. Even when "ALL" is included, it isn't underlining some of the things that my previous "Pycodestyle" linter would, so I think that confused me a bit. Would you be able to explain how to have a global .toml file instead of a project-specific one? I don't know where to put it. I have a Mac and a PC.Banneret
You can set arguments for ruff in the VSCode settings. For global linter settings create a ruff.toml configuration and use "ruff.lint.args": ["--config=/path/to/global/ruff.toml"] in the settings.json of VSCode.Hebrides
B
6

Based on the answer by @anit3res, I had to use something like this in my pyproject.toml to get ruff going in vsc:

[tool.ruff.lint]
select = ["ALL"]

[tool.ruff]
line-length = 120

I also needed to restart the ruff server for this to take effect.

Bossism answered 17/3 at 14:46 Comment(0)
H
5

Take another look at the ruff documentation. You must enable or disable your desired linter rules and/or your formatting rules. For example, if you create a ruff.toml configuration in the root of your project with

[lint]
select = ["ALL"]

the output looks more like what you expect.

enter image description here

Hebrides answered 21/1 at 7:14 Comment(2)
Thank you. I ended up discovering this after about a day of research and trial and error. Even when "ALL" is included, it isn't underlining some of the things that my previous "Pycodestyle" linter would, so I think that confused me a bit. Would you be able to explain how to have a global .toml file instead of a project-specific one? I don't know where to put it. I have a Mac and a PC.Banneret
You can set arguments for ruff in the VSCode settings. For global linter settings create a ruff.toml configuration and use "ruff.lint.args": ["--config=/path/to/global/ruff.toml"] in the settings.json of VSCode.Hebrides
T
2

One of the things I had to do to get it working in my VSCode was to enable the new beta native server.

enter image description here

Here the language server needs to be enabled, I enabled it like so:

enter image description here

Now, I get this when I hover over the yellow in the example

enter image description here

Tsana answered 28/5 at 14:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.