Skip to content

[isort] support for configurable import section heading comments#23151

Merged
amyreese merged 27 commits intoastral-sh:mainfrom
Alex-ley-scrub:feat/6371-isort-headings
Feb 12, 2026
Merged

[isort] support for configurable import section heading comments#23151
amyreese merged 27 commits intoastral-sh:mainfrom
Alex-ley-scrub:feat/6371-isort-headings

Conversation

@Alex-ley-scrub
Copy link
Contributor

@Alex-ley-scrub Alex-ley-scrub commented Feb 9, 2026

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

@ntBre ntBre added isort Related to import sorting configuration Related to settings and configuration labels Feb 9, 2026
@astral-sh-bot
Copy link

astral-sh-bot bot commented Feb 9, 2026

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@Alex-ley-scrub
Copy link
Contributor Author

Alex-ley-scrub commented Feb 9, 2026

thanks @ntBre for approving the tests to run. I've pushed an update now to fix the clippy warnings etc.
will have a look at the other failing tests as well

cargo clippy --workspace --all-targets --all-features --locked -- -D warnings
cargo insta test --all-features --unreferenced reject --test-runner nextest
cargo nextest run --cargo-profile profiling --all-features
cargo nextest run --all-features --profile ci

@ntBre
Copy link
Contributor

ntBre commented Feb 9, 2026

No problem! It looks like you just need to accept a few configuration snapshot changes and regenerate the JSON schema. I'll try to give this a review soon.

(I think the python_package check that just failed is probably unrelated)

@ntBre ntBre self-requested a review February 9, 2026 19:50
Copy link
Member

@amyreese amyreese left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are the ty snapshots changing?

Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! This looks good to me overall, I mostly had very minor comments. I think trying to use the Tokens and double-checking the duplicate heading handling are the most significant.

Alex-ley-scrub and others added 7 commits February 11, 2026 00:00
astral-sh#23151 (comment)

Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
astral-sh#23151 (comment)

Co-authored-by: Amethyst Reese <amethyst@n7.gg>
astral-sh#23151 (comment)

Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
astral-sh#23151 (comment)

Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
astral-sh#23151 (comment)

Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
astral-sh#23151 (comment)

(might need to add a & ref to make this compile, will check locally)

Co-authored-by: Amethyst Reese <amethyst@n7.gg>
…ies field when running locally with `cargo dev generate-all`
…rties are not missing when we run `cargo dev generate-all`
Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@amyreese amyreese changed the title feat: #6371 isort headings [isort] support for configurable import section heading comments Feb 12, 2026
@amyreese amyreese merged commit d056a9f into astral-sh:main Feb 12, 2026
42 checks passed
carljm added a commit that referenced this pull request Feb 14, 2026
* main: (209 commits)
  [ty] Defer base inference for functional `type(...)` classes (#22792)
  flake8-executable: allow global flags in uv shebangs (EXE003) (#22582)
  [ty] Add `replace-imports-with-any` option (#23122)
  Update html comments in mdtests (#23269)
  Apply ruff formatting to mdtests (#22935)
  [ty] Exclude test-related symbols from non-first-party packages in auto-import completions
  [ty] Refactor `CursorTest` helper to support site-packages
  [ty] Qualify inlay hint edit symbol when possibly referencing another variable (#23265)
  [ty] Avoid `UnionBuilder` overhead when creating a new union from the filtered elements of an existing union (#22352)
  [ty] Refactor TypedDict key assignment validation (#23262)
  [ty] Improve Python environment path documentation (#23256)
  [ty] loop control flow analysis using loop header definitions
  Prepare for 0.15.1 (#23253)
  Remove docker-run-action (#23254)
  [ty] Allow discovering dependencies in system Python environments (#22994)
  Ensure pending suppression diagnostics are reported (#23242)
  [`isort`] support for configurable import section heading comments (#23151)
  [ty] Fix method calls on subclasses of `Any` (#23248)
  [ty] Fix bound method access on `None` (#23246)
  Make range suppression test snapshot actually useful (#23251)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

configuration Related to settings and configuration isort Related to import sorting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support heading setting for isort

3 participants

Comments