mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-12 07:44:16 +00:00
Merge pull request #1518 from PyCQA/refactor_plugin_opts
refactor plugin loading options to prepare for --require-plugins
This commit is contained in:
commit
bff62cdf86
5 changed files with 59 additions and 29 deletions
|
|
@ -119,8 +119,8 @@ class Application:
|
||||||
"""
|
"""
|
||||||
raw = finder.find_plugins(cfg)
|
raw = finder.find_plugins(cfg)
|
||||||
local_plugin_paths = finder.find_local_plugin_paths(cfg, cfg_dir)
|
local_plugin_paths = finder.find_local_plugin_paths(cfg, cfg_dir)
|
||||||
enabled = finder.parse_enabled(cfg, enable_extensions)
|
opts = finder.parse_plugin_options(cfg, enable_extensions)
|
||||||
self.plugins = finder.load_plugins(raw, local_plugin_paths, enabled)
|
self.plugins = finder.load_plugins(raw, local_plugin_paths, opts)
|
||||||
|
|
||||||
def register_plugin_options(self) -> None:
|
def register_plugin_options(self) -> None:
|
||||||
"""Register options provided by plugins to our option manager."""
|
"""Register options provided by plugins to our option manager."""
|
||||||
|
|
|
||||||
|
|
@ -5,12 +5,12 @@ import logging
|
||||||
import sys
|
import sys
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
|
from typing import FrozenSet
|
||||||
from typing import Generator
|
from typing import Generator
|
||||||
from typing import Iterable
|
from typing import Iterable
|
||||||
from typing import List
|
from typing import List
|
||||||
from typing import NamedTuple
|
from typing import NamedTuple
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from typing import Set
|
|
||||||
|
|
||||||
from flake8 import utils
|
from flake8 import utils
|
||||||
from flake8._compat import importlib_metadata
|
from flake8._compat import importlib_metadata
|
||||||
|
|
@ -179,22 +179,43 @@ def find_local_plugin_paths(
|
||||||
return utils.normalize_paths(paths, cfg_dir)
|
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,
|
cfg: configparser.RawConfigParser,
|
||||||
enable_extensions: Optional[str],
|
cfg_opt_name: str,
|
||||||
) -> Set[str]:
|
opt: Optional[str],
|
||||||
"""Parse --enable-extensions."""
|
) -> List[str]:
|
||||||
if enable_extensions is not None:
|
# specified on commandline: use that
|
||||||
return set(utils.parse_comma_separated_list(enable_extensions))
|
if opt is not None:
|
||||||
|
return utils.parse_comma_separated_list(opt)
|
||||||
else:
|
else:
|
||||||
# ideally this would reuse our config parsing framework but we need to
|
# ideally this would reuse our config parsing framework but we need to
|
||||||
# parse this from preliminary options before plugins are enabled
|
# parse this from preliminary options before plugins are enabled
|
||||||
for opt in ("enable_extensions", "enable-extensions"):
|
for opt_name in (cfg_opt_name, cfg_opt_name.replace("_", "-")):
|
||||||
val = cfg.get("flake8", opt, fallback=None)
|
val = cfg.get("flake8", opt_name, fallback=None)
|
||||||
if val is not None:
|
if val is not None:
|
||||||
return set(utils.parse_comma_separated_list(val))
|
return utils.parse_comma_separated_list(val)
|
||||||
else:
|
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]:
|
def _parameters_for(func: Any) -> Dict[str, bool]:
|
||||||
|
|
@ -246,7 +267,7 @@ def _import_plugins(
|
||||||
|
|
||||||
def _classify_plugins(
|
def _classify_plugins(
|
||||||
plugins: List[LoadedPlugin],
|
plugins: List[LoadedPlugin],
|
||||||
enabled: Set[str],
|
opts: PluginOptions,
|
||||||
) -> Plugins:
|
) -> Plugins:
|
||||||
tree = []
|
tree = []
|
||||||
logical_line = []
|
logical_line = []
|
||||||
|
|
@ -257,7 +278,7 @@ def _classify_plugins(
|
||||||
for loaded in plugins:
|
for loaded in plugins:
|
||||||
if (
|
if (
|
||||||
getattr(loaded.obj, "off_by_default", False)
|
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)
|
disabled.append(loaded)
|
||||||
elif loaded.plugin.entry_point.group == "flake8.report":
|
elif loaded.plugin.entry_point.group == "flake8.report":
|
||||||
|
|
@ -285,7 +306,7 @@ def _classify_plugins(
|
||||||
def load_plugins(
|
def load_plugins(
|
||||||
plugins: List[Plugin],
|
plugins: List[Plugin],
|
||||||
paths: List[str],
|
paths: List[str],
|
||||||
enabled: Set[str],
|
opts: PluginOptions,
|
||||||
) -> Plugins:
|
) -> Plugins:
|
||||||
"""Load and classify all flake8 plugins.
|
"""Load and classify all flake8 plugins.
|
||||||
|
|
||||||
|
|
@ -293,4 +314,4 @@ def load_plugins(
|
||||||
- next: converts the ``Plugin``s to ``LoadedPlugins``
|
- next: converts the ``Plugin``s to ``LoadedPlugins``
|
||||||
- finally: classifies plugins into their specific types
|
- finally: classifies plugins into their specific types
|
||||||
"""
|
"""
|
||||||
return _classify_plugins(_import_plugins(plugins, paths), enabled)
|
return _classify_plugins(_import_plugins(plugins, paths), opts)
|
||||||
|
|
|
||||||
|
|
@ -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
|
# Prevent it from reading lines from stdin or somewhere else
|
||||||
with mock.patch(
|
with mock.patch(
|
||||||
|
|
|
||||||
|
|
@ -55,7 +55,8 @@ def test_enable_local_plugin_from_config(local_config):
|
||||||
cfg, cfg_dir = config.load_config(local_config, [], isolated=False)
|
cfg, cfg_dir = config.load_config(local_config, [], isolated=False)
|
||||||
plugins = finder.find_plugins(cfg)
|
plugins = finder.find_plugins(cfg)
|
||||||
plugin_paths = finder.find_local_plugin_paths(cfg, cfg_dir)
|
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,) = (
|
(custom_extension,) = (
|
||||||
loaded
|
loaded
|
||||||
|
|
@ -82,8 +83,8 @@ def test_local_plugin_can_add_option(local_config):
|
||||||
|
|
||||||
plugins = finder.find_plugins(cfg)
|
plugins = finder.find_plugins(cfg)
|
||||||
plugin_paths = finder.find_local_plugin_paths(cfg, cfg_dir)
|
plugin_paths = finder.find_local_plugin_paths(cfg, cfg_dir)
|
||||||
enabled = finder.parse_enabled(cfg, stage1_args.enable_extensions)
|
opts = finder.PluginOptions(frozenset())
|
||||||
loaded_plugins = finder.load_plugins(plugins, plugin_paths, enabled)
|
loaded_plugins = finder.load_plugins(plugins, plugin_paths, opts)
|
||||||
|
|
||||||
option_manager = OptionManager(
|
option_manager = OptionManager(
|
||||||
version="123",
|
version="123",
|
||||||
|
|
|
||||||
|
|
@ -376,15 +376,18 @@ def test_find_local_plugins(local_plugin_cfg):
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def test_parse_enabled_not_specified():
|
def test_parse_plugin_options_not_specified():
|
||||||
assert finder.parse_enabled(configparser.RawConfigParser(), None) == set()
|
cfg = configparser.RawConfigParser()
|
||||||
|
ret = finder.parse_plugin_options(cfg, None)
|
||||||
|
assert ret == finder.PluginOptions(frozenset())
|
||||||
|
|
||||||
|
|
||||||
def test_parse_enabled_from_commandline():
|
def test_parse_enabled_from_commandline():
|
||||||
cfg = configparser.RawConfigParser()
|
cfg = configparser.RawConfigParser()
|
||||||
cfg.add_section("flake8")
|
cfg.add_section("flake8")
|
||||||
cfg.set("flake8", "enable_extensions", "A,B,C")
|
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"))
|
@pytest.mark.parametrize("opt", ("enable_extensions", "enable-extensions"))
|
||||||
|
|
@ -392,7 +395,8 @@ def test_parse_enabled_from_config(opt):
|
||||||
cfg = configparser.RawConfigParser()
|
cfg = configparser.RawConfigParser()
|
||||||
cfg.add_section("flake8")
|
cfg.add_section("flake8")
|
||||||
cfg.set("flake8", opt, "A,B,C")
|
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(
|
def test_find_plugins(
|
||||||
|
|
@ -600,7 +604,7 @@ def test_classify_plugins():
|
||||||
logical_line_plugin,
|
logical_line_plugin,
|
||||||
physical_line_plugin,
|
physical_line_plugin,
|
||||||
],
|
],
|
||||||
set(),
|
finder.PluginOptions(frozenset()),
|
||||||
)
|
)
|
||||||
|
|
||||||
assert classified == finder.Plugins(
|
assert classified == finder.Plugins(
|
||||||
|
|
@ -619,8 +623,10 @@ def test_classify_plugins_enable_a_disabled_plugin():
|
||||||
plugin = _plugin(ep=_ep(name="ABC"))
|
plugin = _plugin(ep=_ep(name="ABC"))
|
||||||
loaded = _loaded(plugin=plugin, parameters={"tree": True}, obj=obj)
|
loaded = _loaded(plugin=plugin, parameters={"tree": True}, obj=obj)
|
||||||
|
|
||||||
classified_normal = finder._classify_plugins([loaded], set())
|
normal_opts = finder.PluginOptions(frozenset())
|
||||||
classified_enabled = finder._classify_plugins([loaded], {"ABC"})
|
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(
|
assert classified_normal == finder.Plugins(
|
||||||
checkers=finder.Checkers([], [], []),
|
checkers=finder.Checkers([], [], []),
|
||||||
|
|
@ -638,7 +644,8 @@ def test_classify_plugins_enable_a_disabled_plugin():
|
||||||
def test_load_plugins():
|
def test_load_plugins():
|
||||||
plugin = _plugin(ep=_ep(value="aplugin:ExtensionTestPlugin2"))
|
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
|
import aplugin
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue