4 ms·
Shameless self-promotion, as a former coworker of Łukasz, Black's creator: Another coworker and I have created an import sorter that fits well alongside Black,
by jreese 5y ago
Shameless self-promotion, as a former coworker of Łukasz, Black's creator:
Another coworker and I have created an import sorter that fits well alongside Black, called µsort. It is designed from the ground up to be a safe, stable import sorter that won't move imports in ways that potentially change behavior of the codebase, and without needing developers to litter their code with "skip" directives. We use it in our daily formatting codemods on tens of thousands of source files every day, and just finished our 1.0 release in December.
https://usort.readthedocs.io https://usort.readthedocs.io
Going further, if you like enforcing both formatting and import order in your CI pipeline, I also created the project µfmt, which combines both Black and µsort into a single, atomic formatting step. This ensures there's never any conflict of opinion between the two tools, and any formatting changes are presented as a single diff result.
https://ufmt.readthedocs.io https://ufmt.readthedocs.io
- chrisseaton 5y ago> that won't move imports in ways that potentially change behavior of the codebase How can that be the case? Can't Python imports have behaviour that varies on the time of the day if they want? An import could monkey patch a basic operation one day but not the next.
- jreese 5y agoAbsolutely! We're hoping that the vast majority of modules are good citizens, but we also know that the reality not perfect. That's why µsort allows you to configure a list of modules with known import-time side effects, and µsort will then treat those are barriers everywhere. https://usort.readthedocs.io/en/stable/guide.html#side-effect-imports https://usort.readthedocs.io/en/stable/guide.html#side-effec...
- stefan_ 5y agoEven better, you can overwrite the default importer to substitute your own with any behavior you want. Used in EVE Online to do their "hide the python code somewhere" scheme.
- wcdolphin 5y agoInteresting. Now that isort has support for black-compatibility, what’s the main difference with usort?
- jreese 5y agoWe've written a number of words on "Why µsort", including a comparison to isort: https://usort.readthedocs.io/en/stable/why.html https://usort.readthedocs.io/en/stable/why.html The biggest reason is µsort's detection of sortable "blocks" of imports, and only sorting within those blocks to limit breaking changes, combined with use of proper parsing and CST manipulation to guarantee valid syntax after sorting.
- wcdolphin 5y agoVery cool. I love the idea of one tool rather than two, I’ll check it out!
- stdbrouw 5y agoFWIW, there's one particular use case where the isort behavior is incredibly useful: in eval-as-you-type data science e.g. when using Hydrogen in Atom you type an import whenever you need it but that leads to a lot of duplication and imports scattered across the file, and it's nice that isort just moves that up top on every save.
- jreese 5y agoThat's something we're aware of, and something we've considered, but also potentially dangerous from a safety standpoint, due to Python's dynamic nature, and how much moving imports can affect runtime semantics. Any implementation of this would need to be aware of those dangers, and also be able to provide stable and predictable behavior. At the moment, it's not a huge priority given the availability of tooling to automatically add missing imports, or the limited impact of under-sorting in the context of notebooks.
- deleted 5y ago[deleted]