Skip to content

chore: enable the perflint and comprehension ruff rules - #381

Merged
cloudsmith-iduffy merged 1 commit into
lint/ban-heavy-module-level-importsfrom
lint/perf-lint-rules
Aug 25, 2026
Merged

chore: enable the perflint and comprehension ruff rules#381
cloudsmith-iduffy merged 1 commit into
lint/ban-heavy-module-level-importsfrom
lint/perf-lint-rules

Conversation

@cloudsmith-iduffy

@cloudsmith-iduffy cloudsmith-iduffy commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Description

Ruff can catch a handful of slow Python patterns for us, so this turns on two of its rule groups:

  • perflint (PERF) — flags loop patterns that do more work than they need to, like building a list with .append() in a loop when a comprehension would do
  • flake8-comprehensions (C4) — flags unnecessary or roundabout comprehensions, like wrapping something in a list comprehension that's already a list

Then it fixes everything they flagged: mostly table-building loops in the list, repos, dependencies and download commands rewritten as comprehensions, a couple of hand-rolled dicts replaced with dict.fromkeys() / a dict comprehension, and one comprehension that was a no-op deleted outright.

To be honest about the motivation: none of these rewrites make the CLI measurably faster today — they're microseconds on loops of ~30 rows, so there are no flame graphs or benchmarks here. The point is the guard. The credential helpers now run on every docker/npm request, and I'd rather have the linter shout before an accidentally slow loop lands in one of those paths than find it in a profile later. Both rules run in pre-commit and in CI via the existing ruff hook, so there's nothing new to set up.

One rule from the group is deliberately switched off: try/except inside a loop. All four places it flagged use the except as actual control flow — for example the credential chain trying each provider in turn and moving on when one fails. Restructuring those to save nanoseconds would make the code worse.

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update
  • Refactoring
  • Other (please describe)

@cloudsmith-iduffy cloudsmith-iduffy changed the title chore(no-ticket): enable the perflint and comprehension ruff rules chore: enable the perflint and comprehension ruff rules Aug 21, 2026
@cloudsmith-iduffy
cloudsmith-iduffy marked this pull request as ready for review August 24, 2026 06:51
Copilot AI lite review requested due to automatic review settings August 24, 2026 06:51
@cloudsmith-iduffy
cloudsmith-iduffy requested a review from a team as a code owner August 24, 2026 06:51

Copilot AI 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.

Pull request overview

This pull request enables Ruff’s C4 and PERF rules and applies collection-building refactors across the CLI and core code.

Changes:

  • Enables C4 and PERF, excluding intentional PERF203 cases.
  • Replaces manual list and dictionary construction with comprehensions and dict.fromkeys().
  • Removes redundant comprehensions.

Blocking findings:

  • Critical: Remaining PERF401 violations exist in check.py and mcp.py.
  • Critical: Remaining PERF403 violations exist in cloudsmith_cli/core/mcp/server.py.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.

Show a summary per file
File Summary
pyproject.toml Updates Ruff rule configuration; critical lint violations remain.
packaging/pyinstaller/entry.py Simplifies keyring failure collection.
cloudsmith_cli/credential_helpers/docker/installer.py Refactors action collection.
cloudsmith_cli/core/download.py Uses a filtering comprehension.
cloudsmith_cli/core/api/vulnerabilities.py Simplifies table and count construction.
cloudsmith_cli/core/api/packages.py Simplifies payload and default dictionary creation.
cloudsmith_cli/cli/table.py Removes a redundant comprehension.
cloudsmith_cli/cli/config.py Simplifies config path construction.
cloudsmith_cli/cli/commands/repos.py Uses a table-row comprehension.
cloudsmith_cli/cli/commands/list_.py Uses a package-row comprehension.
cloudsmith_cli/cli/commands/download.py Uses an item comprehension.
cloudsmith_cli/cli/commands/dependencies.py Uses a dependency-row comprehension.
Suppressed comments (1)

cloudsmith_cli/cli/table.py:25

  • This assignment is immediately overwritten by the initialization at line 42, so the new comprehension still calls str and strip_ansi for every row and discards the result. Removing only the outer no-op comprehension did not remove the dead work; delete this precomputation (and the similarly overwritten plain_headers assignment above) instead.
    plain_rows = [strip_ansi(str(v)) for v in rows]

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread pyproject.toml
Comment thread pyproject.toml
@cloudsmith-iduffy
cloudsmith-iduffy force-pushed the lint/perf-lint-rules branch 2 times, most recently from 9ff69bd to 81652d0 Compare August 25, 2026 12:08
Select PERF and C4. Rewrite the flagged loops as comprehensions and
extend() calls. Ignore PERF203: the flagged try/except loops are
fault-tolerant by design.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@cloudsmith-iduffy
cloudsmith-iduffy merged commit 8ac4ce9 into master Aug 25, 2026
22 checks passed
@cloudsmith-iduffy
cloudsmith-iduffy deleted the lint/perf-lint-rules branch August 25, 2026 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants