From 9597c2c638872f5f453cee8c4fb9957a4175e21c Mon Sep 17 00:00:00 2001 From: Amethyst Reese Date: Fri, 21 Jun 2024 09:54:25 -0700 Subject: [PATCH] Allow a targeted (named rule) enable to override a module disable (#464) 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 --- src/fixit/config.py | 14 ++++++++++++-- src/fixit/tests/config.py | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/fixit/config.py b/src/fixit/config.py index af1b7e60..29c64012 100644 --- a/src/fixit/config.py +++ b/src/fixit/config.py @@ -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: @@ -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: diff --git a/src/fixit/tests/config.py b/src/fixit/tests/config.py index c741375a..35fcdeaa 100644 --- a/src/fixit/tests/config.py +++ b/src/fixit/tests/config.py @@ -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(