News for hackers. Stuff that matters.
Re:Black, the uncompromising Python code formatter, is stable by jreese on Saturday January 29, 2022 @05:11PM (#30131088)
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

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

Re:Black, the uncompromising Python code formatter, is stable by chrisseaton on Saturday January 29, 2022 @05:15PM (#30131124)
> 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.

Re:Black, the uncompromising Python code formatter, is stable by jreese on Saturday January 29, 2022 @05:19PM (#30131167)
Absolutely! 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-effec...

Re:Black, the uncompromising Python code formatter, is stable by stefan_ on Saturday January 29, 2022 @05:57PM (#30131471)
Even 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.
Re:Black, the uncompromising Python code formatter, is stable by wcdolphin on Saturday January 29, 2022 @05:17PM (#30131143)
Interesting. Now that isort has support for black-compatibility, what’s the main difference with usort?
Re:Black, the uncompromising Python code formatter, is stable by jreese on Saturday January 29, 2022 @05:22PM (#30131182)
We've written a number of words on "Why µsort", including a comparison to isort: 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.

Re:Black, the uncompromising Python code formatter, is stable by stdbrouw on Saturday January 29, 2022 @05:46PM (#30131366)
FWIW, 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.
Re:Black, the uncompromising Python code formatter, is stable by jreese on Saturday January 29, 2022 @05:57PM (#30131475)
That'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.
Re:Black, the uncompromising Python code formatter, is stable by wcdolphin on Saturday January 29, 2022 @05:25PM (#30131199)
Very cool. I love the idea of one tool rather than two, I’ll check it out!