Skip to content

Commit

Permalink
Allow a targeted (named rule) enable to override a module disable
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>

ghstack-source-id: 51fad59fc8536a5447abe88ececa40e27a49ef96
Pull Request resolved: #463
  • Loading branch information
amyreese committed Jun 21, 2024
1 parent 4cd8a6e commit 396a01f
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 396a01f

Please sign in to comment.