Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve comments when sorting imports #749

Merged
merged 5 commits into from
Nov 16, 2022
Merged

Conversation

charliermarsh
Copy link
Member

@charliermarsh charliermarsh commented Nov 15, 2022

It's tough to come up with a strategy that does the "right" thing in all cases (in part, because it's hard to even define the "right" output in every case), but this PR preserves all comments, is stable across re-runs, and tries to do sensible things when merging across imports.

Resolves #675.

@charliermarsh charliermarsh force-pushed the charlie/isort-comments branch 3 times, most recently from 72041bc to 5ed4a4e Compare November 15, 2022 05:09
@charliermarsh
Copy link
Member Author

I'll add a few examples here tomorrow.

@andersk
Copy link
Contributor

andersk commented Nov 15, 2022

There’s a slight Black incompatibility—Black requires this newline:

import math
import os

# a comment
import pathlib
import sys

while Ruff deletes it:

import math
import os
# a comment
import pathlib
import sys

@andersk
Copy link
Contributor

andersk commented Nov 15, 2022

Comments seem to be unexpectedly counted toward the length of the following line in some cases, causing the following import to wrap early:

import a
# ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
from b import c

import a
# ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
from b import (
    c,
)

@charliermarsh
Copy link
Member Author

Woo thanks for trying! I noticed the second issue when running on Zulip :)

It's late here so will fix these both tomorrow (then hopefully merge).

@andersk
Copy link
Contributor

andersk commented Nov 15, 2022

Multi-line comments are unexpectedly sorted and deduplicated:

import io
# Old MacDonald had a farm,
# EIEIO
# And on his farm he had a cow,
# EIEIO
# With a moo-moo here and a moo-moo there
# Here a moo, there a moo, everywhere moo-moo
# Old MacDonald had a farm,
# EIEIO
from errno import EIO

import io
# And on his farm he had a cow,
# EIEIO
# Here a moo, there a moo, everywhere moo-moo
# Old MacDonald had a farm,
# With a moo-moo here and a moo-moo there
from errno import (
    EIO,
)

@charliermarsh
Copy link
Member Author

Ok, I believe all of those are addressed. Here's the diff on Zulip:

Screen Shot 2022-11-15 at 9 27 05 PM

@charliermarsh charliermarsh force-pushed the charlie/isort-comments branch 2 times, most recently from dba0f7f to f6e19e7 Compare November 16, 2022 02:40
@charliermarsh charliermarsh merged commit 82fea36 into main Nov 16, 2022
@charliermarsh charliermarsh deleted the charlie/isort-comments branch November 16, 2022 03:02
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.

Import sorting deletes comments
2 participants