lightly icon indicating copy to clipboard operation
lightly copied to clipboard

style: migrate to `ruff`

Open SauravMaheshkar opened this issue 2 years ago • 16 comments

astral-sh/ruff has emerged as a competitor to the existing linters. It's considerably faster than the current linter pylint viz.

Source: ruff repository

Switching to ruff would enable us to switch to a single tool, as it has drop-in parity with black, isort and support for pylint rules.

Happy to open a PR if interested 😄

References:

  • https://github.com/astral-sh/ruff/issues/689

SauravMaheshkar avatar Jun 25 '24 14:06 SauravMaheshkar

Great idea, we already use ruff internally. We'd welcome a PR.

philippmwirth avatar Jun 25 '24 14:06 philippmwirth

I would like to contribute.

TakshPanchal avatar Jun 25 '24 14:06 TakshPanchal

@TakshPanchal sounds good, LMK if you need any help with the PR

SauravMaheshkar avatar Jun 26 '24 07:06 SauravMaheshkar

Currently, isort and black is being used right? So I have to pylintrc as reference config. From docs,

Ruff can be configured through a pyproject.toml, ruff.toml, or .ruff.toml file.

So majorly I have to convert pylintrc with one of these file right?

TakshPanchal avatar Jun 26 '24 09:06 TakshPanchal

Currently, isort and black is being used right? So I have to pylintrc as reference config. From docs,

Ruff can be configured through a pyproject.toml, ruff.toml, or .ruff.toml file.

So majorly I have to convert pylintrc with one of these file right?

Yes, and let's stick with one single configuration file, so convert pylintrc to a ruff configuration within pyproject.toml.

Ideally the changes lead to no style changes, i.e. running make lint with ruff leads to no changes in code files.

SauravMaheshkar avatar Jun 26 '24 15:06 SauravMaheshkar

can I work on this issue?

Abhrajitdas02 avatar Oct 12 '24 08:10 Abhrajitdas02

Sure, we only assign one issue at a time. So, I'll assign this issue for now. If you want to work on some other issue first, let me know.

SauravMaheshkar avatar Oct 12 '24 11:10 SauravMaheshkar

@Abhrajitdas02 would you like any help with this issue?

SauravMaheshkar avatar Nov 07 '24 15:11 SauravMaheshkar

@guarin I was working on this and noticed that we actually don't enforce pylint anywhere.

Source: https://github.com/search?q=repo%3Alightly-ai%2Flightly%20pylint&type=code

SauravMaheshkar avatar Dec 12 '24 02:12 SauravMaheshkar

Hi there, I was looking into this and thinking of picking it up. But before opening a PR I'd like to settle the question @SauravMaheshkar raised above, since it changes the scope of the PR.

Current state of the toolchain (verified against master) :

  • black and isort are enforced in CI via make format-check in .github/workflows/test_code_format.yml.
  • pylint is defined in the Makefile (lint, lint-lightly, lint-tests) and listed as a dev dependency, but no workflow ever calls it, grep -ri lint .github/workflows/ returns nothing. The pylintrc is effectively dead config: most of its disable= list is Python 2/3 compat cruft, and max-line-length=88 already matches black's default.
  • One lonely # pylint: disable=broad-except pragma in lightly/api/api_workflow_download_dataset.py.

So the original framing ("convert pylintrc to ruff config, no style changes") doesn't quite map onto reality as there's no enforced lint config to preserve parity with. The real decision is:

  • Option A: like-for-like, no enforcement change. Ruff replaces black + isort in make format-check; ruff also replaces pylint in make lint but make lint stays uncalled by CI. Conservative ruleset (E, F, W, I - pycodestyle errors/warnings, pyflakes, isort) so the PR produces zero code diffs. Smallest, easiest to review, matches the original "no style changes" goal.
  • Option B: start enforcing lint. Same as A, plus wire make lint (or fold it into format-check) intotest_code_format.yml. Higher value, but likely surfaces latent issues that need fixing in the same PR.

I'd lean towards Option A as a clean, mergeable first step, with enforcement as a follow-up. What do you think? @guarin

Either way the PR would touch:

  • pyproject.toml (drop black/isort/pylint deps + their [tool.*] sections, add [tool.ruff] with the same openapi_generated exclusion and target-version = "py37" to match the CI matrix),
  • Makefile (rewrite format / format-check / lint),
  • .pre-commit-config.yaml (replace nbqa-black / nbqa-isort with ruff-pre-commit, ruff has native .ipynb support so the nbQA wrappers aren't needed),
  • delete pylintrc and remove the lone pylint pragma.

RecreationalMath avatar Apr 09 '26 17:04 RecreationalMath

I'd lean towards Option A as a clean, mergeable first step, with enforcement as a follow-up. What do you think? @guarin

While reading on some other open issues, I found the answer (Option A) in this comment. Before I start working on it, could you please assign this issue to me? and please feel free to let me know if you have any feedback on my approach/analysis from the above comment. Thanks!

RecreationalMath avatar Apr 09 '26 19:04 RecreationalMath

Hi @RecreationalMath, Thanks for the good breakdown into options. I would also suggest to go forward with option A for now. We can then later progressively add a broader ruleset if we feel the necessity (probably not enforcing it on all modules at the same time), but iteratively to keep the diffs manageable per PR. This then lets us eventually also add it to CI (which is indeed a pity that it isn't yet). I think that approach matches @guarin's proposed timeline. I am assigning the issue to you!

liopeer avatar Apr 10 '26 08:04 liopeer

I have been doing some digging on the comparison between ruff and black, and it's worth noting that ruff's formatter has some known, documented differences from black. The ruff project documents these known deviations from Black here and states that >99.9% of lines are formatted identically on large Black-formatted projects.

That still leaves 0.1%, which may manifest as a one-time normalisation cost within the same PR. Just a heads up, as this will translate to review overhead for the maintainers/reviewers.

RecreationalMath avatar Apr 10 '26 14:04 RecreationalMath

Thanks for the heads-up! Let's see how many diff files it generates. But since it's going to be formatting and linting, it should anyway be reasonably fast to review.

liopeer avatar Apr 10 '26 15:04 liopeer

You might want to throw in a .git-blame-ignore-revs file if the diff is too large

guarin avatar Apr 10 '26 15:04 guarin

You might want to throw in a .git-blame-ignore-revs file if the diff is too large

It is indeed too large, ~96 files. Added .git-blame-ignore-revs.

RecreationalMath avatar Apr 11 '26 06:04 RecreationalMath