GenericMappingTools / pygmt

A Python interface for the Generic Mapping Tools.
https://www.pygmt.org
BSD 3-Clause "New" or "Revised" License
747 stars 216 forks source link

Use ruff to improve code quality #2741

Closed seisman closed 9 months ago

seisman commented 11 months ago

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.

weiji14 commented 11 months ago

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:

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

seisman commented 11 months ago

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:

seisman commented 10 months ago

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.1.8 provides the following linters/rules:

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

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 commented 2 months ago
  • [ ] 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 41a0fe0a6a79849565348203737360855d40add8 (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 commented 2 months ago

Sounds good.