Merge branch 'simplify-normalize-paths' into 'master'

Hoist comma-separated string parsing out of path normalization

See merge request pycqa/flake8!334
This commit is contained in:
Anthony Sottile 2019-07-28 14:48:47 +00:00
commit e8de432f8e
4 changed files with 29 additions and 40 deletions

View file

@ -25,13 +25,8 @@
"E121,W123,F904"
"E121,\nW123,\nF804"
"E121,\n\tW123,\n\tF804"
Or it will take a list of strings (potentially with whitespace) such as
.. code-block:: python
[" E121\n", "\t\nW123 ", "\n\tF904\n "]
" E121,\n\tW123,\n\tF804 "
" E121\n\tW123 \n\tF804"
And converts it to a list that looks as follows
@ -50,9 +45,9 @@ path if the string has a ``/`` in it. It also removes trailing ``/``\ s.
.. autofunction:: flake8.utils.normalize_paths
This function utilizes :func:`~flake8.utils.parse_comma_separated_list` and
:func:`~flake8.utils.normalize_path` to normalize its input to a list of
strings that should be paths.
This function utilizes :func:`~flake8.utils.normalize_path` to normalize a
sequence of paths. See :func:`~flake8.utils.normalize_path` for what defines a
normalized path.
.. autofunction:: flake8.utils.stdin_get_value

View file

@ -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):

View file

@ -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):

View file

@ -22,11 +22,7 @@ RELATIVE_PATHS = ["flake8", "pep8", "pyflakes", "mccabe"]
("E123,W234,,E206,,", ["E123", "W234", "E206"]),
("E123, W234,, E206,,", ["E123", "W234", "E206"]),
("E123,,W234,,E206,,", ["E123", "W234", "E206"]),
(["E123", "W234", "E206"], ["E123", "W234", "E206"]),
(["E123", "\n\tW234", "\n E206"], ["E123", "W234", "E206"]),
(["E123", "\n\tW234", "\n E206", "\n"], ["E123", "W234", "E206"]),
(["E123", "\n\tW234", "", "\n E206", "\n"], ["E123", "W234", "E206"]),
(["E123", "\n\tW234", "", "\n E206", ""], ["E123", "W234", "E206"]),
("", []),
])
def test_parse_comma_separated_list(value, expected):
"""Verify that similar inputs produce identical outputs."""
@ -132,14 +128,13 @@ def test_normalize_path(value, expected):
@pytest.mark.parametrize("value,expected", [
("flake8,pep8,pyflakes,mccabe", ["flake8", "pep8", "pyflakes", "mccabe"]),
("flake8,\n\tpep8,\n pyflakes,\n\n mccabe",
(["flake8", "pep8", "pyflakes", "mccabe"],
["flake8", "pep8", "pyflakes", "mccabe"]),
("../flake8,../pep8,../pyflakes,../mccabe",
(["../flake8", "../pep8", "../pyflakes", "../mccabe"],
[os.path.abspath("../" + p) for p in RELATIVE_PATHS]),
])
def test_normalize_paths(value, expected):
"""Verify we normalize comma-separated paths provided to the tool."""
"""Verify we normalizes a sequence of paths provided to the tool."""
assert utils.normalize_paths(value) == expected