Skip to content

Commit

Permalink
feat(python): switch from black to ruff-format for formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ahal committed Aug 6, 2024
1 parent e04586e commit 2a3d54f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 57 deletions.
5 changes: 1 addition & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@ repos:
- id: yamllint
repo: https://github.com/adrienverge/yamllint.git
rev: v1.35.1
- hooks:
- id: black
repo: https://github.com/psf/black
rev: 24.3.0
- hooks:
- args:
- --fix
- --exit-non-zero-on-fix
id: ruff
- id: ruff-format
repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.3.5
exclude: |
Expand Down
53 changes: 19 additions & 34 deletions STANDARD.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,39 +367,40 @@ commands = coverage erase

[tox]: https://tox.wiki/

### Formatting
### Formatting and Linting

Projects should use [black] for formatting. The default configuration is
recommended, but if extra configuration is necessary (e.g to exclude files), it
should go in the top-level `pyproject.toml` file:
Projects should use [ruff] for formatting and linting. The default
configuration is recommended, but if extra configuration is necessary (e.g to
exclude files), it should go in the top-level `pyproject.toml` file:

```toml
[tool.black]
extend-exclude = """(\
test/data\
src/foo)\
"""
[tool.ruff]
extend-exclude = [
"test/data",
"src/foo",
]
```

Additionally, black should be configured to run in the pre-commit hook by
Additionally, `ruff` should be configured to run in the pre-commit hook by
adding the following to the top-level `.pre-commit-config.yaml` file:

```yaml
repos:
- repo: https://github.com/psf/black
rev: 23.3.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.6
hooks:
- id: black
- id: ruff
args: ["--fix"]
- id: ruff-format
```

Be sure to run `pre-commit autoupdate` to pick up the latest version.

[black]: https://pypi.org/project/black/
[ruff]: https://pypi.org/project/ruff/

### Linting
#### Lint Rules

Projects should use [ruff] for linting. Ruff should be configured to run the
following rules:
Ruff should be configured to run the following lint rules:

* pycodestyle
* pyflakes
Expand All @@ -421,28 +422,12 @@ select = [
"TCH", # flake8-type-checking
]
ignore = [
"E501", # let black handle line-length
"E501", # let ruff-format handle line-length
]
# change to your minimum supported Python version
target-version = "py38"
```

Additionally ruff should be configured to run in the `pre-commit` hook by adding
the following to the top-level `.pre-commit-config.yaml` file:

```yaml
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: 'v0.1.5'
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
```

Be sure to run `pre-commit autoupdate` to pick up the latest version.

[ruff]: https://pypi.org/project/ruff/

### Type Checking

Projects should use [Pyright] for type checking. Pyright has advanced type
Expand Down
7 changes: 1 addition & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,14 @@ requests = "^2.31.0"
[tool.poetry.group.type.dependencies]
pyright = "^1.1.336"

[tool.black]
extend-exclude = """(\
reps/templates)\
"""

[tool.coverage.run]
omit = ["reps/templates/*"]

[tool.pytest.ini_options]
testpaths = ["test"]

[tool.ruff]
exclude = ["reps/templates"]
extend-exclude = ["reps/templates"]

[tool.pyright]
include = ["reps"]
Expand Down
12 changes: 4 additions & 8 deletions reps/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,13 @@ def merge_pre_commit(items: CookiecutterContext):
pre_commit_config["repos"].extend(
[
{
"repo": "https://github.com/psf/black",
"rev": "23.3.0",
"repo": "https://github.com/astral-sh/ruff-pre-commit",
"rev": "v0.5.6",
"hooks": [
{"id": "black"},
{"id": "ruff", "args": ["--fix", "--exit-non-zero-on-fix"]},
{"id": "ruff-format"},
],
},
{
"repo": "https://github.com/astral-sh/ruff-pre-commit",
"rev": "v0.1.1",
"hooks": [{"id": "ruff", "args": ["--fix", "--exit-non-zero-on-fix"]}],
},
]
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ readme = "README.md"
[tool.poetry.dependencies]
python = "^{{cookiecutter.min_python_version}}"

[tool.black]
line-length = 88

[tool.pytest.ini_options]
xfail_strict = true

Expand All @@ -21,6 +18,10 @@ branch = true
source = ["src/{{cookiecutter.__package_name}}/"]

[tool.ruff]
line-length = 88
target-version = "py{{cookiecutter.__min_tox_python_version}}"

[tool.ruff.lint]
select = [
"E", "W", # pycodestyle
"F", # pyflakes
Expand All @@ -30,9 +31,8 @@ select = [
"TCH", # flake8-type-checking
]
ignore = [
"E501", # let black handle line-length
"E501", # let ruff-format handle line-length
]
target-version = "py{{cookiecutter.__min_tox_python_version}}"

[tool.ruff.isort]
known-first-party = ["{{cookiecutter.__package_name}}"]
Expand Down

0 comments on commit 2a3d54f

Please sign in to comment.