All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/3] simplediff: Remove redundant parentheses
@ 2022-02-19 20:19 Zygmunt Krynicki
  2022-02-19 20:19 ` [PATCH 2/3] simplediff: Use "a if b else c" expression Zygmunt Krynicki
  2022-02-19 20:19 ` [PATCH 3/3] simplediff: Add type annotations Zygmunt Krynicki
  0 siblings, 2 replies; 4+ messages in thread
From: Zygmunt Krynicki @ 2022-02-19 20:19 UTC (permalink / raw)
  To: bitbake-devel; +Cc: Zygmunt Krynicki

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@huawei.com>
---
 lib/simplediff/__init__.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/simplediff/__init__.py b/lib/simplediff/__init__.py
index 57ee3c5c..11eb7e70 100644
--- a/lib/simplediff/__init__.py
+++ b/lib/simplediff/__init__.py
@@ -90,7 +90,7 @@ def diff(old, new):
             # now we are considering all values of iold such that
             # `old[iold] == new[inew]`.
             _overlap[iold] = (iold and overlap.get(iold - 1, 0)) + 1
-            if(_overlap[iold] > sub_length):
+            if _overlap[iold] > sub_length:
                 # this is the largest substring seen so far, so store its
                 # indices
                 sub_length = _overlap[iold]
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/3] simplediff: Use "a if b else c" expression
  2022-02-19 20:19 [PATCH 1/3] simplediff: Remove redundant parentheses Zygmunt Krynicki
@ 2022-02-19 20:19 ` Zygmunt Krynicki
  2022-02-19 20:19 ` [PATCH 3/3] simplediff: Add type annotations Zygmunt Krynicki
  1 sibling, 0 replies; 4+ messages in thread
From: Zygmunt Krynicki @ 2022-02-19 20:19 UTC (permalink / raw)
  To: bitbake-devel; +Cc: Zygmunt Krynicki

The diff code used "a and b or c" as an old-style Python implementation
of the ternary operator. The more correct way to do this is to use "b if
a else c", as it has cleaner type signature.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@huawei.com>
---
 lib/simplediff/__init__.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/simplediff/__init__.py b/lib/simplediff/__init__.py
index 11eb7e70..64ec64b4 100644
--- a/lib/simplediff/__init__.py
+++ b/lib/simplediff/__init__.py
@@ -100,7 +100,7 @@ def diff(old, new):
 
     if sub_length == 0:
         # If no common substring is found, we return an insert and delete...
-        return (old and [('-', old)] or []) + (new and [('+', new)] or [])
+        return ([('-', old)] if old else []) + ([('+', new)] if new else [])
     else:
         # ...otherwise, the common substring is unchanged and we recursively
         # diff the text before and after that substring
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 3/3] simplediff: Add type annotations
  2022-02-19 20:19 [PATCH 1/3] simplediff: Remove redundant parentheses Zygmunt Krynicki
  2022-02-19 20:19 ` [PATCH 2/3] simplediff: Use "a if b else c" expression Zygmunt Krynicki
@ 2022-02-19 20:19 ` Zygmunt Krynicki
  2022-02-19 20:25   ` [bitbake-devel] " Richard Purdie
  1 sibling, 1 reply; 4+ messages in thread
From: Zygmunt Krynicki @ 2022-02-19 20:19 UTC (permalink / raw)
  To: bitbake-devel; +Cc: Zygmunt Krynicki

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(-)

diff --git a/lib/simplediff/__init__.py b/lib/simplediff/__init__.py
index 64ec64b4..a1866ba1 100644
--- a/lib/simplediff/__init__.py
+++ b/lib/simplediff/__init__.py
@@ -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
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [bitbake-devel] [PATCH 3/3] simplediff: Add type annotations
  2022-02-19 20:19 ` [PATCH 3/3] simplediff: Add type annotations Zygmunt Krynicki
@ 2022-02-19 20:25   ` Richard Purdie
  0 siblings, 0 replies; 4+ messages in thread
From: Richard Purdie @ 2022-02-19 20:25 UTC (permalink / raw)
  To: zygmunt.krynicki, bitbake-devel

On Sat, 2022-02-19 at 21:19 +0100, Zygmunt Krynicki via lists.openembedded.org
wrote:
> 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(-)

Just to be really clear on this, we have no interest in adding typing to
bitbake.

I've also mentioned on irc that some of the files in lib/ are upstream modules
so we limit our changes in those cases.

Cheers,

Richard



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-02-19 20:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-19 20:19 [PATCH 1/3] simplediff: Remove redundant parentheses Zygmunt Krynicki
2022-02-19 20:19 ` [PATCH 2/3] simplediff: Use "a if b else c" expression Zygmunt Krynicki
2022-02-19 20:19 ` [PATCH 3/3] simplediff: Add type annotations Zygmunt Krynicki
2022-02-19 20:25   ` [bitbake-devel] " Richard Purdie

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.