Skip to content

Commit

Permalink
Formatting: black -> ruff format (#27)
Browse files Browse the repository at this point in the history
Retain the formatting style of black while eliminating a dev dependency
by using our existing rust-based ruff linter.

See also: https://docs.astral.sh/ruff/formatter/
  • Loading branch information
tony committed Nov 18, 2023
2 parents af9830c + 0dac7b2 commit 5cf225f
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 120 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ jobs:
- name: Lint with ruff
run: poetry run ruff .

- name: Format with ruff
run: poetry run ruff format . --check

- name: Lint with mypy
run: poetry run mypy .

Expand Down
19 changes: 19 additions & 0 deletions .vim/coc-settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"[markdown][python]": {
"coc.preferences.formatOnSave": true
},
"python.analysis.autoSearchPaths": true,
"python.analysis.typeCheckingMode": "basic",
"python.analysis.useLibraryCodeForTypes": true,
"python.formatting.provider": "ruff",
"python.linting.ruffEnabled": true,
"python.linting.mypyEnabled": true,
"python.linting.flake8Enabled": false,
"python.linting.pyflakesEnabled": false,
"python.linting.pycodestyleEnabled": false,
"python.linting.banditEnabled": false,
"python.linting.pylamaEnabled": false,
"python.linting.pylintEnabled": false,
"pyright.organizeimports.provider": "ruff",
"pyright.testing.provider": "pytest",
}
6 changes: 6 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ $ pip install --user --upgrade --pre gp-libs
- poetry: 1.6.1 -> 1.7.0

See also: https://github.com/python-poetry/poetry/blob/1.7.0/CHANGELOG.md
- Move formatting from `black` to [`ruff format`] (#27)

This retains the same formatting style of `black` while eliminating a
dev dependency by using our existing rust-based `ruff` linter.

[`ruff format`]: https://docs.astral.sh/ruff/formatter/

### Packaging

Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ start_docs:
design_docs:
$(MAKE) -C docs design

black:
poetry run black `${PY_FILES}`
ruff_format:
poetry run ruff format .

ruff:
ruff .
Expand Down
56 changes: 27 additions & 29 deletions docs/developing.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,86 +48,84 @@ Rebuild docs and run server via one terminal: `make dev_docs` (requires above, a

## Formatting / Linting

### black
### ruff

[black] is used for formatting.
The project uses [ruff] to handles formatting, sorting imports and linting.

````{tab} Command
poetry:
```console
$ poetry run black .
$ poetry run ruff
```
If you setup manually:
```console
$ black .
$ ruff .
```
````

````{tab} make
```console
$ make black
$ make ruff
```
````

In the future, `ruff` (below) may replace black as formatter.
````{tab} Watch
### ruff
```console
$ make watch_ruff
```
The project uses [ruff] to handles formatting, sorting imports and linting.
requires [`entr(1)`].
````{tab} Command
````

````{tab} Fix files
poetry:
```console
$ poetry run ruff
$ poetry run ruff . --fix
```
If you setup manually:
```console
$ ruff .
$ ruff . --fix
```
````

````{tab} make
#### ruff format

```console
$ make ruff
```
[ruff format] is used for formatting.

````
````{tab} Command
````{tab} Watch
poetry:
```console
$ make watch_ruff
$ poetry run ruff format .
```
requires [`entr(1)`].
````

````{tab} Fix files
poetry:
If you setup manually:
```console
$ poetry run ruff . --fix
$ ruff format .
```
If you setup manually:
````

````{tab} make
```console
$ ruff . --fix
$ make ruff_format
```
````
Expand Down Expand Up @@ -186,6 +184,6 @@ Update `__version__` in `src/gp_libs.py` and `pyproject.toml`::
[poetry]: https://python-poetry.org/
[entr(1)]: http://eradman.com/entrproject/
[`entr(1)`]: http://eradman.com/entrproject/
[black]: https://github.com/psf/black
[ruff format]: https://docs.astral.sh/ruff/formatter/
[ruff]: https://ruff.rs
[mypy]: https://mypy-lang.org/
84 changes: 1 addition & 83 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ coverage = "*"
pytest-cov = "*"

### Lint ###
black = "*"
ruff = "*"
mypy = "*"
typing-extensions = { version = "*" }
Expand All @@ -85,7 +84,6 @@ docs = [
test = ["pytest", "pytest-rerunfailures", "pytest-watcher"]
coverage = ["codecov", "coverage", "pytest-cov"]
lint = [
"black",
"ruff",
"mypy",
"types-docutils",
Expand Down
8 changes: 5 additions & 3 deletions src/doctest_docutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,8 @@ def condition(node: Node) -> bool:
or (
isinstance(node, nodes.literal_block)
and re.match(
doctest.DocTestParser._EXAMPLE_RE, node.astext() # type:ignore
doctest.DocTestParser._EXAMPLE_RE, # type:ignore
node.astext(),
)
is not None
)
Expand Down Expand Up @@ -378,8 +379,9 @@ def _from_module(

# Type ignored because this is a private function.
return t.cast(
bool, super()._from_module(module, object) # type:ignore[misc]
)
bool,
super()._from_module(module, object), # type:ignore[misc]
)

else: # pragma: no cover
pass
Expand Down
1 change: 1 addition & 0 deletions src/docutils_compat.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Helpers for cross compatibility across dependency versions."""

import typing as t

if t.TYPE_CHECKING:
Expand Down
6 changes: 5 additions & 1 deletion src/pytest_doctest_docutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
This is a derivative of my PR https://github.com/thisch/pytest-sphinx/pull/38 to
pytest-sphinx (BSD 3-clause), 2022-09-03.
"""

import bdb
import doctest
import io
Expand Down Expand Up @@ -237,5 +238,8 @@ def collect(self) -> t.Iterable["DoctestItem"]:
):
if test.examples: # skip empty doctests
yield DoctestItem.from_parent(
self, name=test.name, runner=runner, dtest=test # type: ignore
self, # type: ignore
name=test.name,
runner=runner,
dtest=test,
)

0 comments on commit 5cf225f

Please sign in to comment.