Skip to content
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

Pre-commit: add Ruff #261

Merged
merged 17 commits into from
Nov 20, 2024
Merged

Pre-commit: add Ruff #261

merged 17 commits into from
Nov 20, 2024

Conversation

edoardob90
Copy link
Member

Fixes #255

  • Replaces flake8 and all its enabled plugins with Ruff
  • Use Ruff formatter as a drop-in replacement of Black
  • Simplifies the configuration using a single pyproject.toml instead of multiple files (pytest.ini, flake8).

@edoardob90
Copy link
Member Author

edoardob90 commented Nov 14, 2024

There are a few errors to fix in the code (or to add to the ignore, while it's not the ideal way):

  • TRY003: Avoid specifying long messages outside the exception class
  • E741: Ambiguous variable name

@edoardob90 edoardob90 changed the title Pre-commit add ruff Pre-commit: add Ruff Nov 14, 2024
@Snowwpanda
Copy link
Collaborator

Thanks for starting this, since a change on flake8 pre-commit is not passing, so why not add ruff in one go. I fixed the places where pre-commit was not passing, don't understand enough about the rest though, so someone else should approve or I will read later.

pyproject.toml Outdated Show resolved Hide resolved
@edoardob90
Copy link
Member Author

I think this is good to go.

Just a comment on why we ignore B904 from the object-oriented tests: we do want to raise an assertion error if the check on the solution function attributes fails, but Python by default will raise a TypeError via vars(solution_result) if the result is not a class and therefore doesn't have a __dict__ attribute.

@edoardob90 edoardob90 requested a review from despadam November 18, 2024 12:23
@despadam
Copy link
Contributor

I think this is good to go.

Just a comment on why we ignore B904 from the object-oriented tests: we do want to raise an assertion error if the check on the solution function attributes fails, but Python by default will raise a TypeError via vars(solution_result) if the result is not a class and therefore doesn't have a __dict__ attribute.

Thanks for the explanation! Can we document this in a comment?

@despadam
Copy link
Contributor

I tried testing it and this is what happens in my case:

image

@edoardob90
Copy link
Member Author

I tried testing it and this is what happens in my case:

image

Seems okay to me. It's the "annoying" yaml formatter that keeps replacing 2 spaces with 4. Many editors are okay with 2. It happens to me as well. We could actually fix this...

@edoardob90
Copy link
Member Author

I think the problem with yaml formatting is fixed now.

@edoardob90
Copy link
Member Author

I think this is good to go.
Just a comment on why we ignore B904 from the object-oriented tests: we do want to raise an assertion error if the check on the solution function attributes fails, but Python by default will raise a TypeError via vars(solution_result) if the result is not a class and therefore doesn't have a __dict__ attribute.

Thanks for the explanation! Can we document this in a comment?

Good idea: I added it in 9bdfe5d

@despadam
Copy link
Contributor

I think the problem with yaml formatting is fixed now.

I keep having problems with YAML (check) and YAML (format). Check always complains about wrong new line character: expected \n, even after I fix it. Format always fails. Maybe I am doing something wrong? We can have a look together during tomorrow's meeting.

@Snowwpanda
Copy link
Collaborator

I tried testing it and this is what happens in my case:

image

Ah and here i thought this was just me, after seeing you have the same issue, this seems to be a known problem:
jumanjihouse/pre-commit-hook-yamlfmt#32
where windows path is messed up, i ran on codespaces (linux env) where it worked fine. but since some of us are using windows this isn't so nice.

@Snowwpanda
Copy link
Collaborator

I could fix the YAML formating using the pull request fix they have. Instead of
rev 0.2.3
i used
rev: 71af8556bfcf12745fdfdb3ed57b6698b444f58c
See here for the PR:
jumanjihouse/pre-commit-hook-yamlfmt#42
And the discussion:
jumanjihouse/pre-commit-hook-yamlfmt#25 (comment)
Not sure if this is a nice fix though, but i think its that or leave the hook out entirely

@edoardob90
Copy link
Member Author

I could fix the YAML formating using the pull request fix they have. Instead of rev 0.2.3 i used rev: 71af8556bfcf12745fdfdb3ed57b6698b444f58c See here for the PR: jumanjihouse/pre-commit-hook-yamlfmt#42 And the discussion: jumanjihouse/pre-commit-hook-yamlfmt#25 (comment) Not sure if this is a nice fix though, but i think its that or leave the hook out entirely

My memory didn't help me here: I already raised this problem last year. There were indeed a few users running the tutorial locally, and that means 99% of the times on Windows.

@edoardob90
Copy link
Member Author

Okay, I found an alternative formatter that should work on Windows as well. Please, Windows people, double check. I know it's marginally relevant since most people will use Codespaces/Binder/whatever.

@despadam
Copy link
Contributor

Yaml format works for me now, but Yaml lint still fails.

@edoardob90
Copy link
Member Author

edoardob90 commented Nov 20, 2024

@despadam @Snowwpanda As discussed, I removed yamllint. Everything works, including the formatting. Also, we have already a basic check-yaml hook built-in in pre-commit. I think it's enough for our purposes.

despadam
despadam previously approved these changes Nov 20, 2024
Copy link
Contributor

@despadam despadam left a comment

Choose a reason for hiding this comment

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

Works as expected now, thanks!

@edoardob90 edoardob90 merged commit 04ab64c into main Nov 20, 2024
1 check passed
@edoardob90 edoardob90 deleted the precommit-add-ruff branch November 20, 2024 10:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Ruff as a pre-commit hook?
4 participants