Skip to content

Commit

Permalink
Merge pull request #158 from eshwen/esh-use-ruff-format
Browse files Browse the repository at this point in the history
Replace Black with Ruff format
  • Loading branch information
eshwen authored Jan 30, 2024
2 parents b678441 + 2e6c402 commit cf1a0fe
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 179 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/api-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
contents: 'write'
actions: 'write'

uses: eshwen/adrenaline/.github/workflows/api-docs-build.yml@v0.2.5
uses: eshwen/adrenaline/.github/workflows/api-docs-build.yml@v0.3.0
with:
python-version: '3.10'
changelog: 'CHANGELOG.md'
2 changes: 1 addition & 1 deletion .github/workflows/quality-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ concurrency:

jobs:
lint:
uses: eshwen/adrenaline/.github/workflows/python-quality-check.yml@v0.2.5
uses: eshwen/adrenaline/.github/workflows/python-quality-check.yml@v0.3.0
with:
python-version: '3.10'
path: 'my_project/'
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- uses: actions/checkout@v4

- name: Test
uses: eshwen/adrenaline/test-python@v0.2.5
uses: eshwen/adrenaline/test-python@v0.3.0
with:
python-version: '3.10'
path: 'my_project/'
Expand Down
8 changes: 2 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,11 @@ repos:
- id: auto-walrus

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: 'v0.1.9' # Must match requirements-dev.txt
rev: 'v0.1.15' # Must match requirements-dev.txt
hooks:
- id: ruff
args: [ --fix, --exit-non-zero-on-fix ]

- repo: https://github.com/psf/black
rev: 23.12.1 # Must match requirements-dev.txt
hooks:
- id: black
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.8.0 # Must match requirements-dev.txt
Expand Down
5 changes: 0 additions & 5 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
"--ignore=E712,W503"
],

"python.formatting.provider": "black",
"python.formatting.blackArgs": [
"--line-length 120"
],

"[python]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
[![Poetry](https://img.shields.io/endpoint?url=https://python-poetry.org/badge/v0.json)](https://python-poetry.org/)
[![Docker](https://badgen.net/badge/icon/docker?icon=docker&label)](https://docker.com/)

[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v1.json)](https://github.com/charliermarsh/ruff)
[![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/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
[![Checked with mypy](http://www.mypy-lang.org/static/mypy_badge.svg)](http://mypy-lang.org/)
[![Imports: isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336)](https://pycqa.github.io/isort/)
[![pydocstyle](https://img.shields.io/badge/pydocstyle-enabled-AD4CD3)](http://www.pydocstyle.org/en/stable/)
Expand Down
2 changes: 1 addition & 1 deletion docs/tooling/pipelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Several pipelines are included to execute automatically on various triggers:

| What | When | How |
|--------------------------------------------------|--------------------------------------|-----------------------|
| Check code with `black`, `ruff`, and `mypy` | On PR creation and subsequent pushes | [quality-check.yml] |
| Check code with `ruff` and `mypy` | On PR creation and subsequent pushes | [quality-check.yml] |
| Unit test with `pytest` | On PR creation and subsequent pushes | [test.yml] |
| Build the API documentation | On the creation of a new release | [api-docs.yml] |
| Check for dependency updates and vulnerabilities | On a schedule (check the file) | [dependabot.yml] |
Expand Down
41 changes: 10 additions & 31 deletions docs/tooling/quality.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,27 @@ To automatically fix any issues, run
ruff --fix my_project/ tests/
```

### PyCharm integration

You can also install the [Ruff plugin for PyCharm] to run it automatically on file changes. In **Preferences** |
**Tools** | **Ruff**, configure it as you like.

To aid in documentation, in PyCharm go to **Preferences** | **Tools** | **Python Integrated Tools**. Under
**Docstrings**, select the docstring format as Google.

## Black

[Black] is a widely-used tool to ensure consistently-formatted code.
Ruff now contains a formatter to replace [Black]. It ensures consistently-formatted, highly readable code.

To automatically reformat your code, run
To automatically format your code, run

```shell
black my_project/ tests/
ruff format my_project/ tests/
```

To instead just show what `black` would change, run
To instead just show what `ruff` would change, run

```shell
black --diff my_project/ tests/
ruff format --diff my_project/ tests/
```

### PyCharm integration

`black` is integrated into PyCharm as of v2023.2. Go to **Settings** | **Tools** | **Black** to configure.

??? tip "For older versions"

PyCharm supports file watchers, which automatically run a command when a file is saved. This is useful for
transparently formatting code.

To include `black` as a file watcher, go to **Settings** | **Tools** | **File Watchers** and import the
[watchers.xml]. This will automatically apply formatting to Python files and Jupyter notebooks. You may need to
edit the **Program** path to point to your `black` executable.
You can also install the [Ruff plugin for PyCharm] to run it automatically on file changes. In **Preferences** |
**Tools** | **Ruff**, configure it as you like.

Otherwise, to set it up yourself, follow the instructions
at <https://black.readthedocs.io/en/stable/integrations/editors.html>.
To aid in documentation, in PyCharm go to **Preferences** | **Tools** | **Python Integrated Tools**. Under
**Docstrings**, select the docstring format as Google.

## Mypy

Expand Down Expand Up @@ -112,8 +94,7 @@ The [.pre-commit-config.yaml] file contains hooks to run

- built-in `pre-commit` checks
- Automatic use of the walrus operator
- `ruff` for linting (fixes fixable issues)
- `black` for formatting (fixes fixable issues)
- `ruff` for linting and formatting (fixes fixable issues)
- `mypy` type-hinting errors
- `poetry` dependency status (`poetry.lock` and requirements files are up-to-date)

Expand All @@ -132,8 +113,6 @@ pre-commit run -a

[pyproject.toml]: https://github.com/eshwen/ds-python-boilerplate/blob/main/pyproject.toml

[watchers.xml]: https://github.com/eshwen/ds-python-boilerplate/blob/main/pycharm/watchers.xml

[pre-commit]: https://pre-commit.com/

[Ruff]: https://beta.ruff.rs/docs/
Expand Down
97 changes: 19 additions & 78 deletions poetry.lock

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

42 changes: 0 additions & 42 deletions pycharm/watchers.xml

This file was deleted.

13 changes: 7 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ PyYAML = "*"

[tool.poetry.group.dev.dependencies]
pre-commit = "^3.3.3"
black = { version = "^23.12.1", extras = ["jupyter"] }
ruff = "^0.1.9"
ruff = "^0.1.15"

# Type hinting
mypy = "^1.8.0"
Expand Down Expand Up @@ -78,10 +77,6 @@ mkdocs-literate-nav = "^0.6.1"
mkdocs-section-index = "^0.3.8"
git-changelog = "^2.4.0"

[tool.black]
target-version = ['py310']
line-length = 120

[tool.mypy]
python_version = "3.10"
warn_return_any = true
Expand Down Expand Up @@ -135,6 +130,12 @@ extend-exclude = [
"D103", # D103 complains when there is no docstring in a function
]

[tool.ruff.format]
# Like Black
quote-style = "double"
indent-style = "space"
skip-magic-trailing-comma = false
line-ending = "auto"

[tool.ruff.pydocstyle]
convention = "google"
Expand Down
Loading

0 comments on commit cf1a0fe

Please sign in to comment.