Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to Ruff #147

Merged
merged 38 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
64bd195
First attempt at ruff
flying-sheep Feb 21, 2023
5388724
Add a bunch of codes
flying-sheep Feb 21, 2023
b3df871
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 21, 2023
68b65a5
conf fixes
flying-sheep Feb 21, 2023
f0267d6
re-add black
flying-sheep Feb 21, 2023
4be145e
ignore rules
flying-sheep Feb 21, 2023
db6346a
missed one
flying-sheep Feb 21, 2023
de69c25
we don’t use the plugin providing P101
flying-sheep Feb 21, 2023
7d22424
test using `src` options
flying-sheep Feb 21, 2023
eb82271
add yesqa
flying-sheep Feb 21, 2023
5f0fbd5
ruffify pyupgrade
flying-sheep Feb 22, 2023
3a70379
Cache pre-commit
flying-sheep Feb 22, 2023
a2e1e5d
oops
flying-sheep Feb 22, 2023
fc08f4a
check if cache works
flying-sheep Feb 22, 2023
b3e02d5
ignore mutually incompatible rules
flying-sheep Feb 22, 2023
e7602f4
use codes
flying-sheep Feb 22, 2023
77d0484
test doc change
flying-sheep Feb 22, 2023
4c9bb9f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 22, 2023
d340ad7
docs
flying-sheep Feb 22, 2023
b113319
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 22, 2023
2072069
adapt ignore docs
flying-sheep Feb 22, 2023
2d8197f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 22, 2023
046bb9c
add ruff to hooks
flying-sheep Feb 22, 2023
1942eff
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 22, 2023
d8eeb51
fix link
flying-sheep Feb 22, 2023
dd963b9
we’re using `select` now
flying-sheep Feb 22, 2023
4e4b4a1
fix link syntax
flying-sheep Feb 22, 2023
b8989d7
grammar
flying-sheep Feb 22, 2023
22e30d9
re-trigger build
flying-sheep Feb 22, 2023
585de85
allow linking h4
flying-sheep Feb 22, 2023
33a2112
be more explicit about Python vs text files
flying-sheep Feb 22, 2023
cce7271
Update {{cookiecutter.project_name}}/pyproject.toml
grst Feb 22, 2023
22da7ac
Hopefully fix GitHub’s syntax highlighting
flying-sheep Feb 22, 2023
d73c37b
Update {{cookiecutter.project_name}}/pyproject.toml
flying-sheep Feb 22, 2023
cf3fed7
Ruff defaults to `respect-gitignore = true`
flying-sheep Feb 22, 2023
7364220
Add .ruff_cache/ to .gitignore
flying-sheep Feb 22, 2023
109a3c4
normalize newlines
flying-sheep Feb 22, 2023
f692b76
explain RUF100
flying-sheep Feb 22, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@ jobs:
overrides = dict(package_name='${{ matrix.package-name }}')
cookiecutter('.', no_input=True, extra_context=overrides)
# Check
- name: Set up pre-commit cache
uses: actions/cache@v3
with:
path: ~/.cache/pre-commit
key: pre-commit-3|${{ matrix.python }}|${{ hashFiles(format('{0}/.pre-commit-config.yaml', env.PROJECT_ROOT)) }}
- name: Run pre-commit
run: |
cd "$PROJECT_ROOT"
git add .
pre-commit run --color=always --show-diff-on-failure --all-files
pre-commit run --verbose --color=always --show-diff-on-failure --all-files
- name: Install the package
run: |
cd $PROJECT_ROOT
Expand Down
57 changes: 0 additions & 57 deletions {{cookiecutter.project_name}}/.flake8

This file was deleted.

4 changes: 4 additions & 0 deletions {{cookiecutter.project_name}}/.gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# Temp files
.DS_Store
*~
buck-out/

# Compiled files
.venv/
__pycache__/
.mypy_cache/
flying-sheep marked this conversation as resolved.
Show resolved Hide resolved
.ruff_cache/

# Distribution / packaging
/build/
Expand Down
53 changes: 9 additions & 44 deletions {{cookiecutter.project_name}}/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ default_stages:
minimum_pre_commit_version: 2.16.0
repos:
- repo: https://github.com/psf/black
rev: 22.3.0
rev: "22.3.0"
flying-sheep marked this conversation as resolved.
Show resolved Hide resolved
hooks:
- id: black
- repo: https://github.com/asottile/blacken-docs
rev: v1.12.1
hooks:
- id: blacken-docs
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v2.5.1
hooks:
Expand All @@ -20,25 +24,11 @@ repos:
# See https://github.com/scverse/cookiecutter-scverse/issues/143 and
# https://github.com/jupyterlab/jupyterlab/issues/12675
language_version: "17.9.1"
- repo: https://github.com/asottile/blacken-docs
rev: v1.12.1
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.250
hooks:
- id: blacken-docs
- repo: https://github.com/PyCQA/isort
rev: 5.10.1
hooks:
- id: isort
- repo: https://github.com/asottile/yesqa
rev: v1.3.0
hooks:
- id: yesqa
additional_dependencies:
- flake8-tidy-imports
- flake8-docstrings
- flake8-rst-docstrings
- flake8-comprehensions
- flake8-bugbear
- flake8-blind-except
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
hooks:
Expand All @@ -49,31 +39,6 @@ repos:
args: [--fix=lf]
- id: trailing-whitespace
- id: check-case-conflict
- repo: https://github.com/myint/autoflake
rev: v1.4
hooks:
- id: autoflake
args:
- --in-place
- --remove-all-unused-imports
- --remove-unused-variable
- --ignore-init-module-imports
- repo: https://github.com/PyCQA/flake8
rev: 4.0.1
hooks:
- id: flake8
additional_dependencies:
- flake8-tidy-imports
- flake8-docstrings
- flake8-rst-docstrings
- flake8-comprehensions
- flake8-bugbear
- flake8-blind-except
- repo: https://github.com/asottile/pyupgrade
rev: v2.31.1
hooks:
- id: pyupgrade
args: [--py3-plus, --py38-plus, --keep-runtime-typing]
- repo: local
hooks:
- id: forbid-to-commit
Expand Down
2 changes: 1 addition & 1 deletion {{cookiecutter.project_name}}/docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
napoleon_include_init_with_doc = False
napoleon_use_rtype = True # having a separate entry generally helps readability
napoleon_use_param = True
myst_heading_anchors = 3 # create anchors for h1-h3
myst_heading_anchors = 6 # create anchors for h1-h6
myst_enable_extensions = [
"amsmath",
"colon_fence",
Expand Down
122 changes: 74 additions & 48 deletions {{cookiecutter.project_name}}/docs/template_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,78 +142,104 @@ Once authorized, pre-commit.ci should automatically be activated.

#### Overview of pre-commit hooks used by the template

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

- [black](https://black.readthedocs.io/en/stable/): standard code
formatter in Python.
- [isort](https://pycqa.github.io/isort/): sort module imports into
sections and types.
- [prettier](https://prettier.io/docs/en/index.html): standard code
formatter for non-Python files (e.g. YAML).
- [blacken-docs](https://github.com/asottile/blacken-docs): black on
python code in docs.

The following pre-commit checks are for errors and inconsistencies:

- [flake8](https://flake8.pycqa.org/en/latest/): standard check for errors in Python files.
- [flake8-tidy-imports](https://github.com/adamchainz/flake8-tidy-imports):
tidy module imports.
- [flake8-docstrings](https://github.com/PyCQA/flake8-docstrings):
pydocstyle extension of flake8.
- [flake8-rst-docstrings](https://github.com/peterjc/e8-rst-docstrings):
extension of `flake8-docstrings` for `rst` docs.
- [flake8-comprehensions](https://github.com/adamchainz/e8-comprehensions):
write better list/set/dict comprehensions.
- [flake8-bugbear](https://github.com/PyCQA/flake8-bugbear):
find possible bugs and design issues in program.
- [flake8-blind-except](https://github.com/elijahandrews/flake8-blind-except):
checks for blind, catch-all `except` statements.
- [yesqa](https://github.com/asottile/yesqa):
remove unneccesary `# noqa` comments, follows additional dependencies listed above.
- [autoflake](https://github.com/PyCQA/autoflake):
remove unused imports and variables.
- [pre-commit-hooks](https://github.com/pre-commit/pre-commit-hooks): generic pre-commit hooks.
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).

The following pre-commit hooks are for errors and inconsistencies:

- [ruff][]: Many configurable checks for Python code, see [Overview of Ruff checks](#overview-of-ruff-checks).
- [pre-commit-hooks](https://github.com/pre-commit/pre-commit-hooks): generic pre-commit hooks for text files.
- **detect-private-key**: checks for the existence of private keys.
- **check-ast**: check whether files parse as valid python.
- **end-of-file-fixer**:check files end in a newline and only a newline.
- **end-of-file-fixer**: check files end in a newline and only a newline.
- **mixed-line-ending**: checks mixed line ending.
- **trailing-whitespace**: trims trailing whitespace.
- **check-case-conflict**: check files that would conflict with case-insensitive file systems.
- [pyupgrade](https://github.com/asottile/pyupgrade):
- **forbid-to-commit**: Make sure that `*.rej` files cannot be commited.
These files are created by the [automated template sync](#automated-template-sync)
if there's a merge conflict and need to be addressed manually.

[ruff]: https://beta.ruff.rs/docs/

#### Overview of Ruff checks

[Ruff][] implements several checks for Python code and docstrings.
Therefore each check below mentions a the rule prefix that enables it when added to `pyproject.toml`.
E.g. `isort` is enabled with:

```toml
[tool.ruff]
select = [ ..., "I", ... ]
```

The following checks are for code style and format:
flying-sheep marked this conversation as resolved.
Show resolved Hide resolved

- [isort](https://beta.ruff.rs/docs/rules/#isort-i) (`I`):
sort module imports into sections and types.
- [pydocstyle](https://beta.ruff.rs/docs/rules/#pydocstyle-d) (`D`):
pydocstyle extension of flake8.
flying-sheep marked this conversation as resolved.
Show resolved Hide resolved
- [flake8-tidy-imports](https://beta.ruff.rs/docs/rules/#flake8-tidy-imports-tid) (`TID`):
tidy module imports.
- [flake8-comprehensions](https://beta.ruff.rs/docs/rules/#flake8-comprehensions-c4) (`C4`):
write better list/set/dict comprehensions.
- [pyupgrade](https://beta.ruff.rs/docs/rules/#pyupgrade-up) (`UP`):
upgrade syntax for newer versions of the language.
- **forbid-to-commit**: Make sure that `*.rej` files cannot be commited. These files are created by the
[automated template sync](#automated-template-sync) if there's a merge conflict and need to be addressed manually.

The following checks are for errors and inconsistencies:

- [pyflakes](https://beta.ruff.rs/docs/rules/#pyflakes-f) (`F`):
various checks for errors.
- [pycodestyle](https://beta.ruff.rs/docs/rules/#pycodestyle-e-w) (`E`, `W`):
various checks for errors.
- [flake8-bugbear](https://beta.ruff.rs/docs/rules/#flake8-bugbear-b) (`B`):
find possible bugs and design issues in program.
- [flake8-blind-except](https://beta.ruff.rs/docs/rules/#flake8-blind-except-ble) (`BLE`):
checks for blind, catch-all `except` statements.
- [Ruff-specific rules](https://beta.ruff.rs/docs/rules/#ruff-specific-rules-ruf) (`RUF`):
- `RUF100`: remove unneccesary `# noqa` comments.
flying-sheep marked this conversation as resolved.
Show resolved Hide resolved
See [How to ignore certain lint warnings](#how-to-ignore-certain-lint-warnings) for details.

### How to disable or add pre-commit checks

- To ignore lint warnigs from **flake8**, see [Ignore certain lint warnings](#how-to-ignore-certain-lint-warnings).
- To ignore lint warnigs from [Ruff][], see [How to ignore certain lint warnings](#how-to-ignore-certain-lint-warnings).
- You can add or remove pre-commit checks by simply deleting relevant lines in the `.pre-commit-config.yaml` file.
Some pre-commit checks have additional options that can be specified either in the `pyproject.toml` or tool-specific
config files, such as `.prettierrc.yml` for **prettier** and `.flake8` for **flake8**.

### How to ignore certain lint warnings

The [pre-commit checks](#pre-commit-checks) include [flake8](https://flake8.pycqa.org/en/latest/) which checks
The [pre-commit checks](#pre-commit-checks) include [Ruff][] which checks
for errors in Python files, including stylistic errors.

In some cases it might overshoot and you may have good reasons to ignore certain warnings.

To ignore an specific error on a per-case basis, you can add a comment `# noqa` to the offending line. You can also
specify the error ID to ignore, with e.g. `# noqa: E731`. Check the [flake8 guide][] for reference.
To ignore an specific error on a per-case basis, you can add a `# noqa: <rule>[, <rule>, …]` comment to the offending line.
Specify the rule ID(s) to ignore, with e.g. `# noqa: E731`. Check the [Ruff guide][] for reference.

Alternatively, you can disable certain error messages for the entire project. To do so, edit the `.flake8`
file in the root of the repository. Add one line per linting code you wish to ignore and don't forget to add a comment.
The `RUF100` check will remove `noqa` IDs that are no longer necessary.
If you want to add an ID that comes from a tool other than Ruff,
add it to Ruff’s [`external = [...]`](https://beta.ruff.rs/docs/settings/#external) setting to prevent `RUF100` from removing it.

You can also disable certain error messages for the entire project.
To do so, edit the `[tool.ruff]` section in `pyproject.toml` in the root of the repository.
Add the rule ID(s) you want to ignore and don't forget to add a comment explaining why.

```toml
...
# line break before a binary operator -> black does not adhere to PEP8
W503
# line break occured after a binary operator -> black does not adhere to PEP8
W504
...
ignore = [
...
# __magic__ methods are are often self-explanatory, allow missing docstrings
"D105",
...
]
```

[flake8 guide]: https://flake8.pycqa.org/en/3.1.1/user/ignoring-errors.html
[ruff guide]: https://beta.ruff.rs/docs/configuration/#suppressing-errors

### API design

Expand Down
Loading