Skip to content

Commit d4f98ea

Browse files
Don't duplicate ignored rules matching glob
1 parent 6d6cb4f commit d4f98ea

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

src/usethis/_tool/impl/ruff.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,8 @@ def get_ignored_rules(self) -> list[Rule]:
433433

434434
def ignore_rules_in_glob(self, rules: list[Rule], *, glob: str) -> None:
435435
"""Ignore Ruff rules in the project for a specific glob pattern."""
436+
rules = sorted(set(rules) - set(self.get_ignored_rules_in_glob(glob)))
437+
436438
if not rules:
437439
return
438440

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

452+
def get_ignored_rules_in_glob(self, glob: str) -> list[Rule]:
453+
"""Get the Ruff rules ignored in the project for a specific glob pattern."""
454+
(file_manager,) = self.get_active_config_file_managers()
455+
keys = self._get_per_file_ignore_keys(file_manager, glob=glob)
456+
try:
457+
rules: list[Rule] = file_manager[keys]
458+
except (KeyError, FileNotFoundError):
459+
rules = []
460+
461+
return rules
462+
450463
def apply_rule_config(self, rule_config: RuleConfig) -> None:
451464
"""Apply the Ruff rules associated with a rule config to the project.
452465

tests/usethis/_core/test_core_tool.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,6 +1447,34 @@ def test_inp_rules_selected(self, tmp_path: Path):
14471447
# Assert
14481448
assert "INP" in RuffTool().get_selected_rules()
14491449

1450+
@pytest.mark.usefixtures("_vary_network_conn")
1451+
def test_no_duplicate_inp_rules(self, tmp_path: Path):
1452+
# https://github.com/usethis-python/usethis-python/issues/935
1453+
1454+
# Arrange
1455+
(tmp_path / "ruff.toml").touch()
1456+
1457+
with change_cwd(tmp_path), files_manager():
1458+
# ... preparing for duplicate call
1459+
use_import_linter()
1460+
1461+
# Act - duplicate
1462+
use_import_linter()
1463+
1464+
# Assert
1465+
assert (
1466+
len(
1467+
[
1468+
rule
1469+
for rule in RuffTOMLManager()[
1470+
["lint", "per-file-ignores", "tests/**"]
1471+
]
1472+
if rule == "INP"
1473+
]
1474+
)
1475+
== 1
1476+
)
1477+
14501478
@pytest.mark.usefixtures("_vary_network_conn")
14511479
def test_inp_rules_not_selected_for_tests_dir(
14521480
self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str]

0 commit comments

Comments
 (0)