Skip to content

Commit 2baf123

Browse files
committed
Add mypy types
1 parent 54a7bca commit 2baf123

File tree

6 files changed

+70
-14
lines changed

6 files changed

+70
-14
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "difflib-parser"
7-
version = "1.0.0"
7+
version = "1.2.0"
88
authors = [{ name = "Jiahao, Woo", email = "woojiahao1234@gmail.com" }]
99
description = "Lightweight Python package for parsing Python difflib's diff results"
1010
readme = "README.md"

requirements.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
pytest
1+
pytest
2+
twine
3+
build
4+
mypy

src/difflib_parser/diff_line.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class DiffLineCode(Enum):
1111
class DiffLine:
1212
def __init__(self, line: str | None):
1313
self.__line = line
14-
print(self.__line)
1514

1615
@staticmethod
1716
def parse(line: str | None) -> "DiffLine":
@@ -32,6 +31,8 @@ def code(self) -> DiffLineCode | None:
3231
case "? ":
3332
return DiffLineCode.MISSING
3433

34+
return None
35+
3536
@property
3637
def line(self) -> str | None:
3738
if self.__line is None:

src/difflib_parser/diff_line.pyi

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from enum import Enum
2+
3+
class DiffLineCode(Enum):
4+
ADDED = 0
5+
REMOVED = 1
6+
COMMON = 2
7+
MISSING = 3
8+
9+
class DiffLine:
10+
def __init__(self, line: str | None) -> None: ...
11+
@staticmethod
12+
def parse(line: str | None) -> DiffLine: ...
13+
@property
14+
def code(self) -> DiffLineCode | None: ...
15+
@property
16+
def line(self) -> str | None: ...

src/difflib_parser/difflib_parser.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from typing import List, Generator, Any
21
import difflib
3-
from enum import Enum
42
from dataclasses import dataclass
3+
from enum import Enum
4+
from typing import Iterator, List
55

66
from difflib_parser.diff_line import DiffLine, DiffLineCode
77

@@ -38,10 +38,13 @@ def __init__(self, left_text: List[str], right_text: List[str]):
3838
self.__diff = list(difflib.ndiff(self.__left_text, self.__right_text))
3939
self.__line_no = 0
4040

41-
def iter_diffs(self) -> Generator[Diff, Any, Any]:
41+
def iter_diffs(self) -> Iterator[Diff]:
42+
current_line = self.__diff[self.__line_no]
4243
while self.__line_no < len(self.__diff):
43-
current_line = self.__diff[self.__line_no]
4444
diff_line = DiffLine.parse(current_line)
45+
if diff_line.line is None:
46+
self.__line_no += 1
47+
continue
4548
code = diff_line.code
4649
diff = Diff(code=DiffCode.SAME, line=diff_line.line)
4750

@@ -78,11 +81,13 @@ def __get_incremental_change(self, line_no: int) -> DiffChange | None:
7881
DiffLineCode.ADDED,
7982
DiffLineCode.MISSING,
8083
]
84+
# We can ignore all of these lines because we know that a None line would have
85+
# been skipped
8186
if self.__match_pattern(lines, pattern_a):
8287
return DiffChange(
83-
left=[i for (i, c) in enumerate(b.line) if c in ["-", "^"]],
84-
right=[i for (i, c) in enumerate(d.line) if c in ["+", "^"]],
85-
newline=c.line,
88+
left=[i for (i, c) in enumerate(b.line) if c in ["-", "^"]], # type: ignore
89+
right=[i for (i, c) in enumerate(d.line) if c in ["+", "^"]], # type: ignore
90+
newline=c.line, # type: ignore
8691
skip_lines=3,
8792
)
8893

@@ -91,18 +96,18 @@ def __get_incremental_change(self, line_no: int) -> DiffChange | None:
9196
if self.__match_pattern(lines, pattern_b):
9297
return DiffChange(
9398
left=[],
94-
right=[i for (i, c) in enumerate(c.line) if c in ["+", "^"]],
95-
newline=b.line,
99+
right=[i for (i, c) in enumerate(c.line) if c in ["+", "^"]], # type: ignore
100+
newline=b.line, # type: ignore
96101
skip_lines=2,
97102
)
98103

99104
# This represents the case where only removals are present in the edit
100105
pattern_c = [DiffLineCode.REMOVED, DiffLineCode.MISSING, DiffLineCode.ADDED]
101106
if self.__match_pattern(lines, pattern_c):
102107
return DiffChange(
103-
left=[i for (i, c) in enumerate(b.line) if c in ["-", "^"]],
108+
left=[i for (i, c) in enumerate(b.line) if c in ["-", "^"]], # type: ignore
104109
right=[],
105-
newline=c.line,
110+
newline=c.line, # type: ignore
106111
skip_lines=2,
107112
)
108113

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from dataclasses import dataclass
2+
from difflib_parser.diff_line import DiffLine as DiffLine, DiffLineCode as DiffLineCode
3+
from enum import Enum
4+
from typing import Iterator
5+
6+
class DiffCode(Enum):
7+
SAME = 0
8+
RIGHT_ONLY = 1
9+
LEFT_ONLY = 2
10+
CHANGED = 3
11+
12+
@dataclass
13+
class DiffChange:
14+
left: list[int]
15+
right: list[int]
16+
newline: str
17+
skip_lines: int
18+
def __init__(self, left, right, newline, skip_lines) -> None: ...
19+
20+
@dataclass
21+
class Diff:
22+
code: DiffCode
23+
line: str
24+
left_changes: list[int] | None = ...
25+
right_changes: list[int] | None = ...
26+
newline: str | None = ...
27+
def __init__(self, code, line, left_changes=..., right_changes=..., newline=...) -> None: ...
28+
29+
class DiffParser:
30+
def __init__(self, left_text: list[str], right_text: list[str]) -> None: ...
31+
def iter_diffs(self) -> Iterator[Diff]: ...

0 commit comments

Comments
 (0)