Skip to content

Refactor proposal for isort implementation #7738

Closed
@bluthej

Description

@bluthej

I've been working on #1567 and I got a working implementation but after taking the time to make a small refactor in #7665 and coming back to the original issue I'm thinking another refactor might be welcome regarding extensibility of the isort functionality.

Current state

Currently, the formatting is performed by format_import_block which relies on order_imports from isort/order.rs, and there are different methods that are called along the way depending on the kinds of imports we are comparing (they are kind of intertwined).

The ordering rules themselves are implemented in isort/sorting.rs, and here each different combination of import types has its own method.

The main drawback I see from this architecture is that when trying to add a new functionality one needs to sprinkle some changes in the different methods instead of making changes in a single place.

Proposal

I thus propose to reverse the logic, i.e. have a single entry point for comparisons (like the current cmp_either_import) that would chain the different sorting rules (cmp_force_to_top, etc...), and each of them would match on the import types.

I think this would make the isort functionality more easily extensible. With that change, to implement #1567 we just need to add a cmp_string_length method and insert it in the chain of sorting rules. This will simplify the process for similar settings like length sort straight and length sort sections.

What do you think? Any feedback is welcome 😃

Metadata

Metadata

Assignees

No one assigned

    Labels

    internalAn internal refactor or improvementisortRelated to import sorting

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions