Skip to content

Commit

Permalink
Use Ruff formatting, run Ruff on notebooks (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep authored Dec 18, 2023
1 parent 172b9d7 commit cccd4e9
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 30 deletions.
7 changes: 2 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ repos:
hooks:
- id: prettier
# Hooks that are run for scripts
- repo: https://github.com/psf/black
rev: "23.11.0"
hooks:
- id: black
files: ^scripts/
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.8
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
files: ^scripts/
- id: ruff-format
files: ^scripts/
6 changes: 3 additions & 3 deletions scripts/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ addopts = [
]
filterwarnings = ["error"]

[tool.black]
line-length = 120

[tool.ruff]
line-length = 120
allowed-confusables = ["", "×"]
Expand Down Expand Up @@ -99,6 +96,9 @@ ignore = [
]
unfixable = ["RUF001"] # never “fix” “confusables”

[tool.ruff.format]
docstring-code-format = true

[tool.ruff.isort]
known-first-party = ["scverse_template_scripts"]

Expand Down
11 changes: 3 additions & 8 deletions {{cookiecutter.project_name}}/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ default_stages:
- push
minimum_pre_commit_version: 2.16.0
repos:
- repo: https://github.com/psf/black
rev: "23.12.0"
hooks:
- id: black
- repo: https://github.com/asottile/blacken-docs
rev: 1.16.0
hooks:
- id: blacken-docs
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v4.0.0-alpha.5
hooks:
Expand All @@ -22,7 +14,10 @@ repos:
rev: v0.1.8
hooks:
- id: ruff
types_or: [python, pyi, jupyter]
args: [--fix, --exit-non-zero-on-fix]
- id: ruff-format
types_or: [python, pyi, jupyter]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
Expand Down
11 changes: 4 additions & 7 deletions {{cookiecutter.project_name}}/docs/template_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ All dependencies listed in such optional dependency groups can then be installed
#### Tool configurations

The `pyproject.toml` file also serves as single configuration file for many tools such as many {ref}`pre-commit`.
For example, the line length of [black](https://github.com/psf/black) can be configured as follows:
For example, the line length for auto-formatting can be configured as follows:

```toml
[tool.black]
[tool.ruff]
line-length = 120
```

Expand Down Expand Up @@ -230,12 +230,9 @@ Once authorized, pre-commit.ci should automatically be activated.

The following pre-commit hooks are for code style and format:

- [black](https://black.readthedocs.io/en/stable/):
standard code formatter in Python.
- [blacken-docs](https://github.com/asottile/blacken-docs):
black on Python code in docs.
- [prettier](https://prettier.io/docs/en/index.html):
standard code formatter for non-Python files (e.g. YAML).
- [ruff][] formatting (`ruff-format`)
- [ruff][] based checks:
- [isort](https://beta.ruff.rs/docs/rules/#isort-i) (rule category: `I`):
sort module imports into sections and types.
Expand Down Expand Up @@ -281,7 +278,7 @@ This section shows you where these checks are defined, and how to enable/ disabl
##### pre-commit
You can add or remove pre-commit checks by simply deleting relevant lines in the `.pre-commit-config.yaml` file under the repository root.
Some pre-commit checks have additional options that can be specified either in the `pyproject.toml` (for `ruff` and `black`) or tool-specific
Some pre-commit checks have additional options that can be specified either in the `pyproject.toml` (for `ruff`) or tool-specific
config files, such as `.prettierrc.yml` for **prettier**.
##### Ruff
Expand Down
15 changes: 8 additions & 7 deletions {{cookiecutter.project_name}}/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ urls.Home-page = "{{ cookiecutter.project_repo }}"
dependencies = [
"anndata",
# for debug logging (referenced from the issue template)
"session-info"
"session-info",
]

[project.optional-dependencies]
dev = [
"pre-commit",
"twine>=4.0.2"
"twine>=4.0.2",
]
doc = [
"docutils>=0.8,!=0.18.*,!=0.19.*",
Expand Down Expand Up @@ -67,9 +67,6 @@ addopts = [
"--import-mode=importlib", # allow using test files with same name
]

[tool.black]
line-length = 120

[tool.ruff]
src = ["src"]
line-length = 120
Expand All @@ -87,7 +84,7 @@ select = [
"RUF100", # Report unused noqa directives
]
ignore = [
# line too long -> we accept long comment lines; black gets rid of long code lines
# line too long -> we accept long comment lines; formatter gets rid of long code lines
"E501",
# Do not assign a lambda expression, use a def -> lambda expression assignments are convenient
"E731",
Expand All @@ -113,6 +110,10 @@ ignore = [
# We want docstrings to start immediately after the opening triple quote
"D213",
]
extend-include = ["*.ipynb"]

[tool.ruff.format]
docstring-code-format = true

[tool.ruff.pydocstyle]
convention = "numpy"
Expand All @@ -131,5 +132,5 @@ skip = [
"docs/changelog.md",
"docs/references.bib",
"docs/references.md",
"docs/notebooks/example.ipynb"
"docs/notebooks/example.ipynb",
]

0 comments on commit cccd4e9

Please sign in to comment.