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 to improve code quality #2741

Closed
seisman opened this issue Oct 12, 2023 · 6 comments
Closed

Use ruff to improve code quality #2741

seisman opened this issue Oct 12, 2023 · 6 comments
Labels
maintenance Boring but important stuff for the core devs
Milestone

Comments

@seisman
Copy link
Member

seisman commented Oct 12, 2023

It seems ruff has been very hot recently. The README says it can replace flake8 and pylint, and more importantly, it's 10-100x faster.

Someone interested may try it and tell us how it works for the PyGMT project.

@seisman seisman added the question Further information is requested label Oct 12, 2023
@weiji14
Copy link
Member

weiji14 commented Oct 15, 2023

Opened a PR to replace flakeheaven and isort with ruff at #2747, so that we can move ahead with the Python 3.12 support.

It should also be possible to replace pylint with ruff (see https://docs.astral.sh/ruff/faq/#how-does-ruff-compare-to-pylint), which would:

  • Pro:
    • Allow us to remove make lint (which is slow) from the Makefile (i.e. only have make check and make format)
    • Bonus is that ruff can actually automatically fix some pylint rules (see those marked with 🛠️ at https://docs.astral.sh/ruff/rules/#pylint-pl)
  • Con: Not all pylint rules are applied (see Implement Pylint astral-sh/ruff#970), but I'm not sure if we even need to check all of them...

So should we replace pylint with ruff? Vote 👍 for yes, 👎 for no. If everyone is generally in favour, we can open a PR after #2747 is merged.

@seisman seisman added maintenance Boring but important stuff for the core devs and removed question Further information is requested labels Oct 16, 2023
@seisman
Copy link
Member Author

seisman commented Oct 16, 2023

So should we replace pylint with ruff?

Instead of replacing pylint with ruff, I think we can use both to see if there are any linting issues reported by pylint but not detected by ruff. We can then decide if we want to remove pylint before releasing v0.11.0. So I propose to have two separate PRs:

  • Configure ruff as a linting tool
  • Remove pylint before v0.11.0

@seisman seisman added this to the 0.11.0 milestone Oct 16, 2023
@seisman
Copy link
Member Author

seisman commented Nov 9, 2023

Last updated on 2024/10/18

ruff supports many groups of rules (https://docs.astral.sh/ruff/rules/). I think we should go through the available rules and apply the rules that make sense to our project.

ruff v0.7.0 (released on 2024/10/18) provides the following linters/rules:

ruff linter | sort -k1 | awk '{printf("- [ ] [`%s`](https://docs.astral.sh/ruff/rules/#%s-%s): %s\n"), $1, tolower($2), tolower($1), $2}' 

@seisman
Copy link
Member Author

seisman commented Dec 21, 2023

I've enabled ~32 rulesets that make sense to our project. Closing the issue now and we may revisit it in the feature if new ruff versions provide more rulesets.

@weiji14
Copy link
Member

weiji14 commented Jul 8, 2024

  • FURB: refurb [preview only]

I've been trying out some of these FURB rules in preview by running ruff check . --statistics, and got the following output at commit 41a0fe0 (those marked with an asterisk * are automatically fixable):

ruff check . --statistics
16	PLR6201	[*] Use a `set` literal when testing for membership
15	PLC0415	[ ] `import` should be at the top-level of a file
15	PLW1514	[*] `pathlib.Path(...).open` in text mode without explicit `encoding` argument
 8	E226   	[*] Missing whitespace around arithmetic operator
 4	S404   	[ ] `subprocess` module is possibly insecure
 3	PLR6104	[*] Use `+=` to perform an augmented assignment directly
 3	FURB113	[*] Use `lines.extend(('WARNING:', f'  {warnmsg}'))` instead of repeatedly calling `lines.append()`
 2	PLC1901	[ ] `err == ""` can be simplified to `not err` as an empty string is falsey
 2	PLW3201	[ ] Bad or misspelled dunder method name `_repr_html_`
 1	PLR0904	[ ] Too many public methods (30 > 20)
 1	PLR0917	[ ] Too many positional arguments (11/10)
 1	PLW0108	[*] Lambda may be unnecessary; consider inlining inner function
 1	RUF027 	[*] Possible f-string without an `f` prefix

Thinking of applying PLR6201 and maybe PLW1514, does that sound ok?

@seisman
Copy link
Member Author

seisman commented Jul 8, 2024

Sounds good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Boring but important stuff for the core devs
Projects
None yet
Development

No branches or pull requests

2 participants