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

Change isort.split-on-trailing-comma default to false #4153

Closed
janosh opened this issue Apr 29, 2023 · 11 comments
Closed

Change isort.split-on-trailing-comma default to false #4153

janosh opened this issue Apr 29, 2023 · 11 comments
Labels
question Asking for support or clarification

Comments

@janosh
Copy link

janosh commented Apr 29, 2023

Currently defaults to true, causing this to stay as is

from a import (
    b,
)

I think a better default would be to fold:

from a import b
@charliermarsh
Copy link
Member

I think the current default is "correct" because it's consistent with Black's behavior and isort's profile = "black", unless I'm misunderstanding. Black respects these "magic trailing commas" (which has the downside you've outlined above: if you have a trailing comma, Black will never remove it) and so removing them would break Black compatibility by default.

So, I'd rather err on the side of leaving this default as-is. Users are, of course, welcome to change the setting in their own projects :)

@charliermarsh charliermarsh added the question Asking for support or clarification label Apr 29, 2023
@janosh
Copy link
Author

janosh commented Apr 29, 2023

Good point. Didn't consider black. I wish they'd change this default but seems very unlikely.

@janosh janosh closed this as completed Apr 29, 2023
@intgr
Copy link
Contributor

intgr commented May 20, 2023

I also found this behavior surprising. I think blacks's behavior is not relevant here, black is fine with either formatting. We should follow isort's behavior instead.

split-on-trailing-comma = false matches isort's default behavior (with or without profile = "black").

@intgr
Copy link
Contributor

intgr commented May 20, 2023

it's consistent with Black's behavior and isort's profile = "black", unless I'm misunderstanding

I think you're misunderstanding isort's behavior :)

janosh added a commit to materialsproject/pymatgen that referenced this issue May 20, 2023
@charliermarsh
Copy link
Member

Is this not a bug in isort? The documentation says that split-on-trailing-comma defaults to true for the Black profile (and collapsing imports like that is definitely inconsistent with Black).

@intgr
Copy link
Contributor

intgr commented May 20, 2023

If that's the case, it sounds like a bug indeed.

Where does it say that? https://pycqa.github.io/isort/docs/configuration/options.html#split-on-trailing-comma only says the behavior mimics black when enabled, not that it's set to true with black profile.

@charliermarsh
Copy link
Member

In the Profiles documentation:

Screen Shot 2023-05-20 at 6 15 10 PM

@charliermarsh
Copy link
Member

(I agree with you that I'm not seeing this behavior in practice, so not certain what's wrong: the documentation or the implementation.)

@intgr
Copy link
Contributor

intgr commented May 20, 2023

Ah OK, I was looking at the "Black Compatibility" and "Options" sections of their docs.

@johnthagen
Copy link

(and collapsing imports like that is definitely inconsistent with Black).

One minor note I wanted to bring up, it is inconsistent with how Black would have formatted it, but if isort or Ruff do format it to:

from a import b

Black is happy with it, just like if a user would have compressed it to one line manually. We've been running this way with isort + black for years, but it does seem like this might not have been intentional as mentioned in this thread.

I do find this useful so I'm glad to hear there is an opt-out, since unlike other parts of code, it'd be much less likely for a user to intentionally want to lengthen imports as they might some other code since imports are mostly boilerplate. In my experience it's much more likely that a user has their editor automatically remove an unused import but it leaves the trailing comma for another import from that same module. I've observed that over time in large code bases without isort collapsing these it can lead to a lot of wasted vertical space.

@janosh
Copy link
Author

janosh commented May 21, 2023

if isort or Ruff do format it to:

from a import b

Black is happy with it

Great point. I'd appreciate departing from isort profile black in this respect since split-on-trailing-comma=true is a poor default imo for the same reason @johnthagen mentioned

it'd be much less likely for a user to intentionally want to lengthen imports

lbluque pushed a commit to lbluque/pymatgen that referenced this issue May 23, 2023
andersk added a commit to andersk/zulip that referenced this issue Aug 3, 2023
isort did this by default, though it’s unclear whether that was
intended; see astral-sh/ruff#4153.

Signed-off-by: Anders Kaseorg <[email protected]>
andersk added a commit to zulip/zulip that referenced this issue Aug 3, 2023
isort did this by default, though it’s unclear whether that was
intended; see astral-sh/ruff#4153.

Signed-off-by: Anders Kaseorg <[email protected]>
jychen630 pushed a commit to jychen630/zulip that referenced this issue Aug 4, 2023
isort did this by default, though it’s unclear whether that was
intended; see astral-sh/ruff#4153.

Signed-off-by: Anders Kaseorg <[email protected]>
alexmv pushed a commit to alexmv/zulip that referenced this issue Aug 23, 2023
isort did this by default, though it’s unclear whether that was
intended; see astral-sh/ruff#4153.

Signed-off-by: Anders Kaseorg <[email protected]>
(cherry picked from commit 733083c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Asking for support or clarification
Projects
None yet
Development

No branches or pull requests

4 participants