-
-
Notifications
You must be signed in to change notification settings - Fork 158
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
Use ruff
to also sort imports
#3237
base: main
Are you sure you want to change the base?
Conversation
@@ -96,6 +96,9 @@ exclude = [ | |||
"setup.py", | |||
] | |||
|
|||
[tool.ruff.lint] | |||
fixable = ["I"] |
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 don't think this is necessary for the pre-commit. It should probably be select = ["I"]
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.
This will be necessary later when further configuring ruff as the linter
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.
Okay. Could you elaborate more?
- reason for using
fixable
and notselect
- reason it is necessary to include
fixable = ["I"]
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.
As explained in my other comment, this is not where we should configure linting rules using select
because we may want to add other rules on top and then it would lint those rules during the pre-commit
run, which we don't want because it is meant purely for formatting the code. So, we override the entire select
for formatting using the CLI so that it only cares about that rule and also only fixes that rule. The fixable
config won't have any impact on the pre-commit
runs in this configuration.
It's not strictly necessary to include the fixable
config, but it might help if someone runs ruff check --fix
because it won't create a massive diff that they did not want to cause with that command. It's a guard rail in a sense.
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.
If select
has no impact on this pre-commit
run because of the --select
option, then we should include select
.
With your current changes, the ruff select list implicitly is this (the ruff default, as we do not set it):
select = ["E4", "E7", "E9", "F"]
When the user uses ruff check
and ruff check --fix
, we only want it to warn them about the rules that we use (in this case, only isort), so that they may fix it before the pre-commit
fixes it (including for isort 'formatting').
Rather than restricting just what is fixed, we should restrict what is checked.
What is fixed is always a subset of what is checked, so it is erroneous to include isort in fixable
without including it in select
. With the current changes, ruff check [--fix]
will not detect or fix import sorting because the rule is not selected.
fixable = ["I"] | |
select = ["I"] |
Ruff considers isort to be linting, so we must configure linting rules even if we consider it formatting.
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.
Fair enough, I get what you're saying, if a contributor runs ruff check
themselves, this would check those default rules and if they did run ruff check --fix
, it would not fix anything at all (which is certainly not the expected behavior, since sorting the imports is part of what is expected).
I'm not going to remove the fixable = ["I"]
configuration though, I don't see the need to do that and again, we're likely to move to ruff as the linter, so, I would like to avoid the case that it gets released without this fixable rule restriction and fixes rules that contributors may not expect it to fix.
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 see your reasoning with fixable
, but I slightly disagree with it, drawing from my experience using ruff
. If we introduce more linting rules in the future, then it means they are enforced within the pygame-ce source, and contributors should be able to fix it.
fixes rules that contributors may not expect it to fix
A contributor would actually expect basically all auto-fixable rules to be fixed, the default. This generally doesn't break anything either, ruff is very careful with auto-fixing.
The intended purpose of fixable
and unfixable
is to allow the user to manually correct certain rules in case the auto-fix for it may break something. (Of course, many rules in ruff don't support auto-fix in the first place.)
Considering this, we would instead use the more common unfixable
list instead of fixable
list, so exceptions are for the excluded instead of the included. (The default for fixable
is "ALL"
, then we narrow it down.)
Just a capability that was not being utilized (it was done before when we were using
black
+isort
)From
ruff
docs: