Skip to content

First-party vs third-party import detection for modules built with C++/Rust #17761

@heiner

Description

@heiner

Summary

This is a spin out of #16712

Consider this setup:

Consider the following setup

$ tree
.
├── crates
│   └── my-package
│       └── pyproject.toml
├── known.py
├── main.py
└── pyproject.toml

3 directories, 4 files

With these contents:

$ cat crates/my-package/pyproject.toml
[project]
name = "my-package"
version = "0.1.0"

[build-system]
build-backend = "maturin"
requires = ["maturin>=1.0,<2.0"]

[tool.maturin]
bindings = "pyo3"
module-name = "my_package"


$ cat known.py


$ cat main.py
import os
import sys

import my_package
import torch
import unknown

import known


def main():
    print("Hello from myexample!")
    sys / my_package / os / unknown / torch / known


if __name__ == "__main__":
    main()


$ cat pyproject.toml
[project]
name = "myexample"
version = "0.1.0"
description = "Add your description here"
requires-python = ">=3.13"
dependencies = []

[tool.ruff]
src = [
    ".",
    "crates/*",
]

[tool.ruff.lint]
preview = true
select = [
    "I00",    # unsorted-imports, missing-required-import.
]

(imagine there's also crates/my-package/src/lib.rs that implements a PyO3 package, say).

The sort order here is what ruff will produce:

import os
import sys

import my_package
import torch
import unknown

import known

However, my_package is first-party. The actual module will be produced as an .so upon building.

Note that having crates/my-package/my_package.pyi does not change the behavior.

There seems to be no setting for src in [tool.ruff] that makes it identify the my_package import as first-party.

Initially I assumed it was the underscore that mislead it, but renaming crates/my-package to crates/my_package does not change its behavior.

Version

ruff 0.11.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingisortRelated to import sorting

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions