Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--sort-reexports does not respect --profile=black #2280

Open
Helveg opened this issue Jul 18, 2024 · 2 comments
Open

--sort-reexports does not respect --profile=black #2280

Helveg opened this issue Jul 18, 2024 · 2 comments

Comments

@Helveg
Copy link

Helveg commented Jul 18, 2024

Problem

Attempting to sort a multiline __all__ attribute that is longer than the line limit with isort . --sort-reexports --profile=black wraps the lines in a non-black compatible way:

Before

__all__ = [
    "ArgumentReference",
    "ConfigurationSource",
    "HeptoFlow",
    "HeptoFlowContainer",
    "InMemoryCache",
    "Input",
    "InputReference",
    "InputValue",
    "Service",
    "parse_input_expression",
]

After

__all__ = ['ArgumentReference', 'ConfigurationSource', 'HeptoFlow', 'HeptoFlowContainer',
 'InMemoryCache', 'Input', 'InputReference', 'InputValue', 'Service',
 'parse_input_expression']

Env

Python 3.12
isort 5.13.2
black 24.4.2

PS: Very willing to fix this myself and to PR it if someone can triage this and point me in the right direction.

@Helveg
Copy link
Author

Helveg commented Jul 28, 2024

I did some digging and it seems that code sorting (sort_reexports and the undocumented and quasi-untested sort comments # isort: list, # isort: unique-list, etc) is the only feature that uses isort.literal.assigments. This function relies on the Python stdlib pprint formatting, and completely ignores the config, except for line_length:

class ISortPrettyPrinter(PrettyPrinter):
    """an isort customized pretty printer for sorted literals"""

    def __init__(self, config: Config):
        super().__init__(width=config.line_length, compact=True)

There is no regard for the config.multi_line_output, config.include_trailing_comma, config.split_on_trailing_comma, and likely many more that I am not aware of as a first-time contributor.

I believe fixing this would require us to write our own formatting logic, since pprint does not support any of the configurable formatting features isort needs.

I would need the input of the developers here, since there are no tests to form a de facto specification, but I believe that code sorting should follow the Config formatting options, and the pprint logic should be dropped and an isort algorithm like the ones in isort.output should be used or custom written for it.

@Helveg
Copy link
Author

Helveg commented Aug 6, 2024

@timothycrosley Hi, sorry for the direct ping, I'm sure you're very busy! Would you have a moment to confirm my suspicions that the code_sorting logic is insufficient, and preapprove my attempts to rewrite it? If I have your green light I can get started on it.

I already have a PR open for fixing similar sort_reexport problems that would require your attention: #2283

Hope to hear from you, and have a good day

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant