Black: isort removes newlines before standalone comments if the following line is an import of the same group

Created on 23 May 2018  路  13Comments  路  Source: psf/black

Operating system: Arch Linux
Python version: Python 3.6.6
Black version: 18.5b0
Does also happen on master: yes

Given:

import a
# noinspection PyProtectedMember
import _a

Black formats this to (note the introduced empty line):

import a
# noinspection PyProtectedMember

import _a

However isort correctly (in the recommended mode 3) formats it to:

import a
# noinspection PyProtectedMember
import _a
enhancement

Most helpful comment

@ambv any plans to address this? This makes the line of This also makes Black compatible withisortwith the following configuration. from the readme not entirely true. And the only solution in the meantime is to remove such comments, which is not good generally. I get that black does not want to venture into the business of import ordering; but in that case it should play nice with tools out there that do this task, e.g. isort. I personally would place this as a pretty critical bug; not enhancement per se. If there's a decision on this maybe we (the community) can try to create a PR, but for that, we would need to discuss what a solution would be.

All 13 comments

Black puts an empty line after every group of imports. A standalone comment signifies an end of a group of imports before it.

So, your example is wrong, Black will do this instead with the original file:

--- /tmp/asd.py  (original)
+++ /tmp/asd.py  (formatted)
@@ -1,4 +1,5 @@
 import a
+
 # noinspection PyProtectedMember
 import _a

The behavior between isort and Black is indeed different here. I haven't decided yet how to resolve this.

Thanks for the explanation, would be nice to somehow support this use case though.

I am affected by this as well :/ Just to clarify, what is the problem here? Is Black wrong, or is isort? Is the newline supposed to be there?

@ambv any plans to address this? This makes the line of This also makes Black compatible withisortwith the following configuration. from the readme not entirely true. And the only solution in the meantime is to remove such comments, which is not good generally. I get that black does not want to venture into the business of import ordering; but in that case it should play nice with tools out there that do this task, e.g. isort. I personally would place this as a pretty critical bug; not enhancement per se. If there's a decision on this maybe we (the community) can try to create a PR, but for that, we would need to discuss what a solution would be.

I'm also affected. what is the current status about this bug?

I think black is being too prescriptive here: yes comments between imports can be used as headers, but they can also be things like:

import a
# XXX: unused import, that however shouldn't be removed 
# because our framework depends on it being present
import b

Hello @ambv ! Do you have an update on this?

The behavior between isort and Black is indeed different here. I haven't decided yet how to resolve this.

Perhaps you can talk through your thoughts so we understand if a fix is likely?

Also, re:

Black puts an empty line after every group of imports. A standalone comment signifies an end of a group of imports before it.

Is there some documentation that describes this behavior? I couldn't find it in the code style - I ask because without knowing the reasoning behind this behavior, I find it hard to suggest a way forward. I do find the rest of the decisions well documented, and do a great job of answering my questions and feature requests before I make them ;)

Does anyone know what the best way to get a contributor's attention here is? I'm happy to submit a proposal + fix, it's just that the radio silence makes me nervous about investing time :)

Any update on this ? What is the current status of this issue ? Not sure which way to proceed here .... Black or isort ?

This is now fixed in isort by adding a new ensure_newline_before_comments option, but it's not yet released:

If you use pre-commit, you can pin to the latest isort commit, like this:

This is now fixed in isort by adding a new ensure_newline_before_comments option, but it's not yet released:

Looks like isort 5.0.0 is scheduled for tomorrow!

By the way, I have switched to https://github.com/asottile/reorder_python_imports, which doesn't have this problem and runs statically without isort's troubles when the sorted packages aren't installed.

Closing as isort 5 has been released.

Either use isort 5's profiles of which one is built for Black, or manually configure ensure_newline_before_comments to true, although it only works with isort 5 and higher. For more information please read the documentation for isort + Black compatiblity.

Was this page helpful?
0 / 5 - 0 ratings