PyCQA / isort

A Python utility / library to sort imports.
https://pycqa.github.io/isort/
MIT License
6.49k stars 580 forks source link

`HANGING_INDENT` Multi Line Import adds comma in profile hug #1864

Open Bengt opened 2 years ago

Bengt commented 2 years ago

I have imports like so:

from vr_backend.shims.tensorboard_shim.tensorboard_writer import \
    TensorBoardWriter

I run isort on that file like so:

venv/bin/python -m isort --profile hug --line-length=79 -m HANGING_INDENT main.py

I get superfluous commas in the imports:

from vr_backend.shims.tensorboard_shim.tensorboard_writer import \
    TensorBoardWriter,

This is not valid Python syntax and thus even causes errors when trying to execute the file:

  File "/home/bengt/Downloads/DFKI/gitlab.ni.dfki.de/VR/VR.Backend/vr_backend/vae/main.py", line 14
    TensorBoardWriter,
    ^
SyntaxError: trailing comma not allowed without surrounding parentheses
Bengt commented 2 years ago

My current workaround is to manually not check in these faulty lines. This is however cumbersome and error-prone.

On a ~ 1.3k line code base, I get this error 33 times, so it is quite widespread for me.

Bengt commented 2 years ago

According to the documentation, there should be no trailing comma in the last line of a hanging-indent multi line import:

from third_party import \
    lib1, lib2, lib3, \
    lib4, lib5, lib6

Source: https://pycqa.github.io/isort/docs/configuration/multi_line_output_modes.html#2-hanging-indent

So, I would have expected my imports to remain unchanged like so:

from vr_backend.shims.tensorboard_shim.tensorboard_writer import \
    TensorBoardWriter
Bengt commented 2 years ago

Not using --profile hug seems to avoid the problem as well.

Avasam commented 2 years ago

Basically this config results in this behaviour:

include_trailing_comma = true
multi_line_output = 2