astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
32.38k stars 1.08k forks source link

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

Open Jackenmen opened 1 year ago

Jackenmen commented 1 year ago

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.

charliermarsh commented 1 year ago

What you're describing could work... I believe we already require namespace packages to be defined explicitly (namespace-packages) so that might be fine.

(You could also mark schema as known-third-party to unblock you though.)

Cjkjvfnby commented 1 year ago

The same behavior is in isort https://github.com/PyCQA/isort/issues/2101