isort icon indicating copy to clipboard operation
isort copied to clipboard

Long package and module names cause isort to violate the line length limit

Open Bengt opened this issue 4 years ago • 3 comments

I have code with a long import like this:

from vr_backend.shims.torch_shim.get_first_dataset_entry_with_label_module \
    import \
    get_first_dataset_entry_with_label

I run isort on that file like so:

venv/bin/python -m isort --line-length 79 main.py

I get imports formatted like this:

from vr_backend.shims.torch_shim.get_first_dataset_entry_with_label_module import \
    get_first_dataset_entry_with_label

Note that the import crosses the 79-character line length limit. This causes errors with my linters, because they correctly complain about the line length limit being violated.

Bengt avatar Dec 06 '21 18:12 Bengt

isort does not seem to honor "Noqa" comments, even if I configure it to do so:

from vr_backend.shims.torch_shim.get_first_dataset_entry_with_label_module \
    import \
    get_first_dataset_entry_with_label  # NoQA
from vr_backend.shims.torch_shim.get_first_dataset_entry_with_label_module \
    import \
    get_first_dataset_entry_with_label  # noqa
venv/bin/python -m isort --line-length 79  --honor-noqa main.py
from vr_backend.shims.torch_shim.get_first_dataset_entry_with_label_module import \
    get_first_dataset_entry_with_label  # noqa

Bengt avatar Dec 06 '21 18:12 Bengt

My current workaround is to manually not check in these changes into version control. This cumbersome and error-prone, but at least my linters will catch user errors.

Bengt avatar Dec 06 '21 18:12 Bengt

Another workaround is to not have package and module names that long. In this project, that works for me, but in my last, shortening wouldn't have worked due to the sheer size of its code base. So for larger projects, a proper fix would certainly be appreciated.

Bengt avatar Dec 06 '21 18:12 Bengt

I think this is a duplicate of #1531.

Yeah, having paths that long is a sign of too deep nesting, but for bigger projects it's harder to fit for example 79 line-length limit. In my case i won a lot of space by shortening folder's names, preserving the same names for files. Also a good way to decrease import length is to use __init__.py to link deep nested objects higher, but i don't like this approach because of refactoring issues.

ryzhovalex avatar Dec 26 '22 00:12 ryzhovalex