Add ruff docs and configuration #311
No reviewers
Labels
No labels
Closed As
Duplicate
Closed As
Fixed
Closed As
Invalid
discussions
easyfix
enhancement
task
ai-review-please
Backlog Status
Needs Review
Backlog Status
Ready
chore
documentation
points
01
points
02
points
03
points
05
points
08
points
13
pr2jira
Priority
Critical
Priority
High
Priority
Low
Priority
Medium
Sprint Status
Blocked
Sprint Status
Done
Sprint Status
In Progress
Sprint Status
Review
Sprint Status
To Do
Technical Debt
Work Item
Bug
Work Item
Epic
Work Item
Spike
Work Item
Task
Work Item
User Story
No milestone
No project
No assignees
3 participants
Notifications
Due date
No due date set.
Blocks
#306 Reformat codebase to satisfy CI linter check
quality/blockerbugs
Reference
quality/blockerbugs!311
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "feature/306-1-introducing-ruff"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@ -35,3 +35,3 @@steps:- name: Install Python, nodejs, git, and build dependenciesrun: dnf -y install nodejs git python3.11 python3.11-pip python3.11-setuptools-wheel python3.11-setuptools krb5-devel python3.11-devel gccrun: dnf -y install nodejs git python3.11 python3.11-pip python3.11-setuptools-wheel python3.11-setuptools krb5-devel python3.11-devel gcc sqlite-develwhy does this pr need sqlite-devel?
This fixes failing test suite to demonstrate that this PR is otherwise not breaking any tests.
@ -197,0 +202,4 @@isort, Black, pycodestyle, Pyflakes, and more) with a single, unified tool thatruns orders of magnitude faster. Ruff supports over 800 lint rules, canautomatically fix many of them, and also provides an opinionated code formattercompatible with Black.We don't need this blurb. A simple "This project uses
Ruff <https://docs.astral.sh/ruff/>_ for linting and formatting." would suffice.@ -197,0 +215,4 @@sudo dnf install ruffProject configurationthis section is way too long. Nobody wants to read a tedious LLM-generated readout of the config file. Reduce it to bits humans might be interested in, or, if the comments in the file itself are sufficient, remove this section entirely. Also no human would paste in a chunk of the config file that is commented to explain what it does, then explain the same things below in more tedious detail.
Well, I for one was not very familiar with ruff linter settings and welcomed a little refresher. But you are right about the duplicity.
That's the seductive nature of AI - whatever it generates may well be kinda interesting at the time. But we have to take a step back and say "yes, but does it belong here?" or else we'll wind up with doc files the size of fantasy novels.
Look at how many lines this one feature PR adds to the doc, now imagine if every feature PR did that, what would the doc look like? It's not sustainable.
I think we should just say that we're using Ruff, and that it's configured in
pyproject.toml. All config options can be documented inside that file (and most already are). This doc section feels like triplicating that info.Instead, let's include a link to Ruff config documentation inside
pyproject.toml, so that people can find out what a particular option means in detail.@ -197,0 +290,4 @@Editor integration------------------Ruff provides first-class editor extensions, giving you real-time diagnostics,Again, this is advertising copy. We don't need it.
@ -197,0 +293,4 @@Ruff provides first-class editor extensions, giving you real-time diagnostics,quick-fix actions, and format-on-save support.**VS Code**I'm kinda on the fence about the laundry list of IDEs here and in the blockerbugs devcontainers PR. On the one hand, sure, I guess it's useful? On the other hand, what is the end state of this approach, every software project in the world duplicates "how to set up your IDE" instructions for every IDE under the sun? I sort of feel like a generic "Ruff extensions are available for many IDEs, you should be able to find instructions with your favorite search engine or chatbot" or something would suffice?
This was sort of requested by Kamil, at least this is how I read it, just replaced git pre-commit hook by format-on-save:
But we can just put some pointers in there instead for sure.
yeah, I guess I'm saying make it a bit shorter and more generic rather than step-by-step instructions for every IDE in the world :D
And yes, I tend to write the code and make it work, commit it (locally, no push), then run the formatter. That way, I can just run
git diffand see exactly what the formatter did, and easily revert its changes if I don't like them and want to adjust something manually and run the formatter again, or whatever.Adam, Jaroslav, do you use any pre-commit/pre-push hook? Or rely just on manual execution, and/or IDE integration?
I think this section is fine, but I'd move it to
development_tasks.rstinstead. There are more guides like these, it fits there.If we can make it shorter, e.g. by linking somewhere where these instructions already exist, we certainly can do that. E.g. "Configuring VS Code" at https://marketplace.visualstudio.com/items?itemName=charliermarsh.ruff seems to contain similar information. But it's harder to follow (more information) and the section can't be linked directly, so I'm completely sure what's better.
I am usually happy with whatever formatter outputs and use format-on-save IDE feature. However this is only useful for new or already preformatted codebases. When working on existing code base like this I either disable formatter completely or just format new code parts manually (VSCode supports formatting just the selected lines).
It is up for a debate whether to enforce formatting/linting strictly using Forge pre-commit check or just make a quick check using CI task. As for me CI task is good enough.
Ok, I think I'd also avoid hooks at this moment. It can always be added later.
78f77f2d2ee85b5fbc33I extracted the test fix from this PR and merged it, so we aren't starting from a base of broken tests. I've rebased this branch.
@ -197,0 +209,4 @@Install Ruff into your virtualenv::pip install ruffThis is already done in a previous step
pip install -r requirements-development.txt. I think we can drop the install section.@ -197,0 +273,4 @@Check for lint issues::ruff check .I think the dot is not needed in any of these examples
@ -197,0 +279,4 @@ruff check --fix .Format code (equivalent to Black)::Let's also say e.g. "Make sure to commit your changes first, if you want to inspect the diff".
@ -197,0 +285,4 @@Check formatting without modifying files::ruff format --check .I guess
ruff format --diffis more useful here?--checkseems useful for pre-commit hooks, etc.@ -1,6 +1,29 @@[tool.black]line-length = 100[tool.ruff]We need to exclude some paths, probably using extend-exclude. I think we don't want to cover these:
@ -4,0 +18,4 @@"C4", # flake8-comprehensions (unnecessary list/dict/set wrappers)"SIM", # flake8-simplify (simplifiable code patterns)"RUF", # Ruff-specific rules (unused noqa, mutable class defaults, etc.)]We're enabling quite a lot of rules here. I'm a bit concerned about it. The official guide says:
https://docs.astral.sh/ruff/rules/
The default value is:
Do you have personal experience with these? I'd rather start with known non-problematic defaults (maybe include
isorton top of it), and once we get more experience with it, we can increase the scope and see it if works well for us or causes some issues somewhere. If we start with expert mode from the beginning, I'm afraid it might be overwhelming.I think including a rule set that's approx equivalent to flake8 is reasonable. I found the output of this rule set mostly fine, which implies the rule set itself is OK, but I didn't really look at the rules themselves in detail...
Jaroslav already reverted this to default, which is, I believe, a better way to start. But just to clarify - ruff defaults just to a couple specific rules from the
Ecategory, and the wholeFcategory. If you enable preview, which is "a collection of unstable features", you receive "an expanded set of default rules that includes rules from the B, UP, and RUF categories, as well as many more". But this went far beyond that, this enabled everything fromE,F,W,I,B,UP,C4,SIMandRUF. If you look into those categories, there are lots of rules which are marked as unstable. I don't think this is a reasonable start.@ -4,0 +19,4 @@"SIM", # flake8-simplify (simplifiable code patterns)"RUF", # Ruff-specific rules (unused noqa, mutable class defaults, etc.)]ignore = []This is the default value, so we can skip this line.
I made some tweaks. I would also adjust
ci.ymlto simply run./run lintinside the linting phase (I feel that it would be great if the same exact commands would apply to both local development and CI), but I'm not sure how to override the default python version to 3.11. Thoughts? (On how to do it or whether to delay it to a future PR?)Do we still need to run
flake8during linting?ruffis supposed to replace that and it has flake8 rules enabled even in the default config.ruff has python version specified in
pyproject.tomlusingtarget-version = "py311"directive.That's a good question. Here's the output that flake8 currently reports but is not captured by ruff in a default config:
All of these will be resolved with
ruff format, so I think we can drop flake8. I'll add a commit.That's good. But we still run
mypyfrom the script as well. And it would also be nice to be able to run./run test(the same way as in our docs) to execute the test suite. I guess we need to figure out some envvar, if we want to do this.@kparal wrote in #311 (comment):
I think you should be able to specify python version for
mypyinpyproject.tomlas well:Regarding testing I guess you could use
pyenvfor that or use devcontainers.@jgroman wrote in #311 (comment):
Offloaded it to #315
This is now thumbs up from me. If I get another thumbs up, I'll squash a merge it.
LGTM
a4186d5b443d54a63596