Skip to content

Allow independent inclusion of the ruff linter and formatter #580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

nathanjmcdougall
Copy link
Collaborator

No description provided.

@nathanjmcdougall nathanjmcdougall linked an issue Apr 26, 2025 that may be closed by this pull request
Copy link

codspeed-hq bot commented Apr 26, 2025

CodSpeed Performance Report

Merging #580 will not alter performance

Comparing 561-allow-independent-inclusion-of-the-ruff-linter-and-formatter (a278864) with main (42fee40)

Summary

✅ 1 untouched benchmarks

Copy link

codecov bot commented Apr 26, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.54%. Comparing base (c3bd088) to head (a278864).
Report is 3 commits behind head on main.

✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #580      +/-   ##
==========================================
+ Coverage   97.51%   97.54%   +0.02%     
==========================================
  Files         102      102              
  Lines        4463     4515      +52     
==========================================
+ Hits         4352     4404      +52     
  Misses        111      111              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@nathanjmcdougall nathanjmcdougall marked this pull request as ready for review April 27, 2025 01:59
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR allows users to enable the Ruff linter and formatter independently by introducing separate flags across the tool’s implementation and CLI interface.

  • Introduces new linter and formatter parameters to RuffTool and updates its methods accordingly.
  • Adjusts pre-commit repository definitions, Bitbucket step generation, and test expectations to match the new behavior.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
tests/usethis/_core/test_core_tool.py Updates expected hook removal message order to match the new hook ID assignment.
src/usethis/_tool/impl/ruff.py Adds independent toggles for linter/formatter and adjusts hook definitions and steps.
src/usethis/_interface/tool.py Updates the CLI options for the ruff command to pass the new linter and formatter flags.
src/usethis/_core/tool.py Modifies use_ruff to accept and forward the new flags; updates rule config logic accordingly.
Comments suppressed due to low confidence (1)

src/usethis/_core/tool.py:350

  • Consider using the existing 'tool' instance (i.e. tool.get_selected_rules()) instead of creating a new RuffTool instance to ensure the correct linter/formatter configuration is used.
or not RuffTool().get_selected_rules()

@nathanjmcdougall
Copy link
Collaborator Author

I think this needs some more thought. The attributes self.using_linter and self.using_formatter will be set to defaults when using RuffTool outside of a use_ruff context - e.g. in integrations with pre-commit etc.
I wonder whether we need a heuristic e.g. to see whether tool.ruff.lint config is set.

Bearing in mind that the user might be using the Ruff formatter even if they haven't used usethis tool ruff to do it, so we shouldn't assume too much.

@nathanjmcdougall
Copy link
Collaborator Author

Two options - [tool.usethis] configuration to over-ride the heuristic. Or - add dummy default config for the formatter to use instead.

@nathanjmcdougall
Copy link
Collaborator Author

[tool.ruff.format.docstring-code-format] could be a good candidate

@nathanjmcdougall
Copy link
Collaborator Author

nathanjmcdougall commented May 19, 2025

However, I think we need to be realistic about how common it will be for the ruff formatter to be used without any configuration... A lack of config shouldn't be a sign that the formatter isn't being used necessarily. On the other hand, if the linter doesn't have any config, that's another matter.

@nathanjmcdougall
Copy link
Collaborator Author

Ultimately, there needs to be some heuristic to determine whether each of the linter and formatter are being used. The exact heuristic that is used might not be clear yet. But that shouldn't affect this re-architecting. For now, I will keep it simple and rely on the presence of a [tool.ruff.format] section for the formatter. I'll seek some advice with others regarding other heuristic options.

This might be perceived as a breaking change, since users without the [tool.ruff.format] section will not get the formatter integrations anymore, e.g. with usethis tool pre-commit. I think that's okay though, because if they run usethis tool ruff the relevant section will be added. Also the only alternative I can think of is a usethis-bespoke configuration to opt-out of using the ruff formatter integrations, but that seems quite unpleasant.

@nathanjmcdougall nathanjmcdougall requested a review from Copilot May 26, 2025 11:55
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enables users to include the Ruff linter and formatter independently by introducing --linter and --formatter flags throughout the CLI, core logic, and tool implementation. Key changes include:

  • Adding force_linter/force_formatter flags and subtool detection to RuffTool
  • Extending the CLI (usethis tool ruff) and use_ruff core function to accept and pass through new flags
  • Updating tests to cover default, linter-only, and formatter-only scenarios, and adjusting removal output order

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/usethis/_tool/impl/test_ruff.py Added tests for default, linter-only, and formatter-only cases
tests/usethis/_core/test_core_tool.py Introduced tests for subtool-only addition and reordered removal output
src/usethis/_tool/impl/ruff.py Enhanced RuffTool with conditional config, hooks, and CI steps based on subtool flags
src/usethis/_interface/tool.py Added --linter/--formatter CLI options and passed them into _run_tool
src/usethis/_core/tool.py Updated use_ruff signature and logic to use force_linter/force_formatter
Comments suppressed due to low confidence (2)

src/usethis/_core/tool.py:415

  • The removal branch always drops both linter and formatter artifacts, ignoring the linter/formatter flags. Update this to conditionally remove only the requested subtool (e.g., wrap remove_pre_commit_repo_configs, remove_configs, etc., in if linter: or if formatter: checks).
else:

src/usethis/_interface/tool.py:249

  • Any is only imported under if TYPE_CHECKING, so this runtime annotation will raise a NameError. Move from typing import Any out of the TYPE_CHECKING block or quote the annotation to defer evaluation.
def _run_tool(caller: UseToolFunc, *, remove: bool, how: bool, **kwargs: Any):

@nathanjmcdougall nathanjmcdougall requested a review from Copilot June 2, 2025 03:42
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enables independent inclusion and removal of Ruff’s linter and formatter by updating both the underlying tool configuration and the CI pipelines.

  • Updated test cases to assert independent detection of linter and formatter usage.
  • Modified the command‐line interface to support distinct linter/formatter options.
  • Refactored the RuffTool class to add detection logic and adjust configuration specifications accordingly.

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/usethis/_tool/impl/test_ruff.py Added tests for independent detection of linter and formatter configurations.
tests/usethis/_interface/test_ci.py Updated CI test output to include the Ruff Formatter.
tests/usethis/_core/test_core_ci.py Enhanced Bitbucket pipeline tests with Ruff Formatter steps.
src/usethis/_tool/impl/ruff.py Refactored RuffTool to accept separate detection parameters and adjusted config and pre-commit repo settings.
src/usethis/_tool/base.py Optimized config spec retrieval by reusing a stored variable for maintainability.
src/usethis/_interface/tool.py Extended the CLI, adding options for linter and formatter selection.
src/usethis/_integrations/ci/bitbucket/steps.py Updated Bitbucket steps to include running Ruff Formatter.
src/usethis/_core/tool.py Modified the use_ruff function to pass linter/formatter options to RuffTool, adjusting behavior for adding and removal.
Comments suppressed due to low confidence (2)

src/usethis/_tool/impl/ruff.py:208

  • The hook IDs have been swapped (linter now uses 'ruff' and formatter uses 'ruff-format'), which might be confusing. Please confirm that these IDs align with the project's naming conventions and update comments if necessary.
id="ruff",

src/usethis/_tool/impl/ruff.py:535

  • The auto detection logic now hinges on the absence of both subtool configurations. Consider adding test cases to cover scenarios where only one configuration is present versus none to ensure the logic behaves as expected.
def is_no_subtool_config_present(self) -> bool:

@nathanjmcdougall nathanjmcdougall merged commit c94f471 into main Jun 2, 2025
21 checks passed
@nathanjmcdougall nathanjmcdougall deleted the 561-allow-independent-inclusion-of-the-ruff-linter-and-formatter branch June 2, 2025 03:44
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.

Allow independent inclusion of the Ruff linter and formatter
1 participant