Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/usethis/_tool/impl/ruff.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,8 @@ def get_ignored_rules(self) -> list[Rule]:

def ignore_rules_in_glob(self, rules: list[Rule], *, glob: str) -> None:
"""Ignore Ruff rules in the project for a specific glob pattern."""
rules = sorted(set(rules) - set(self.get_ignored_rules_in_glob(glob)))

if not rules:
return

Expand All @@ -447,6 +449,17 @@ def ignore_rules_in_glob(self, rules: list[Rule], *, glob: str) -> None:
keys = self._get_per_file_ignore_keys(file_manager, glob=glob)
file_manager.extend_list(keys=keys, values=rules)

def get_ignored_rules_in_glob(self, glob: str) -> list[Rule]:
"""Get the Ruff rules ignored in the project for a specific glob pattern."""
(file_manager,) = self.get_active_config_file_managers()
keys = self._get_per_file_ignore_keys(file_manager, glob=glob)
try:
rules: list[Rule] = file_manager[keys]
except (KeyError, FileNotFoundError):
rules = []

return rules

def apply_rule_config(self, rule_config: RuleConfig) -> None:
"""Apply the Ruff rules associated with a rule config to the project.

Expand Down
28 changes: 28 additions & 0 deletions tests/usethis/_core/test_core_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -1447,6 +1447,34 @@ def test_inp_rules_selected(self, tmp_path: Path):
# Assert
assert "INP" in RuffTool().get_selected_rules()

@pytest.mark.usefixtures("_vary_network_conn")
def test_no_duplicate_inp_rules(self, tmp_path: Path):
# https://github.com/usethis-python/usethis-python/issues/935

# Arrange
(tmp_path / "ruff.toml").touch()

with change_cwd(tmp_path), files_manager():
# ... preparing for duplicate call
use_import_linter()

# Act - duplicate
use_import_linter()

# Assert
assert (
len(
[
rule
for rule in RuffTOMLManager()[
["lint", "per-file-ignores", "tests/**"]
]
if rule == "INP"
]
)
== 1
)

@pytest.mark.usefixtures("_vary_network_conn")
def test_inp_rules_not_selected_for_tests_dir(
self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str]
Expand Down