Skip to content

Ruff misc: small comprehension/perf grab-bag (PERF102, SIM105, RUF005, C4xx, RUF015, stray RUF046) #498

Description

@squid-protocol

Objective

Part of the ruff lint baseline cleanup (several 1-2 count rule families -- C401 (unnecessary generator where a builtin call would do), C403 (unnecessary list comprehension where a set/generator would do), C414 (redundant cast (e.g. list()/sorted() double-wrapping)), C416 (unnecessary comprehension that just rebuilds the same collection), C419 (unnecessary comprehension inside a call that takes an iterable directly), PERF102 (iterating .items() when only keys or only values are used), RUF005 (list concatenation via + instead of unpacking), RUF015 (allocating a full list just to grab the first element), SIM105 (try/except/pass that could be contextlib.suppress(...))). Chunked out of the larger sub-issue this replaces, so it's a manageable, self-contained pickup.

Findings

  • gitgalaxy/core/detector.py:2158 (C403) -- Unnecessary list comprehension (rewrite as a set comprehension)
  • gitgalaxy/core/guidestar_lens.py:139 (C401) -- Unnecessary generator (rewrite as a set comprehension)
  • gitgalaxy/core/network_risk_sensor.py:183 (C419) -- Unnecessary list comprehension
  • gitgalaxy/metrics/signal_processor.py:498 (RUF046) -- Value being cast to int is already an integer
  • gitgalaxy/metrics/signal_processor.py:549 (C419) -- Unnecessary list comprehension
  • gitgalaxy/recorders/audit_recorder.py:290 (C416) -- Unnecessary dict comprehension (rewrite using dict())
  • gitgalaxy/recorders/audit_recorder.py:307 (C414) -- Unnecessary list() call within sorted()
  • gitgalaxy/recorders/gpu_recorder.py:285 (C414) -- Unnecessary list() call within sorted()
  • gitgalaxy/recorders/record_keeper.py:700 (RUF005) -- Consider iterable unpacking instead of concatenation
  • gitgalaxy/recorders/record_keeper.py:771 (RUF005) -- Consider iterable unpacking instead of concatenation
  • gitgalaxy/security/security_auditor.py:359 (RUF046) -- Value being cast to int is already an integer
  • gitgalaxy/standards/language_lens.py:132 (SIM105) -- Use contextlib.suppress(re.error) instead of try-except-pass
  • gitgalaxy/standards/language_lens.py:760 (PERF102) -- When using only the values of a dict use the values() method
  • gitgalaxy/standards/language_lens.py:897 (PERF102) -- When using only the values of a dict use the values() method
  • gitgalaxy/tools/cobol_to_cobol/cobol_jcl_auditor.py:107 (RUF015) -- Prefer next(iter(generated_metrics["exec_pgms"])) over single element slice

Approach

Each of these rules only has 1-2 occurrences, so they're bundled into one grab-bag rather than filed as individual one-line issues. Each finding is independent -- fix whichever you like, in any order.

Fix in a small PR, removing resolved entries from tests/ruff_audit_baseline.json (see tests/ruff_audit.py's module docstring for the regen command).

Parent: #469

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueAccessible entry point for new community contributorspriority: lowUI tweaks, documentation, and minor optimizationspythonPython-specific runtime or environment optimizationsrefactorStructural cleanup or optimization without altering behavior

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions