Skip to content

refactor: hoist shared if-else logic; document principle in skill#1890

Merged
nathanjmcdougall merged 4 commits into
mainfrom
copilot/refactor-uninstall-pre-commit-hooks-logic
Apr 8, 2026
Merged

refactor: hoist shared if-else logic; document principle in skill#1890
nathanjmcdougall merged 4 commits into
mainfrom
copilot/refactor-uninstall-pre-commit-hooks-logic

Conversation

Copilot AI commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

When an if-elif chain branches on a backend enum, identical statements appearing in multiple sibling branches signal a structural problem — the shared logic should be hoisted to an outer combined membership check.

Code fix (src/usethis/_deps.py)

remove_deps_from_group() had the same tick_print call duplicated verbatim in both the uv and poetry branches:

# Before: identical tick_print in two sibling branches
if backend is BackendEnum.uv:
    tick_print(f"Removing dependenc{ies} {deps_str} from the '{group}' group in 'pyproject.toml'.")
    for dep in _deps:
        remove_dep_from_group_via_uv(dep, group)
elif backend is BackendEnum.poetry:
    tick_print(f"Removing dependenc{ies} {deps_str} from the '{group}' group in 'pyproject.toml'.")
    for dep in _deps:
        remove_dep_from_group_via_poetry(dep, group)

# After: shared message hoisted; distinguishing logic nested inside
if backend in (BackendEnum.uv, BackendEnum.poetry):
    tick_print(f"Removing dependenc{ies} {deps_str} from the '{group}' group in 'pyproject.toml'.")
    if backend is BackendEnum.uv:
        for dep in _deps:
            remove_dep_from_group_via_uv(dep, group)
    elif backend is BackendEnum.poetry:
        for dep in _deps:
            remove_dep_from_group_via_poetry(dep, group)
    else:
        assert_never(backend)

Skill update (.agents/skills/usethis-python-code/SKILL.md, v1.9 → v1.10)

Added section "Structuring if-else to avoid duplicated logic" — the generalised principle, a procedure, a before/after example, and common mistakes (including the nested-guard variant where the same guard condition is independently checked inside each backend branch).

…lse nesting skill guidance

Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/666ce694-85bc-4b4e-9d64-4dd29447b897

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor uninstall_pre_commit_hooks to eliminate duplicated string literals refactor: hoist shared if-else logic; document principle in skill Apr 8, 2026
Copilot AI requested a review from nathanjmcdougall April 8, 2026 00:40
@nathanjmcdougall nathanjmcdougall marked this pull request as ready for review April 8, 2026 00:44
@codspeed-hq

codspeed-hq Bot commented Apr 8, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 3 untouched benchmarks


Comparing copilot/refactor-uninstall-pre-commit-hooks-logic (d5958d5) with main (9c6a728)

Open in CodSpeed

@codecov

codecov Bot commented Apr 8, 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!

Also fix priority config issues in prek
@nathanjmcdougall nathanjmcdougall merged commit 5f17d9a into main Apr 8, 2026
20 checks passed
@nathanjmcdougall nathanjmcdougall deleted the copilot/refactor-uninstall-pre-commit-hooks-logic branch April 8, 2026 02:43
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.

agent: structure if-else nesting to avoid duplicated string literals

2 participants