-
Notifications
You must be signed in to change notification settings - Fork 22
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
Examples comparing to isort's heuristics in the documentation seem outdated #186
Comments
That's fair! The examples on that page don't misbehave on isort 5.x anymore. That page (including some of the bugs) was from our experience running 4.x in a megarepo, and what drove us to start this project rather than trying to piecemeal "fix" isort. There are still some major differences between our approaches. I'll post some ideas in followup comments, but didn't want to delay confirmation, thanks for reporting. |
I did some manual fuzzing of isort to try to come up with better thoughts on how we're different. In addition to our "first, do no harm" principle, I think using more words: An import sorter should never take valid code and make it invalidWe get most of this safety by using an actual parser (LibCST) that means it's unlikely we will output mangled lines, and we put care into ensuring that there aren't a lot of branches that are hard to test.
An import sorter should never take invalid code and make it validThis is mainly about syntactically-invalid code, and less about import-time side effects which we consider an acceptable risk and I wish didn't exist.
I think the only case where we silently "fix" invalid code in usort is moving An import sorter should make understandable decisions, even if you disagree with them...and ideally be able to explain them.
Configuration should be minimal, and behave the same universallyIt shouldn't matter what version of Python you're using, or what you have on your sys.path, or what OS you're on. This causes us to make some compromises (e.g. what names are considered stdlib, or case-insensitivity) that I imagine spark disagreement though. The one exception to the environment we have is inference of first-party name under the assumption you're running it from a vcs repo. We assume the union of all py3 stdlibs currently. It's okay to leave a file alone, or crash as long as you don't overwrite it, but do so as gracefully as you can.
We had one bug this week that raised KeyError, but generally we raise something like SyntaxError pointing to a relevant part of the source. ConclusionI recommend we:
|
Is this expected?
|
@ofek yes. The first change is due to µsort using case-insensitive, lexicographical sorting — the reasoning for this is the second section of the Comparison to isort section of the "Why µsort" page. We believe this is more consistent overall, and more predictable as someone looking for a specific name in the import. The second change is because µsort attempts to reflow long lines to match black. If you don't use black, but would like to configure the target line length for µsort, you can just set black's line length in your |
Thanks! I do use black so I wonder why the line length isn't read |
I think this is because µsort is only looking for the closest |
Ohhh I see, thank you 🙏 |
I've tried the examples of isort breaking code that are given here and it doesn't seem like any of those are broken by isort:
https://usort.readthedocs.io/en/latest/why.html#comparison-to-isort
Based on my testing, they haven't been broken since isort 5 (released July 2020) for sure so I'm a bit surprised to see those listed in the documentation that was added just 6 months ago. I can understand the benefit of usort being designed with safety as a priority but the way it's compared to isort is currently unfair.
isort doesn't really seem to advertise safety as a priority (though I'm pretty sure it's nonetheless considered important to it since isort 5) so that is still a good selling point (I find the mention of the strictly-parsed tree and how it causes bugs in isort to be particularly convincing), it would be good to not show code examples that isort doesn't actually break though :)
The text was updated successfully, but these errors were encountered: