Skip to content

Commit

Permalink
DX: implement Ruff formatter (#301)
Browse files Browse the repository at this point in the history
* BREAK: remove `lint` optional dependencies
* DX: format docstrings with Ruff
* DX: remove `black` settings
* DX: switch from `black` to ruff
* ENH: ignore Ruff rule ISC001 for formatter
* MAINT: move `ruff` to `lint` optional dependencies
* MAINT: uninstall `radon`
  • Loading branch information
redeboer authored Feb 12, 2024
1 parent 80dc096 commit f59f181
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 106 deletions.
1 change: 0 additions & 1 deletion .gitpod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ vscode:
- executablebookproject.myst-highlight
- github.vscode-github-actions
- github.vscode-pull-request-github
- ms-python.black-formatter
- ms-python.mypy-type-checker
- ms-python.python
- ms-python.vscode-pylance
Expand Down
23 changes: 7 additions & 16 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ repos:
- id: check-hooks-apply
- id: check-useless-excludes

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.2.1
hooks:
- id: ruff
args: [--fix]
- id: ruff-format

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
Expand Down Expand Up @@ -57,16 +64,6 @@ repos:
language: python
files: ^\.pre\-commit\-(config|hooks)\.yaml$

- repo: https://github.com/psf/black-pre-commit-mirror
rev: 24.1.1
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.8
hooks:
Expand Down Expand Up @@ -115,9 +112,3 @@ repos:
rev: v1.1.350
hooks:
- id: pyright

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.2.1
hooks:
- id: ruff
args: [--fix]
2 changes: 1 addition & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"executablebookproject.myst-highlight",
"github.vscode-github-actions",
"github.vscode-pull-request-github",
"ms-python.black-formatter",
"ms-python.mypy-type-checker",
"ms-python.python",
"ms-python.vscode-pylance",
Expand All @@ -26,6 +25,7 @@
"bungcip.better-toml",
"davidanson.vscode-markdownlint",
"garaioag.garaio-vscode-unwanted-recommendations",
"ms-python.black-formatter",
"ms-python.flake8",
"ms-python.isort",
"ms-python.pylint",
Expand Down
3 changes: 1 addition & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit"
},
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.rulers": [88]
},
"[yaml]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"black-formatter.importStrategy": "fromEnvironment",
"coverage-gutters.coverageFileNames": ["coverage.xml"],
"coverage-gutters.coverageReportFileName": "**/htmlcov/index.html",
"coverage-gutters.showGutterCoverage": false,
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
[![pytest](https://github.com/ComPWA/qrules/workflows/pytest/badge.svg)](https://github.com/ComPWA/qrules/actions?query=branch%3Amain+workflow%3Apytest)
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/ComPWA/policy/main.svg)](https://results.pre-commit.ci/latest/github/ComPWA/policy/main)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)

This repository sets the policies for developer environment in repositories if the [ComPWA organization](https://github.com/ComPWA) (See our [Help developing](https://compwa.github.io/develop) page). The policies are automatically enforced through [pre-commit](https://pre-commit.com).
Expand Down
56 changes: 14 additions & 42 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,31 +59,23 @@ doc = [
"sphinx-book-theme",
"sphinx-copybutton",
]
format = [
"black",
]
lint = [
"compwa-policy[mypy]",
"radon",
"ruff",
]
mypy = [
"mypy",
"sphinx-api-relink >=0.0.3",
"types-PyYAML",
"types-toml",
]
sty = [
"compwa-policy[format]",
"compwa-policy[lint]",
"compwa-policy[test]", # for pytest type hints
"compwa-policy[types]",
"mypy",
"pre-commit >=1.4.0",
"ruff",
]
test = [
"pytest",
"pytest-cov",
"pytest-xdist",
]
types = [
"pytest",
"sphinx-api-relink >=0.0.3",
"types-PyYAML",
"types-toml",
]

[project.readme]
content-type = "text/markdown"
Expand Down Expand Up @@ -123,31 +115,6 @@ where = ["src"]
[tool.setuptools_scm]
write_to = "src/compwa_policy/version.py"

[tool.black]
exclude = '''
/(
.*\.egg-info
| .*build
| \.eggs
| \.git
| \.pytest_cache
| \.tox
| \.venv
| \.vscode
| dist
)/
'''
include = '\.pyi?$'
preview = true
target-version = [
"py310",
"py311",
"py312",
"py37",
"py38",
"py39",
]

[tool.coverage.run]
branch = true
source = ["src"]
Expand Down Expand Up @@ -245,6 +212,10 @@ src = [
]
target-version = "py37"

[tool.ruff.format]
docstring-code-format = true
line-ending = "lf"

[tool.ruff.lint]
extend-select = [
"A",
Expand Down Expand Up @@ -290,6 +261,7 @@ ignore = [
"D407",
"D416",
"E501",
"ISC001",
"PLR0913",
"PLW1514",
"PLW2901",
Expand Down
9 changes: 5 additions & 4 deletions src/compwa_policy/check_dev_files/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def main(argv: Sequence[str] | None = None) -> int:
executor(toml.main) # has to run before pre-commit
executor(prettier.main, args.no_prettierrc)
if is_python_repo:
executor(black.main, has_notebooks)
if args.no_ruff:
executor(black.main, has_notebooks)
if not args.no_github_actions:
executor(
release_drafter.main,
Expand Down Expand Up @@ -287,11 +288,11 @@ def _create_argparse() -> ArgumentParser:
def _to_list(arg: str) -> list[str]:
"""Create a comma-separated list from a string argument.
>>> _to_list('a c , test,b')
>>> _to_list("a c , test,b")
['a', 'b', 'c', 'test']
>>> _to_list(' ')
>>> _to_list(" ")
[]
>>> _to_list('')
>>> _to_list("")
[]
"""
space_separated = arg.replace(",", " ")
Expand Down
1 change: 1 addition & 0 deletions src/compwa_policy/check_dev_files/jupyter.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def _update_dev_requirements() -> None:
return
hierarchy = ["jupyter", "dev"]
dependencies = [
"black",
"isort",
"jupyterlab",
"jupyterlab-code-formatter",
Expand Down
Loading

0 comments on commit f59f181

Please sign in to comment.