From 1a4060cd5f5d3d2f638d01f563e3c42aeb89a135 Mon Sep 17 00:00:00 2001 From: "Eric N. Vander Weele" Date: Wed, 1 Jan 2020 23:38:51 -0500 Subject: [PATCH] config: Change ConfigFileFinder._read_config() to accept variadic args This simplifies `._read_config()` by removing a conditional branch in the situation where it is called with one file to process. Now the contract accepts any number of arguments of the same type. Where callers invoke `._read_config()` with a `Sequence`, the call site has been changed to unpack arguments (i.e., `*`). The tests in `test_merged_config_parser.py` needed to return a string for the user configuration path instead of an empty list since `ConfigFileFinder.user_config_file()` returns a string. --- src/flake8/options/config.py | 12 ++++++------ tests/unit/test_config_file_finder.py | 2 +- tests/unit/test_merged_config_parser.py | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/flake8/options/config.py b/src/flake8/options/config.py index 9aebcba..e1952be 100644 --- a/src/flake8/options/config.py +++ b/src/flake8/options/config.py @@ -4,7 +4,7 @@ import configparser import logging import os.path import sys -from typing import Dict, List, Sequence, Tuple, Union +from typing import Dict, List, Tuple from flake8 import utils @@ -55,11 +55,9 @@ class ConfigFileFinder(object): # fmt: on @staticmethod - def _read_config(files): - # type: (Union[Sequence[str], str]) -> Tuple[configparser.RawConfigParser, List[str]] # noqa: E501 + def _read_config(*files): + # type: (*str) -> Tuple[configparser.RawConfigParser, List[str]] config = configparser.RawConfigParser() - if isinstance(files, (str, type(u""))): - files = [files] found_files = [] for filename in files: @@ -129,7 +127,9 @@ class ConfigFileFinder(object): 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( + *self.local_config_files() + ) if found_files: LOG.debug("Found local configuration files: %s", found_files) self._local_configs = config diff --git a/tests/unit/test_config_file_finder.py b/tests/unit/test_config_file_finder.py index 1c8330f..b562c4a 100644 --- a/tests/unit/test_config_file_finder.py +++ b/tests/unit/test_config_file_finder.py @@ -113,7 +113,7 @@ def test_local_configs_double_read(): ]) def test_read_config_catches_broken_config_files(files): """Verify that we do not allow the exception to bubble up.""" - _, parsed = config.ConfigFileFinder._read_config(files) + _, parsed = config.ConfigFileFinder._read_config(*files) assert BROKEN_CONFIG_PATH not in parsed diff --git a/tests/unit/test_merged_config_parser.py b/tests/unit/test_merged_config_parser.py index 77b1ba0..a28a4c3 100644 --- a/tests/unit/test_merged_config_parser.py +++ b/tests/unit/test_merged_config_parser.py @@ -183,7 +183,7 @@ def test_parsed_configs_are_equivalent( localcfs.return_value = [config_fixture_path] with mock.patch.object(config_finder, 'user_config_file') as usercf: - usercf.return_value = [] + usercf.return_value = '' parsed_config = parser.merge_user_and_local_config() assert parsed_config['ignore'] == ['E123', 'W234', 'E111'] @@ -215,7 +215,7 @@ def test_parsed_hyphenated_and_underscored_names( localcfs.return_value = [config_file] with mock.patch.object(config_finder, 'user_config_file') as usercf: - usercf.return_value = [] + usercf.return_value = '' parsed_config = parser.merge_user_and_local_config() assert parsed_config['max_line_length'] == 110