astral-sh / ruff-vscode

A Visual Studio Code extension with support for the Ruff linter.
Other
1.14k stars 55 forks source link

Not all linting errors being highlighted in VS Code #610

Closed krusion closed 2 months ago

krusion commented 2 months ago

Hello,

I was previously using flake8, black formatter, and isort and wanted to replace these with ruff since I've heard so many good things.

I have black format on save which I plan to have ruff act the same way. I did a quick test with a python file where I did deliberate errors to make sure ruff will work as expected. I turned off the format on save for my old setup and the new ruff profile and did the following:

  1. imported but unused (F401)
  2. Line too long (E501)
  3. too many blank lines (E303)
  4. blank line contains whitespace (W293)

With Flake 8, all of these are being highlighted. With Ruff only the imported but unused is being highlighted.

The weird thing is when I enabled the format on save, it does actually work and formats them. So it knows the code is wrong but not highlighting everything. I'm unsure what I'm doing wrong, this is a fresh profile on VS code.

Tried playing with different settings but nothing is working, here's a copy of my latest ssettings.json after some experimenting:

{
    "python.analysis.autoImportCompletions": true,
    "python.analysis.fixAll": ["source.unusedImports"],  
    "editor.defaultFormatter": "charliermarsh.ruff",
    "files.exclude": {
        "**/__pycache__": true,
        "**/.cache": true,
        "**/.coverage": true,
        "**/.coverage.*": true,
        "**/.hypothesis": true,
        "**/.mypy_cache": true,
        "**/.nox": true,
        "**/.pytest_cache": true,
        "**/.ruff_cache": true,
        "**/.tox": true
    },

    "[python]": {
        "editor.formatOnType": true,
        "editor.defaultFormatter": "charliermarsh.ruff",

      },
    "ruff.lineLength": 79,
    "ruff.lint.preview": true,
    "ruff.showNotifications": "always",
    "ruff.format.preview": true,
}

Here's a copy of the Ruff output:

2024-09-09 14:41:33.689 [info] Using interpreter: d:\Karim\Mark\merging tool\.venv\Scripts\python.exe
2024-09-09 14:41:33.699 [info] 2024-09-09 14:41:33,685 INFO Shutting down the server
2024-09-09 14:41:33,685 INFO Closing the event loop.

2024-09-09 14:41:33.856 [info] Falling back to bundled executable: c:\Users\C95043076\.vscode\extensions\charliermarsh.ruff-2024.44.0-win32-x64\bundled\libs\bin\ruff.exe
2024-09-09 14:41:33.980 [info] Resolved 'ruff.nativeServer: auto' to use the native server
2024-09-09 14:41:33.980 [info] Found Ruff 0.6.4 at c:\Users\C95043076\.vscode\extensions\charliermarsh.ruff-2024.44.0-win32-x64\bundled\libs\bin\ruff.exe
2024-09-09 14:41:33.980 [info] Server run command: c:\Users\C95043076\.vscode\extensions\charliermarsh.ruff-2024.44.0-win32-x64\bundled\libs\bin\ruff.exe server
2024-09-09 14:41:33.980 [info] Server: Start requested.

Please help me understand what I'm doing wrong and what can I change to make it work.

dhruvmanila commented 2 months ago

Can you provide the source code where you're facing this issue?

krusion commented 2 months ago
import blahblah
import numpy as np

def dms_to_decimal(dms):
    """Function to convert degree, minutes, and seconds to
    decimal for various DMS input formats.

    Args:
        dms (str): DMS input in various formats.

    Returns:
        float: Decimal equivalent rounded to 4 decimal places, or None if input is not in DMS format.

    """

df_scripters = 5
df_scripters["Nominal Value (MT)"] = np.where(
    (
        df_scripters["BP Upper Limit (AFS)"] - df_scripters["BP Lower Limit (AFS)"] == df_scripters["BP Upper Limit (MT)"] - df_scripters["BP Lower Limit (MT)"]
    ),
    np.where(
        round(
            df_scripters["BP Upper Limit (AFS)"]

            - df_scripters["Nominal Value"],
            4,
        )
        == round(
            df_scripters["Nominal Value"]
            - df_scripters["BP Lower Limit (AFS)"],
            4,
        ),
        round(
            (
                df_scripters["BP Upper Limit (MT)"]
                + df_scripters["BP Lower Limit (MT)"]
            )
            * 0.5,
            4,
        ),
        "Non Symmetrical",

    ),
    "Range Mismatch",
)

df_scripters.rename(
    {
        "Nominal Value": "Nominal Value (AFS)",
    },
    axis="columns",
    inplace=True,
)

you can use this. I'd like to also add that it missed "expected 2 blank lines, found 1 (E302)"

dhruvmanila commented 2 months ago
  • imported but unused (F401)
  • Line too long (E501)
  • too many blank lines (E303)
  • blank line contains whitespace (W293)

Oh never mind, I see the problem. This is happening because from the above rules only F401 is included in the default rule set. You'll need to provide additional rules in the Ruff config because the default ruleset doesn't include them. You can find the default Ruff config at https://github.com/astral-sh/ruff#configuration. You can learn more about rule selection at https://docs.astral.sh/ruff/linter/#rule-selection.

Closing this as it's working as expected.

krusion commented 2 months ago

I see, thanks! I guess it's confusing because it keeps on mentioning how it is a 1 to 1 replacement for flake8 but by default flake8 highlights these errors.

Just to understand better, from these rules, the ones that have a test tube are ones that are not active by default and I need to enable them? How do I do that if I'm using ruff as a VS code extension and not as an installed package. It seems the pyproject.toml and ruff.toml are both files within the installed package.

dhruvmanila commented 2 months ago

How do I do that if I'm using ruff as a VS code extension and not as an installed package. It seems the pyproject.toml and ruff.toml are both files within the installed package.

This section mentions on how you can configure Ruff via the config file. You can still create the config file and it should be picked up the Ruff.

krusion commented 2 months ago

Update: I guess I found this setting for VS code:

"ruff.lint.extendSelect": [
        "ALL"
    ],

Also can be accessed under Lint: Extend Select in the GUI. But I think I have more questions if you don't mind helping me out.

With ALL selected, I'm getting a lot more errors than before obviously. Two of the errors that I'm currently getting are:

  1. D205: 1 blank line required between summary line and description
  2. D401: First line of docstring should be in imperative mood: "{first_line}"

The first one should be fixable using the "Fix All" command based on the rules. The second one I would like to disable using the google convention. But again, I'm unsure how to do that using VS code.

Also to double check, for me to have ruff.toml, I need to install ruff, should that be as a pip install in the original python directory (so I can have it for all projects?) or as a standalone installer? I'm running windows without admin privileges.

Thank you.

dhruvmanila commented 2 months ago

Also to double check, for me to have ruff.toml, I need to install ruff, should that be as a pip install in the original python directory (so I can have it for all projects?) or as a standalone installer? I'm running windows without admin privileges.

You don't necessarily need to install Ruff to use the VS Code extension as we ship the executable bundled with it. Regarding the config file, the Ruff executable should pickup the config file automatically.

krusion commented 2 months ago

Regarding the config file, the Ruff executable should pickup the config file automatically.

The ruff config file doesn't exist right now. That's why I'm asking. I'll try to create it manually and see. So in general, what is the order that should be respected by ruff assuming conflicting information like line length or conventions and select/ignore lists?

Also can you let me know if you know the answer to the other 2 questions please?