Skip to content

Third-party namespace packages can be incorrectly considered first-party packages by isort rules #12984

@tehkirill

Description

@tehkirill

Hi, team. Thanks for your hard work.

I noticed a change in how known-first-party imports get classified recently.
If I'm working on my_namespace.a project, I expect my_namespace.b to be classified as a Known(ThirdParty) (so does isort). This was the case with ruff as well, up until version 0.5.7 .

Working on my_namespace.a project, with the following stucture:

ruff-namespace-src-imports-test/
  ├─ main.py
  ├─ ruff.toml
  └─ src/
     └─ my_namespace/
        └─ a/
           └─ ...
# ruff.toml
[lint.isort]
known-first-party = ["my_namespace.a"]
# main.py
import os

import my_namespace.b
import numpy

import my_namespace.a

^ I see it as a valid import order, so does v0.5.6:

$ python3.12 -m ruff check main.py --select I --diff -v
[2024-08-19][11:11:40][ruff::resolve][DEBUG] Using configuration file (via parent) at: C:\ruff-namespace-src-imports-test\ruff.toml
[2024-08-19][11:11:40][ruff_workspace::pyproject][DEBUG] `project.requires_python` in `pyproject.toml` will not be used to set `target_version` when using `ruff.toml`.
[2024-08-19][11:11:40][ruff::commands::check][DEBUG] Identified files to lint in: 4.5661ms
[2024-08-19][11:11:40][ruff::diagnostics][DEBUG] Checking: C:\ruff-namespace-src-imports-test\main.py
[2024-08-19][11:11:40][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'my_namespace.a' as Known(FirstParty) (KnownFirstParty)
[2024-08-19][11:11:40][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'os' as Known(StandardLibrary) (KnownStandardLibrary)
[2024-08-19][11:11:40][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'my_namespace.b' as Known(ThirdParty) (NoMatch)
[2024-08-19][11:11:40][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'numpy' as Known(ThirdParty) (NoMatch)
[2024-08-19][11:11:40][ruff::commands::check][DEBUG] Checked 1 files in: 2.7995ms

But 0.5.7+ (including the latest 0.6.1) sees otherwise:

$ python3.12 -m ruff check main.py --select I --diff -v
[2024-08-19][11:12:44][ruff::resolve][DEBUG] Using configuration file (via parent) at: C:\ruff-namespace-src-imports-test\ruff.toml
[2024-08-19][11:12:44][ruff_workspace::pyproject][DEBUG] `project.requires_python` in `pyproject.toml` will not be used to set `target_version` when using `ruff.toml`.
[2024-08-19][11:12:44][ruff::commands::check][DEBUG] Identified files to lint in: 4.8756ms
[2024-08-19][11:12:44][ruff::diagnostics][DEBUG] Checking: C:\ruff-namespace-src-imports-test\main.py
[2024-08-19][11:12:44][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'my_namespace.a' as Known(FirstParty) (KnownFirstParty)
[2024-08-19][11:12:44][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'os' as Known(StandardLibrary) (KnownStandardLibrary)
[2024-08-19][11:12:44][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'my_namespace.b' as Known(FirstParty) (SourceMatch("C:\\ruff-namespace-src-imports-test\\src"))
[2024-08-19][11:12:44][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'numpy' as Known(ThirdParty) (NoMatch)
[2024-08-19][11:12:44][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'my_namespace.b' as Known(FirstParty) (SourceMatch("C:\\ruff-namespace-src-imports-test\\src"))
[2024-08-19][11:12:44][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'os' as Known(StandardLibrary) (KnownStandardLibrary)
[2024-08-19][11:12:44][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'numpy' as Known(ThirdParty) (NoMatch)
[2024-08-19][11:12:44][ruff_linter::rules::isort::categorize][DEBUG] Categorized 'my_namespace.a' as Known(FirstParty) (KnownFirstParty)
--- main.py
+++ main.py
@@ -1,6 +1,6 @@
 import os

-import my_namespace.b
 import numpy

 import my_namespace.a
+import my_namespace.b

[2024-08-19][11:12:44][ruff::commands::check][DEBUG] Checked 1 files in: 2.8168ms
Would fix 1 error.

Removing src/my_namespace correctly classifies my_namespace.b as known-third-party again.

Metadata

Metadata

Assignees

No one assigned

    Labels

    isortRelated to import sorting

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions