PyCQA / isort

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

BUG: --sort-reexports don't work with multiple lines __all__ #2193

Open givemepillow opened 1 year ago

givemepillow commented 1 year ago
import datetime

from mylib import (
    module1,
    module2,
    module3,
    module4,
)

__all__ = (
    "func1",
    "func2",
    "func3",
    "func4",
)

after using isort file.py --sort-reexports, becomes:

import datetime

fro__all__ = ('func1', 'func2', 'func3', 'func4')

It seems to me that there is a problem with calculating the current index when using reexports in cases where we use multiline exports or add comments to the beginning of the file.

M1troll commented 1 year ago

Hi! I have the same problem

Before sorting:

from pomcorn.locators.base_locators import (
    Locator,
    TInitLocator,
    TLocator,
    XPathLocator,
)
from pomcorn.locators.xpath_locators import (
    ButtonWithTextLocator,
    ClassLocator,
    DataTestIdLocator,
    ElementWithTextLocator,
    IdLocator,
    InputByLabelLocator,
    NameLocator,
    PropertyLocator,
    TagNameLocator,
    TextAreaByLabelLocator,
)

__all__ = (
    "Locator",
    "XPathLocator",
    "TInitLocator",
    "TLocator",
    "ButtonWithTextLocator",
    "ClassLocator",
    "DataTestIdLocator",
    "ElementWithTextLocator",
    "IdLocator",
    "InputByLabelLocator",
    "NameLocator",
    "PropertyLocator",
    "TagNameLocator",
    "TextAreaByLabelLocator",
)

After sorting:

from __all__ = ('ButtonWithTextLocator', 'ClassLocator', 'DataTestIdLocator',
 'ElementWithTextLocator', 'IdLocator', 'InputByLabelLocator', 'Locator',
 'NameLocator', 'PropertyLocator', 'TInitLocator', 'TLocator',
 'TagNameLocator', 'TextAreaByLabelLocator', 'XPathLocator')
ByLabelLocator,
    NameLocator,
    PropertyLocator,
    TagNameLocator,
    TextAreaByLabelLocator,
)

__all__ = (

My isort config:

[tool.isort]
profile="black"
line_length=79
multi_line_output=3
skip=[
    "_tmp",
    "src",
    ".env",
    "env",
    ".venv",
    "venv",
]
known_pytest=[
    "pytest",
    "_pytest",
    "xdist",
]
sections=[
    "FUTURE",
    "STDLIB",
    "THIRDPARTY",
    "PYTEST",
    "FIRSTPARTY",
    "LOCALFOLDER",
]
include_trailing_comma=true
sort_reexports=true
daskol commented 4 months ago

The issue still persists. It did enourmous mess with all __init__ modules in package.