From 737e0492d0f30f65d9af9bfc8657fd98822b96f1 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Wed, 7 Apr 2021 08:52:26 -0700 Subject: [PATCH] improve code coverage in a few places --- src/flake8/processor.py | 14 +------------- src/flake8/utils.py | 5 +---- tests/unit/test_utils.py | 7 +++++++ 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/src/flake8/processor.py b/src/flake8/processor.py index 7c4672a..5fd78a8 100644 --- a/src/flake8/processor.py +++ b/src/flake8/processor.py @@ -446,8 +446,6 @@ def log_token(log: logging.Logger, token: _Token) -> None: ) -# NOTE(sigmavirus24): This was taken wholesale from -# https://github.com/PyCQA/pycodestyle def expand_indent(line: str) -> int: r"""Return the amount of indentation. @@ -462,17 +460,7 @@ def expand_indent(line: str) -> int: >>> expand_indent(' \t') 16 """ - if "\t" not in line: - return len(line) - len(line.lstrip()) - result = 0 - for char in line: - if char == "\t": - result = result // 8 * 8 + 8 - elif char == " ": - result += 1 - else: - break - return result + return len(line.expandtabs(8)) # NOTE(sigmavirus24): This was taken wholesale from diff --git a/src/flake8/utils.py b/src/flake8/utils.py index c5c134e..96c3485 100644 --- a/src/flake8/utils.py +++ b/src/flake8/utils.py @@ -231,9 +231,7 @@ def parse_unified_diff(diff: Optional[str] = None) -> Dict[str, Set[int]]: parsed_paths: Dict[str, Set[int]] = collections.defaultdict(set) for line in diff.splitlines(): if number_of_rows: - # NOTE(sigmavirus24): Below we use a slice because stdin may be - # bytes instead of text on Python 3. - if line[:1] != "-": + if not line or line[0] != "-": number_of_rows -= 1 # We're in the part of the diff that has lines starting with +, -, # and ' ' to show context and the changes made. We skip these @@ -317,7 +315,6 @@ def _default_predicate(*args: str) -> bool: def filenames_from( arg: str, predicate: Optional[Callable[[str], bool]] = None ) -> Generator[str, None, None]: - # noqa: E501 """Generate filenames from an argument. :param str arg: diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 4304057..d31c113 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -313,3 +313,10 @@ def test_stdin_get_value_crlf(): stdin = io.TextIOWrapper(io.BytesIO(b'1\r\n2\r\n'), 'UTF-8') with mock.patch.object(sys, 'stdin', stdin): assert utils.stdin_get_value.__wrapped__() == '1\n2\n' + + +def test_stdin_unknown_coding_token(): + """Ensure we produce source even for unknown encodings.""" + stdin = io.TextIOWrapper(io.BytesIO(b'# coding: unknown\n'), 'UTF-8') + with mock.patch.object(sys, 'stdin', stdin): + assert utils.stdin_get_value.__wrapped__() == '# coding: unknown\n'