Skip to content

Commit a8dc7e1

Browse files
Merge pull request #5 from Advanced-Infrastructure/update-ruff-101
Add ruff, pre-commit usage details at AITL
2 parents c41506a + d9c9e06 commit a8dc7e1

1 file changed

Lines changed: 115 additions & 43 deletions

File tree

enforcing_ruff.md

Lines changed: 115 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@ You can install Ruff using `pip` or by adding it to your `requirements.txt` file
1313
pip install ruff
1414
```
1515

16-
Or, add the following line to `requirements.txt`:
17-
18-
```
19-
ruff==0.9.2
20-
```
21-
2216
### 2. Add `.ruff_cache` to `.gitignore`
2317
The `.ruff_cache` directory stores cache files related to Ruff. To avoid committing unnecessary files, add the following line to your `.gitignore`:
2418

@@ -30,27 +24,105 @@ The `.ruff_cache` directory stores cache files related to Ruff. To avoid committ
3024
Create a `ruff.toml` file in the root of your project and add the following configuration:
3125

3226
```toml
33-
# Target Python version
34-
[tool.ruff]
27+
# find default setting at: https://docs.astral.sh/ruff/configuration/
28+
29+
# Exclude a variety of commonly ignored directories.
30+
exclude = [
31+
".bzr",
32+
".direnv",
33+
".eggs",
34+
".git",
35+
".git-rewrite",
36+
".hg",
37+
".ipynb_checkpoints",
38+
".mypy_cache",
39+
".nox",
40+
".pants.d",
41+
".pyenv",
42+
".pytest_cache",
43+
".pytype",
44+
".ruff_cache",
45+
".svn",
46+
".tox",
47+
".venv",
48+
".vscode",
49+
"__pypackages__",
50+
"_build",
51+
"buck-out",
52+
"build",
53+
"dist",
54+
"node_modules",
55+
"site-packages",
56+
"venv",
57+
"test",
58+
"tests",
59+
"scripts",
60+
"test_flow",
61+
"common_utils",
62+
"common-utils",
63+
"infra_access",
64+
"ogr2ogr.py",
65+
]
66+
67+
# Same as Black.
68+
line-length = 88
69+
indent-width = 4
70+
71+
# Assume Python 3.13
3572
target-version = "py313"
3673

37-
# Enable additional linting rules
3874
[lint]
75+
# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default.
76+
# Unlike Flake8, Ruff doesn't enable pycodestyle warnings (`W`) or
77+
# McCabe complexity (`C901`) by default.
78+
select = ["E4", "E7", "E9", "F"]
3979
extend-select = [
4080
"UP", # pyupgrade
41-
"I", # isort
42-
"C90", # complexity
43-
"N", # pep8-naming
81+
"I", # isort
82+
"N", # pep8-naming
4483
"ASYNC", # flake8-async
45-
"S", # flake8-bandit
46-
"B", # flake8-bugbear
47-
"A", # flake8-builtins
48-
"C4", # flake8-comprehensions
84+
"S", # flake8-bandit
85+
"B", # flake8-bugbear
86+
"A", # flake8-builtins
87+
"C4", # flake8-comprehensions
88+
"PTH", # flake8-use-pathlib
89+
"DTZ", # flake8-datetimez
4990
]
91+
ignore = []
92+
93+
# Allow fix for all enabled rules (when `--fix`) is provided.
94+
fixable = ["ALL"]
95+
unfixable = []
96+
97+
# Allow unused variables when underscore-prefixed.
98+
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
99+
100+
[format]
101+
# Like Black, use double quotes for strings.
102+
quote-style = "double"
103+
104+
# Like Black, indent with spaces, rather than tabs.
105+
indent-style = "space"
50106

51-
# Ignore specific rules for test files
52-
[lint.per-file-ignores]
53-
"tests/*.py" = ["S101"] # Ignore assert statements in tests
107+
# Like Black, respect magic trailing commas.
108+
skip-magic-trailing-comma = false
109+
110+
# Like Black, automatically detect the appropriate line ending.
111+
line-ending = "auto"
112+
113+
# Enable auto-formatting of code examples in docstrings. Markdown,
114+
# reStructuredText code/literal blocks and doctests are all supported.
115+
#
116+
# This is currently disabled by default, but it is planned for this
117+
# to be opt-out in the future.
118+
docstring-code-format = false
119+
120+
# Set the line length limit used when formatting code snippets in
121+
# docstrings.
122+
#
123+
# This only has an effect when the `docstring-code-format` setting is
124+
# enabled.
125+
docstring-code-line-length = "dynamic"
54126
```
55127

56128
Refer to the following links for more details on Ruff rules and settings:
@@ -63,16 +135,22 @@ Once Ruff is installed and configured, run the following commands to check and f
63135

64136
```sh
65137
# Check for linting errors
66-
ruff check
138+
ruff check .
67139

68140
# Automatically fix fixable errors
69-
ruff check --fix
141+
ruff check . --fix
142+
143+
# check for a specific rule (say B: https://docs.astral.sh/ruff/rules/#flake8-bugbear-b)
144+
ruff check . --select="B"
145+
146+
# add a no quality assesment check flag to a line (use only if unavoidable)
147+
# use only for a specific file and a specific rule
148+
ruff check insert/file/path/ --select="B002" --add-noqa
70149

71150
# Format code according to standards
72151
ruff format
73152
```
74153

75-
> **ℹ️ Info:** The pre-commit step is WIP. Please don't follow it yet.
76154
### 5. Enforce Ruff in Pre-Commit Hooks
77155
To prevent committing unformatted or non-linted code, integrate Ruff with `pre-commit`.
78156

@@ -89,12 +167,16 @@ pip install pre-commit
89167
Add the following configuration to your `.pre-commit-config.yaml` file:
90168

91169
```yaml
92-
- repo: https://github.com/astral-sh/ruff-pre-commit
93-
rev: v0.1.4
94-
hooks:
95-
- id: ruff
96-
args: [ --fix ]
97-
- id: ruff-format
170+
repos:
171+
- repo: https://github.com/astral-sh/ruff-pre-commit
172+
# Ruff version.
173+
rev: v0.12.7
174+
hooks:
175+
# Run the linter.
176+
- id: ruff-check
177+
# Run the formatter.
178+
- id: ruff-format
179+
args: [--check]
98180
```
99181
100182
#### Install Pre-Commit Hooks
@@ -106,22 +188,12 @@ pre-commit install
106188
```
107189

108190
### 6. Handling Pre-Commit Failures
109-
If Ruff detects issues during a commit, it will prevent the commit until the issues are fixed. Follow these steps to resolve the errors:
191+
If pre-commit detects that ruff check and ruff format are not done during a commit, it will prevent the commit until the issues are fixed. Fix the ruff linting and formatting violations and try committing again.
110192

111-
1. Review the errors printed by Ruff.
112-
2. Run `ruff check --fix` to auto-fix issues.
113-
3. Run `ruff format` to format the code.
114-
4. Add the modified files to staging:
115-
116-
```sh
117-
git add .
118-
```
119-
120-
5. Retry committing:
121-
122-
```sh
123-
git commit -m "Fix linting issues"
124-
```
193+
You can check check if all pre-commit hooks are satisfied even before making the commit by running:
194+
```sh
195+
pre-commit run
196+
```
125197

126198
### 7. Integrate Ruff in IDEs
127199
To ensure code is formatted properly while writing, install the Ruff extension in your IDE:

0 commit comments

Comments
 (0)