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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruff: smaller steps #364

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open

Ruff: smaller steps #364

wants to merge 12 commits into from

Conversation

rcomer
Copy link
Member

@rcomer rcomer commented May 3, 2023

馃殌 Pull Request

Description

SciTools/iris#353 proposed introducing ruff to cf-units for linting, but also changed some style choices (max line length and import sort order). IMO this made it difficult to spot what effect ruff was having on the code. This PR breaks it down so hopefully it's easier to see what's going on:

  • First commit takes the config file changes from Define test structure聽iris#353
  • Second commit reinstates the previous style choices in the config
  • Third commit shows ruff's autofixes. The majority of these modernise the string formatting, but there are also
    • removals of things that became redundant when we dropped python2
    • change dictionary creation from dict(...) to {...} - maybe for speed?
    • change if thing == None to if thing is None - I'm surprised flake8 didn't catch that one

CI will fail with remaining problems that ruff found:

  • Some of our comment lines are longer than 79 characters
  • In the tests we touch an object's attribute but don't get hold of it and use it after, which seems to be correct in context but the linter can't tell that

@bjlittle
Copy link
Member

bjlittle commented May 3, 2023

@rcomer Thanks for this 馃殌

I was waiting for SciTools/iris#5254 to conclude, then reopen SciTools/iris#353, but totally happy to replace it with this 馃憤

@rcomer
Copy link
Member Author

rcomer commented Nov 13, 2023

Rebased, updated the ruff version and replaced black with ruff's formatter.

Swapping black for ruff's formatter only affects one line, shown in the most recent commit.

We now have more failures and fewer autofixes from main ruff. I think because some fixes that were previously done automatically are now marked "unsafe" so we would need to manually opt in (see https://astral.sh/blog/ruff-v0.1.0#respecting-fix-safety).

@rcomer rcomer marked this pull request as ready for review November 14, 2023 17:06
@rcomer
Copy link
Member Author

rcomer commented Nov 14, 2023

I applied the "unsafe" fixes but then tweaked a few that didn't look quite right to me. Also tidied up the remaining errors so I think this is now ready for review.

Unlike most of my branches, the individual commits here may be meaningful enough to review individually.

@pp-mo
Copy link
Member

pp-mo commented Nov 15, 2023

@SciTools/peloton would you be interested in taking this on, @tkknight ?
We think you expressed interest in getting this one in before the Iris one, and that @bjlittle is unlikely to find time right now.

pelson
pelson previously approved these changes Feb 25, 2024
Copy link
Member

@pelson pelson left a comment

Choose a reason for hiding this comment

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

Looks like a good improvement to me.

(I doubt I will get a chance to follow-up, but hopefully my review moves the PR forwards 馃槈

cf_units/__init__.py Show resolved Hide resolved
cf_units/__init__.py Show resolved Hide resolved
@@ -1,6 +1,6 @@
# Generated from /home/ruth/git_stuff/cf-units/cf_units/_udunits2_parser/parser/udunits2Lexer.g4 by ANTLR 4.11.1
Copy link
Member

Choose a reason for hiding this comment

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

Not sure we should be re-formatting this (since it is auto-generated)? Perhaps add an exception to the pre-commit config.

Copy link
Member Author

Choose a reason for hiding this comment

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

I wondered the same when these files were blackened at #368. Here, though, the code changes in udunits2Parser.py seem correct to me. Might be worth skipping the line length rule so we don鈥檛 have to manually wrangle the comments in future though 馃.

Copy link
Member Author

Choose a reason for hiding this comment

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

I have skipped the line length rule for these files.

cf_units/tests/integration/parse/test_parse.py Outdated Show resolved Hide resolved
cf_units/tests/integration/parse/test_parse.py Outdated Show resolved Hide resolved
cf_units/tests/test_coding_standards.py Show resolved Hide resolved
cf_units/util.py Show resolved Hide resolved
pyproject.toml Show resolved Hide resolved
Copy link
Member Author

@rcomer rcomer left a comment

Choose a reason for hiding this comment

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

Thanks @pelson - great to hear from you again! I will follow up more when I am back on my work machine.

cf_units/__init__.py Show resolved Hide resolved
@@ -1,6 +1,6 @@
# Generated from /home/ruth/git_stuff/cf-units/cf_units/_udunits2_parser/parser/udunits2Lexer.g4 by ANTLR 4.11.1
Copy link
Member Author

Choose a reason for hiding this comment

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

I wondered the same when these files were blackened at #368. Here, though, the code changes in udunits2Parser.py seem correct to me. Might be worth skipping the line length rule so we don鈥檛 have to manually wrangle the comments in future though 馃.

cf_units/tests/test_coding_standards.py Show resolved Hide resolved
pyproject.toml Show resolved Hide resolved
@rcomer
Copy link
Member Author

rcomer commented Feb 26, 2024

New commit dismissed an approving review. Not seen that before 馃槙

@@ -1,6 +1,6 @@
# Generated from /home/ruth/git_stuff/cf-units/cf_units/_udunits2_parser/parser/udunits2Lexer.g4 by ANTLR 4.11.1
# Generated from cf_units/_udunits2_parser/parser/udunits2Lexer.g4 by
Copy link
Member

Choose a reason for hiding this comment

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

Still changes to auto-generated files - would suggest not making any changes to auto-regeneratable files. (everything in _udunits2_parser.parswr)

verbose = "False"
[tool.ruff.per-file-ignores]
# Allow the longer comment lines that the generated code produces.
"cf_units/_udunits2_parser/parser/*.py" = ["E501"]
Copy link
Member

Choose a reason for hiding this comment

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

The alternative way of blanket ignoring everything with ruff-formatter is to put this exclude in pre-commit

Copy link
Member Author

Choose a reason for hiding this comment

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

To be clear, the changes in these files came from the ruff linter (which has some auto-fixes) rather than ruff formatter.

I have no objection to skipping linting/formatting for these files but, since they were previously flake8-linted and black-formatted I feel like that decision has already been made for the current generation. If whoever does the next re-generation wants to turn off the linting and their reviewer agrees then that's a decision for then.

pelson
pelson previously approved these changes Feb 27, 2024
@bjlittle bjlittle self-assigned this Feb 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

None yet

4 participants