Skip to content

Check unused imports via ruff - #299

Open
ulgens wants to merge 4 commits into
JWock82:mainfrom
ulgens:ruff-unused-imports
Open

Check unused imports via ruff#299
ulgens wants to merge 4 commits into
JWock82:mainfrom
ulgens:ruff-unused-imports

Conversation

@ulgens

@ulgens ulgens commented Dec 17, 2025

Copy link
Copy Markdown
Contributor

Adds a very basic ruff config to check F401 - unused import rule.

The ruff setup can be extended later.

I added a simple workflow to run pre-commit hooks in CI.

@ulgens

ulgens commented Dec 17, 2025

Copy link
Copy Markdown
Contributor Author

@JBloss1517 👋🏻

@ulgens

ulgens commented Jun 7, 2026

Copy link
Copy Markdown
Contributor Author

Rebased and ready for review.

@JBloss1517

Copy link
Copy Markdown
Collaborator

Thanks for following up and keeping this PR alive! I can get it reviewed this week.

@jonbiemond jonbiemond left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Awesome to see! Amazing how many unused imports stick around.

I just want to note that by running ruff via prek we're missing out on inline annotations in GitHub. See the workflow examples provided by Astral: https://docs.astral.sh/ruff/integrations/#github-actions

Of course, in the case maintainers or contributors choose to adopt prek locally, the benefit of using prek in CI is that the lint config is specified in only one location.

@ulgens

ulgens commented Jun 12, 2026

Copy link
Copy Markdown
Contributor Author

I agree the inline annotations looks nice, but they are limited to certain tools and normally the expectation is that the developers will fix these issues in their local and CI check is there just to ensure they are running it locally.

@JBloss1517

Copy link
Copy Markdown
Collaborator

Thanks for kicking off the ruff stack — nice to see #299#300 laddering up the ruleset incrementally. Keeping this PR to F401 only is the right call; my notes are all small/config-level so the foundation grows cleanly:

Worth fixing here:

  1. target-version mismatchruff.toml sets py310, but setup.py requires >=3.11 (since Drop support for Python 3.7 #291 dropped 3.7). Match them so ruff (and the UP rules coming in Enable pyupgrade (UP) ruleset in ruff config  #300) target the right baseline:
    target-version = "py311"
  2. Align excludes with .flake8.flake8 already skips Derivations, docs, Resources, .Archived, build, dist. Porting those now keeps noise down as the ruleset expands in Enable pyupgrade (UP) ruleset in ruff config  #300:
    extend-exclude = ["*.ipynb", ".Archived", "Derivations", "docs", "Resources", "build", "dist"]
  3. Drop --fix from the committed hook — the same .pre-commit-config.yaml drives local commits and CI. In CI, --fix edits files that then get discarded and the failure reads as "files were modified by this hook" instead of a clean violation list. Let the shared config report; devs run ruff check --fix locally:
    hooks:
      - id: ruff-check   # report only

On @jonbiemond's annotation point — agreed, and since ruff is currently the only check, I'd lean toward Astral's native ruff action (--output-format=github) for this job to get inline PR annotations now, and revisit prek-in-CI once the hook set grows beyond ruff — that's when the single-config-location benefit actually pays for the lost annotations. Either way config stays in ruff.toml, so it's reversible.

Optional: bump j178/prek-action@v1@v2 (checksum validation + caching) if you stay on prek; rename the Prek checks workflow to lint; and align its trigger ([push, pull_request], all branches) with build-and-test.yml's branches: [main].

All good as-is — the __all__ addition correctly marks the re-exports as intentional for F401, and the commented-out import notes are a nice touch. No need to touch flake8 here; see #327 for the overall direction.

@JWock82

JWock82 commented Jun 20, 2026

Copy link
Copy Markdown
Owner

I agree with @JBloss1517's assessment. @ulgens, if you address @JBloss1517's comments I'll merge the pull request. Thanks for your work on this!

ulgens added 4 commits June 24, 2026 00:25
Resolves

> imported but unused; consider removing, adding to `__all__`, or using a redundant alias

warnings
@ulgens
ulgens force-pushed the ruff-unused-imports branch from 59aca05 to 7f8e0e0 Compare June 23, 2026 21:28
@ulgens

ulgens commented Jun 23, 2026

Copy link
Copy Markdown
Contributor Author

target-version mismatch

Fixed.


Align excludes with .flake8

build and dist are already parts of the default ruff config, I added the rest.


Drop --fix from the committed hook — the same .pre-commit-config.yaml drives local commits and CI. In CI, --fix edits files that then get discarded and the failure reads as "files were modified by this hook" instead of a clean violation list. Let the shared config report; devs run ruff check --fix locally

I believe that the auto-fix feature is useful for the development process, and personally, I use it a lot to auto-apply the fixes. This is also the common behaviour among the git hooks.

That being said, I agree that a non-descriptive message in CI can be improved. I think this can be solved by --show-fixes:

      --show-fixes
          Show an enumeration of all fixed lint violations. Use
          `--no-show-fixes` to disable

https://docs.astral.sh/ruff/configuration/#full-command-line-interface

I added --show-fixes to the ruff args but if you think this doesn't produce the result you are looking for, I can remove both of the args.

On a personal note; not all git hooks provide this feature and my take is that for this specific setup, it's more important to show that the workflow is failing than showing the exact details of why it's failing. If we assume that the expectation from contributors is to run it (automatically) before committing their changes and the CI is here to check that. In case of a failure, the only thing they need to do is to do run the hooks locally - they are not expected to observe and fix individual issues.


About the annotations vs git hooks case,

I think the git hooks setup can be enriched a lot beyond ruff. This is the standard config I use for the projects I work on. Afaik, except zizmor, none of the other tools provide this annotation feature and I value the uniformity of the reports, so I lean towards the git hooks option.


Applied the rest of the recommendations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants