Skip to content

Conversation

@Andrewwango
Copy link
Collaborator

@Andrewwango Andrewwango commented Sep 2, 2025

Add more ruff rules for better linting.

For more details, see the hackathon project: #700 (comment)

Closes #539

Help wanted! If you want to help, just choose a rule in the ones added, and try to fix the code to make ruff pass it!

Checks to be done before submitting your PR

  • python3 -m pytest deepinv/tests runs successfully.
  • black . runs successfully.
  • make html runs successfully (in the docs/ directory).
  • Updated docstrings related to the changes (as applicable).
  • Added an entry to the CHANGELOG.rst.

@Andrewwango Andrewwango added good first issue Good for newcomers - well-scoped, easy issue. open to contribs Issue or PR welcomes help from any contributor, new or old, especially if in their expertise. labels Sep 12, 2025
@Andrewwango Andrewwango mentioned this pull request Sep 12, 2025
@Tmodrzyk
Copy link
Contributor

Hi Andrew can you give me access rights for this PR please ? 😄

@github-actions
Copy link
Contributor

🚀 GPU test workflows successfully triggered!

@Tmodrzyk
Copy link
Contributor

Tmodrzyk commented Oct 23, 2025

Motivations for the ignores of each rules:

  • E402 (module level import not a top of file) and TID253 (banned-module-level-import) are ignored in examples to allow for imports at any steps of the notebook, which makes the example easier to read.
  • SLF001 (private member accessed) is ignored in deepinv/transform/base.py because we need access to private pytorch methods there. It is also ignored in tests to allow for easy testing of private methods.
  • F811 (redefinition of unused name) is ignored in tests because it doesn't work well with fixtures. The linter doesn't understand that the imported fixture is actually used when using it as argument to a test.
  • F401, E402, F403, F405 (all related to star imports) are ignored in deepinv.__init__.py, I'm not really sure if it's possible to avoid star imports there.
  • Another notable ignore of F401 (star import) is in deepinv/optim/optimizers.py. The optim builder uses class attribute __name__ to instantiate iterators. Importing the iterators explicitly raises a linting error (unused import) because they are not explicitly used in the code. A workaround could be using a registry containing explicit iterator class names, but that creates a dependency between this file and the addition of new iterators. Overall I decided that this star import was fine.
  • Exactly the same star import issue for sampling iterators, which I think is fair to ignore too for now. This probably points to a deeper design issue with the builder classes (both sampling and optim)

@codecov
Copy link

codecov bot commented Nov 5, 2025

Codecov Report

❌ Patch coverage is 85.02304% with 65 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.66%. Comparing base (9018462) to head (26823c5).
⚠️ Report is 3 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
deepinv/models/diffunet.py 51.61% 15 Missing ⚠️
deepinv/physics/functional/radon.py 35.71% 3 Missing and 6 partials ⚠️
deepinv/optim/utils.py 60.00% 6 Missing ⚠️
deepinv/models/tgv.py 75.00% 2 Missing and 2 partials ⚠️
deepinv/optim/potential.py 33.33% 4 Missing ⚠️
deepinv/physics/noise.py 76.47% 2 Missing and 2 partials ⚠️
deepinv/tests/test_optim.py 92.30% 4 Missing ⚠️
deepinv/loss/sup.py 0.00% 3 Missing ⚠️
deepinv/physics/compressed_sensing.py 25.00% 3 Missing ⚠️
deepinv/loss/score.py 0.00% 2 Missing ⚠️
... and 9 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #705      +/-   ##
==========================================
+ Coverage   84.61%   84.66%   +0.05%     
==========================================
  Files         208      208              
  Lines       21281    21345      +64     
  Branches     2893     2889       -4     
==========================================
+ Hits        18006    18072      +66     
+ Misses       2359     2355       -4     
- Partials      916      918       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Tmodrzyk Tmodrzyk marked this pull request as ready for review November 5, 2025 13:24
@Tmodrzyk Tmodrzyk requested a review from tachella November 5, 2025 13:24
@Tmodrzyk
Copy link
Contributor

Tmodrzyk commented Nov 5, 2025

I think this is ready for review, check my comment above for more details / motivations behind some linting exceptions. While a large portion of the changes were performed automatically by ruff, it still took a lot of manual fixes to get all these rules to pass. In the future it'll probably be better to enforce more specific sets of rules within a single PR.

@jscanvic jscanvic self-requested a review November 5, 2025 18:01
@jscanvic jscanvic mentioned this pull request Nov 5, 2025
Copy link
Collaborator

@jscanvic jscanvic left a comment

Choose a reason for hiding this comment

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

Thanks guys! @Andrewwango @Tmodrzyk

I did a 1st pass and identified one rule (N805) that I believe can be enabled right away without much discussion and distilled it in #892

I find the other rules trickier mostly due to either

  • not being currently breached anywhere
  • needing to be fixed manually / nontrivial
  • not providing a clear benefit as far as I can tell at the moment

The PR is also a bit difficult to review given the intermingling of automated and manual fixes - I prefer if we distill it into multiple PRs if that's okay with everyone (happy to do the git shenanigans to avoid loosing / having to redo work)

I'll do a 2nd pass shortly

@jscanvic jscanvic self-requested a review November 5, 2025 20:37
@Tmodrzyk
Copy link
Contributor

Tmodrzyk commented Nov 6, 2025

I agree that the PR is very hard to review between automatic and manual fixes. Dividing it into multiple PRs would be better but I'm afraid that I made the CI pass only in the last few commits, so taking only previous commits might break things.

@jscanvic
Copy link
Collaborator

jscanvic commented Nov 6, 2025

@Tmodrzyk Yeah it would definitely be easier if we could just extract the commits here but I have a couple other tricks up my sleeve I think it's going to be doable. I'll start with extracting stuff related to E402 first we'll see how it goes!

Quick questions:

  • I see A005 was unlisted in the merge commit ffcc0f2 I think it is a mistake?
  • You mention TID253 in your message but it doesn't look like it was changed in any way, am I missing something?

@Tmodrzyk
Copy link
Contributor

Tmodrzyk commented Nov 7, 2025

Hi @jscanvic !

  • TID253 was already in the rule set, but it is know ignored in /examples to allow for more flexible placements of the imports.
  • A005 being unlisted was indeed a mistake, I've added it back to the rule set

OOps I think I did an error with the last commit ?

@jscanvic
Copy link
Collaborator

jscanvic commented Nov 7, 2025

But it is already ignored in the main branch, not that it matters too much

Thanks for adding the rule that was dropped!

I'll have a look shortly

@jscanvic jscanvic mentioned this pull request Nov 8, 2025
Copy link
Contributor

@nucli-vicky nucli-vicky left a comment

Choose a reason for hiding this comment

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

Copying a comment I made in #910 which turns out to not be relevant there, but still relevant here :)

Comment on lines -356 to +355
raise NotImplementedError("Undefined type: ".format(t))
raise NotImplementedError("Undefined type: ")
Copy link
Contributor

Choose a reason for hiding this comment

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

This makes the error message less informative; I think we can simply make it something like
raise NotImplementedError(f"Invalid part in mode: {t}")

@nucli-vicky nucli-vicky mentioned this pull request Jan 4, 2026
5 tasks
nucli-vicky added a commit to nucli-vicky/deepinv that referenced this pull request Jan 4, 2026
Andrewwango pushed a commit that referenced this pull request Jan 5, 2026
* add F821 822 823

* add minor fix from #705
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

good first issue Good for newcomers - well-scoped, easy issue. open to contribs Issue or PR welcomes help from any contributor, new or old, especially if in their expertise.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ruff linter

4 participants