-
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
Feature request: Add Length-Sort config to isort #1567
Comments
Love the Annoy-o-Tron. |
We also need this to be able to migrate to ruff! |
Hi @charliermarsh 👋🏼 I've started learning Rust for a while but haven't contributed yet and want to start from somewhere. So if you allow can I pick this one ? |
Hi @charliermarsh, just wanted to know that if you have any idea for the implementation approach for this..if so could let me know I am thinking to pick this up. I saw the comment, a bit confused by it.. so if you can elaborate it would be great. |
Hi there 👋 |
While you're at supporting |
I don't know the answer to that. My guess is that they are not mutually exclusive since different isort options control them, but I don't know how they interact if both are set. For |
Hey! I'm currently working on this one and I have a working implementation of the basic functionality. @suharnikov if you're still working on it as well I'll leave it for you of course :) I do have one question though, I couldn't help but notice that there are a bunch of functions that take in a whole lot of |
@bluthej - I think it's probably okay to refactor those methods to accept |
@charliermarsh absolutely! That's no problem at all :) Then I'll start with the refactor and come back to this one when it's merged 😊 |
## Summary Pass around a `Settings` struct instead of individual members to simplify function signatures and to make it easier to add new settings. This PR was suggested in [this comment](#1567 (comment)). ## Note on the choices I chose which functions to modify based on which seem most likely to use new settings, but suggestions on my choices are welcome!
## 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.
#7963 is merged, which should pave the way for this. |
Hey, so I got back to working on this, and that led me to the following question. When using length-sort, isort will count the dots of relative imports in the length instead of sorting by distance and then length of the module name itself (without the dots). Is that the behavior we want? Or do we want to first sort based on distance and then length of module name? |
Note: We should clarify whether the |
That's a fair point! On my system (Python 12 on Arch Linux) I cannot seem to be able to import a module with a non-ASCII character, I get a module not found error. However importing members with non-ASCII characters works. Now isort uses the built-in I guess we can use |
What’s a “character”? I think For a more accurate length, I think we’d have to iterate grapheme clusters and check which ones are likely to render at double width. (Don’t let yourself be confused by “wide character” sometimes meaning storage size and sometimes literal width on screen) E.g. check out how my terminal renders “平”, which of course is a valid Python identifier: |
According to the Python docs:
and they add:
So you are correct,
I wouldn't necessarily say "more accurate", but the argument can be made that it's more "natural" for humans. The downside is that it would make ruff behave differently from isort, so there's a bit of a tradeoff.
Interesting! I'm going to have to figure out why my example didn't work :) |
I would prefer that we use Unicode width, which is what we use everywhere else (even though we use the term "length" -- we nearly renamed them all when we released the formatter but decided to defer that decision). |
(You should be able to call |
That’s exactly the concept I was talking about btw 😃 |
I think you meant the What about relative imports? Should we follow isort and count the dots in the length? |
## Summary Closes #1567. Add both `length-sort` and `length-sort-straight` settings for isort. Here are a few notable points: - The length is determined using the [`unicode_width`](https://crates.io/crates/unicode-width) crate, i.e. we are talking about displayed length (this is explicitly mentioned in the description of the setting) - The dots are taken into account in the length to be compatible with the original isort - I had to reorder a few fields of the module key struct for it all to make sense (notably the `force_to_top` field is now the first one) ## Test Plan I added tests for the following cases: - Basic tests for length-sort with ASCII characters only - Tests with non-ASCII characters - Tests with relative imports - Tests for length-sort-straight
Thanks for an awsome library. I am currently trying to replace the old flake8 and isort setup on our repo with ruff.
We can almost replace isort with ruff, but we are currently using the Length-Sort configuration from isort, which currently can't be found in ruff.
The text was updated successfully, but these errors were encountered: