Skip to content

fix: resolve ruff-check lint errors in non-template Python files#245

Open
TheSaiEaranti wants to merge 1 commit into
awslabs:mainfrom
TheSaiEaranti:fix-ruff-lint-errors
Open

fix: resolve ruff-check lint errors in non-template Python files#245
TheSaiEaranti wants to merge 1 commit into
awslabs:mainfrom
TheSaiEaranti:fix-ruff-lint-errors

Conversation

@TheSaiEaranti

Copy link
Copy Markdown

Fixes #218

Scope

ruff check (v0.15.0, the pinned pre-commit rev) reports 91 findings on main. They split cleanly in two:

findings files
code_templates/*.py 46 (44 invalid-syntax, 2 E402) 12 #214 — untouched here
everything else 45 (F541 35, F841 5, F401 4, F821 1) 12 this PR

Non-template findings are now 0. The 46 belonging to #214 are unchanged.

Two notes on how this differs from the issue description, which was written on 2026-07-06:

Mechanical fixes

Applied with ruff check --fix (safe fixes only; no --unsafe-fixes):

  • F541 (35) — dropped the f prefix from f-strings with no placeholders.
  • F401 (2 of 4) — removed typing.Union from validate_custom_metrics.py and INT from test_safe_query.py. I checked that INT is not reachable through that file's globals()-based fallback test discovery, which selects only callables named test_*; INT_INJECTIONS is a separate name and is still used.

Reviewed individually

tools/validate-urls.pyF821 undefined name httpx
This is a PEP 723 inline script that imports httpx lazily inside check_url and main, so the module-scope annotation client: "httpx.AsyncClient" had nothing to resolve against. Added a TYPE_CHECKING-guarded import rather than a noqa, which keeps the lazy-import behaviour intact — I verified the module still imports with httpx absent, and the annotation is preserved.

verify-traceability.pyF841 overall_pct
generate_html computed overall_total, overall_impl and overall_pct, but only rules_pct and reqs_pct are ever rendered. Removing just overall_pct would have left the other two unused, so all three go. This looks like an intended "overall" dashboard stat that was never wired into the HTML — worth a follow-up if you want that row displayed rather than deleted.

post_process_drawio.pyF841 actor_x2
Removed. Flagging what it hints at, since it may be worth its own issue: the overlap test on the next line is

if actor_x >= cloud_x and actor_x < cloud_x2:

which only detects an actor whose left edge is inside the AWS Cloud. An actor starting left of the cloud and extending into it is not detected. actor_x2 is exactly the term a full interval-overlap test would need, so this reads like an unfinished check. I have not changed that behaviour here — it is a layout change, not a lint fix, and should be your call.

Templates

The three *_source_template.py files are customer-facing starter templates ("INITIAL SUGGESTION ONLY - MUST BE CUSTOMIZED"), where from typing import Dict, Any, List, Optional # For type hints and the example variables are deliberate scaffolding for the reader. Stripping them would remove the thing the template exists to provide, so each gets a file-level directive instead, as the issue suggests:

# Starter template: the imports and example variables below are intentional
# scaffolding for the reader to build on, so unused-name rules do not apply.
# ruff: noqa: F401, F841

Verification

  • ruff check . → non-template findings 0; remaining 46 are all code_templates/* (pre-commit: 12 code_templates/*.py files fail Python-parse hooks (check-ast, ruff-format) #214).
  • All 12 changed files parse (ast.parse) and byte-compile.
  • tools/evals/databases-on-aws/dsql/scripts/test_safe_query.py63 passed, 0 failed.
  • validate-urls.py imports at module scope with httpx not installed.
  • verify-traceability.py and post_process_drawio.py behave identically to main on import (the latter raises the same pre-existing defusedxml AttributeError in my environment both before and after).

Out of scope: ruff-format fails repo-wide on main — 25 files, including files this PR touches. That count is 25 before and 25 after, so nothing here adds to it. Running the formatter would turn this into an unrelated 25-file diff, so I left it alone; happy to file it separately if it is not already tracked under #213.


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of the project license.

ruff-check reported 45 findings across 12 tracked, non-template Python files:
F541 (35), F841 (5), F401 (4) and F821 (1). Non-template findings are now zero.

Mechanical fixes, applied with `ruff check --fix`:

- F541: drop the f prefix from f-strings that contain no placeholders.
- F401: remove genuinely unused imports (`typing.Union` in
  validate_custom_metrics.py, `INT` in test_safe_query.py; the latter is not
  reachable through the module's `globals()` test discovery, which selects only
  callables named `test_*`).

Reviewed individually:

- tools/validate-urls.py imports httpx lazily inside functions, so the module
  scope annotation `client: "httpx.AsyncClient"` had no binding to resolve.
  Add a TYPE_CHECKING guarded import, which keeps the lazy import behaviour
  and leaves the module importable without httpx installed.
- verify-traceability.py computed overall_total, overall_impl and overall_pct
  in generate_html but never rendered them, unlike rules_pct and reqs_pct.
  Remove all three, since dropping only overall_pct would leave the other two
  unused.
- post_process_drawio.py computed actor_x2 but never used it.

The three *_source_template.py files are customer facing starter templates
whose unused imports and example variables are deliberate scaffolding, so they
get a file level `# ruff: noqa: F401, F841` rather than having that scaffolding
stripped, per the suggestion in the issue.

The 12 code_templates/*.py files are left untouched; their 44 invalid-syntax
findings and 2 E402 findings are awslabs#214. Pre-existing repo wide ruff-format
failures are also out of scope and unchanged at 25 files before and after.

Fixes awslabs#218
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.

pre-commit: ruff-check fails with real lint errors (F841/E402/F821) in non-template scripts

1 participant