Skip to content

docs: document how isort moves comments - #2710

Closed
ump45nose wants to merge 1 commit into
PyCQA:mainfrom
ump45nose:contrib/add-some-documentation-about-how-isort-m-1843
Closed

ump45nose wants to merge 1 commit into
PyCQA:mainfrom
ump45nose:contrib/add-some-documentation-about-how-isort-m-1843

Conversation

@ump45nose

Copy link
Copy Markdown

Closes #1843

isort moves comments around when it sorts, and until now nothing documented the rules, so the behaviour looked arbitrary. The new docs/configuration/comments.md page explains:

  • a comment directly above an import is attached to that import and re-sorted with it, even if the file imports were originally in the opposite order
  • a comment separated from the imports by blank lines on both sides is a floating comment and stays where it was put
  • comments inside a from ... import block stay attached to their name, so the whole block is reordered around them
  • the settings that change this behaviour (force_sort_within_sections, ignore_comments, float_to_top)

Each rule is shown as a before/after pair.

Testing: the before/after examples are all real isort output, produced by running isort with the default settings (plus the variants named in each example) on the inputs shown. nox -s docs builds without warnings, so the new page is wired into the docs navigation correctly. I could not run the full nox test suite in this environment; no isort code was changed, so the behaviour described here is unchanged by this PR.

This pull request was prepared with the assistance of an AI coding agent and
has not yet been reviewed by a human.

isort attaches comments to the import statements they belong to rather
than to a position in the file, so a comment can end up somewhere other
than where it was written. Nothing in the docs described this, which led
to PyCQA#1843.

Document where comments end up:

- a comment directly above an import is sorted with it, and a blank line
  above the comment does not detach it
- a comment detached from every import stays put
- a comment on its own line inside a multiline import moves to the line
  that opens the import
- a trailing comment on an imported name is kept with that name, putting
  the name on its own from-import
- when a module is imported more than once, a comment attached to a later
  statement floats above the first statement for that module

Each example is checked against the current isort output.

Closes: PyCQA#1843
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add some documentation about how isort moves comments.

2 participants