# HG changeset patch # User Matt Harbison # Date 2022-11-08 18:59:16 # Node ID 594fc56c0af7a908054ce9e0fc817de1f1b5b028 # Parent b2666e767029fd4c8b32265c6b23482a045478c3 typing: add type hints to bdiff implementations Not super important code, but this was an exercise in using `merge-pyi` to fold type stubs back into the code on something small. The cext stubs don't seem to be getting used (at least the only thing in `.pytype/pyi/mercurial/cext` after a run generating the stubs is `__init__.pyi`), so maybe this will help some. diff --git a/mercurial/cffi/bdiff.py b/mercurial/cffi/bdiff.py --- a/mercurial/cffi/bdiff.py +++ b/mercurial/cffi/bdiff.py @@ -8,6 +8,11 @@ import struct +from typing import ( + List, + Tuple, +) + from ..pure.bdiff import * from . import _bdiff # pytype: disable=import-error @@ -15,7 +20,7 @@ ffi = _bdiff.ffi lib = _bdiff.lib -def blocks(sa, sb): +def blocks(sa: bytes, sb: bytes) -> List[Tuple[int, int, int, int]]: a = ffi.new(b"struct bdiff_line**") b = ffi.new(b"struct bdiff_line**") ac = ffi.new(b"char[]", str(sa)) @@ -43,7 +48,7 @@ def blocks(sa, sb): return rl -def bdiff(sa, sb): +def bdiff(sa: bytes, sb: bytes) -> bytes: a = ffi.new(b"struct bdiff_line**") b = ffi.new(b"struct bdiff_line**") ac = ffi.new(b"char[]", str(sa)) diff --git a/mercurial/pure/bdiff.py b/mercurial/pure/bdiff.py --- a/mercurial/pure/bdiff.py +++ b/mercurial/pure/bdiff.py @@ -10,8 +10,13 @@ import difflib import re import struct +from typing import ( + List, + Tuple, +) -def splitnewlines(text): + +def splitnewlines(text: bytes) -> List[bytes]: '''like str.splitlines, but only split on newlines.''' lines = [l + b'\n' for l in text.split(b'\n')] if lines: @@ -22,7 +27,9 @@ def splitnewlines(text): return lines -def _normalizeblocks(a, b, blocks): +def _normalizeblocks( + a: List[bytes], b: List[bytes], blocks +) -> List[Tuple[int, int, int]]: prev = None r = [] for curr in blocks: @@ -57,7 +64,7 @@ def _normalizeblocks(a, b, blocks): return r -def bdiff(a, b): +def bdiff(a: bytes, b: bytes) -> bytes: a = bytes(a).splitlines(True) b = bytes(b).splitlines(True) @@ -84,7 +91,7 @@ def bdiff(a, b): return b"".join(bin) -def blocks(a, b): +def blocks(a: bytes, b: bytes) -> List[Tuple[int, int, int, int]]: an = splitnewlines(a) bn = splitnewlines(b) d = difflib.SequenceMatcher(None, an, bn).get_matching_blocks() @@ -92,7 +99,7 @@ def blocks(a, b): return [(i, i + n, j, j + n) for (i, j, n) in d] -def fixws(text, allws): +def fixws(text: bytes, allws: bool) -> bytes: if allws: text = re.sub(b'[ \t\r]+', b'', text) else: