Skip to content

Commit 0510bd4

Browse files
committed
docs: more details about using Ruff as a formatter
1 parent 0f459f6 commit 0510bd4

File tree

1 file changed

+41
-21
lines changed

1 file changed

+41
-21
lines changed

README.rst

+41-21
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ in regions which have changed in the Git working tree between the two revisions.
3737

3838
The reformatters supported are:
3939

40-
- Black_ for code reformatting
40+
- Black_ and `the Ruff formatter`_ for code reformatting
4141
- isort_ for sorting imports
4242
- flynt_ for turning old-style format strings to f-strings
4343

@@ -48,8 +48,9 @@ To easily run Darker as a Pytest_ plugin, see pytest-darker_.
4848
To integrate Darker with your IDE or with pre-commit_,
4949
see the relevant sections below in this document.
5050

51-
.. _Black: https://github.com/python/black
52-
.. _isort: https://github.com/timothycrosley/isort
51+
.. _Black: https://black.readthedocs.io/
52+
.. _the Ruff formatter: https://docs.astral.sh/ruff/formatter/
53+
.. _isort: https://pycqa.github.io/isort/
5354
.. _flynt: https://github.com/ikamensh/flynt
5455
.. _Pytest: https://docs.pytest.org/
5556
.. _pytest-darker: https://pypi.org/project/pytest-darker/
@@ -76,9 +77,10 @@ see the relevant sections below in this document.
7677
Why?
7778
====
7879

79-
You want to start unifying code style in your project using Black_.
80+
You want to start unifying code style in your project
81+
using Black_ or `the Ruff formatter`_.
8082
Maybe you also like to standardize on how to order your imports,
81-
or do static type checking or other static analysis for your code.
83+
or convert string formatting to use f-strings.
8284

8385
However, instead of formatting the whole code base in one giant commit,
8486
you'd like to only change formatting when you're touching the code for other reasons.
@@ -92,6 +94,11 @@ for various good reasons, and so far there hasn't been a plan to implemented it
9294
However, in September 2021 Black developers started to hint towards adding this feature
9395
after all (`1352`__). This might at least simplify Darker's algorithm substantially.
9496

97+
The ``--range`` option in `the Ruff formatter`_
98+
does allow for partial formatting of a single range,
99+
but to make use of it,
100+
Darker would need call `the Ruff formatter`_ once for each modified chunk.
101+
95102
__ https://github.com/psf/black/issues/134
96103
__ https://github.com/psf/black/issues/142
97104
__ https://github.com/psf/black/issues/245
@@ -105,7 +112,8 @@ This tool is for those who want to do partial formatting right now.
105112

106113
Note that this tool is meant for special situations
107114
when dealing with existing code bases.
108-
You should just use Black_ and isort_ as is when starting a project from scratch.
115+
You should just use Black_ or `the Ruff formatter`_, Flynt_ and isort_ as is
116+
when starting a project from scratch.
109117

110118
You may also want to still consider whether reformatting the whole code base in one
111119
commit would make sense in your particular case. You can ignore a reformatting commit
@@ -161,13 +169,14 @@ Use ``python -m darker`` instead of ``darker`` in that case.
161169
By default, ``darker`` just runs Black_ to reformat the code.
162170
You can enable additional features with command line options:
163171

172+
- ``--formatter=ruff``: Use `the Ruff formatter`_ instead of Black_.
164173
- ``-i`` / ``--isort``: Reorder imports using isort_. Note that isort_ must be
165174
run in the same Python environment as the packages to process, as it imports
166175
your modules to determine whether they are first or third party modules.
167176
- ``-f`` / ``--flynt``: Also convert string formatting to use f-strings using the
168177
``flynt`` package
169178

170-
If you only want to run those tools without reformatting with Black,
179+
If you only want to run isort_ and/or Flynt_ without reformatting code,
171180
use the ``--formatter=none`` option.
172181

173182
*New in version 1.1.0:* The ``-L`` / ``--lint`` option.
@@ -300,6 +309,9 @@ flynt_ (option ``-f`` / ``--flynt``) is also invoked as a subprocess, but passin
300309
command line options to it is currently not supported. Configuration files need to be
301310
used instead.
302311

312+
Options for `the Ruff formatter`_ are read as usual directly by Ruff itself
313+
when Darker invokes it as a subprocess.
314+
303315
Darker does honor exclusion options in Black configuration files when recursing
304316
directories, but the exclusions are only applied to Black reformatting.
305317
Isort is still run on excluded files. Also, individual files explicitly listed on the
@@ -308,6 +320,7 @@ command line are still reformatted even if they match exclusion patterns.
308320
For more details, see:
309321

310322
- `Black documentation about pyproject.toml`_
323+
- `Ruff documentation about pyproject.toml and ruff.toml`_
311324
- `isort documentation about config files`_
312325
- `public GitHub repositories which install and run Darker`_
313326
- `flynt documentation about configuration files`_
@@ -384,7 +397,7 @@ To change default values for these options for a given project,
384397
add a ``[tool.darker]`` section to ``pyproject.toml`` in the project's root directory,
385398
or to a different TOML file specified using the ``-c`` / ``--config`` option.
386399

387-
You should configure invoked tools like Black_, isort_ and flynt_
400+
You should configure invoked tools like Black_, `the Ruff formatter`_, isort_ and flynt_
388401
using their own configuration files.
389402

390403
As an exception, the ``line-length`` and ``target-version`` options in ``[tool.darker]``
@@ -467,6 +480,7 @@ the Graylint_ package. Also removed ``lint =``, ``skip_string_normalization =``
467480

468481
.. _Black documentation about pyproject.toml: https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#configuration-via-a-file
469482
.. _isort documentation about config files: https://timothycrosley.github.io/isort/docs/configuration/config_files/
483+
.. _Ruff documentation about pyproject.toml and ruff.toml: https://docs.astral.sh/ruff/formatter/#configuration
470484
.. _public GitHub repositories which install and run Darker: https://github.com/search?q=%2Fpip+install+.*darker%2F+path%3A%2F%5E.github%5C%2Fworkflows%5C%2F.*%2F&type=code
471485
.. _flynt documentation about configuration files: https://github.com/ikamensh/flynt#configuration-files
472486
.. _command line arguments: https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#command-line-options
@@ -820,16 +834,19 @@ line options always take highest precedence.
820834
.. _Pygments: https://pypi.org/project/Pygments/
821835

822836

823-
Guarding against Black compatibility breakage
824-
=============================================
837+
Guarding against Black, Flynt and isort compatibility breakage
838+
==============================================================
825839

826-
Darker accesses some Black internals which don't belong to its public API. Darker is
827-
thus subject to becoming incompatible with future versions of Black.
840+
Darker accesses some Black_, Flynt_ and isort_ internals
841+
which don't belong to their public APIs.
842+
Darker is thus subject to becoming incompatible with future versions of those tools.
828843

829-
To protect users against such breakage, we test Darker daily against the `Black main
830-
branch`_ and strive to proactively fix any potential incompatibilities through this
831-
process. If a commit to Black ``main`` branch introduces an incompatibility with
832-
Darker, we will release a first patch version for Darker that prevents upgrading Black
844+
To protect users against such breakage, we test Darker daily against
845+
the `Black main branch`_, `Flynt master branch`_ and `isort main branch`_,
846+
and strive to proactively fix any potential incompatibilities through this process.
847+
If a commit to those branches introduces an incompatibility with Darker,
848+
we will release a first patch version for Darker
849+
that prevents upgrading the corresponding tool
833850
and a second patch version that fixes the incompatibility. A hypothetical example:
834851

835852
1. Darker 9.0.0; Black 35.12.0
@@ -867,6 +884,8 @@ See issue `#382`_ and PR `#430`_ for more information.
867884

868885
.. _compatible release: https://peps.python.org/pep-0440/#compatible-release
869886
.. _Black main branch: https://github.com/psf/black/commits/main
887+
.. _Flynt master branch: https://github.com/ikamensh/flynt/commits/master
888+
.. _isort main branch: https://github.com/PyCQA/isort/commits/main
870889
.. _test-future: https://github.com/akaihola/darker/blob/master/.github/workflows/test-future.yml
871890
.. _#382: https://github.com/akaihola/darker/issues/382
872891
.. _#430: https://github.com/akaihola/darker/issues/430
@@ -882,7 +901,7 @@ Darker does the following:
882901
the ``--revision=REV1..REV2`` option
883902
- record current line numbers of lines edited or added between those revisions
884903
- run flynt_ on edited and added files (if Flynt is enabled by the user)
885-
- run Black_ on edited and added files
904+
- run Black_ or `the Ruff formatter`_ on edited and added files
886905
- compare before and after reformat, noting each continuous chunk of reformatted lines
887906
- discard reformatted chunks on which no edited/added line falls on
888907
- keep reformatted chunks on which some edited/added lines fall on
@@ -899,14 +918,15 @@ To sort imports when the ``--isort`` option was specified, Darker proceeds like
899918
Limitations and work-arounds
900919
=============================
901920

902-
Black doesn't support partial formatting natively.
903-
Because of this, Darker lets Black reformat complete files.
904-
Darker then accepts or rejects chunks of contiguous lines touched by Black,
921+
Black doesn't support partial formatting natively,
922+
and `the Ruff formatter`_ only accepts a single line range.
923+
Because of this, Darker lets them reformat complete files.
924+
Darker then accepts or rejects chunks of contiguous lines touched by the formatter,
905925
depending on whether any of the lines in a chunk were edited or added
906926
between the two revisions.
907927

908928
Due to the nature of this algorithm,
909-
Darker is often unable to minimize the number of changes made by Black
929+
Darker is often unable to minimize the number of changes made by reformatters
910930
as carefully as a developer could do by hand.
911931
Also, depending on what kind of changes were made to the code,
912932
diff results may lead to Darker applying reformatting in an invalid way.

0 commit comments

Comments
 (0)