Skip to content

Add PreCommitConfig.from_system_hook() to deduplicate backend dispatch in tool specs#1606

Merged
nathanjmcdougall merged 4 commits into
mainfrom
copilot/functionalize-system-hook-config-method
Mar 27, 2026
Merged

Add PreCommitConfig.from_system_hook() to deduplicate backend dispatch in tool specs#1606
nathanjmcdougall merged 4 commits into
mainfrom
copilot/functionalize-system-hook-config-method

Conversation

Copilot AI commented Mar 27, 2026

Copy link
Copy Markdown
Contributor

Three tool specs (ty, deptry, import_linter) each duplicated ~40 lines of identical backend dispatch logic to construct a local system hook PreCommitConfig. This adds a single classmethod that encapsulates the pattern.

New method

PreCommitConfig.from_system_hook() handles backend dispatch (prefixing uv run --frozen --offline for uv), system language resolution, and LocalRepo construction internally. It takes only two parameters (hook_id and entry) and hardcodes the common defaults (always_run=True, pass_filenames=False, requires_venv=True, inform_how_to_use_on_migrate=False):

# Before: ~40 lines of if/elif/else per tool
backend = get_backend()
if backend is BackendEnum.uv:
    return PreCommitConfig.from_single_repo(
        pre_commit_schema.LocalRepo(repo="local", hooks=[
            pre_commit_schema.HookDefinition(
                id="ty", name="ty",
                entry="uv run --frozen --offline ty check",
                language=get_system_language(), ...
            )
        ]),
        requires_venv=True, inform_how_to_use_on_migrate=False,
    )
elif backend is BackendEnum.none:
    # Same thing without the uv prefix...
else:
    assert_never(backend)

# After
return PreCommitConfig.from_system_hook(
    hook_id="ty",
    entry="ty check",
)

Changes

  • src/usethis/_tool/pre_commit.py — Added from_system_hook() classmethod with 2 params (hook_id, entry), hardcoding always_run=True, pass_filenames=False
  • src/usethis/_tool/impl/spec/{ty,deptry,import_linter}.py — Replaced backend dispatch blocks with single from_system_hook() calls; removed now-unused imports (get_backend, assert_never, BackendEnum, pre_commit_schema, get_system_language)
  • tests/usethis/_tool/test_pre_commit.py — 10 tests covering both backends, all hook fields, and hardcoded defaults

📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.

Copilot AI and others added 2 commits March 27, 2026 12:06
Copilot AI changed the title [WIP] Functionalize system hook config method Add PreCommitConfig.from_system_hook() to deduplicate backend dispatch in tool specs Mar 27, 2026
Copilot AI requested a review from nathanjmcdougall March 27, 2026 12:11
Comment thread src/usethis/_tool/pre_commit.py Outdated
@nathanjmcdougall nathanjmcdougall marked this pull request as ready for review March 27, 2026 12:59
@codecov

codecov Bot commented Mar 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

@codspeed-hq

codspeed-hq Bot commented Mar 27, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 2 untouched benchmarks


Comparing copilot/functionalize-system-hook-config-method (a48ac59) with main (ee85faf)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (04719d1) during the generation of this report, so ee85faf was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@nathanjmcdougall nathanjmcdougall merged commit 23296c2 into main Mar 27, 2026
20 checks passed
@nathanjmcdougall nathanjmcdougall deleted the copilot/functionalize-system-hook-config-method branch March 27, 2026 13:16
@nathanjmcdougall nathanjmcdougall linked an issue Mar 28, 2026 that may be closed by this pull request
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.

Functionalize system hook config method

2 participants