Commit d056a9f
[
<!--
Thank you for contributing to Ruff/ty! To help us out with reviewing,
please consider the following:
- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title? (Please prefix
with `[ty]` for ty pull
requests.)
- Does this pull request include references to any relevant issues?
-->
## Summary
fixes #6371
- `settings.rs` - Added import_headings: FxHashMap<ImportSection,
String> field to Settings struct with default and display support
- `options.rs` - Added import_heading configuration option with
#[option] metadata, documentation, and TOML table syntax. Added
validation for unknown sections and mapping to Settings
- `mod.rs` - Core logic in format_import_block:
- Collects all configured heading values as "# {heading}" strings
- Strips matching heading comments from ALL imports in each section
(handles reordering)
- Inserts heading comments above each section after blank line logic
- `organize_imports.rs` - Extended fix range backward to include heading
comment lines above the first import, preventing duplication on fix
application
## Test Plan
Test Coverage (9 new tests, 156 total passing)
```
cargo test -p ruff_linter -- isort::tests
cargo test -p ruff_linter -- isort::tests::import_heading
```
| Test | Scenario |
|-------------------------------------------|------------------------------------------------------|
| import_heading.py | Basic unsorted imports get headings added |
| import_heading_already_present.py | Existing headings stripped and
re-added correctly |
| import_heading_already_correct.py | Properly sorted+headed imports
produce NO diagnostic |
| import_heading_unsorted.py | Completely unsorted imports get sorted
with headings |
| import_heading_with_no_lines_before.py | Interaction with
no_lines_before setting |
| import_heading_partial.py | Headings configured for only some sections
|
| import_heading_wrong_heading.py | Non-matching comments preserved as
regular comments |
| import_heading_single_section.py | Only one section present gets its
heading |
| import_heading_force_sort_within_sections.py | Works with
force_sort_within_sections |
---------
Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
Co-authored-by: Amethyst Reese <amethyst@n7.gg>isort] support for configurable import section heading comments (#23151)1 parent e22fa4f commit d056a9f
36 files changed
Lines changed: 684 additions & 4 deletions
File tree
- crates
- ruff_linter
- resources/test/fixtures/isort
- src/rules/isort
- rules
- snapshots
- ruff_workspace/src
- ruff/tests
- cli/snapshots
- snapshots
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
208 | 208 | | |
209 | 209 | | |
210 | 210 | | |
| 211 | + | |
211 | 212 | | |
212 | 213 | | |
213 | 214 | | |
| |||
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
210 | 210 | | |
211 | 211 | | |
212 | 212 | | |
| 213 | + | |
213 | 214 | | |
214 | 215 | | |
215 | 216 | | |
| |||
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
212 | 212 | | |
213 | 213 | | |
214 | 214 | | |
| 215 | + | |
215 | 216 | | |
216 | 217 | | |
217 | 218 | | |
| |||
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
212 | 212 | | |
213 | 213 | | |
214 | 214 | | |
| 215 | + | |
215 | 216 | | |
216 | 217 | | |
217 | 218 | | |
| |||
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
209 | 209 | | |
210 | 210 | | |
211 | 211 | | |
| 212 | + | |
212 | 213 | | |
213 | 214 | | |
214 | 215 | | |
| |||
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
210 | 210 | | |
211 | 211 | | |
212 | 212 | | |
| 213 | + | |
213 | 214 | | |
214 | 215 | | |
215 | 216 | | |
| |||
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
208 | 208 | | |
209 | 209 | | |
210 | 210 | | |
| 211 | + | |
211 | 212 | | |
212 | 213 | | |
213 | 214 | | |
| |||
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
208 | 208 | | |
209 | 209 | | |
210 | 210 | | |
| 211 | + | |
211 | 212 | | |
212 | 213 | | |
213 | 214 | | |
| |||
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
208 | 208 | | |
209 | 209 | | |
210 | 210 | | |
| 211 | + | |
211 | 212 | | |
212 | 213 | | |
213 | 214 | | |
| |||
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
321 | 321 | | |
322 | 322 | | |
323 | 323 | | |
| 324 | + | |
324 | 325 | | |
325 | 326 | | |
326 | 327 | | |
| |||
0 commit comments