-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make isort's
detect-same-package
behavior configurable (#6833)
## Summary Our first-party import detection uses a heuristic that doesn't exist in isort: if an import appears to be from within the same package as the containing file, we mark it as first-party. For example, if you have a directory `./foo/__init__.py`, and you import `from foo import bar` in `./foo/baz.py`, we'll mark that as first-party. (See: #1266.) This is often unnecessary, and arguably should be removed (though it does have some important use-cases that are otherwise unserved -- I believe Dagster uses it to ensure that all packages mark imports from within the same package as first-party, but not imports _across_ different first-party packages)... but it does exist, and it does help in cases in which the `src` field is not properly configured. This PR adds an option to turn off this behavior: ```toml [tool.ruff.isort] detect-same-package = false ``` This is being introduced to help codebases migrating over from isort that may want more consistent behavior with their current sorting. ## Test Plan `cargo test`
- Loading branch information
1 parent
3bd199c
commit 281ce56
Showing
11 changed files
with
123 additions
and
2 deletions.
There are no files selected for viewing
Empty file.
3 changes: 3 additions & 0 deletions
3
crates/ruff/resources/test/fixtures/isort/detect_same_package/foo/bar.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import os | ||
import pandas | ||
import foo.baz |
2 changes: 2 additions & 0 deletions
2
crates/ruff/resources/test/fixtures/isort/detect_same_package/pyproject.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[tool.ruff] | ||
line-length = 88 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
crates/ruff/src/rules/isort/snapshots/ruff__rules__isort__tests__detect_same_package.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
source: crates/ruff/src/rules/isort/mod.rs | ||
--- | ||
bar.py:1:1: I001 [*] Import block is un-sorted or un-formatted | ||
| | ||
1 | / import os | ||
2 | | import pandas | ||
3 | | import foo.baz | ||
| | ||
= help: Organize imports | ||
|
||
ℹ Fix | ||
1 1 | import os | ||
2 |+ | ||
2 3 | import pandas | ||
4 |+ | ||
3 5 | import foo.baz | ||
|
||
|
19 changes: 19 additions & 0 deletions
19
crates/ruff/src/rules/isort/snapshots/ruff__rules__isort__tests__no_detect_same_package.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
source: crates/ruff/src/rules/isort/mod.rs | ||
--- | ||
bar.py:1:1: I001 [*] Import block is un-sorted or un-formatted | ||
| | ||
1 | / import os | ||
2 | | import pandas | ||
3 | | import foo.baz | ||
| | ||
= help: Organize imports | ||
|
||
ℹ Fix | ||
1 1 | import os | ||
2 |-import pandas | ||
2 |+ | ||
3 3 | import foo.baz | ||
4 |+import pandas | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.