diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 661710b..fe252f6 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -3,7 +3,7 @@ repos: rev: v0.23 hooks: - id: validate-pyproject - additional_dependencies: ["validate-pyproject-schema-store[all]"] + additional_dependencies: ['validate-pyproject-schema-store[all]'] - repo: https://github.com/tox-dev/pyproject-fmt rev: v2.5.0 hooks: diff --git a/src/usethis/_core/tool.py b/src/usethis/_core/tool.py index 2a6986c..5513c67 100644 --- a/src/usethis/_core/tool.py +++ b/src/usethis/_core/tool.py @@ -7,14 +7,17 @@ ) from usethis._console import box_print from usethis._integrations.pre_commit.core import ( - # add_pre_commit_config_file, install_pre_commit_hooks, remove_pre_commit_config, uninstall_pre_commit_hooks, ) from usethis._integrations.pre_commit.hooks import add_placeholder_hook, get_hook_names from usethis._integrations.pytest.core import add_pytest_dir, remove_pytest_dir -from usethis._integrations.ruff.rules import deselect_ruff_rules, select_ruff_rules +from usethis._integrations.ruff.rules import ( + deselect_ruff_rules, + ignore_ruff_rules, + select_ruff_rules, +) from usethis._integrations.uv.deps import add_deps_to_group, remove_deps_from_group from usethis._tool import ( ALL_TOOLS, @@ -157,11 +160,13 @@ def use_ruff(*, remove: bool = False) -> None: for _tool in ALL_TOOLS: if _tool.is_used(): rules += _tool.get_associated_ruff_rules() + ignored_rules = ["SIM108"] if not remove: add_deps_to_group(tool.dev_deps, "dev") tool.add_pyproject_configs() select_ruff_rules(rules) + ignore_ruff_rules(ignored_rules) if PreCommitTool().is_used(): tool.add_pre_commit_repo_configs() diff --git a/src/usethis/_integrations/ruff/rules.py b/src/usethis/_integrations/ruff/rules.py index 0941cc0..caed5a7 100644 --- a/src/usethis/_integrations/ruff/rules.py +++ b/src/usethis/_integrations/ruff/rules.py @@ -20,6 +20,20 @@ def select_ruff_rules(rules: list[str]) -> None: append_config_list(["tool", "ruff", "lint", "select"], rules) +def ignore_ruff_rules(rules: list[str]) -> None: + """Ignore ruff rules in the project.""" + rules = sorted(set(rules) - set(get_ignored_ruff_rules())) + + if not rules: + return + + rules_str = ", ".join([f"'{rule}'" for rule in rules]) + s = "" if len(rules) == 1 else "s" + tick_print(f"Ignoring ruff rule{s} {rules_str} in 'pyproject.toml'.") + + append_config_list(["tool", "ruff", "lint", "ignore"], rules) + + def deselect_ruff_rules(rules: list[str]) -> None: """Ensure ruff rules are not selected in the project.""" @@ -44,3 +58,14 @@ def get_ruff_rules() -> list[str]: rules = [] return rules + + +def get_ignored_ruff_rules() -> list[str]: + """Get the ruff rules ignored in the project.""" + + try: + rules: list[str] = get_config_value(["tool", "ruff", "lint", "ignore"]) + except KeyError: + rules = [] + + return rules diff --git a/tests/usethis/_core/test_tool.py b/tests/usethis/_core/test_tool.py index c1cffa4..22e61a7 100644 --- a/tests/usethis/_core/test_tool.py +++ b/tests/usethis/_core/test_tool.py @@ -786,6 +786,7 @@ def test_stdout( "✔ Adding 'ruff' to the 'dev' dependency group.\n" "✔ Adding ruff config to 'pyproject.toml'.\n" "✔ Enabling ruff rules 'A', 'C4', 'E4', 'E7', 'E9', 'F', 'FURB', 'I', 'PLE', \n'PLR', 'RUF', 'SIM', 'UP' in 'pyproject.toml'.\n" + "✔ Ignoring ruff rule 'SIM108' in 'pyproject.toml'.\n" "☐ Call the 'ruff check --fix' command to run the ruff linter with autofixes.\n" "☐ Call the 'ruff format' command to run the ruff formatter.\n" )