-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement isort's
default-section
setting (#10149)
## Summary This fixes #7868. Support isort's `default-section` feature which allows any imports that match sections that are not in `section-order` to be mapped to a specifically named section. https://pycqa.github.io/isort/docs/configuration/options.html#default-section This has a few implications: - It is no longer required that all known sections are defined in `section-order`. - This is technically a bw-incompat change because currently if folks define custom groups, and do not define a `section-order`, the code used to add all known sections to `section-order` while emitting warnings. **However, when this happened, users would be seeing warnings so I do not think it should count as a bw-incompat change.** ## Test Plan - Added a new test. - Did not break any existing tests. Finally, I ran the following config against Pyramid's complex codebase that was previously using isort and this change worked there. ### pyramid's previous isort config https://github.com/Pylons/pyramid/blob/5f7e286b0629b0a5f1225fe51172cba77eb8fda1/pyproject.toml#L22-L37 ```toml [tool.isort] profile = "black" multi_line_output = 3 src_paths = ["src", "tests"] skip_glob = ["docs/*"] include_trailing_comma = true force_grid_wrap = false combine_as_imports = true line_length = 79 force_sort_within_sections = true no_lines_before = "THIRDPARTY" sections = "FUTURE,THIRDPARTY,FIRSTPARTY,LOCALFOLDER" default_section = "THIRDPARTY" known_first_party = "pyramid" ``` ### tested with ruff isort config ```toml [tool.ruff.lint.isort] case-sensitive = true combine-as-imports = true force-sort-within-sections = true section-order = [ "future", "third-party", "first-party", "local-folder", ] default-section = "third-party" known-first-party = [ "pyramid", ] ```
- Loading branch information
Showing
11 changed files
with
174 additions
and
23 deletions.
There are no files selected for viewing
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
9 changes: 9 additions & 0 deletions
9
crates/ruff_linter/resources/test/fixtures/isort/default_section_user_defined.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,9 @@ | ||
from __future__ import annotations | ||
|
||
import django.settings | ||
from library import foo | ||
import os | ||
import pytz | ||
import sys | ||
|
||
from . import local |
9 changes: 9 additions & 0 deletions
9
crates/ruff_linter/resources/test/fixtures/isort/no_standard_library.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,9 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
import django.settings | ||
from library import foo | ||
import pytz | ||
|
||
from . import local | ||
import sys |
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
4 changes: 4 additions & 0 deletions
4
...sts__default_section_can_map_to_user_defined_section_default_section_user_defined.py.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,4 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/isort/mod.rs | ||
--- | ||
|
29 changes: 29 additions & 0 deletions
29
...apshots/ruff_linter__rules__isort__tests__no_standard_library_no_standard_library.py.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,29 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/isort/mod.rs | ||
--- | ||
no_standard_library.py:1:1: I001 [*] Import block is un-sorted or un-formatted | ||
| | ||
1 | / from __future__ import annotations | ||
2 | | | ||
3 | | import os | ||
4 | | import django.settings | ||
5 | | from library import foo | ||
6 | | import pytz | ||
7 | | | ||
8 | | from . import local | ||
9 | | import sys | ||
| | ||
= help: Organize imports | ||
|
||
ℹ Safe fix | ||
1 1 | from __future__ import annotations | ||
2 2 | | ||
3 |-import os | ||
4 3 | import django.settings | ||
5 4 | from library import foo | ||
5 |+import os | ||
6 6 | import pytz | ||
7 |+import sys | ||
7 8 | | ||
8 9 | from . import local | ||
9 |-import sys |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.