astral-sh/ruff-vscode

tomllib is not considered as in standard library in Format imports

GuillaumeLeGoc opened this issue · 5 comments

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 astral-sh/ruff#2343, and supposedly fixed here astral-sh/ruff#2345.

System information :

  • Windows 10
  • vscode 1.89.1
  • Ruff vscode extension v2024.20.0

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

Cheers !

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)

👍 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"

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