Skip to content

Commit b063652

Browse files
committed
chore: Adopt ruff for unused-import enforcement
Wire ruff F401 (unused-import) checking into the repo so dead imports are caught automatically going forward, instead of relying on IDE highlights. - [tool.ruff] in pyproject.toml: select F401, exempt __init__.py (public re-exports), and exclude four files that hardcode googleapis.com URLs so cleanup does not trip the check-file-contents mTLS policy. - ruff pre-commit hook scoped to src/. - scripts/run_precommit_checks.py (the no-git standalone runner) learns the ruff hook, passing --force-exclude so excludes are honored on explicit file args. - Pin ruff in the dev extra to match the hook version. Change-Id: I0d1dab0011e2418af41f9ff983beb7a4930bae1f
1 parent b79096a commit b063652

3 files changed

Lines changed: 36 additions & 1 deletion

File tree

.pre-commit-config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ repos:
1111
rev: v2.24.0
1212
hooks:
1313
- id: pyproject-fmt
14+
- repo: https://github.com/astral-sh/ruff-pre-commit
15+
rev: v0.15.17
16+
hooks:
17+
- id: ruff
18+
args: [--fix]
19+
files: ^src/
1420
- repo: https://github.com/PyCQA/isort
1521
rev: 8.0.1
1622
hooks:

pyproject.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ optional-dependencies.dev = [
117117
"pyink==25.12",
118118
"pylint>=2.6",
119119
"pyproject-fmt==2.24",
120+
"ruff==0.15.17",
120121
"tox>=4.23.2",
121122
"tox-uv>=1.33.2",
122123
]
@@ -260,6 +261,25 @@ module.include = [ "py.typed" ]
260261
sdist.include = [ "src/**/*", "README.md", "pyproject.toml", "LICENSE" ]
261262
sdist.exclude = [ "src/**/*.sh", "src/**/README.md" ]
262263

264+
[tool.ruff]
265+
extend-exclude = [
266+
"src/google/adk/cli/browser/",
267+
# These hardcode googleapis.com endpoints and trip the check-file-contents
268+
# mTLS policy check the moment they change. Excluded so unused-import
269+
# cleanup does not pull them into a PR; clean them up when the mTLS policy
270+
# is addressed.
271+
"src/google/adk/integrations/bigquery/bigquery_credentials.py",
272+
"src/google/adk/integrations/bigquery/data_insights_tool.py",
273+
"src/google/adk/plugins/bigquery_agent_analytics_plugin.py",
274+
"src/google/adk/tools/data_agent/data_agent_tool.py",
275+
"src/google/adk/v1/",
276+
"v1_tests/",
277+
]
278+
lint.select = [ "F401" ]
279+
# __init__.py files re-export symbols for the public API; unused imports
280+
# there are intentional, not dead code.
281+
lint.per-file-ignores."**/__init__.py" = [ "F401" ]
282+
263283
[tool.isort]
264284
profile = "google"
265285
line_length = 200

scripts/run_precommit_checks.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ class HookSpec:
9595
# from its .pre-commit-hooks.yaml. The `local` hooks (addlicense,
9696
# check-new-py-prefix) are handled by _LOCAL_HOOKS below instead.
9797
_HOOK_SPECS: dict[str, HookSpec] = {
98+
'ruff': HookSpec(
99+
['ruff', 'check', '--force-exclude'],
100+
['ruff', 'check', '--fix', '--force-exclude'],
101+
_PY,
102+
),
98103
'isort': HookSpec(['isort', '--check-only', '--diff'], ['isort'], _PY),
99104
'pyink': HookSpec(['pyink', '--check', '--diff'], ['pyink'], _PY),
100105
'pyproject-fmt': HookSpec(
@@ -317,7 +322,11 @@ def run_standard_hook(
317322
if spec.is_fixer and not fix:
318323
return _run_fixer_in_check_mode(tool, files)
319324
command = spec.fix_cmd if (fix and spec.fix_cmd) else spec.check_cmd
320-
return _run(command + hook.args, files)
325+
# Drop `--fix` from the config args: check mode must not modify files, and
326+
# fix mode already gets `--fix` from the spec's fix_cmd (passing it twice is
327+
# an error, e.g. ruff rejects a repeated `--fix`).
328+
args = [a for a in hook.args if a != '--fix']
329+
return _run(command + args, files)
321330

322331

323332
# --- local hooks (no upstream tool; bespoke handling) -----------------------

0 commit comments

Comments
 (0)