Skip to content

Commit

Permalink
Automatically disable SIM108 for projects (#153)
Browse files Browse the repository at this point in the history
* Automatically disable SIM108 for projects

* Update test
  • Loading branch information
nathanjmcdougall authored Jan 9, 2025
1 parent 277ea6e commit 10fdb08
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 7 additions & 2 deletions src/usethis/_core/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()

Expand Down
25 changes: 25 additions & 0 deletions src/usethis/_integrations/ruff/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand All @@ -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
1 change: 1 addition & 0 deletions tests/usethis/_core/test_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down

0 comments on commit 10fdb08

Please sign in to comment.