Skip to content

Use pipeweld to insert pre-commit hooks#1595

Merged
nathanjmcdougall merged 11 commits intomainfrom
copilot/better-respect-pre-commit-repos
Apr 1, 2026
Merged

Use pipeweld to insert pre-commit hooks#1595
nathanjmcdougall merged 11 commits intomainfrom
copilot/better-respect-pre-commit-repos

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 27, 2026

Hook insertion in add_repo() used hand-rolled precedent/successor logic against a static _HOOK_ORDER list. This replaces it with pipeweld's Adder, which models the same ordering constraints as a series-parallel graph — laying groundwork for repo-aware insertion and prek priority support.

Changes

  • func.pyAdder: Added force_linear setting that post-processes the solution to flatten any Parallel nodes into a deterministic linear Series. Added _extract_ordered_steps() and _linearize_component() helpers internal to the pipeweld layer.
  • func.pyget_predecessor(): Added a public function that recursively walks the full component tree (str, Series, Parallel, DepGroup, nested structures) to find the step that immediately precedes a given step. Returns None if the step is first, raises ValueError if not found.
  • hooks.pyadd_repo(): Build a Series pipeline from existing hook IDs, derive prerequisites/postrequisites sets from _HOOK_ORDER, delegate insertion positioning to Adder.add() with force_linear=True, and use get_predecessor() to extract the predecessor from the solution.
  • test_func.py: Unit tests for _extract_ordered_steps, _linearize_component, Adder with force_linear=True, and get_predecessor() with comprehensive coverage across all component types and nesting depths (32 new tests)
  • test_hooks.py: Integration tests exercising pipeweld-based insertion (prerequisite present, postrequisite only, mixed non-dependent/postrequisite)

Before/After

# Before: manual list slicing
precedents = _HOOK_ORDER[:hook_idx]
successors = _HOOK_ORDER[hook_idx + 1:]
existing_precedents = [h for h in existing_hooks if h in precedents]
# ... 15 lines of if/elif/else to find last_precedent

# After: pipeweld solver
adder = Adder(
    pipeline=series(*existing_hooks),
    step=hook_config.id,
    prerequisites=set(_HOOK_ORDER[:hook_idx]),
    postrequisites=set(_HOOK_ORDER[hook_idx + 1:]),
    force_linear=True,
)
result = adder.add()
predecessor = get_predecessor(result.solution, hook_config.id)

All existing tests pass unchanged — behavioral compatibility is preserved for current hook orderings.


⌨️ Start Copilot coding agent tasks without leaving your editor — available in VS Code, Visual Studio, JetBrains IDEs and Eclipse.

Copilot AI and others added 2 commits March 27, 2026 10:26
Replace manual precedent/successor ordering logic in add_repo() with
pipeweld's Adder algorithm. This uses the Series-Parallel graph model
to determine the correct insertion position based on prerequisites and
postrequisites derived from _HOOK_ORDER.

Add _get_predecessor_from_solution() and _linearize() helper functions
to extract a linear hook ordering from pipeweld's solution pipeline.
Within parallel groups, existing hooks maintain their original order
and the new hook is placed after them.

Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/babc26ed-68ad-44e8-8a8c-00c72d32ddba

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/babc26ed-68ad-44e8-8a8c-00c72d32ddba

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix handling of pre-commit repos using pipeweld Use pipeweld to insert pre-commit hooks Mar 27, 2026
Copilot AI requested a review from nathanjmcdougall March 27, 2026 10:32
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 27, 2026

Codecov Report

❌ Patch coverage is 97.01493% with 2 lines in your changes missing coverage. Please review.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/usethis/_pipeweld/func.py 96.61% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Mar 27, 2026

Merging this PR will not alter performance

✅ 2 untouched benchmarks


Comparing copilot/better-respect-pre-commit-repos (c734d83) with main (bbd11fe)

Open in CodSpeed

@nathanjmcdougall
Copy link
Copy Markdown
Collaborator

@copilot static checks are failing

Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/a1927db8-7b00-4ea4-b7a8-23c4626ad979

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 28, 2026

@copilot static checks are failing

Fixed in cb3d129 — moved DepGroup import into the TYPE_CHECKING block to resolve the TC001 ruff error. It's only used in type annotations (not in any isinstance() check), so it doesn't need a runtime import.

@nathanjmcdougall nathanjmcdougall marked this pull request as ready for review March 28, 2026 23:51
Comment thread src/usethis/_integrations/pre_commit/hooks.py Outdated
Copilot AI and others added 2 commits March 29, 2026 21:43
…linear setting

Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/17dc4709-1485-40c9-916d-b68f146ec22f

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
Comment thread src/usethis/_integrations/pre_commit/hooks.py Outdated
…sor()

Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/8826449d-79c0-497c-82e8-e05993afcb6a

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 1, 2026

@copilot static checks are failing

Static checks are all passing as of fb5d9c0 — full prek run clean.

@nathanjmcdougall nathanjmcdougall merged commit e41efcf into main Apr 1, 2026
20 checks passed
@nathanjmcdougall nathanjmcdougall deleted the copilot/better-respect-pre-commit-repos branch April 1, 2026 19:29
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.

Better respect pre-commit repos by using pipeweld to insert pre-commit hooks

2 participants