astral-sh / ruff-vscode

A Visual Studio Code extension with support for the Ruff linter.
Other
945 stars 45 forks source link

tomllib is not considered as in standard library in Format imports #465

Closed GuillaumeLeGoc closed 1 month ago

GuillaumeLeGoc commented 1 month ago

Hi, I'm using vscode on a project with a conda environment using python 3.12. The conda environment is selected in vscode. When running Ruff: Format imports from the command palette, tomllib is moved with non-standard imports.

Before Ruff: Format imports :

import os
import tomllib

import numpy as np

After Ruff: Format imports :

import os

import numpy as np
import tomllib

The issue was raised here https://github.com/astral-sh/ruff/issues/2343, and supposedly fixed here https://github.com/astral-sh/ruff/pull/2345.

System information :

Do I have to take further action to fix the problem (configuration, ...) ?

Cheers !

dhruvmanila commented 1 month ago

I think this will only work if the required Python version is 3.11 or greater as tomllib was added in that Python version. Can you share your pyproject.toml? You could also run ruff from the command-line with the --verbose flag which will provide which category an import is in:

[2024-05-20][11:53:16][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'tomllib' as Known(StandardLibrary) (KnownStandardLibrary)
charliermarsh commented 1 month ago

👍 You can add something like:

[project]
...
requires-python = ">=3.11"

...to your pyproject.toml, to indicate that your project only supports Python 3.11 and later. Alternatively, you can tell Ruff to treat 3.11 as your minimum supported version with:

[tool.ruff]
target-version = "py311"
charliermarsh commented 1 month ago

Closing for now, let me know if you're continuing to see this after applying the above.