PyCQA / isort

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

`--force-alphabetical-sort` breaks `--lines-between-types=0` #1972

Open rec opened 2 years ago

rec commented 2 years ago

Hello, and thanks for a very useful package!

If I set --force-alphabetical-sort then an extra line is inserted between import and from even if --lines-between-types=0

Start with this test file:

import one
import two
from three import four
from five import six

HELLO = 'world'

Running isort --lines-between-types=0 test_isort.py gets:

import one
import two
from five import six
from three import four

HELLO = 'world'

This has no empty line between import and from as expected.

isort --force-alphabetical-sort --lines-between-types=0 test_isort.py gives:

from five import six
from three import four

import one
import two

HELLO = 'world'

which you will note adds an empty line!


Thanks and have an excellent weekend.

nnrepos commented 1 year ago

it seems like this is by design: https://github.com/PyCQA/isort/blob/12cc5fbd67eebf92eb2213b03c07b138ae1fb448/isort/settings.py#L278-L281 if this was done my mistake, or a warning should be displayed, let me know and i will create a PR 😄

rec commented 1 year ago

Hello, how are you? :-) Thanks for a great program.

I did see that code, but I wasn't convinced it was right at all, mainly because it prevented me from getting the formatting we already have in our code:

from five import six
from three import four
import one
import two

and line of code means that we get

from five import six
from three import four

import one
import two

regardless of whether --lines-between-types=0 or not.

In fact, we run my hacked version of isort with line 281 commented out!

I could easily emit a pull request.