Skip to content

Commit 965e5c4

Browse files
committed
Add: ruff setup and instructions on autoformatting with vscode
1 parent 5fd4bde commit 965e5c4

File tree

4 files changed

+102
-6
lines changed

4 files changed

+102
-6
lines changed

.pre-commit-config.yaml

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
# To run on all files: pre-commit run --all-files
1313

1414
ci:
15-
# pyosMeta disables autofixing of PRs to simplify new contributor experience and cleaner git history
15+
# We prefer to disable autofixing of PRs to simplify new contributor
16+
# We use the autofix as an option before we merge to run linting if a conributor
17+
# doesn't setup pre-commit (which can be confusing to new contributors)
1618
autofix_prs: false
1719
# Frequency of hook updates
18-
autoupdate_schedule: weekly
20+
autoupdate_schedule: quarterly
1921

2022
repos:
2123
# Out of the box hooks for pre-commit https://github.com/pre-commit/pre-commit-hooks

.readthedocs.yaml

-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ build:
1010
os: ubuntu-22.04
1111
tools:
1212
python: "3.12"
13-
# You can also specify other tool versions:
14-
# nodejs: "19"
15-
# rust: "1.64"
16-
# golang: "1.19"
1713

1814
# Build documentation in the "docs/" directory with Sphinx
1915
sphinx:
+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Linting and Code Formatting
2+
3+
In our [packaging guide](https://www.pyopensci.org/python-package-guide/package-structure-code/code-style-linting-format.html), we
4+
discuss various linters and code formatters used in the scientific Python
5+
ecosystem, including tools like:
6+
7+
* [black](https://github.com/psf/black)
8+
* [flake8](https://github.com/pycqa/flake8)
9+
* [isort](https://pycqa.github.io/isort/)
10+
11+
[Ruff](https://github.com/charliermarsh/ruff) is a tool that has been
12+
quickly becoming more popular. It is written in the Rust programming language
13+
and is fast. We like Ruff because it can perform both code formatting
14+
(similar to Black) and linting (similar to Flake8). This makes your setup
15+
simpler.
16+
17+
## Running Linters with pre-commit
18+
19+
There are a few ways to call the linters when you are working on your code.
20+
21+
One option is to use pre-commit hooks. [pre-commit](https://pre-commit.com/)
22+
runs any defined linters, code and text formatters, spellcheckers, and other
23+
tools on your code locally when you use `git commit` to make a change. For
24+
example:
25+
26+
```bash
27+
git commit -m "message here"
28+
```
29+
30+
By configuring pre-commit hooks, you can automatically run tools like Ruff and
31+
codespell on your code and documentation every time you commit. This ensures
32+
consistency and catches errors early.
33+
34+
While pre-commit is a powerful tool to add to your workflow, many do not like it:
35+
36+
* For beginner contributors, running pre-commit hooks can be confusing. You need
37+
to understand that each time you commit, it will run the checks. If it finds
38+
issues in your files, it will not actually commit your files to history. This
39+
can be confusing for even seasoned developers if they haven't used pre-commit
40+
before.
41+
* Some prefer to set up autoformatters that run every time you save a file in
42+
your preferred code editor (like VSCode). More on that below.
43+
44+
## Pre-commit.ci Bot
45+
46+
The [pre-commit CI bot](https://pre-commit.ci/) integrates with your GitHub
47+
repository to run pre-commit checks in an online continuous integration pipeline.
48+
The bot will run any pre-commit hooks that you have set up on new pull requests
49+
submitted to your repository.
50+
51+
Pre-commit.ci can be a nice tool to use for pull requests if set up correctly.
52+
Ideally, you can set pre-commit CI to run only when you call it in a pull
53+
request. This way, if you have a new contributor (or a seasoned one) who doesn't
54+
want to set up pre-commit locally, or someone who wants to submit a pull request
55+
(e.g., as a first contribution!) fully from the GitHub interface, you can enable
56+
the bot to run pre-commit checks and fixes in the pull request yourself, as a
57+
maintainer, before you merge the PR.
58+
59+
## Setting Up Autosave
60+
61+
TODO: More here on setting this up in VSCode and other tools.
62+
63+
# Setting Up Autosave for Ruff in VSCode and Spyder
64+
65+
## VSCode
66+
67+
1. Make sure you have the the Python extension for vscode installed:
68+
69+
1. **Install Ruff:**
70+
Ensure you have Ruff installed in your environment. You can install it using pip:
71+
72+
```bash
73+
pip install ruff
74+
```
75+
76+
1. Configure VSCode to run Ruff on save:
77+
78+
You can add this configuration to your workspace to ensure ruff is run on your
79+
files every time you edit and save them.
80+
81+
```json
82+
{
83+
"[python]": {
84+
"editor.formatOnSave": true,
85+
"editor.defaultFormatter": "charliermarsh.ruff",
86+
"editor.codeActionsOnSave": {
87+
"source.fixAll": "always",
88+
"source.organizeImports": "always",
89+
},
90+
},
91+
// TODO: figure out how the formatter is selected for notebooks
92+
"notebook.formatOnSave.enabled": true,
93+
}
94+
```
95+
96+
## Spyder
97+
98+
todo: add instructions here
File renamed without changes.

0 commit comments

Comments
 (0)