Black: isort inserts a trailing comma even if there's just a single name in parentheses

Created on 22 May 2018  路  6Comments  路  Source: psf/black

Operating system: Mac OS 10.13.4
Python version: 3.6.5 (through pyenv)
Black version: 18.4a4
Does also happen on master: yes


Given this setup:

$ tail -n+1 foo.py .pre-commit-config.yaml setup.cfg
==> foo.py <==
from a_really.really.really.really.really.long.package.namespace import a_really_long_package
from really.really.really.really.really.long.package.namespace import really_long_package as pkg

==> .pre-commit-config.yaml <==
repos:
- repo: https://github.com/pre-commit/mirrors-isort
  rev: v4.3.4
  hooks:
  - id: isort
- repo: https://github.com/ambv/black
  rev: 18.5b0
  hooks:
  - id: black
    python_version: python3.6

==> setup.cfg <==
[isort]
# Using config from
# https://github.com/ambv/black/blame/c7bc22388d30f1ba503eefd574e4bb794749b782/README.md
force_grid_wrap = 0
include_trailing_comma = true
line_length = 88
multi_line_output = 3

Running isort changes the file to

from a_really.really.really.really.really.long.package.namespace import (
    a_really_long_package,
)
from really.really.really.really.really.long.package.namespace import \
    really_long_package as pkg

Running black changes the file to

from a_really.really.really.really.really.long.package.namespace import (
    a_really_long_package
)
from really.really.really.really.really.long.package.namespace import (
    really_long_package as pkg
)

The difference being the trailing comma and the \ line break

--- foo_isort.py    2018-05-22 15:46:53.000000000 -0400
+++ foo_black.py    2018-05-22 15:46:46.000000000 -0400
@@ -1,5 +1,6 @@
 from a_really.really.really.really.really.long.package.namespace import (
-    a_really_long_package,
+    a_really_long_package
 )
-from really.really.really.really.really.long.package.namespace import \
+from really.really.really.really.really.long.package.namespace import (
     really_long_package as pkg
+)

If I add this option to setup.cfg

--- setup.cfg   2018-05-22 15:39:19.000000000 -0400
+++ setup.cfg.new   2018-05-22 15:47:41.000000000 -0400
@@ -1,6 +1,5 @@
 [isort]
-# Using config from
-# https://github.com/ambv/black/blame/c7bc22388d30f1ba503eefd574e4bb794749b782/README.md
+combine_as_imports = true
 force_grid_wrap = 0
 include_trailing_comma = true
 line_length = 88

I get a slightly closer diff, but the trailing commas are inconsistent.

--- foo_isort.py    2018-05-22 15:48:59.000000000 -0400
+++ foo_black.py    2018-05-22 15:49:04.000000000 -0400
@@ -1,6 +1,6 @@
 from a_really.really.really.really.really.long.package.namespace import (
-    a_really_long_package,
+    a_really_long_package
 )
 from really.really.really.really.really.long.package.namespace import (
-    really_long_package as pkg,
+    really_long_package as pkg
 )
enhancement trailing comma

Most helpful comment

We're still running into this issue and looking forward to a fix. 馃槃

All 6 comments

I will add combine_as_imports to our recommendation, thanks for the find.

I will fix the missing trailing comma when there is a single import that doesn't fit in a line, too.

Thanks!

We're still running into this issue and looking forward to a fix. 馃槃

I switched my affected repos to a different import sorting tool (reorder_python_imports) to work around this, not to diminish the hard work of the black maintainers in working on a fix!

@ambv thanks for taking a look at this! and thanks for black!

At the moment, I'm just rerunning isort implicitly after black runs. I think (hopefully) this is the last thing to get isort/black playing together nicely.

Please replace combine_as_imports=True as that is forcing an unwanted isort style as a side effect of fixing this issue. The actual setting should be use_parentheses=True

Was this page helpful?
0 / 5 - 0 ratings

Related issues

decibyte picture decibyte  路  3Comments

JelleZijlstra picture JelleZijlstra  路  3Comments

asottile picture asottile  路  3Comments

dgnsrekt picture dgnsrekt  路  3Comments

madig picture madig  路  3Comments