@@ -13,8 +13,18 @@ May be used and distributed under the zlib/libpng license
__all__ = ['diff', 'string_diff', 'html_diff']
__version__ = '1.0'
-
-def diff(old, new):
+from typing import (
+ Callable,
+ Dict,
+ List,
+ Sequence,
+ Tuple,
+ TypeVar,
+)
+
+T = TypeVar('T', str, int)
+
+def diff(old: List[T], new: List[T]) -> List[Tuple[str, List[T]]]:
'''
Find the differences between two lists. Returns a list of pairs, where the
first value is in ['+','-','='] and represents an insertion, deletion, or
@@ -53,7 +63,7 @@ def diff(old, new):
'''
# Create a map from old values to their indices
- old_index_map = dict()
+ old_index_map: Dict[T, List[int]] = dict()
for i, val in enumerate(old):
old_index_map.setdefault(val,list()).append(i)
@@ -73,7 +83,7 @@ def diff(old, new):
# seen so far (`sub_length`), we update the largest substring
# to the overlapping strings.
- overlap = dict()
+ overlap: Dict[int, int] = dict()
# `sub_start_old` is the index of the beginning of the largest overlapping
# substring in the old list. `sub_start_new` is the index of the beginning
# of the same substring in the new list. `sub_length` is the length that
@@ -85,7 +95,7 @@ def diff(old, new):
sub_length = 0
for inew, val in enumerate(new):
- _overlap = dict()
+ _overlap: Dict[int, int] = dict()
for iold in old_index_map.get(val,list()):
# now we are considering all values of iold such that
# `old[iold] == new[inew]`.
@@ -110,7 +120,7 @@ def diff(old, new):
new[sub_start_new + sub_length : ])
-def string_diff(old, new):
+def string_diff(old: str, new: str) -> List[Tuple[str, List[str]]]:
'''
Returns the difference between the old and new strings when split on
whitespace. Considers punctuation a part of the word
@@ -139,7 +149,7 @@ def string_diff(old, new):
return diff(old.split(), new.split())
-def html_diff(old, new):
+def html_diff(old: str, new: str) -> str:
'''
Returns the difference between two strings (as in stringDiff) in
HTML format. HTML code in the strings is NOT escaped, so you
@@ -160,13 +170,14 @@ def html_diff(old, new):
>>> html_diff('The quick brown fox', 'The fast blue fox')
'The <del>quick brown</del> <ins>fast blue</ins> fox'
'''
- con = {'=': (lambda x: x),
+ con: Dict[str, Callable[[str], str]] = {
+ '=': (lambda x: x),
'+': (lambda x: "<ins>" + x + "</ins>"),
'-': (lambda x: "<del>" + x + "</del>")}
return " ".join([(con[a])(" ".join(b)) for a, b in string_diff(old, new)])
-def check_diff(old, new):
+def check_diff(old: Sequence[str], new: Sequence[str]) -> None:
'''
This tests that diffs returned by `diff` are valid. You probably won't
want to use this function, but it's provided for documentation and
I was trying to grok how various parts of bitbake work. As a habit I make type annotations, as those help both me and, perhaps, others. Introducing typing to an existing project is somewhat challenging, but by now, well established. It's good to start from any leaf module and work up from there. I've picked, somewhat at random, the simplediff module, since it's small, self-contained and might be used as a starting point for the discussion around type hints. With the patch applied, one can run mypy as follows: mypy --strict ./lib/simplediff The module now has strict typing compatibility. Allowing it to be used in a typed context. Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@huawei.com> --- lib/simplediff/__init__.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-)