Skip to content

Commit ffc45a2

Browse files
committed
✨ Template: add rules to Ruff linting ignore
Ruff, the tool Hatch uses under the hood, has an extensive list of rules: https://docs.astral.sh/ruff/rules/ Although most of them have a reasonable motivation, we might not agree with all of them for certain reasons. For example: https://docs.astral.sh/ruff/rules/magic-value-comparison/ would not permit comparing a certain input with a fixed value without first declaring a constant variable. That would not be sustainable in our code, leading to a lot of constant variable and making the code harder to understand. Here we add a set of rules to the Ruff ignore list, and document the motivation in the template developer docs.
1 parent f709b83 commit ffc45a2

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

template/docs/developer.md.jinja

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,14 @@ You can install the [pre-commit](https://pre-commit.com/) hooks with:
3333
Or run them via:
3434

3535
hatch run precommit:install
36+
37+
From the extensive [Ruff ruleset](https://docs.astral.sh/ruff/rules/) that Hatch uses, we ignore the following:
38+
39+
| Code | Rule | Rationale / Note |
40+
| --------- | ------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
41+
| `TRY003` | [raise-vanilla-args](https://docs.astral.sh/ruff/rules/raise-vanilla-args/) | Formatting warning/exception messages beforehand makes the code less readable, for a minor benefit in readability of the exception. |
42+
| `EM101` | [raw-string-in-exception](https://docs.astral.sh/ruff/rules/raw-string-in-exception/) | Same as `TRY003` |
43+
| `EM102` | [f-string-in-exception](https://docs.astral.sh/ruff/rules/f-string-in-exception/) | Same as `TRY003` |
44+
| `PLR2004` | [magic-value-comparison](https://docs.astral.sh/ruff/rules/magic-value-comparison/) | We have a lot of “magic values” to compare with in scientific code; naming them all would reduce readability for little benefit. |
45+
| `FBT002` | [boolean-default-value-positional-argument](https://docs.astral.sh/ruff/rules/boolean-default-value-positional-argument/) | We understand the concept, but adhering to this rule is not a small change in syntax; disable for now. |
46+
| `TID252` | [relative-imports](https://docs.astral.sh/ruff/rules/relative-imports/) | We don’t mind relative imports; as long as you don’t go up a level, they’re more readable (less verbose). |

template/pyproject.toml.jinja

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,13 @@ scripts.deploy = "mkdocs gh-deploy --force"
4848
dependencies = ["pre-commit"]
4949
scripts.install = "pre-commit install"
5050
scripts.run = "pre-commit run {args:--all-files}"
51+
52+
[tool.ruff]
53+
lint.ignore = [
54+
"TRY003", # https://docs.astral.sh/ruff/rules/raise-vanilla-args/
55+
"EM101", # https://docs.astral.sh/ruff/rules/raw-string-in-exception/
56+
"EM102", # https://docs.astral.sh/ruff/rules/f-string-in-exception/
57+
"PLR2004", # https://docs.astral.sh/ruff/rules/magic-value-comparison/
58+
"FBT002", # https://docs.astral.sh/ruff/rules/boolean-default-value-positional-argument/
59+
"TID252", # https://docs.astral.sh/ruff/rules/relative-imports/
60+
]

0 commit comments

Comments
 (0)