diff options
| author | Anthony Sottile <asottile@umich.edu> | 2019-07-28 14:48:47 +0000 |
|---|---|---|
| committer | Anthony Sottile <asottile@umich.edu> | 2019-07-28 14:48:47 +0000 |
| commit | e8de432f8e81e8bcf6fbffabe35b320cc2dafcae (patch) | |
| tree | 0aadd9cc9ec1c5c2f2ec0d528030d05262018589 /src | |
| parent | 862b17d229454e31e312217237bfe65f90effbe8 (diff) | |
| parent | 1757bce321c8276b6fad77ce15766f0c81e46957 (diff) | |
| download | flake8-e8de432f8e81e8bcf6fbffabe35b320cc2dafcae.tar.gz | |
Merge branch 'simplify-normalize-paths' into 'master'
Hoist comma-separated string parsing out of path normalization
See merge request pycqa/flake8!334
Diffstat (limited to 'src')
| -rw-r--r-- | src/flake8/options/manager.py | 19 | ||||
| -rw-r--r-- | src/flake8/utils.py | 22 |
2 files changed, 20 insertions, 21 deletions
diff --git a/src/flake8/options/manager.py b/src/flake8/options/manager.py index 5d21127..cf94927 100644 --- a/src/flake8/options/manager.py +++ b/src/flake8/options/manager.py @@ -2,7 +2,7 @@ import collections import logging import optparse # pylint: disable=deprecated-module -from typing import Any, Callable, Dict, List, Optional, Set +from typing import Dict, List, Optional, Set from flake8 import utils @@ -142,14 +142,17 @@ class Option(object): def normalize(self, value, *normalize_args): """Normalize the value based on the option configuration.""" + if self.comma_separated_list and isinstance( + value, utils.string_types + ): + value = utils.parse_comma_separated_list(value) + if self.normalize_paths: - # Decide whether to parse a list of paths or a single path - normalize = utils.normalize_path # type: Callable[..., Any] - if self.comma_separated_list: - normalize = utils.normalize_paths - return normalize(value, *normalize_args) - elif self.comma_separated_list: - return utils.parse_comma_separated_list(value) + if isinstance(value, list): + value = utils.normalize_paths(value, *normalize_args) + else: + value = utils.normalize_path(value, *normalize_args) + return value def normalize_from_setuptools(self, value): diff --git a/src/flake8/utils.py b/src/flake8/utils.py index 02f0607..92fec71 100644 --- a/src/flake8/utils.py +++ b/src/flake8/utils.py @@ -24,11 +24,11 @@ string_types = (str, type(u"")) def parse_comma_separated_list(value, regexp=COMMA_SEPARATED_LIST_RE): - # type: (Union[Sequence[str], str], Pattern[str]) -> List[str] + # type: (str, Pattern[str]) -> List[str] """Parse a comma-separated list. :param value: - String or list of strings to be parsed and normalized. + String to be parsed and normalized. :param regexp: Compiled regular expression used to split the value when it is a string. @@ -39,13 +39,10 @@ def parse_comma_separated_list(value, regexp=COMMA_SEPARATED_LIST_RE): :rtype: list """ - if not value: - return [] + assert isinstance(value, string_types), value # nosec (for bandit) - if isinstance(value, string_types): - value = regexp.split(value) - - item_gen = (item.strip() for item in value) + separated = regexp.split(value) + item_gen = (item.strip() for item in separated) return [item for item in item_gen if item] @@ -158,17 +155,16 @@ def parse_files_to_codes_mapping(value_): # noqa: C901 def normalize_paths(paths, parent=os.curdir): - # type: (Union[Sequence[str], str], str) -> List[str] - """Parse a comma-separated list of paths. + # type: (Sequence[str], str) -> List[str] + """Normalize a list of paths relative to a parent directory. :returns: The normalized paths. :rtype: [str] """ - return [ - normalize_path(p, parent) for p in parse_comma_separated_list(paths) - ] + assert isinstance(paths, list), paths # nosec (for bandit) + return [normalize_path(p, parent) for p in paths] def normalize_path(path, parent=os.curdir): |
