Skip to content

Commit

Permalink
Add typing.
Browse files Browse the repository at this point in the history
  • Loading branch information
jelmer committed Sep 7, 2022
1 parent 70b7083 commit c8a6326
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
7 changes: 5 additions & 2 deletions patiencediff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

from __future__ import absolute_import

import os
import sys
import time
import difflib

from typing import Type


__all__ = ['PatienceSequenceMatcher', 'unified_diff', 'unified_diff_files']

Expand Down Expand Up @@ -128,6 +128,9 @@ def unified_diff_files(a, b, sequencematcher=None):
sequencematcher=sequencematcher)


PatienceSequenceMatcher: Type[difflib.SequenceMatcher]


try:
from ._patiencediff_c import (
unique_lcs_c as unique_lcs,
Expand Down
17 changes: 17 additions & 0 deletions patiencediff/_patiencediff_c.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import difflib
from typing import List, Tuple, Sequence, Any


class PatienceSequenceMatcher_c(difflib.SequenceMatcher):

def get_matching_blocks(self) -> List[difflib.Match]: ...


def unique_lcs_c(a: Sequence[Any], b: Sequence[Any]) -> List[Tuple[int, int]]: ...


def recurse_matches_c(
a: Sequence[Any], b: Sequence[Any],
alo: int, blo: int, ahi: int, bhi: int,
answer: List[Tuple[int, int]], maxrecursion: int) -> None: ...

34 changes: 19 additions & 15 deletions patiencediff/_patiencediff_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

from __future__ import absolute_import
from typing import List, Tuple, Sequence, Dict, Any, Optional

from bisect import bisect
import difflib
Expand All @@ -26,7 +26,7 @@ def __init__(self):
super(MaxRecursionDepth, self).__init__('max recursion depth reached')


def unique_lcs_py(a, b):
def unique_lcs_py(a: Sequence[Any], b: Sequence[Any]) -> List[Tuple[int, int]]:
"""Find the longest common subset for unique lines.
:param a: An indexable object (such as string or list of strings)
Expand All @@ -40,9 +40,10 @@ def unique_lcs_py(a, b):
The longest common subset uses the Patience Sorting algorithm:
http://en.wikipedia.org/wiki/Patience_sorting
"""
line: Any
# set index[line in a] = position of line in a unless
# a is a duplicate, in which case it's set to None
index = {}
index: Dict[Any, Optional[int]] = {}
for i, line in enumerate(a):
if line in index:
index[line] = None
Expand All @@ -51,8 +52,8 @@ def unique_lcs_py(a, b):
# make btoa[i] = position of line i in a, unless
# that line doesn't occur exactly once in both,
# in which case it's set to None
btoa = [None] * len(b)
index2 = {}
btoa: List[Optional[int]] = [None] * len(b)
index2: Dict[Any, int] = {}
for pos, line in enumerate(b):
next = index.get(line)
if next is not None:
Expand All @@ -66,10 +67,10 @@ def unique_lcs_py(a, b):
btoa[pos] = next
# this is the Patience sorting algorithm
# see http://en.wikipedia.org/wiki/Patience_sorting
backpointers = [None] * len(b)
stacks = []
lasts = []
k = 0
backpointers: List[Optional[int]] = [None] * len(b)
stacks: List[int] = []
lasts: List[int] = []
k: int = 0
for bpos, apos in enumerate(btoa):
if apos is None:
continue
Expand All @@ -95,15 +96,18 @@ def unique_lcs_py(a, b):
if len(lasts) == 0:
return []
result = []
k = lasts[-1]
while k is not None:
result.append((btoa[k], k))
k = backpointers[k]
m: Optional[int] = lasts[-1]
while m is not None:
result.append((btoa[m], m))
m = backpointers[m]
result.reverse()
return result
return result # type: ignore


def recurse_matches_py(a, b, alo, blo, ahi, bhi, answer, maxrecursion):
def recurse_matches_py(
a: Sequence[Any], b: Sequence[Any],
alo: int, blo: int, ahi: int, bhi: int,
answer: List[Tuple[int, int]], maxrecursion: int) -> None:
"""Find all of the matching text in the lines of a and b.
:param a: A sequence
Expand Down
Empty file added patiencediff/py.typed
Empty file.

0 comments on commit c8a6326

Please sign in to comment.