config: Remove checks for configs being previously parsed

Remove the checks to see if a configuration file has already been seen
and parsed.  These checks aren't necessary because the entire run of
`flake8` calls these methods *only* once per configuration provided.
This commit is contained in:
Eric N. Vander Weele 2020-01-20 12:50:33 -05:00
parent d583f051ed
commit 716db1167e
2 changed files with 13 additions and 54 deletions

View file

@ -4,7 +4,7 @@ import configparser
import logging import logging
import os.path import os.path
import sys import sys
from typing import Dict, List, Optional, Tuple from typing import List, Optional, Tuple
from flake8 import utils from flake8 import utils
@ -61,14 +61,6 @@ class ConfigFileFinder(object):
self.local_directory = os.path.abspath(os.curdir) self.local_directory = os.path.abspath(os.curdir)
# caches to avoid double-reading config files
self._local_configs = None
self._local_found_files = [] # type: List[str]
self._user_config = None
# fmt: off
self._cli_configs = {} # type: Dict[str, configparser.RawConfigParser]
# fmt: on
@staticmethod @staticmethod
def _read_config(*files): def _read_config(*files):
# type: (*str) -> Tuple[configparser.RawConfigParser, List[str]] # type: (*str) -> Tuple[configparser.RawConfigParser, List[str]]
@ -95,12 +87,10 @@ class ConfigFileFinder(object):
def cli_config(self, files): def cli_config(self, files):
# type: (str) -> configparser.RawConfigParser # type: (str) -> configparser.RawConfigParser
"""Read and parse the config file specified on the command-line.""" """Read and parse the config file specified on the command-line."""
if files not in self._cli_configs: config, found_files = self._read_config(files)
config, found_files = self._read_config(files) if found_files:
if found_files: LOG.debug("Found cli configuration files: %s", found_files)
LOG.debug("Found cli configuration files: %s", found_files) return config
self._cli_configs[files] = config
return self._cli_configs[files]
def generate_possible_local_files(self): def generate_possible_local_files(self):
"""Find and generate all local config files.""" """Find and generate all local config files."""
@ -140,15 +130,10 @@ class ConfigFileFinder(object):
Return (config, found_config_files) tuple. Return (config, found_config_files) tuple.
""" """
if self._local_configs is None: config, found_files = self._read_config(*self.local_config_files())
config, found_files = self._read_config( if found_files:
*self.local_config_files() LOG.debug("Found local configuration files: %s", found_files)
) return (config, found_files)
if found_files:
LOG.debug("Found local configuration files: %s", found_files)
self._local_configs = config
self._local_found_files = found_files
return (self._local_configs, self._local_found_files)
def local_configs(self): def local_configs(self):
"""Parse all local config files into one config object.""" """Parse all local config files into one config object."""
@ -162,12 +147,10 @@ class ConfigFileFinder(object):
def user_config(self): def user_config(self):
"""Parse the user config file into a config object.""" """Parse the user config file into a config object."""
if self._user_config is None: config, found_files = self._read_config(self.user_config_file())
config, found_files = self._read_config(self.user_config_file()) if found_files:
if found_files: LOG.debug("Found user configuration files: %s", found_files)
LOG.debug("Found user configuration files: %s", found_files) return config
self._user_config = config
return self._user_config
class MergedConfigParser(object): class MergedConfigParser(object):

View file

@ -34,18 +34,6 @@ def test_cli_config():
assert parsed_config.has_section('flake8') assert parsed_config.has_section('flake8')
def test_cli_config_double_read():
"""Second request for CLI config is cached."""
finder = config.ConfigFileFinder('flake8')
parsed_config = finder.cli_config(CLI_SPECIFIED_FILEPATH)
boom = Exception("second request for CLI config not cached")
with mock.patch.object(finder, '_read_config', side_effect=boom):
parsed_config_2 = finder.cli_config(CLI_SPECIFIED_FILEPATH)
assert parsed_config is parsed_config_2
@pytest.mark.parametrize('cwd,expected', [ @pytest.mark.parametrize('cwd,expected', [
# Root directory of project # Root directory of project
(os.path.abspath('.'), (os.path.abspath('.'),
@ -96,18 +84,6 @@ def test_local_configs():
assert isinstance(finder.local_configs(), configparser.RawConfigParser) assert isinstance(finder.local_configs(), configparser.RawConfigParser)
def test_local_configs_double_read():
"""Second request for local configs is cached."""
finder = config.ConfigFileFinder('flake8')
first_read = finder.local_configs()
boom = Exception("second request for local configs not cached")
with mock.patch.object(finder, '_read_config', side_effect=boom):
second_read = finder.local_configs()
assert first_read is second_read
@pytest.mark.parametrize('files', [ @pytest.mark.parametrize('files', [
[BROKEN_CONFIG_PATH], [BROKEN_CONFIG_PATH],
[CLI_SPECIFIED_FILEPATH, BROKEN_CONFIG_PATH], [CLI_SPECIFIED_FILEPATH, BROKEN_CONFIG_PATH],