From 102a6bd67644faa5ef3f766ea74cc8415988cac3 Mon Sep 17 00:00:00 2001 From: Yuki Igarashi Date: Sat, 27 Mar 2021 20:58:12 +0900 Subject: [PATCH] Sync with internal repository Thank you for all your contributions. Contributions: Yuki Igarashi: 2 commits, 50 lines Ryo Miyajima: 1 commits, 38 lines git-pysen-release-hash: 30f522dccd4a43d4f8b80919a430fb24bbbbcff9 Co-authored-by: Yuki Igarashi Co-authored-by: Ryo Miyajima --- README.md | 4 ++-- pysen/_version.py | 2 +- pysen/error_lines.py | 14 ++++++++++++-- tests/test_error_lines.py | 24 +++++++++++++++++++++++- 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 63e64e8..fce526f 100644 --- a/README.md +++ b/README.md @@ -41,9 +41,9 @@ pip install "pysen[lint]" ```sh # pipenv -pipenv install --dev "pysen[lint]==0.9.0" +pipenv install --dev "pysen[lint]==0.9.1" # poetry -poetry add -D pysen==0.9.0 -E lint +poetry add -D pysen==0.9.1 -E lint ``` diff --git a/pysen/_version.py b/pysen/_version.py index 3e2f46a..d69d16e 100644 --- a/pysen/_version.py +++ b/pysen/_version.py @@ -1 +1 @@ -__version__ = "0.9.0" +__version__ = "0.9.1" diff --git a/pysen/error_lines.py b/pysen/error_lines.py index 7228ce7..aa423c5 100644 --- a/pysen/error_lines.py +++ b/pysen/error_lines.py @@ -1,7 +1,7 @@ import logging import re from pathlib import Path -from typing import Callable, Iterable, Optional +from typing import Callable, Generator, Iterable, Optional import unidiff @@ -90,10 +90,20 @@ def _is_changed(line: unidiff.patch.Line) -> bool: _warn_parse_error(patch, logger) continue + def filter_hunk( + hunk: unidiff.patch.Hunk, + ) -> Generator[unidiff.patch.Line, None, None]: + for line in hunk: + if _is_changed(line): + yield line + elif line.source_line_no is not None: + if start_line <= line.source_line_no <= end_line: + yield line + yield Diagnostic( start_line=start_line, end_line=end_line, start_column=1, file_path=file_path, - diff="".join(map(str, filter(_is_changed, hunk))), + diff="".join(map(str, filter_hunk(hunk))), ) diff --git a/tests/test_error_lines.py b/tests/test_error_lines.py index 65ad8ce..cd75222 100644 --- a/tests/test_error_lines.py +++ b/tests/test_error_lines.py @@ -67,6 +67,19 @@ class Hoge: """ # NOQA +diff_err4 = """--- /home/user/pysen/foo.py 2021-03-25 16:43:53.875256 +0000 ++++ /home/user/pysen/foo.py 2021-03-25 16:44:02.267033 +0000 +@@ -1,8 +1,5 @@ + { + foo: "", +- +- + bar: [], +- + baz: {}, + } +""" # NOQA + def test_standard_parser() -> None: err1, err2 = parse_error_lines(std_err) @@ -134,7 +147,7 @@ def test_diff_parser() -> None: err = errors[0] assert err.start_line == 152 assert err.end_line == 152 - assert err.diff == "+\n" + assert err.diff == "+\n parser.add_argument(\n" # has only source diff errors = list(parse_error_diffs(diff_err3, _parse_file_path)) @@ -143,3 +156,12 @@ def test_diff_parser() -> None: assert err.start_line == 152 assert err.end_line == 152 assert err.diff == "-\n\n" + + # has multiple deletion + errors = list(parse_error_diffs(diff_err4, _parse_file_path)) + + assert len(errors) == 1 + err1 = errors[0] + assert err1.start_line == 3 + assert err1.end_line == 6 + assert err1.diff == "-\n-\n bar: [],\n-\n"