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"