Skip to content

Conversation

jwallwork23
Copy link
Contributor

@jwallwork23 jwallwork23 commented Aug 11, 2025

Python linting with Ruff

Fixes #891

This PR adds ruff for Python linting, which is used widely by the community.

Useful info

  • With ruff check we can see the current issues.
  • With ruff check --fix we can get ruff to automatically fix many issues that are guaranteed not to break things.
  • There is also a ruff check --fix --unsafe-fixes option but it should be used with care.
  • There is also ruff format but it's quite opinionated so would likely completely reformat everything.
  • ruff.toml contains the configuration, including any rules to ignore.
  • The vast majority of the lines changed are in test/era5_topaz4_test_data.py because this is an enormous file.
  • At some point in working on this PR, running ruff check --statistics gave the following:
58	D103   	undocumented-public-function
52	PLR2004	magic-value-comparison
27	D100   	undocumented-public-module
11	S101   	assert
 7	PLR0911	too-many-return-statements
 4	PLR0912	too-many-branches
 3	ARG001 	unused-function-argument
 3	PLR0915	too-many-statements
 2	E722   	bare-except
 2	PLR0913	too-many-arguments
 2	S602   	subprocess-popen-with-shell-equals-true
 1	A001   	builtin-variable-shadowing
 1	B007   	unused-loop-control-variable
 1	S607   	start-process-with-partial-path
Found 174 errors.

To get things to work, I applied ruff fixes, made code changes, and ignored rules that'd require more work to satisfy. The ignored rules are:

  • Magic value comparison
  • Too many statements/branches/arguments/return statements
  • Subprocess call with shell=True
  • Start process with partial path
  • Missing docstrings for public modules/functions

I also applied per-file ignores to some of the code generation code for use of assertions as they'd need some refactoring to remove them and retain checks.

Checklist

  • Make ruff check pass by fixing issues and ignoring rules, as appropriate.

@jwallwork23 jwallwork23 self-assigned this Aug 11, 2025
@jwallwork23 jwallwork23 added enhancement New feature or request ICCS Tasks or reviews for the ICCS team labels Aug 11, 2025
@jwallwork23 jwallwork23 mentioned this pull request Aug 29, 2025
@jwallwork23 jwallwork23 marked this pull request as ready for review August 29, 2025 09:31
@jwallwork23
Copy link
Contributor Author

[Rebased on top of develop, excluding large data generation scripts from linting.]


def generator(module_class_name, fq_interface_name, fq_impl_names, help_names):
"""Generates the text for a module support file."""
"""Generate the text for a module support file."""
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a reason the prescriptive second person form (generate), rather than the descriptive third person form (generates)? Admittedly I got this from writing javadoc comments many years ago, rather than any Python standard, but I feel it makes more sense to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It comes from D401 First line of docstring should be in imperative mood. We could ignore this rule if preferred?

Copy link
Collaborator

Choose a reason for hiding this comment

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

No, I'll try to follow that rule in future.

Copy link
Collaborator

@timspainNERSC timspainNERSC left a comment

Choose a reason for hiding this comment

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

Big, but not complex. I can only apologize for the amount of trailing whitespace.

@jwallwork23 jwallwork23 merged commit e785603 into develop Sep 17, 2025
25 checks passed
@jwallwork23 jwallwork23 deleted the issue891_ruff branch September 17, 2025 08:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request ICCS Tasks or reviews for the ICCS team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Applying auto-formatting to Python files, as well as C++

2 participants