mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-09 06:14:17 +00:00
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:
commit
e8de432f8e
4 changed files with 29 additions and 40 deletions
|
|
@ -25,13 +25,8 @@
|
||||||
|
|
||||||
"E121,W123,F904"
|
"E121,W123,F904"
|
||||||
"E121,\nW123,\nF804"
|
"E121,\nW123,\nF804"
|
||||||
"E121,\n\tW123,\n\tF804"
|
" E121,\n\tW123,\n\tF804 "
|
||||||
|
" 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 "]
|
|
||||||
|
|
||||||
And converts it to a list that looks as follows
|
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
|
.. autofunction:: flake8.utils.normalize_paths
|
||||||
|
|
||||||
This function utilizes :func:`~flake8.utils.parse_comma_separated_list` and
|
This function utilizes :func:`~flake8.utils.normalize_path` to normalize a
|
||||||
:func:`~flake8.utils.normalize_path` to normalize its input to a list of
|
sequence of paths. See :func:`~flake8.utils.normalize_path` for what defines a
|
||||||
strings that should be paths.
|
normalized path.
|
||||||
|
|
||||||
.. autofunction:: flake8.utils.stdin_get_value
|
.. autofunction:: flake8.utils.stdin_get_value
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
import collections
|
import collections
|
||||||
import logging
|
import logging
|
||||||
import optparse # pylint: disable=deprecated-module
|
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
|
from flake8 import utils
|
||||||
|
|
||||||
|
|
@ -142,14 +142,17 @@ class Option(object):
|
||||||
|
|
||||||
def normalize(self, value, *normalize_args):
|
def normalize(self, value, *normalize_args):
|
||||||
"""Normalize the value based on the option configuration."""
|
"""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:
|
if self.normalize_paths:
|
||||||
# Decide whether to parse a list of paths or a single path
|
if isinstance(value, list):
|
||||||
normalize = utils.normalize_path # type: Callable[..., Any]
|
value = utils.normalize_paths(value, *normalize_args)
|
||||||
if self.comma_separated_list:
|
else:
|
||||||
normalize = utils.normalize_paths
|
value = utils.normalize_path(value, *normalize_args)
|
||||||
return normalize(value, *normalize_args)
|
|
||||||
elif self.comma_separated_list:
|
|
||||||
return utils.parse_comma_separated_list(value)
|
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def normalize_from_setuptools(self, value):
|
def normalize_from_setuptools(self, value):
|
||||||
|
|
|
||||||
|
|
@ -24,11 +24,11 @@ string_types = (str, type(u""))
|
||||||
|
|
||||||
|
|
||||||
def parse_comma_separated_list(value, regexp=COMMA_SEPARATED_LIST_RE):
|
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.
|
"""Parse a comma-separated list.
|
||||||
|
|
||||||
:param value:
|
:param value:
|
||||||
String or list of strings to be parsed and normalized.
|
String to be parsed and normalized.
|
||||||
:param regexp:
|
:param regexp:
|
||||||
Compiled regular expression used to split the value when it is a
|
Compiled regular expression used to split the value when it is a
|
||||||
string.
|
string.
|
||||||
|
|
@ -39,13 +39,10 @@ def parse_comma_separated_list(value, regexp=COMMA_SEPARATED_LIST_RE):
|
||||||
:rtype:
|
:rtype:
|
||||||
list
|
list
|
||||||
"""
|
"""
|
||||||
if not value:
|
assert isinstance(value, string_types), value # nosec (for bandit)
|
||||||
return []
|
|
||||||
|
|
||||||
if isinstance(value, string_types):
|
separated = regexp.split(value)
|
||||||
value = regexp.split(value)
|
item_gen = (item.strip() for item in separated)
|
||||||
|
|
||||||
item_gen = (item.strip() for item in value)
|
|
||||||
return [item for item in item_gen if item]
|
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):
|
def normalize_paths(paths, parent=os.curdir):
|
||||||
# type: (Union[Sequence[str], str], str) -> List[str]
|
# type: (Sequence[str], str) -> List[str]
|
||||||
"""Parse a comma-separated list of paths.
|
"""Normalize a list of paths relative to a parent directory.
|
||||||
|
|
||||||
:returns:
|
:returns:
|
||||||
The normalized paths.
|
The normalized paths.
|
||||||
:rtype:
|
:rtype:
|
||||||
[str]
|
[str]
|
||||||
"""
|
"""
|
||||||
return [
|
assert isinstance(paths, list), paths # nosec (for bandit)
|
||||||
normalize_path(p, parent) for p in parse_comma_separated_list(paths)
|
return [normalize_path(p, parent) for p in paths]
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
def normalize_path(path, parent=os.curdir):
|
def normalize_path(path, parent=os.curdir):
|
||||||
|
|
|
||||||
|
|
@ -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,,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):
|
def test_parse_comma_separated_list(value, expected):
|
||||||
"""Verify that similar inputs produce identical outputs."""
|
"""Verify that similar inputs produce identical outputs."""
|
||||||
|
|
@ -132,14 +128,13 @@ def test_normalize_path(value, expected):
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("value,expected", [
|
@pytest.mark.parametrize("value,expected", [
|
||||||
("flake8,pep8,pyflakes,mccabe", ["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]),
|
[os.path.abspath("../" + p) for p in RELATIVE_PATHS]),
|
||||||
])
|
])
|
||||||
def test_normalize_paths(value, expected):
|
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
|
assert utils.normalize_paths(value) == expected
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue