-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Refactor proposal for isort implementation #7738
Comments
So we'd merge into a vector of |
I guess we don't necessarily need to always merge the two I understand it's tough to judge without seeing a PR, I wanted to see if in principle that sounded ok before spending some time on it :) BTW, I noticed that initially the implementation looked a lot more like the original |
what do you guys think about my implementation in https://github.com/aspizu/imp? |
@aspizu thanks for sharing! I see you are relying on the So I personally prefer the implementations based on functions that take one or two imports and the settings separately (either in the |
Hey, So I did some digging and eventually found that the I looked at the original Python implementation and saw that they're splitting the keys based on the numbers found in the string. This is a neat little trick, but this is not directly applicable in Rust since we can't have a What we could do though is to introduce a Any thoughts? |
## Summary Refactor for isort implementation. Closes #7738. I introduced a `NatOrdString` and a `NatOrdStr` type to have a naturally ordered `String` and `&str`, and I pretty much went back to the original implementation based on `module_key`, `member_key` and `sorted_by_cached_key` from itertools. I tried my best to avoid unnecessary allocations but it may have been clumsy in some places, so feedback is appreciated! I also renamed the `Prefix` enum to `MemberType` (and made some related adjustments) because I think this fits more what it is, and it's closer to the wording found in the isort documentation. I think the result is nicer to work with, and it should make implementing #1567 and the like easier :) Of course, I am very much open to any and all remarks on what I did! ## Test Plan I didn't add any test, I am relying on the existing tests since this is just a refactor.
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 onorder_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 😃
The text was updated successfully, but these errors were encountered: