astral-sh / astral-sh/ruff

Ruff's first party import detection treats any folder in root as a package

Open
#2,004 2 comments 1 reaction 0 assignees View on GitHub
core
Dominant language
Rust
Stars
49.6k
Forks
2.4k
Avg merge
1d 21h
Merged PRs (30d)
435

Description

Repro:
```
mkdir ruff_isort_repro
cd ruff_isort_repro
python -m venv .venv
. .venv/bin/activate
pip install ruff

mkdir schema
echo '{}' > schema/example-json-schema.json

mkdir example_package
touch example_package/sub.py
echo $'import schema\n\nfrom example_package import sub' > example_package/__init__.py
```
```
$ ruff . --select I001 --fix --diff
--- example_package/__init__.py
+++ example_package/__init__.py
@@ -1,3 +1,2 @@
import schema
-
from example_package import sub

Would fix 1 error(s).
```

I've run into this because I use a `schema` package from PyPI *and* I have a `schema` folder with JSON schemas within the root of the repository which causes ruff to think that `schema` is a package. I think that one way of solving this would be to alter the current heuristic to check for `{base}/__init__.py` file and `{base}.py` file rather than `{base}` directory and `{base.py}` file. This may potentially affect namespace packages and I'm not sure there's a good solution for this that doesn't involve explicitly specifying namespace packages.

Contributor guide

Open the contributing guide

Research direction

Reproduce the report with the ruff commands and the generated example_package/__init__.py, then trace Ruff's first-party import detection and its handling of the schema directory. Done means the PyPI-style schema import is not classified as first-party while example_package remains correctly classified, without regressing namespace-package behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.