diff --git a/src/flake8/main/application.py b/src/flake8/main/application.py index b9a0e66..97df523 100644 --- a/src/flake8/main/application.py +++ b/src/flake8/main/application.py @@ -119,8 +119,8 @@ class Application: """ raw = finder.find_plugins(cfg) local_plugin_paths = finder.find_local_plugin_paths(cfg, cfg_dir) - enabled = finder.parse_enabled(cfg, enable_extensions) - self.plugins = finder.load_plugins(raw, local_plugin_paths, enabled) + opts = finder.parse_plugin_options(cfg, enable_extensions) + self.plugins = finder.load_plugins(raw, local_plugin_paths, opts) def register_plugin_options(self) -> None: """Register options provided by plugins to our option manager.""" diff --git a/src/flake8/plugins/finder.py b/src/flake8/plugins/finder.py index 6df0ccc..3157369 100644 --- a/src/flake8/plugins/finder.py +++ b/src/flake8/plugins/finder.py @@ -5,12 +5,12 @@ import logging import sys from typing import Any from typing import Dict +from typing import FrozenSet from typing import Generator from typing import Iterable from typing import List from typing import NamedTuple from typing import Optional -from typing import Set from flake8 import utils from flake8._compat import importlib_metadata @@ -179,22 +179,43 @@ def find_local_plugin_paths( return utils.normalize_paths(paths, cfg_dir) -def parse_enabled( +class PluginOptions(NamedTuple): + """Options related to plugin loading.""" + + enable_extensions: FrozenSet[str] + # TODO: more options here! + # require_plugins: Tuple[str, ...] + + +def _parse_option( cfg: configparser.RawConfigParser, - enable_extensions: Optional[str], -) -> Set[str]: - """Parse --enable-extensions.""" - if enable_extensions is not None: - return set(utils.parse_comma_separated_list(enable_extensions)) + cfg_opt_name: str, + opt: Optional[str], +) -> List[str]: + # specified on commandline: use that + if opt is not None: + return utils.parse_comma_separated_list(opt) else: # ideally this would reuse our config parsing framework but we need to # parse this from preliminary options before plugins are enabled - for opt in ("enable_extensions", "enable-extensions"): - val = cfg.get("flake8", opt, fallback=None) + for opt_name in (cfg_opt_name, cfg_opt_name.replace("_", "-")): + val = cfg.get("flake8", opt_name, fallback=None) if val is not None: - return set(utils.parse_comma_separated_list(val)) + return utils.parse_comma_separated_list(val) else: - return set() + return [] + + +def parse_plugin_options( + cfg: configparser.RawConfigParser, + enable_extensions: Optional[str], +) -> PluginOptions: + """Parse plugin loading related options.""" + return PluginOptions( + enable_extensions=frozenset( + _parse_option(cfg, "enable_extensions", enable_extensions), + ), + ) def _parameters_for(func: Any) -> Dict[str, bool]: @@ -246,7 +267,7 @@ def _import_plugins( def _classify_plugins( plugins: List[LoadedPlugin], - enabled: Set[str], + opts: PluginOptions, ) -> Plugins: tree = [] logical_line = [] @@ -257,7 +278,7 @@ def _classify_plugins( for loaded in plugins: if ( getattr(loaded.obj, "off_by_default", False) - and loaded.plugin.entry_point.name not in enabled + and loaded.plugin.entry_point.name not in opts.enable_extensions ): disabled.append(loaded) elif loaded.plugin.entry_point.group == "flake8.report": @@ -285,7 +306,7 @@ def _classify_plugins( def load_plugins( plugins: List[Plugin], paths: List[str], - enabled: Set[str], + opts: PluginOptions, ) -> Plugins: """Load and classify all flake8 plugins. @@ -293,4 +314,4 @@ def load_plugins( - next: converts the ``Plugin``s to ``LoadedPlugins`` - finally: classifies plugins into their specific types """ - return _classify_plugins(_import_plugins(plugins, paths), enabled) + return _classify_plugins(_import_plugins(plugins, paths), opts) diff --git a/tests/integration/test_checker.py b/tests/integration/test_checker.py index 9583179..075b08f 100644 --- a/tests/integration/test_checker.py +++ b/tests/integration/test_checker.py @@ -90,7 +90,8 @@ def mock_file_checker_with_plugin(plugin_target): ), ), ] - plugins = finder.load_plugins(to_load, [], set()) + opts = finder.PluginOptions(frozenset()) + plugins = finder.load_plugins(to_load, [], opts) # Prevent it from reading lines from stdin or somewhere else with mock.patch( diff --git a/tests/integration/test_plugins.py b/tests/integration/test_plugins.py index e7f2d38..1085ed1 100644 --- a/tests/integration/test_plugins.py +++ b/tests/integration/test_plugins.py @@ -55,7 +55,8 @@ def test_enable_local_plugin_from_config(local_config): cfg, cfg_dir = config.load_config(local_config, [], isolated=False) plugins = finder.find_plugins(cfg) plugin_paths = finder.find_local_plugin_paths(cfg, cfg_dir) - loaded_plugins = finder.load_plugins(plugins, plugin_paths, set()) + opts = finder.PluginOptions(frozenset()) + loaded_plugins = finder.load_plugins(plugins, plugin_paths, opts) (custom_extension,) = ( loaded @@ -82,8 +83,8 @@ def test_local_plugin_can_add_option(local_config): plugins = finder.find_plugins(cfg) plugin_paths = finder.find_local_plugin_paths(cfg, cfg_dir) - enabled = finder.parse_enabled(cfg, stage1_args.enable_extensions) - loaded_plugins = finder.load_plugins(plugins, plugin_paths, enabled) + opts = finder.PluginOptions(frozenset()) + loaded_plugins = finder.load_plugins(plugins, plugin_paths, opts) option_manager = OptionManager( version="123", diff --git a/tests/unit/plugins/finder_test.py b/tests/unit/plugins/finder_test.py index 0e5f81f..330bbf2 100644 --- a/tests/unit/plugins/finder_test.py +++ b/tests/unit/plugins/finder_test.py @@ -376,15 +376,18 @@ def test_find_local_plugins(local_plugin_cfg): } -def test_parse_enabled_not_specified(): - assert finder.parse_enabled(configparser.RawConfigParser(), None) == set() +def test_parse_plugin_options_not_specified(): + cfg = configparser.RawConfigParser() + ret = finder.parse_plugin_options(cfg, None) + assert ret == finder.PluginOptions(frozenset()) def test_parse_enabled_from_commandline(): cfg = configparser.RawConfigParser() cfg.add_section("flake8") cfg.set("flake8", "enable_extensions", "A,B,C") - assert finder.parse_enabled(cfg, "D,E,F") == {"D", "E", "F"} + ret = finder.parse_plugin_options(cfg, "D,E,F") + assert ret == finder.PluginOptions(frozenset(("D", "E", "F"))) @pytest.mark.parametrize("opt", ("enable_extensions", "enable-extensions")) @@ -392,7 +395,8 @@ def test_parse_enabled_from_config(opt): cfg = configparser.RawConfigParser() cfg.add_section("flake8") cfg.set("flake8", opt, "A,B,C") - assert finder.parse_enabled(cfg, None) == {"A", "B", "C"} + ret = finder.parse_plugin_options(cfg, None) + assert ret == finder.PluginOptions(frozenset(("A", "B", "C"))) def test_find_plugins( @@ -600,7 +604,7 @@ def test_classify_plugins(): logical_line_plugin, physical_line_plugin, ], - set(), + finder.PluginOptions(frozenset()), ) assert classified == finder.Plugins( @@ -619,8 +623,10 @@ def test_classify_plugins_enable_a_disabled_plugin(): plugin = _plugin(ep=_ep(name="ABC")) loaded = _loaded(plugin=plugin, parameters={"tree": True}, obj=obj) - classified_normal = finder._classify_plugins([loaded], set()) - classified_enabled = finder._classify_plugins([loaded], {"ABC"}) + normal_opts = finder.PluginOptions(frozenset()) + classified_normal = finder._classify_plugins([loaded], normal_opts) + enabled_opts = finder.PluginOptions(frozenset(("ABC",))) + classified_enabled = finder._classify_plugins([loaded], enabled_opts) assert classified_normal == finder.Plugins( checkers=finder.Checkers([], [], []), @@ -638,7 +644,8 @@ def test_classify_plugins_enable_a_disabled_plugin(): def test_load_plugins(): plugin = _plugin(ep=_ep(value="aplugin:ExtensionTestPlugin2")) - ret = finder.load_plugins([plugin], ["tests/integration/subdir"], set()) + opts = finder.PluginOptions(frozenset()) + ret = finder.load_plugins([plugin], ["tests/integration/subdir"], opts) import aplugin