blacken-docs icon indicating copy to clipboard operation
blacken-docs copied to clipboard

Support Ruff formatter as an alternative for Black

Open jond01 opened this issue 1 year ago • 4 comments

Description

Ruff formatter can replace Black, and it's much faster: https://docs.astral.sh/ruff/formatter/

I suggest supporting it as an option while keeping Black the default Python formatter. Opting for Ruff can be done via a flag or persistent file configuration.

Implementation-wise it would be different from Black, as Ruff does not have a Python API. It's achievable though.

What do you think?

jond01 avatar Jul 01 '24 20:07 jond01

Yeah, now Ruff has a formatter, I’d be tempted to add this and rename the project to format-docs or similar.

I think without a Ruff Python API, it wouldn't be particularly feasible. The only alternative would be to shell out to ruff format for each snippet, which would be slow due to process launch overhead (no matter how fast Ruff is).

I found the upstream Python API issue: https://github.com/astral-sh/ruff/issues/659

adamchainz avatar Jul 01 '24 21:07 adamchainz

Seem this can now be done via https://github.com/amyreese/ruff-api

gaborbernat avatar Sep 09 '24 04:09 gaborbernat

Thanks for the link, but I’d rather not depend on a “highly experimental” third-party package.

adamchainz avatar Sep 09 '24 05:09 adamchainz

Ruff plans to implement this functionality: https://github.com/astral-sh/ruff/issues/8237

adamchainz avatar Feb 17 '25 14:02 adamchainz