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

Interopability with black #694

Closed
ambv opened this issue Apr 13, 2018 · 28 comments
Closed

Interopability with black #694

ambv opened this issue Apr 13, 2018 · 28 comments
Labels
black Black compatibility issue enhancement New feature or request ongoing A task that represents continous work and coordination and is never meant to be "complete"

Comments

@ambv
Copy link
Member

ambv commented Apr 13, 2018

Hey, Timothy! Thanks for isort, it's a very useful project.

I made an opinionated formatter, Black. Quite a few of my users are also isort users. That's great, I don't want to have to perform import sorting in Black. However, there's a small issue.

None of isort's multiline modes fits what Black is doing. Black wraps lines like this:

  • try to fit everything in one line; if you can't then
  • indent the contents of the parens one level and try to fit those in one line; if you can't then
  • explode one element per line.

The second step in Black's algorithm is like your mode 5, and the third is like mode 3. I'd like to create a pull request for isort to introduce mode 8 (5+3) that does what Black does in this scenario. This would enable Black and isort to co-exist and keep consistent import styling.

Would you accept such pull request?

(I initially wrote this on Twitter but this belongs here better.)

@audiolion
Copy link

@timothycrosley if you would accept a PR for this it would be fantastic!

@ambv
Copy link
Member Author

ambv commented Apr 24, 2018

In fact, Black now does what "Mode 3 + trailing commas" is doing in isort. This is no longer necessary.

@jbmusso
Copy link

jbmusso commented Oct 5, 2018

Unsure whether this is a black or isort thing, but I believe there's a bit of overlap about how many lines are added after imports when using both libraries.

When using VSCode with both libraries set to format on save, the following script will be formatted alternatively with one or two line after imports. The two outputs will be set alternatively:

from collections import Mapping
from typing import Optional

class Headers:
    def __init__(seq: Optional[Mapping] = None, encoding: str = "utf-8") -> None: ...

Will output alternatively (1 line after imports):

from collections import Mapping
from typing import Optional

class Headers:
    def __init__(seq: Optional[Mapping] = None, encoding: str = "utf-8") -> None: ...

and (2 lines after imports):

from collections import Mapping
from typing import Optional


class Headers:
    def __init__(seq: Optional[Mapping] = None, encoding: str = "utf-8") -> None: ...

... back and forth.

I don't want to hijack this issue, so maybe this should be better off discussed as a distinct issue. I'm unsure whether it should be created on black or isort repositories though :). Since the initial discuss interop between the two libraries, I think it's best to start here and move somewhere else.

Thanks for creating these nice libraries.

Pierre-Sassoulas added a commit to Pierre-Sassoulas/generic-python-project that referenced this issue Nov 18, 2018
@timothycrosley timothycrosley changed the title "Mode 8" for multiline imports Interopability with black Mar 14, 2019
@ambv
Copy link
Member Author

ambv commented Mar 14, 2019

Thanks for reopening. Black has since adopted what is almost exactly mode 3 in isort. There are a few edge cases that we can work through to ensure the tools are perfectly matched but they should be pretty close already :-)

@timothycrosley
Copy link
Member

@ambv sounds good!

Mostly, I realized this came up during a period where I was not as attentive to this project as I aim to be - and have been in the past. For the next release of isort in a week or so, I would like to ensure that there is an included black profile so that users don't have to mess with a ton of settings to use the two tools together. I'll start with the settings listed on the black GitHub page. And please, feel empowered to let me know of any incompatibilities and I'll do my best to resolve them in short order.

Thanks!

~Timothy

P.S. black is very cool, and something I've wanted to exist in the Python world for some time. I've introduced it in my work environment with great success and plan on using it with my open source code bases as well. So interoperability is very practically important for me :).

@shreyasbapat
Copy link

black is very cool, and something I've wanted to exist in the Python world for some time. I've introduced it in my work environment with great success and plan on using it with my open source code bases as well. So interoperability is very practically important for me

That is so nice to hear! Infact we use both of the awesome tools. i.e. black and isort in our project https://github.com/einsteinpy/einsteinpy . And this incompatibility creates a lot of issues. Can we not add a CI check for every PR to check if compatibility with black is retained?

kimt33 added a commit to kimt33/repo-template that referenced this issue Jun 15, 2019
It seems that isort is not compatible with black at the moment:
psf/black#127
PyCQA/isort#694

Both isort and black are working on a solution, but at the moment, we'll use
flake8-import-order instead
kimt33 added a commit to kimt33/repo-template that referenced this issue Jun 16, 2019
It seems that isort is not compatible with black at the moment:
psf/black#127
PyCQA/isort#694

Both isort and black are working on a solution, but at the moment, we'll use
flake8-import-order instead
silviot added a commit to Abstract-Tech/derex.runner that referenced this issue Jun 25, 2019
It seems that isort is not compatible with black at the moment:
psf/black#127
PyCQA/isort#694

Both isort and black are working on a solution, but at the moment, we'll use
flake8-import-order instead
moi90 added a commit to morphocut/morphocut that referenced this issue Oct 21, 2019
@demmerichs
Copy link

Not sure if this belongs here:

The following line line is formatted differently by black and isort even though I am using the isort config line_lenght=88, include_trailing_comma=true, multi_line_output=3.

Import:

from aaaaaaa.bbbbbb.ccccccccccccccccccccc import dddddddddd as eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee

Formatted by black:

from aaaaaaa.bbbbbb.ccccccccccccccccccccc import (
    dddddddddd as eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee,
)

Formatted by isort:

from aaaaaaa.bbbbbb.ccccccccccccccccccccc import \
    dddddddddd as eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee

Would be nice if this gets taken care of. Personally I think the black format is the way to go as I am not a big fan of backslashes (also pep8 is not either, right?)

@Jackenmen
Copy link

Jackenmen commented Oct 30, 2019

@DavidS3141 you need to use use_parantheses=True option (and black docs also recommend force_grid_wrap=0)

@demmerichs
Copy link

demmerichs commented Oct 30, 2019

hey, thanks for the fast reply, it works! Just a typo that took me a minute longer to figure it out: use_parentheses=True.
And for all the others, the docs mentioned by @jack1142 are here under A compatible .isort.cfg!

@david-hfx
Copy link

I found the same question as @blueskyjunkie mentioned. When I attemp to format my python file in VSCode, isort and black will compete over the wihtespace before comment.

@1zg12
Copy link

1zg12 commented Apr 28, 2020

I have encountered another scenario where they two are holding an exact opposite opinion.

isort and black is conflicting on this line:
from xxxx.xxxxxxxx.xxxxxxxxxxx.xxxxxxxxx.xxxxxxxxxxxxxxxxxxxxx import xxxxxxxxxxxxxxxxxxxx

the line length is 90.

isort would like to format it to

-from xxxx.xxxxxxxx.xxxxxxxxxxx.xxxxxxxxx.xxxxxxxxxxxxxxxxxxxxx import xxxxxxxxxxxxxxxxxxxx
+from xxxx.xxxxxxxx.xxxxxxxxxxx.xxxxxxxxx.xxxxxxxxxxxxxxxxxxxxx import (
+    xxxxxxxxxxxxxxxxxxxx,
+)

after that, black would like to format it back:

-from xxxx.xxxxxxxx.xxxxxxxxxxx.xxxxxxxxx.xxxxxxxxxxxxxxxxxxxxx import (
-    xxxxxxxxxxxxxxxxxxxx,
-)
+from xxxx.xxxxxxxx.xxxxxxxxxxx.xxxxxxxxx.xxxxxxxxxxxxxxxxxxxxx import xxxxxxxxxxxxxxxxxxxx

these are my isort settings:

[settings]
multi_line_output=3
include_trailing_comma=True
force_grid_wrap=0
use_parentheses=True
line_length=88

@xiaohuazi123
Copy link

I have encountered another scenario where they two are holding an exact opposite opinion.

isort and black is conflicting on this line:
from xxxx.xxxxxxxx.xxxxxxxxxxx.xxxxxxxxx.xxxxxxxxxxxxxxxxxxxxx import xxxxxxxxxxxxxxxxxxxx

the line length is 90.

isort would like to format it to

-from xxxx.xxxxxxxx.xxxxxxxxxxx.xxxxxxxxx.xxxxxxxxxxxxxxxxxxxxx import xxxxxxxxxxxxxxxxxxxx
+from xxxx.xxxxxxxx.xxxxxxxxxxx.xxxxxxxxx.xxxxxxxxxxxxxxxxxxxxx import (
+    xxxxxxxxxxxxxxxxxxxx,
+)

after that, black would like to format it back:

-from xxxx.xxxxxxxx.xxxxxxxxxxx.xxxxxxxxx.xxxxxxxxxxxxxxxxxxxxx import (
-    xxxxxxxxxxxxxxxxxxxx,
-)
+from xxxx.xxxxxxxx.xxxxxxxxxxx.xxxxxxxxx.xxxxxxxxxxxxxxxxxxxxx import xxxxxxxxxxxxxxxxxxxx

these are my isort settings:

[settings]
multi_line_output=3
include_trailing_comma=True
force_grid_wrap=0
use_parentheses=True
line_length=88

I have the same problem. How did you solve it?

@1zg12
Copy link

1zg12 commented May 4, 2020

I have encountered another scenario where they two are holding an exact opposite opinion.
isort and black is conflicting on this line:
from xxxx.xxxxxxxx.xxxxxxxxxxx.xxxxxxxxx.xxxxxxxxxxxxxxxxxxxxx import xxxxxxxxxxxxxxxxxxxx
the line length is 90.
isort would like to format it to

-from xxxx.xxxxxxxx.xxxxxxxxxxx.xxxxxxxxx.xxxxxxxxxxxxxxxxxxxxx import xxxxxxxxxxxxxxxxxxxx
+from xxxx.xxxxxxxx.xxxxxxxxxxx.xxxxxxxxx.xxxxxxxxxxxxxxxxxxxxx import (
+    xxxxxxxxxxxxxxxxxxxx,
+)

after that, black would like to format it back:

-from xxxx.xxxxxxxx.xxxxxxxxxxx.xxxxxxxxx.xxxxxxxxxxxxxxxxxxxxx import (
-    xxxxxxxxxxxxxxxxxxxx,
-)
+from xxxx.xxxxxxxx.xxxxxxxxxxx.xxxxxxxxx.xxxxxxxxxxxxxxxxxxxxx import xxxxxxxxxxxxxxxxxxxx

these are my isort settings:

[settings]
multi_line_output=3
include_trailing_comma=True
force_grid_wrap=0
use_parentheses=True
line_length=88

I have the same problem. How did you solve it?

The conflict is due to these two lines:

use_parentheses=True
line_length=88

and the import statement is 90 characters long.

As a solution, I have removed the use_parentheses=True and increased the line_length=100. However, if you increase the line length alone, it should work as well.

The previously defined line length of 88 is an out-of-date number for me since I do have quite some longer python statements. And since we have larger screens now, this historical line-length of 88 would leave screen space unutilized.

@xiaohuazi123
Copy link

use_parentheses

Just now, I also solved this problem with my isort Settings:

[settings]
multi_line_output=6
include_trailing_comma=False
force_grid_wrap=0
use_parentheses=True
line_length=120  

Maybe I increased the line_length that solved the problem.

casperdcl added a commit to casperdcl/dvc that referenced this issue May 6, 2020
Compatible with black (see PyCQA/isort#694)
efiop pushed a commit to iterative/dvc that referenced this issue May 6, 2020
* test: clean flake8 config root clutter

* test: pre-commit: add isort

Compatible with black (see PyCQA/isort#694)

* lint: reorder imports

* lint: more complex imports

* lint: fix deepsource

* config: isort: add first party dvc

* tests: restyled: add isort

* temp commit to revert before merge

* Revert "temp commit to revert before merge"

This reverts commit 11db0fd.
@meshy
Copy link

meshy commented Jun 24, 2020

I was facing this issue, and couldn't work out what was wrong with my config. It turned out to be #1185

Edit: It wasn't that issue after all, it was just related. I'll open a new issue.

Edit 2: #1237

@hugovk
Copy link
Contributor

hugovk commented Jul 4, 2020

@timothycrosley Congratulations on the isort 5 release with profiles!

https://timothycrosley.github.io/isort/docs/major_releases/introducing_isort_5/

Should the profile for Black include line_length: 88 to match the Black default?

https://timothycrosley.github.io/isort/docs/configuration/profiles/

@timothycrosley
Copy link
Member

@hugovk Thanks!

Good catch! Originally, profile where given the highest priority overriding any other settings - so I didn't include line_length as there would be no easy way to override it and it is configurable in black as well. However, testing showed that wasn't the best approach and now profile sit above the defaults but below custom settings. I've updated the black profile to be 88 line_length.

@ssbarnea
Copy link
Member

This thread is a little bit confusing. As a black user, can I enable isort now or if so what do I need to do it in a way that does not conflict with black?

@timothycrosley
Copy link
Member

@ssbarnea yes you can enable isort alongside black! isort has been compatible with black for a while (with a complex combination of settings). With the latest release isort is fully compatible with black simply by using the black profile isort --profile black .. This thread is meant to be a coordination thread between the two projects to ensure compatibility is never broken.

@ssbarnea
Copy link
Member

Sadly using black profile does not change the default line length from 79 to 88. Also isort seems not to be able to read the value configured in flake8 config either, so I was forced to repeat configuring the line length alongside black profile. There is an open change at https://github.com/ansible/ansible-lint/pull/887/files -- note that the project does not use black but using the black profile should have not mattered, my expectation was to default to the magic 88 value :D

@bersbersbers
Copy link

Sadly using black profile does not change the default line length from 79 to 88.

It should, shouldn't it?
https://github.com/timothycrosley/isort/blob/b49aba99b919473b66f05b663588cfac2472c601/isort/profiles.py#L4-L11

@timothycrosley
Copy link
Member

I'm going to close this issue now, as there as been some misunderstanding that it means that isort isn't compatible with black. If in the future anyone notices an incompatibility please raise it as a new issue and it will have the new black label applied and be fixed with the highest priority. isort is committed to long term compatibility with the black project.

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 ongoing A task that represents continous work and coordination and is never meant to be "complete"
Projects
None yet
Development

No branches or pull requests