From 594c16abb42ef89e36db1701d5f1aa860f4db68d Mon Sep 17 00:00:00 2001 From: "Eric N. Vander Weele" Date: Sat, 9 Nov 2019 09:55:01 +0800 Subject: [PATCH] application: Remove configuration finder state This change removes the `.config_finder` object from the `Application`. Since the configuration finder is only needed during initialization, we constrain the finder to be returned and passed to other methods necessary for initialization. --- src/flake8/api/legacy.py | 8 +++++--- src/flake8/main/application.py | 26 +++++++++++++++----------- tests/unit/test_legacy_api.py | 8 ++++++-- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/flake8/api/legacy.py b/src/flake8/api/legacy.py index b8c69c5..ac3fabb 100644 --- a/src/flake8/api/legacy.py +++ b/src/flake8/api/legacy.py @@ -30,12 +30,14 @@ def get_style_guide(**kwargs): application = app.Application() prelim_opts, remaining_args = application.parse_preliminary_options([]) flake8.configure_logging(prelim_opts.verbose, prelim_opts.output_file) - application.config_finder = application.make_config_finder( + config_finder = application.make_config_finder( application.program, prelim_opts.append_config ) - application.find_plugins(prelim_opts.config, prelim_opts.isolated) + application.find_plugins( + config_finder, prelim_opts.config, prelim_opts.isolated + ) application.register_plugin_options() - application.parse_configuration_and_cli(remaining_args) + application.parse_configuration_and_cli(config_finder, remaining_args) # We basically want application.initialize to be called but with these # options set instead before we make our formatter, notifier, internal # style guide and file checker manager. diff --git a/src/flake8/main/application.py b/src/flake8/main/application.py index a317144..6e7aabf 100644 --- a/src/flake8/main/application.py +++ b/src/flake8/main/application.py @@ -57,8 +57,6 @@ class Application(object): parents=[self.prelim_arg_parser], ) options.register_default_options(self.option_manager) - #: The instance of :class:`flake8.options.config.ConfigFileFinder` - self.config_finder = None # type: config.ConfigFileFinder #: The :class:`flake8.options.config.LocalPlugins` found in config self.local_plugins = None # type: config.LocalPlugins @@ -150,8 +148,8 @@ class Application(object): """ return config.ConfigFileFinder(program_name, extra_config_files) - def find_plugins(self, config_file, ignore_config_files): - # type: (Optional[str], bool) -> None + def find_plugins(self, config_finder, config_file, ignore_config_files): + # type: (config.ConfigFileFinder, Optional[str], bool) -> None """Find and load the plugins for this application. If :attr:`check_plugins`, or :attr:`formatting_plugins` are ``None`` @@ -160,6 +158,8 @@ class Application(object): we want this to be idempotent and so only update those attributes if they are ``None``. + :param config.ConfigFileFinder config_finder: + The finder for finding and reading configuration files. :param str config_file: The optional configuraiton file to override all other configuration files (i.e., the --config option). @@ -168,7 +168,7 @@ class Application(object): --isolated option). """ self.local_plugins = config.get_local_plugins( - self.config_finder, config_file, ignore_config_files + config_finder, config_file, ignore_config_files ) sys.path.extend(self.local_plugins.paths) @@ -191,15 +191,17 @@ class Application(object): self.check_plugins.register_plugin_versions(self.option_manager) self.formatting_plugins.register_options(self.option_manager) - def parse_configuration_and_cli(self, argv): - # type: (List[str]) -> None + def parse_configuration_and_cli(self, config_finder, argv): + # type: (config.ConfigFileFinder, List[str]) -> None """Parse configuration files and the CLI options. + :param config.ConfigFileFinder config_finder: + The finder for finding and reading configuration files. :param list argv: Command-line arguments passed in directly. """ self.options, self.args = aggregator.aggregate_options( - self.option_manager, self.config_finder, argv + self.option_manager, config_finder, argv ) self.running_against_diff = self.options.diff @@ -338,12 +340,14 @@ class Application(object): # our legacy API calls to these same methods. prelim_opts, remaining_args = self.parse_preliminary_options(argv) flake8.configure_logging(prelim_opts.verbose, prelim_opts.output_file) - self.config_finder = self.make_config_finder( + config_finder = self.make_config_finder( self.program, prelim_opts.append_config ) - self.find_plugins(prelim_opts.config, prelim_opts.isolated) + self.find_plugins( + config_finder, prelim_opts.config, prelim_opts.isolated + ) self.register_plugin_options() - self.parse_configuration_and_cli(remaining_args) + self.parse_configuration_and_cli(config_finder, remaining_args) self.make_formatter() self.make_guide() self.make_file_checker_manager() diff --git a/tests/unit/test_legacy_api.py b/tests/unit/test_legacy_api.py index 77d9020..026fdc3 100644 --- a/tests/unit/test_legacy_api.py +++ b/tests/unit/test_legacy_api.py @@ -6,6 +6,7 @@ import pytest from flake8.api import legacy as api from flake8.formatting import base as formatter +from flake8.options.config import ConfigFileFinder def test_get_style_guide(): @@ -20,6 +21,8 @@ def test_get_style_guide(): mockedapp = mock.Mock() mockedapp.parse_preliminary_options.return_value = (prelim_opts, []) mockedapp.program = 'flake8' + config_finder = ConfigFileFinder(mockedapp.program, []) + mockedapp.make_config_finder.return_value = config_finder with mock.patch('flake8.main.application.Application') as application: application.return_value = mockedapp style_guide = api.get_style_guide() @@ -27,9 +30,10 @@ def test_get_style_guide(): application.assert_called_once_with() mockedapp.parse_preliminary_options.assert_called_once_with([]) mockedapp.make_config_finder.assert_called_once_with(mockedapp.program, []) - mockedapp.find_plugins.assert_called_once_with(None, False) + mockedapp.find_plugins.assert_called_once_with(config_finder, None, False) mockedapp.register_plugin_options.assert_called_once_with() - mockedapp.parse_configuration_and_cli.assert_called_once_with([]) + mockedapp.parse_configuration_and_cli.assert_called_once_with( + config_finder, []) mockedapp.make_formatter.assert_called_once_with() mockedapp.make_guide.assert_called_once_with() mockedapp.make_file_checker_manager.assert_called_once_with()