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

Use ruff formatter and pre-commit #2524

Merged
merged 25 commits into from
Sep 26, 2024
Merged

Use ruff formatter and pre-commit #2524

merged 25 commits into from
Sep 26, 2024

Conversation

bouweandela
Copy link
Member

@bouweandela bouweandela commented Sep 12, 2024

Description

Use ruff to format the Python code and pre-commit to manage formatting. Enables pre-commit.ci to check that code has been formatted correctly.

These new tools replace yapf, isort and flake8 (pyflakes and pycodestyle). I have dropped docformatter from the pre-commit hooks because it makes changes to docstrings that do not always look good (e.g. it will happily cut a sentence into two pieces to make the first half of the sentence fit on the first line of the docstring).

Please install the pre-commit hooks by going to the directory where you have checked out ESMValCore and running

pre-commit install

To format your code (and check for pycodestyle, pyflakes, and mypy issues) run:

pre-commit run -a

This will also automatically be done (on changed files only) whenever you commit your changes.

See ESMValGroup/ESMValTool#2161

Link to documentation: https://esmvaltool--2524.org.readthedocs.build/projects/ESMValCore/en/2524/contributing.html#code-quality

Git branch upgrade instructions (once this has been merged)

To upgrade your existing git branches and open pull requests to the new formatting once this pull request has been merged into the main branch, the following procedure is recommended to minimize merge conflicts:

Step 1: reformat changed Python files with ruff, this is needed to minimize merge conflicts in the next step

git checkout your-branch
pip install ruff
ruff format --config line-length=79 $(git diff --name-only origin/main... '*.py')
git add -u
git commit -m 'Apply ruff formatting to changed files' -n

Step 2: merge the main branch into your branch

git pull origin main --no-ff
# edit code to address merge conflicts, use `git add path/to/file.py` to mark as solved and then run:
git commit -n

Step 3: start using pre-commit to format and check your code

pre-commit install
pre-commit run -a
# edit code to fix any issues that ruff did not solve automatically
git add -u
git commit -m 'Fix linter issues'
git push

To test these instructions before this has been merged, you can replace main with ruff-format (this branch) in the commands above.


Before you get started

Checklist

It is the responsibility of the author to make sure the pull request is ready to review. The icons indicate whether the item will be subject to the 🛠 Technical or 🧪 Scientific review.


To help with the number pull requests:

@valeriupredoi
Copy link
Contributor

holy mackerel 🐟

Copy link

codecov bot commented Sep 13, 2024

Codecov Report

Attention: Patch coverage is 91.63498% with 176 lines in your changes missing coverage. Please review.

Project coverage is 94.83%. Comparing base (796a785) to head (9208d58).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
esmvalcore/_task.py 65.55% 31 Missing ⚠️
esmvalcore/_citation.py 70.27% 11 Missing ⚠️
esmvalcore/_main.py 82.53% 11 Missing ⚠️
esmvalcore/experimental/recipe_output.py 79.06% 9 Missing ⚠️
esmvalcore/experimental/recipe_info.py 68.00% 8 Missing ⚠️
esmvalcore/preprocessor/_derive/lwp.py 0.00% 8 Missing ⚠️
esmvalcore/_recipe/check.py 85.10% 7 Missing ⚠️
esmvalcore/preprocessor/_derive/vegfrac.py 0.00% 6 Missing ⚠️
esmvalcore/cmor/_fixes/ipslcm/ipsl_cm6.py 0.00% 4 Missing ⚠️
esmvalcore/cmor/_fixes/native6/era5.py 91.66% 4 Missing ⚠️
... and 41 more
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2524   +/-   ##
=======================================
  Coverage   94.83%   94.83%           
=======================================
  Files         251      251           
  Lines       14191    14191           
=======================================
  Hits        13458    13458           
  Misses        733      733           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bouweandela bouweandela changed the title Use ruff formatter Use ruff formatter and pre-commit Sep 18, 2024
@valeriupredoi
Copy link
Contributor

seems it doesn't like Python 3.10 both Linux and OSX, lemme have a look at that, bud

@bouweandela
Copy link
Member Author

Thanks! I think codespell needed an extra dependency to read the configuration file in toml format on Python 3.10. Added in fe3ff7b.

This reverts commit 901439d.
@valeriupredoi
Copy link
Contributor

Thanks! I think codespell needed an extra dependency to read the configuration file in toml format on Python 3.10. Added in fe3ff7b.

🐈‍⬛ 😃

@bouweandela bouweandela marked this pull request as ready for review September 18, 2024 13:07
@bouweandela bouweandela requested a review from a team September 18, 2024 13:12
@valeriupredoi
Copy link
Contributor

are we going to accept ruffly 92% coverage as the new project coverage?

@bouweandela
Copy link
Member Author

bouweandela commented Sep 18, 2024

are we going to accept ruffly 92% coverage as the new project coverage?

My idea was to assume that reformatting is a safe operation and does not introduce bugs.. writing tests for all the lines missing coverage seems a considerable amount of work.

@valeriupredoi
Copy link
Contributor

are we going to accept ruffly 92% coverage as the new project coverage?

My idea was to assume that reformatting is a safe operation and does not introduce bugs.. writing tests for all the lines missing coverage seems a considerable amount of work.

oh for sure! But I am confused as to why the coverage drops vs the original codebase, since this PR only reformats code, and doesn't introduce any hefty functionality 😕

@bouweandela
Copy link
Member Author

It's only the diff coverage that is partial, if you click the little arrow near the bottom of the Codecov comment where it says "Additional details and impacted files" it says that overall coverage is unchanged.

.circleci/config.yml Show resolved Hide resolved
.github/workflows/create-condalock-file.yml Outdated Show resolved Hide resolved
.github/workflows/create-condalock-file.yml Outdated Show resolved Hide resolved
.yamllint Show resolved Hide resolved
@valeriupredoi
Copy link
Contributor

It's only the diff coverage that is partial, if you click the little arrow near the bottom of the Codecov comment where it says "Additional details and impacted files" it says that overall coverage is unchanged.

ah always found these confusing af, thanks, bud! Phew 😮‍💨

Copy link
Contributor

@valeriupredoi valeriupredoi left a comment

Choose a reason for hiding this comment

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

really hope you didn't have to do all these changes manually, bud 🤣 Cheers muchly! We have to start thinking how we do that in ESMValTool - gonna be messy. What we doing about pull requests after we merge this - they'll be conflicting like mad I imagine

Copy link
Contributor

@valeriupredoi valeriupredoi left a comment

Choose a reason for hiding this comment

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

last changes are ruffling no feathers 🪶

Copy link
Contributor

@schlunma schlunma left a comment

Choose a reason for hiding this comment

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

I love it!! ❤️ This makes our code so much cleaner.

Two comments:

  1. Does pre-commit.ci make any changes to the code? Or does it only check the code?
  2. Should we consider tackling some of the Codacy issues here? Looks like most of them are very easy to fix. Just a suggestion, don't want to put extra work on you.

Thanks so much!!

full: true
full: true
# ignore rules that conflict with ruff formatter
disable: ['E203', 'E501', 'W503']
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there some kind of reference that you could add as a comment here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done in 7b0f8cd

@bouweandela
Copy link
Member Author

Does pre-commit.ci make any changes to the code? Or does it only check the code?

It is possible to configure pre-commit.ci such that it will automatically fix issues and push a commit to your branch. However, I disabled it for now because I am concerned that this may cause trouble for some of our contributors if they forget to install the pre-commit hooks and then pre-commit.ci commits to their branch, they continue editing and add some more commits, try to git push them, and find that they cannot because the branch on GitHub conflicts with their local branch. Of course, they could git push --force, but they may not be familiar enough with git to realize that.

Should we consider tackling some of the Codacy issues here? Looks like most of them are very easy to fix. Just a suggestion, don't want to put extra work on you.

I would prefer not to make any more changes here because there are so many already that it is almost impossible to review. ruff can also automatically fix many issues, so if we enable more ruff rules over time, many of these issues will probably be automatically fixed.

@valeriupredoi
Copy link
Contributor

Does pre-commit.ci make any changes to the code? Or does it only check the code?

It is possible to configure pre-commit.ci such that it will automatically fix issues and push a commit to your branch. However, I disabled it for now because I am concerned that this may cause trouble for some of our contributors if they forget to install the pre-commit hooks and then pre-commit.ci commits to their branch, they continue editing and add some more commits, try to git push them, and find that they cannot because the branch on GitHub conflicts with their local branch. Of course, they could git push --force, but they may not be familiar enough with git to realize that.

Should we consider tackling some of the Codacy issues here? Looks like most of them are very easy to fix. Just a suggestion, don't want to put extra work on you.

I would prefer not to make any more changes here because there are so many already that it is almost impossible to review. ruff can also automatically fix many issues, so if we enable more ruff rules over time, many of these issues will probably be automatically fixed.

fully support these two from Bouwe. We may, however, need to turn on auto-pre-commit commits done by Circle (as Bouwe had it turned on for a bit until I barked at him to turn it off), for some of the older PRs that will need pre-commit be run on them, and the contributor may not be doing it or be weary/scared/not know how to do it (I reckon those aren't many, since most Core devs know their stuff). That, in the case of merge main (after this PR gets merged) will result in too many a conflict, that is

@schlunma
Copy link
Contributor

schlunma commented Sep 26, 2024

However, I disabled it for now because I am concerned that this may cause trouble for some of our contributors if they forget to install the pre-commit hooks and then pre-commit.ci commits to their branch, they continue editing and add some more commits, try to git push them, and find that they cannot because the branch on GitHub conflicts with their local branch.

Perfect, this is what I wanted to hear!

Will look at #2529 now to fix the tests, then we can hopefully merge this soon.

@bouweandela
Copy link
Member Author

I fixed the tests for now by pinning the pandas version to not 2.2.*.

@valeriupredoi
Copy link
Contributor

I fixed the tests for now by pinning the pandas version to not 2.2.*.

Peasant solution, more peasant than my peasant rounding in #2529 🐑

@schlunma schlunma added this to the v2.12.0 milestone Sep 26, 2024
@schlunma schlunma merged commit 436558c into main Sep 26, 2024
5 of 7 checks passed
@schlunma schlunma deleted the ruff-format branch September 26, 2024 14:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants