Skip to content

Commit e8de432

Browse files
committed
Merge branch 'simplify-normalize-paths' into 'master'
Hoist comma-separated string parsing out of path normalization See merge request pycqa/flake8!334
2 parents 862b17d + 1757bce commit e8de432

4 files changed

Lines changed: 29 additions & 40 deletions

File tree

docs/source/internal/utils.rst

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,8 @@
2525
2626
"E121,W123,F904"
2727
"E121,\nW123,\nF804"
28-
"E121,\n\tW123,\n\tF804"
29-
30-
Or it will take a list of strings (potentially with whitespace) such as
31-
32-
.. code-block:: python
33-
34-
[" E121\n", "\t\nW123 ", "\n\tF904\n "]
28+
" E121,\n\tW123,\n\tF804 "
29+
" E121\n\tW123 \n\tF804"
3530
3631
And converts it to a list that looks as follows
3732

@@ -50,9 +45,9 @@ path if the string has a ``/`` in it. It also removes trailing ``/``\ s.
5045

5146
.. autofunction:: flake8.utils.normalize_paths
5247

53-
This function utilizes :func:`~flake8.utils.parse_comma_separated_list` and
54-
:func:`~flake8.utils.normalize_path` to normalize its input to a list of
55-
strings that should be paths.
48+
This function utilizes :func:`~flake8.utils.normalize_path` to normalize a
49+
sequence of paths. See :func:`~flake8.utils.normalize_path` for what defines a
50+
normalized path.
5651

5752
.. autofunction:: flake8.utils.stdin_get_value
5853

src/flake8/options/manager.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import collections
33
import logging
44
import optparse # pylint: disable=deprecated-module
5-
from typing import Any, Callable, Dict, List, Optional, Set
5+
from typing import Dict, List, Optional, Set
66

77
from flake8 import utils
88

@@ -142,14 +142,17 @@ def _make_dest(self, dest):
142142

143143
def normalize(self, value, *normalize_args):
144144
"""Normalize the value based on the option configuration."""
145+
if self.comma_separated_list and isinstance(
146+
value, utils.string_types
147+
):
148+
value = utils.parse_comma_separated_list(value)
149+
145150
if self.normalize_paths:
146-
# Decide whether to parse a list of paths or a single path
147-
normalize = utils.normalize_path # type: Callable[..., Any]
148-
if self.comma_separated_list:
149-
normalize = utils.normalize_paths
150-
return normalize(value, *normalize_args)
151-
elif self.comma_separated_list:
152-
return utils.parse_comma_separated_list(value)
151+
if isinstance(value, list):
152+
value = utils.normalize_paths(value, *normalize_args)
153+
else:
154+
value = utils.normalize_path(value, *normalize_args)
155+
153156
return value
154157

155158
def normalize_from_setuptools(self, value):

src/flake8/utils.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424

2525

2626
def parse_comma_separated_list(value, regexp=COMMA_SEPARATED_LIST_RE):
27-
# type: (Union[Sequence[str], str], Pattern[str]) -> List[str]
27+
# type: (str, Pattern[str]) -> List[str]
2828
"""Parse a comma-separated list.
2929
3030
:param value:
31-
String or list of strings to be parsed and normalized.
31+
String to be parsed and normalized.
3232
:param regexp:
3333
Compiled regular expression used to split the value when it is a
3434
string.
@@ -39,13 +39,10 @@ def parse_comma_separated_list(value, regexp=COMMA_SEPARATED_LIST_RE):
3939
:rtype:
4040
list
4141
"""
42-
if not value:
43-
return []
42+
assert isinstance(value, string_types), value # nosec (for bandit)
4443

45-
if isinstance(value, string_types):
46-
value = regexp.split(value)
47-
48-
item_gen = (item.strip() for item in value)
44+
separated = regexp.split(value)
45+
item_gen = (item.strip() for item in separated)
4946
return [item for item in item_gen if item]
5047

5148

@@ -158,17 +155,16 @@ def _indent(s): # type: (str) -> str
158155

159156

160157
def normalize_paths(paths, parent=os.curdir):
161-
# type: (Union[Sequence[str], str], str) -> List[str]
162-
"""Parse a comma-separated list of paths.
158+
# type: (Sequence[str], str) -> List[str]
159+
"""Normalize a list of paths relative to a parent directory.
163160
164161
:returns:
165162
The normalized paths.
166163
:rtype:
167164
[str]
168165
"""
169-
return [
170-
normalize_path(p, parent) for p in parse_comma_separated_list(paths)
171-
]
166+
assert isinstance(paths, list), paths # nosec (for bandit)
167+
return [normalize_path(p, parent) for p in paths]
172168

173169

174170
def normalize_path(path, parent=os.curdir):

tests/unit/test_utils.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@
2222
("E123,W234,,E206,,", ["E123", "W234", "E206"]),
2323
("E123, W234,, E206,,", ["E123", "W234", "E206"]),
2424
("E123,,W234,,E206,,", ["E123", "W234", "E206"]),
25-
(["E123", "W234", "E206"], ["E123", "W234", "E206"]),
26-
(["E123", "\n\tW234", "\n E206"], ["E123", "W234", "E206"]),
27-
(["E123", "\n\tW234", "\n E206", "\n"], ["E123", "W234", "E206"]),
28-
(["E123", "\n\tW234", "", "\n E206", "\n"], ["E123", "W234", "E206"]),
29-
(["E123", "\n\tW234", "", "\n E206", ""], ["E123", "W234", "E206"]),
25+
("", []),
3026
])
3127
def test_parse_comma_separated_list(value, expected):
3228
"""Verify that similar inputs produce identical outputs."""
@@ -132,14 +128,13 @@ def test_normalize_path(value, expected):
132128

133129

134130
@pytest.mark.parametrize("value,expected", [
135-
("flake8,pep8,pyflakes,mccabe", ["flake8", "pep8", "pyflakes", "mccabe"]),
136-
("flake8,\n\tpep8,\n pyflakes,\n\n mccabe",
131+
(["flake8", "pep8", "pyflakes", "mccabe"],
137132
["flake8", "pep8", "pyflakes", "mccabe"]),
138-
("../flake8,../pep8,../pyflakes,../mccabe",
133+
(["../flake8", "../pep8", "../pyflakes", "../mccabe"],
139134
[os.path.abspath("../" + p) for p in RELATIVE_PATHS]),
140135
])
141136
def test_normalize_paths(value, expected):
142-
"""Verify we normalize comma-separated paths provided to the tool."""
137+
"""Verify we normalizes a sequence of paths provided to the tool."""
143138
assert utils.normalize_paths(value) == expected
144139

145140

0 commit comments

Comments
 (0)