Skip to content

MNT Enforce ruff rules (RUF) #30694

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

Merged
merged 9 commits into from
Mar 18, 2025
Merged

Conversation

DimitriPapadopoulos
Copy link
Contributor

What does this implement/fix? Explain your changes.

Enforce Ruff-specific rules (RUF) rules.

Any other comments?

Some rules have been disabled: either they don't seem that interesting, or they change too much of the codebase.

[doc skip]

Copy link

github-actions bot commented Jan 21, 2025

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: 0aec43b. Link to the linter CI: here

@DimitriPapadopoulos DimitriPapadopoulos changed the title MAINT Enforce ruff rules (RUF) MNT Enforce ruff rules (RUF) Jan 21, 2025
@jeremiedbb
Copy link
Member

Thanks for the PR @DimitriPapadopoulos. These rules mostly summarize to ordering __all__ and properly deal with str and repr in fstrings. I'm okay with that. Some comments though

  • I checked enabling RUF021 and there was no error. Maybe we can remove it from the exceptions ? This rule is "Parenthesize a and b expressions when chaining and and or together, to make the precedence clear" which I think is fine to enforce.

  • RUF015 seems good to have imo. I always find these list(some_generator)[0] a bit clunky. I checked enabling it and there are not many occurrences to fix so maybe worth to remove it from the exceptions ?

  • How come we can remove all these noqa ?

@DimitriPapadopoulos
Copy link
Contributor Author

DimitriPapadopoulos commented Jan 23, 2025

Of course, I can enable RUF015 and RUF021. Some maintainers don't like them, so I tend not to apply them.

The useless noqa must have been left by previous linters or versions of ruff. Some ruff rules have been deprecated over time. Also, the maximum line length might have changed. Or perhaps reformatting with black reduces line length.

@DimitriPapadopoulos DimitriPapadopoulos force-pushed the RUF branch 2 times, most recently from ce9a5d8 to 61501d4 Compare January 23, 2025 13:42
@lesteve
Copy link
Member

lesteve commented Jan 28, 2025

@DimitriPapadopoulos I think you have contributed useful stuff in the past to the project, thanks a lot 🙏!

In this case it looks like you opened multiple PRs on separate aspects on cleaning up/maintenance. Sometimes splitting PRs is good, and sometimes as a busy maintainer it can feel a bit too much.

I would suggest you keep only one PR open and close the others, the one you think is the most important, wait for feed-back, iterate until it is merged, and then open another PR ...

My personal opinion on this kind of PRs, is that they don't have huge benefits and they tend to be controversial. People will have different opinions on whether it is worth it or not, and at the end significant reviewer and contributor time has been spent on something that probably was not so crucial for the project and this time could have been invested somewhere else with a more useful outcome ...

@DimitriPapadopoulos
Copy link
Contributor Author

@lesteve Other maintainers insist on smaller PRs - tastes and colors.

While it's possible to discuss individual rules, linters do result in more maintainable code overall, and sometimes find actual bugs. Perhaps the benefit is not huge, but the cost is small. As for the controversial part of rules, choosing a mainstream linter (or formatter) gets controversy out of the way.

As for closing the PRs, now that they're already opened, I don't know.

warning: Invalid `# noqa` directive: expected `:` followed by a comma-separated list of codes (e.g., `# noqa: F401, F841`).
RUF010 Use explicit conversion flag
RUF015 Prefer `next(...)` over single element slice
RUF017 Avoid quadratic list summation
RUF021 Parenthesize `a and b` expressions when chaining
       `and` and `or` together, to make the precedence clear
RUF022 `__all__` is not sorted
RUF100 Unused blanket `noqa` directive
RUF002 Docstring contains ambiguous ` ` (NO-BREAK SPACE). Did you mean ` ` (SPACE)?
Copy link
Member

@thomasjpfan thomasjpfan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a reviewer, it is a lot of different types of changes to go through. I think if we want to enforce more linting rules, it's best to introduce them one at a time. i.e. One small PR on sorting __all__, one on removing unncessary # noqa, etc and slowly introduce them one at a time.

In any case, given we already have this PR, I'm okay with enforcing the proposed rules.

@@ -341,7 +341,7 @@ def euclidean_distances(

Notes
-----
To achieve a better accuracy, `X_norm_squared` and `Y_norm_squared` may be
To achieve a better accuracy, `X_norm_squared` and `Y_norm_squared` may be
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can not tell what this change is. A unicode character?

Copy link
Contributor Author

@DimitriPapadopoulos DimitriPapadopoulos Mar 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With a single commit per rule, there's the workaround of reviewing the commits individually.

In this case, I manually changed a NO-BREAK SPACE U+00A0 into a regular SPACE U+0020 in 0aec43b, to silence ambiguous-unicode-character-docstring (RUF002).

Copy link
Member

@adrinjalali adrinjalali left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. You need to add a this to .git-blame-ignore-revs after merging this.

@adrinjalali adrinjalali merged commit fe7c417 into scikit-learn:main Mar 18, 2025
36 of 37 checks passed
@DimitriPapadopoulos
Copy link
Contributor Author

DimitriPapadopoulos commented Mar 18, 2025

LGTM. You need to add a this to .git-blame-ignore-revs after merging this.

Do you expect me to add the commits to .git-blame-ignore-revs in an upcoming PR? I could do that for all recent ruff-related PRs.

Note however that while these are mostly automatic ruff fixes, I manually fixed some issues.

@DimitriPapadopoulos DimitriPapadopoulos deleted the RUF branch March 18, 2025 11:56
@adrinjalali
Copy link
Member

Yes, we prefer to completely ignore styling changes to keep the blame relevant in case we need to know who worked on a feature.

DimitriPapadopoulos added a commit to DimitriPapadopoulos/scikit-learn that referenced this pull request Mar 19, 2025
* Enforce ruff rules (RUF) (scikit-learn#30694)
* Apply ruff/flake8-implicit-str-concat rules (ISC) (scikit-learn#30695)
DimitriPapadopoulos added a commit to DimitriPapadopoulos/scikit-learn that referenced this pull request Mar 19, 2025
* Enforce ruff rules (RUF) (scikit-learn#30694)
* Apply ruff/flake8-implicit-str-concat rules (ISC) (scikit-learn#30695)
DimitriPapadopoulos added a commit to DimitriPapadopoulos/scikit-learn that referenced this pull request Mar 24, 2025
* Enforce ruff rules (RUF) (scikit-learn#30694)
* Apply ruff/flake8-implicit-str-concat rules (ISC) (scikit-learn#30695)
DimitriPapadopoulos added a commit to DimitriPapadopoulos/scikit-learn that referenced this pull request Apr 15, 2025
* Enforce ruff rules (RUF) (scikit-learn#30694)
* Apply ruff/flake8-implicit-str-concat rules (ISC) (scikit-learn#30695)
DimitriPapadopoulos added a commit to DimitriPapadopoulos/scikit-learn that referenced this pull request Apr 15, 2025
* Enforce ruff rules (RUF) (scikit-learn#30694)
* Apply ruff/flake8-implicit-str-concat rules (ISC) (scikit-learn#30695)
* black → ruff format (scikit-learn#31015)
DimitriPapadopoulos added a commit to DimitriPapadopoulos/scikit-learn that referenced this pull request Apr 15, 2025
* Enforce ruff rules (RUF) (scikit-learn#30694)
* Apply ruff/flake8-implicit-str-concat rules (ISC) (scikit-learn#30695)
* black → ruff format (scikit-learn#31015)
DimitriPapadopoulos added a commit to DimitriPapadopoulos/scikit-learn that referenced this pull request Apr 15, 2025
* Enforce ruff rules (RUF) (scikit-learn#30694)
* Apply ruff/flake8-implicit-str-concat rules (ISC) (scikit-learn#30695)
* black → ruff format (scikit-learn#31015)
DimitriPapadopoulos added a commit to DimitriPapadopoulos/scikit-learn that referenced this pull request Apr 15, 2025
* Enforce ruff rules (RUF) (scikit-learn#30694)
* Apply ruff/flake8-implicit-str-concat rules (ISC) (scikit-learn#30695)
* black → ruff format (scikit-learn#31015)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants