-
Notifications
You must be signed in to change notification settings - Fork 4
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
Add: ruff setup and instructions on autoformatting with vscode #34
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you still know someone who downloads PDFs of documentation these days? I'd be curious about usage numbers over all of readthedocs. I don't enable PDF on my projects any longer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok i'm responding to these comments in this closed PR (which i know is confusing!!). This is a great question!! |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,98 @@ | ||||||||||||||||||||||||||||||
# Linting and Code Formatting | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe add some discussion on hatch fmt (which only runs ruff). We want to run codespell and markdownlint and others potentially. What is the best way to do this? https://hatch.pypa.io/latest/config/internal/static-analysis/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I looked into using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok this makes sense. so we should for now, not use that. how does jupyter handle linting or where do you all land? |
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
In our [packaging guide](https://www.pyopensci.org/python-package-guide/package-structure-code/code-style-linting-format.html), we | ||||||||||||||||||||||||||||||
discuss various linters and code formatters used in the scientific Python | ||||||||||||||||||||||||||||||
ecosystem, including tools like: | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
* [black](https://github.com/psf/black) | ||||||||||||||||||||||||||||||
* [flake8](https://github.com/pycqa/flake8) | ||||||||||||||||||||||||||||||
* [isort](https://pycqa.github.io/isort/) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
[Ruff](https://github.com/charliermarsh/ruff) is a tool that has been | ||||||||||||||||||||||||||||||
quickly becoming more popular. It is written in the Rust programming language | ||||||||||||||||||||||||||||||
and is fast. We like Ruff because it can perform both code formatting | ||||||||||||||||||||||||||||||
(similar to Black) and linting (similar to Flake8). This makes your setup | ||||||||||||||||||||||||||||||
simpler. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
## Running Linters with pre-commit | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
There are a few ways to call the linters when you are working on your code. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
One option is to use pre-commit hooks. [pre-commit](https://pre-commit.com/) | ||||||||||||||||||||||||||||||
runs any defined linters, code and text formatters, spellcheckers, and other | ||||||||||||||||||||||||||||||
tools on your code locally when you use `git commit` to make a change. For | ||||||||||||||||||||||||||||||
example: | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
```bash | ||||||||||||||||||||||||||||||
git commit -m "message here" | ||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
By configuring pre-commit hooks, you can automatically run tools like Ruff and | ||||||||||||||||||||||||||||||
codespell on your code and documentation every time you commit. This ensures | ||||||||||||||||||||||||||||||
consistency and catches errors early. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
While pre-commit is a powerful tool to add to your workflow, many do not like it: | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
* For beginner contributors, running pre-commit hooks can be confusing. You need | ||||||||||||||||||||||||||||||
to understand that each time you commit, it will run the checks. If it finds | ||||||||||||||||||||||||||||||
issues in your files, it will not actually commit your files to history. This | ||||||||||||||||||||||||||||||
can be confusing for even seasoned developers if they haven't used pre-commit | ||||||||||||||||||||||||||||||
before. | ||||||||||||||||||||||||||||||
Comment on lines
+36
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed this locally! |
||||||||||||||||||||||||||||||
* Some prefer to set up autoformatters that run every time you save a file in | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We've mitigated this in some projects by relying on standard config as much as possible so that the scripts/IDEs can be run independently of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so helpful - i think the same question as above - could you perhaps direct me to an example of how you run linters / formatter? i could see a hatch lint that we create or... ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for this @blink1073 !! |
||||||||||||||||||||||||||||||
your preferred code editor (like VSCode). More on that below. | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed! |
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
## Pre-commit.ci Bot | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
The [pre-commit CI bot](https://pre-commit.ci/) integrates with your GitHub | ||||||||||||||||||||||||||||||
repository to run pre-commit checks in an online continuous integration pipeline. | ||||||||||||||||||||||||||||||
The bot will run any pre-commit hooks that you have set up on new pull requests | ||||||||||||||||||||||||||||||
submitted to your repository. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
Pre-commit.ci can be a nice tool to use for pull requests if set up correctly. | ||||||||||||||||||||||||||||||
Ideally, you can set pre-commit CI to run only when you call it in a pull | ||||||||||||||||||||||||||||||
request. This way, if you have a new contributor (or a seasoned one) who doesn't | ||||||||||||||||||||||||||||||
want to set up pre-commit locally, or someone who wants to submit a pull request | ||||||||||||||||||||||||||||||
(e.g., as a first contribution!) fully from the GitHub interface, you can enable | ||||||||||||||||||||||||||||||
the bot to run pre-commit checks and fixes in the pull request yourself, as a | ||||||||||||||||||||||||||||||
maintainer, before you merge the PR. | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One thing to bear in mind with this is that when the bot pushes a fix, contributors need to pull down that fix before making further changes, which can be confusing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh right - good point! Would being a bit stronger about the language - only run pre-commit right before you merge the PR help clarify? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I meant if the bot itself pushes a fix it can be confusing, which I don't think can be configured to run on demand. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AHHH ok. i do use the bot on demand - even in our repos here at pyos. you can set the bot up to NOT autoformat and just check the repo. then when ready, use my preferred workflow is setup precommit. the bot can't fix spelling errors. what i'm trying to figure out here that i wonder if you / jupyter has solved is how should we suggest people run linters if pre-commit is confusing. (i understand it is confusing!) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remember also the security issues that @ofek mentioned on Slack. I think, that's important to note here. |
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
## Setting Up Autosave | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
TODO: More here on setting this up in VSCode and other tools. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
# Setting Up Autosave for Ruff in VSCode and Spyder | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
## VSCode | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
1. Make sure you have the the Python extension for vscode installed: | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
1. **Install Ruff:** | ||||||||||||||||||||||||||||||
Ensure you have Ruff installed in your environment. You can install it using pip: | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
```bash | ||||||||||||||||||||||||||||||
pip install ruff | ||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
1. Configure VSCode to run Ruff on save: | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
You can add this configuration to your workspace to ensure ruff is run on your | ||||||||||||||||||||||||||||||
files every time you edit and save them. | ||||||||||||||||||||||||||||||
Comment on lines
+78
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Important to mention that there is also a UI for this. Open your settings, e.g., |
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
```json | ||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||
"[python]": { | ||||||||||||||||||||||||||||||
"editor.formatOnSave": true, | ||||||||||||||||||||||||||||||
"editor.defaultFormatter": "charliermarsh.ruff", | ||||||||||||||||||||||||||||||
"editor.codeActionsOnSave": { | ||||||||||||||||||||||||||||||
"source.fixAll": "always", | ||||||||||||||||||||||||||||||
"source.organizeImports": "always", | ||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||
// TODO: figure out how the formatter is selected for notebooks | ||||||||||||||||||||||||||||||
"notebook.formatOnSave.enabled": true, | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Ruff VSCode Details page shows: {
"notebook.formatOnSave.enabled": true,
"notebook.codeActionsOnSave": {
"notebook.source.fixAll": "explicit",
"notebook.source.organizeImports": "explicit"
},
"[python]": {
"editor.defaultFormatter": "charliermarsh.ruff"
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thank you!! |
||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
## Spyder | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
Autoformat with ruff is not yet a Spyder feature. See [this issue](https://github.com/spyder-ide/spyder/issues/21357) for discussions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm in doubt about enabling auto update on a schedule. Wouldn't you want every contributor to run the same hook versions as in the main project branch? In my view, it's up to the maintainer to bump hook versions.