PyCQA / isort

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

isort --profile black produces long lines #2281

Open bje- opened 1 month ago

bje- commented 1 month ago

I have line_length set to 79.

When formatting this code with isort --profile black:

from nemo.polygons import (WILDCARD, cst_limit, offshore_wind_limit,
                           pv_limit, wind_limit)

I get:

from nemo.polygons import WILDCARD, cst_limit, offshore_wind_limit, pv_limit, wind_limit

(which is 89 characters long)

Helveg commented 1 month ago

How exactly did you set line_length? Did you set it for black or for isort?

bje- commented 1 month ago

isort, shown with isort --show-config.

Helveg commented 1 month ago

I can't reproduce your issue, with either the following Python snippet:

import isort
code = """from nemo.polygons import (WILDCARD, cst_limit, offshore_wind_limit,
                           pv_limit, wind_limit)"""
isort.code(code, profile="black", line_length=79)

or the following bash snippet:

printf "from nemo.polygons import (WILDCARD, cst_limit, offshore_wind_limit,\n                  pv_limit, wind_limit)" | isort --profile=black --line-length=79 -

the output is always:

from nemo.polygons import (
    WILDCARD,
    cst_limit,
    offshore_wind_limit,
    pv_limit,
    wind_limit,
)

Is your isort version up to date? If so, can you try passing the arguments like shown in my snippets. If none of that works, could you fully reproduce how you are setting and verifying the isort settings?

bje- commented 1 month ago

isort --version says 5.13.2.

isort --show-config | grep line_length says:

    "line_length": 79,

Your bash snippet works for me, but if I remove the explicit --line-length argument and rely on what is shown in the config output:

printf "from nemo.polygons import (WILDCARD, cst_limit, offshore_wind_limit,\n                  pv_limit,
wind_limit)" | isort --profile=black  -

produces:

from nemo.polygons import WILDCARD, cst_limit, offshore_wind_limit, pv_limit, wind_limit
Helveg commented 1 month ago

Ok, can you provide me everything I need to set my config exactly the same way?

bje- commented 1 month ago
python3 -m venv myenv
. myenv/bin/activate
pip install isort
printf "from nemo.polygons import (WILDCARD, cst_limit, offshore_wind_limit,\n                  pv_limit,
wind_limit)" | isort --profile=black  -
Helveg commented 1 month ago

Ok, so you're not actually setting isort's line_length to anything. In that case everything is working as expected:

The default line_length is indeed 79 characters, which is what isort --show-config shows you. But during the command you are using --profile=black. Using isort --show-config --profile=black you will see that that profile sets a line_length of 88. And the line you produce is not 89, but 88 characters long:

isort --show-config --profile=black
{
  ...
   "line_length": 88,
  ...
}

you can test this by adding 1 character _ to the last import:

printf "from nemo.polygons import (WILDCARD, cst_limit, offshore_wind_limit,\n
         pv_limit,
wind_limit_)" | isort --profile=black  -
from nemo.polygons import (
    WILDCARD,
    cst_limit,
    offshore_wind_limit,
    pv_limit,
    wind_limit_,
)

As you can see, everything is working as expected, your test input just happened to be in the Goldilock zone to mislead you 😂 Keep in mind that the profile option works by setting configuration options. These are the options that the black profile sets:

black = {
    "multi_line_output": 3,
    "include_trailing_comma": True,
    "force_grid_wrap": 0,
    "use_parentheses": True,
    "ensure_newline_before_comments": True,
    "line_length": 88,
}
bje- commented 1 month ago

Many thanks for getting to the bottom of this.