Skip to content

Commit

Permalink
Allow a targeted (named rule) enable to override a module disable (#464)
Browse files Browse the repository at this point in the history
This allows the following configuration to work as a way of disabling
all of the builtin rules except for specific ones:

```
[tool.fixit]
disable = ["fixit.rules"]
enable = ["fixit.rules:UseClsInClassmethod"]
```

Co-authored-by: surge119 <ly.sergio480@gmail.com>
  • Loading branch information
amyreese and surge119 authored Jun 21, 2024
1 parent 4cd8a6e commit 9597c2c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/fixit/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ def collect_rules(
"""

all_rules: Set[Type[LintRule]] = set()
named_enables: Set[Type[LintRule]] = set()
if debug_reasons is not None:
disabled_rules = debug_reasons
else:
Expand All @@ -222,10 +223,19 @@ def collect_rules(
stack.enter_context(append_sys_path(path))

for qualified_rule in config.enable:
all_rules |= set(find_rules(qualified_rule))
rules = set(find_rules(qualified_rule))
if qualified_rule.name:
named_enables |= rules
all_rules |= rules

for qualified_rule in config.disable:
disabled_rules.update({r: "disabled" for r in find_rules(qualified_rule)})
disabled_rules.update(
{
r: "disabled"
for r in find_rules(qualified_rule)
if r not in named_enables
}
)
all_rules -= set(disabled_rules)

if config.tags:
Expand Down
18 changes: 18 additions & 0 deletions src/fixit/tests/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,24 @@ def collect_types(cfg: Config) -> List[Type[LintRule]]:
)
self.assertListEqual([UseClsInClassmethod], rules)

with self.subTest("disable builtins"):
rules = collect_types(
Config(
disable=[QualifiedRule("fixit.rules")],
python_version=None,
)
)
self.assertListEqual([], rules)

with self.subTest("override broad opt-out"):
rules = collect_types(
Config(
disable=[QualifiedRule("fixit.rules")],
enable=[QualifiedRule("fixit.rules", "UseClsInClassmethod")],
)
)
self.assertListEqual([UseClsInClassmethod], rules)

with self.subTest("version match"):
rules = collect_types(
Config(
Expand Down

0 comments on commit 9597c2c

Please sign in to comment.