ruff icon indicating copy to clipboard operation
ruff copied to clipboard

Isort doesn't consider tomllib as part of the standard library

Open yakMM opened this issue 2 years ago • 2 comments

Consider the following:

import tomllib

import sys


def main():
    print(sys)
    print(tomllib)

Formatted with isort:

import sys
import tomllib


def main():
    print(sys)
    print(tomllib)

Formatted with ruff --fix:

import sys

import tomllib


def main():
    print(sys)
    print(tomllib)

Version: ruff 0.0.237

yakMM avatar Jan 30 '23 08:01 yakMM

Isort uses a list of known stdlib modules for each python version: https://github.com/PyCQA/isort/tree/main/isort/stdlibs

While ruff seems to use a static list for every python version: https://github.com/charliermarsh/ruff/blob/main/src/python/sys.rs

Is it worth doing the same as isort here?

yakMM avatar Jan 30 '23 08:01 yakMM

Would be good to respect the target-version for the imports. For the time being we could include tomllib in the existing setup, and keep this issue open to the track version-aware stdlib modules (good issue to get started).

Note that isort auto-generates these files here.

sbrugman avatar Jan 30 '23 09:01 sbrugman

Fixed in #2345.

charliermarsh avatar Jan 30 '23 22:01 charliermarsh

Should another issue be opened to respect the target-version for the imports or is supporting the latest stdlib enough?

yakMM avatar Jan 31 '23 09:01 yakMM

I think I am seeing this issue (again) in ruff 0.0.272. It wants to separate sys and tomllib while isort wants to keep them together. This is not happening in an empty folder with only a single bug.py, but it does happen as soon as I move this pyproject.toml next to it:

[tool.ruff]
select = ["I001"]

Output is

[2023-06-19][06:35:22][ruff_cli::commands::run][DEBUG] Identified files to lint in: 3.5124ms
[2023-06-19][06:35:22][ruff_cli::diagnostics][DEBUG] Checking: C:\Code\bug.py
[2023-06-19][06:35:22][ruff::rules::isort::categorize][DEBUG] Categorized 'tomllib' as Known(ThirdParty) (NoMatch)
[2023-06-19][06:35:22][ruff::rules::isort::categorize][DEBUG] Categorized 'sys' as Known(StandardLibrary) (KnownStandardLibrary)
[2023-06-19][06:35:22][ruff::rules::isort::categorize][DEBUG] Categorized 'tomllib' as Known(ThirdParty) (NoMatch)
[2023-06-19][06:35:22][ruff::rules::isort::categorize][DEBUG] Categorized 'sys' as Known(StandardLibrary) (KnownStandardLibrary)
[2023-06-19][06:35:22][ruff_cli::commands::run][DEBUG] Checked 1 files in: 8.0006ms
Found 1 error (1 fixed, 0 remaining).

bersbersbers avatar Jun 19 '23 04:06 bersbersbers

Update: I noticed I still had requires-python = ">=3.10" in my pyproject.toml - changing that to >=3.11 fixed the issue. (The fact that this is not assumed by running on Python 3.11.4, even without a project.toml file, is probably intended.)

bersbersbers avatar Jun 19 '23 04:06 bersbersbers