Skip to content

Add apply() method to Tool for running formatters on use#1714

Merged
nathanjmcdougall merged 5 commits intomainfrom
copilot/add-apply-method-to-tool
Apr 1, 2026
Merged

Add apply() method to Tool for running formatters on use#1714
nathanjmcdougall merged 5 commits intomainfrom
copilot/add-apply-method-to-tool

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 1, 2026

Formatters like pyproject-fmt and ruff format should run automatically when added to a project via use, so the codebase is immediately formatted to the configured style.

Changes

  • Tool.apply() base method — No-op by default on the Tool Protocol class. Formatters override to invoke their CLI.
  • PyprojectFmtTool.apply() — Runs uv run pyproject-fmt pyproject.toml with tick_print message. Guards on uv backend; suppresses subprocess errors (tool may not yet be installed in frozen mode).
  • RuffTool.apply() — Runs uv run ruff format (not linter auto-fixes). Additionally guards on is_formatter_used().
  • use_pyproject_fmt() / use_ruff() — Call tool.apply() after add_configs(), before print_how_to_use().
  • Tests — Updated 6 existing output assertions, added 5 new unit tests covering both backends and formatter detection states. 100% coverage on new code.
  • Docs — Updated README and detailed-example to reflect the new ✔ Running the Ruff formatter. / ✔ Running pyproject-fmt on 'pyproject.toml'. output lines.
# Pattern for formatter tools:
@override
def apply(self) -> None:
    if get_backend() is not BackendEnum.uv:
        return
    tick_print("Running the Ruff formatter.")
    with contextlib.suppress(UVSubprocessFailedError):
        call_uv_subprocess(["run", "ruff", "format"], change_toml=False)

Copilot AI and others added 2 commits April 1, 2026 11:00
Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/11b1f2fc-5acd-4f62-8486-4fcde33d4b96

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
Copilot AI changed the title [WIP] Add apply method to Tool for automatic formatting Add apply() method to Tool for running formatters on use Apr 1, 2026
Copilot AI requested a review from nathanjmcdougall April 1, 2026 11:06
@nathanjmcdougall nathanjmcdougall marked this pull request as ready for review April 1, 2026 11:11
…tailed-example.md file

Clarified the description of the Ruff formatter application in the documentation.
Comment thread src/usethis/_tool/impl/base/ruff.py Outdated
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 1, 2026

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!

… review

Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/f0d93686-fbf1-44d0-b76a-3d1b41a11dd1

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

codspeed-hq bot commented Apr 1, 2026

Merging this PR will not alter performance

✅ 2 untouched benchmarks


Comparing copilot/add-apply-method-to-tool (489596b) with main (17afa6b)1

Open in CodSpeed

Footnotes

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

@nathanjmcdougall nathanjmcdougall merged commit e057ee9 into main Apr 1, 2026
20 checks passed
@nathanjmcdougall nathanjmcdougall deleted the copilot/add-apply-method-to-tool branch April 1, 2026 11:36
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.

Run formatters automatically when "use" is invoked

2 participants