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

Support for magic trailing comma #1683

Closed
Jackenmen opened this issue Mar 6, 2021 · 6 comments · Fixed by #1876
Closed

Support for magic trailing comma #1683

Jackenmen opened this issue Mar 6, 2021 · 6 comments · Fixed by #1876
Labels
black Black compatibility issue enhancement New feature or request

Comments

@Jackenmen
Copy link

Black auto-explodes imports, collections, function calls, etc. when they contain a trailing comma:
https://github.com/psf/black/blob/master/docs/the_black_code_style.md#the-magic-trailing-comma

This means that even though this would fit into one line:

from seven_dwwarfs import (
    Grumpy,
    Happy,
    Sleepy,
    Bashful,
    Sneezy,
    Dopey,
    Doc,
)

Black will NOT convert it into this:

from seven_dwwarfs import Grumpy, Happy, Sleepy, Bashful, Sneezy, Dopey, Doc

As long as the import list contains a trailing comma.

isort currently does not have the magic trailing comma handling as far as I can tell and will collapse the import list if it can fit within the line length limit.

@timothycrosley timothycrosley added the enhancement New feature or request label Mar 6, 2021
@timothycrosley
Copy link
Member

I agree, this would be great to add! The only saving grace about the current behavior is that since it forces a trailing comma, it is always black compatible - but it would be nice to be compatible with the spirit of black's formatting approach as well.

@timothycrosley timothycrosley added the black Black compatibility issue label Mar 6, 2021
@dzieciou
Copy link
Contributor

dzieciou commented Mar 24, 2021

Exactly, it also makes pylama checks with isort problematiic after applying black :-(

Here's my use case to reproduce the problem:

pip install isort==5.8.0 black==20.8b1

I have the following file test.py

from collections import (defaultdict, namedtuple,)

that black turns into multi-line import because of the trailing comma:

from collections import (
    defaultdict,
    namedtuple,
)

However, isort --profile black does not respect that and changes that into single-line import

from collections import defaultdict, namedtuple

@onerandomusername
Copy link

Hi, I'm having this problem myself, I would really appreciate support for the magic trailing comma, but I understand it would be another configuration option. If I had some ideas to what to name it, I could try to implement this myself, but I haven't looked at the isort source yet, so that may not be possible for me to do,

@Jackenmen
Copy link
Author

Jackenmen commented Sep 26, 2021

If I had some ideas to what to name it, I could try to implement this myself,

magic_trailing_comma or split_on_trailing_comma probably make most sense. The former probably makes it clear for Black users while the latter better explains what the option does and seems to be more in line with other option names in isort.

Either way, I think that the name should be the least of your concerns when it comes to implementing it 😄
It would be a matter of a simple find&replace if someone comes up with a better name during the PR review stage.

@onerandomusername
Copy link

Okay, I'm going to try to work on this :)

@anirudnits
Copy link
Collaborator

@onerandomusername thanks for taking up this issue! I am assigning this issue to you.
Let me know if you need any help, going through the code-base or anything else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
black Black compatibility issue enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants